Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
w2sv committed Mar 4, 2024
1 parent 1711bde commit 9f7c114
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:OptIn(ExperimentalPermissionsApi::class)

package com.w2sv.composeutils.permissions.extendedpermissionstate

import android.annotation.SuppressLint
Expand All @@ -16,7 +18,6 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

@Stable
@OptIn(ExperimentalPermissionsApi::class)
open class ExtendedMultiplePermissionsState(
private val requestLaunchedBefore: StateFlow<Boolean>,
multiplePermissionsState: MultiplePermissionsState,
Expand All @@ -35,14 +36,14 @@ open class ExtendedMultiplePermissionsState(
}
}

@OptIn(ExperimentalPermissionsApi::class)
@SuppressLint("ComposeUnstableCollections")
@Composable
fun rememberExtendedMultiplePermissionsState(
permissions: List<String>,
requestLaunchedBefore: StateFlow<Boolean>,
saveRequestLaunched: () -> Unit,
onPermissionResult: (Map<String, Boolean>) -> Unit = {},
onLaunchingSuppressed: () -> Unit = {},
scope: CoroutineScope = rememberCoroutineScope()
): ExtendedMultiplePermissionsState {

Expand All @@ -67,7 +68,8 @@ fun rememberExtendedMultiplePermissionsState(
ExtendedMultiplePermissionsState(
requestLaunchedBefore = requestLaunchedBefore,
multiplePermissionsState = permissionState,
grantedFromRequest = grantedFromRequest
grantedFromRequest = grantedFromRequest,
onLaunchingSuppressed = onLaunchingSuppressed
)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:OptIn(ExperimentalPermissionsApi::class)

package com.w2sv.composeutils.permissions.extendedpermissionstate

import androidx.compose.runtime.Composable
Expand All @@ -16,7 +18,6 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

@Stable
@OptIn(ExperimentalPermissionsApi::class)
open class ExtendedSinglePermissionState(
private val requestLaunchedBefore: StateFlow<Boolean>,
permissionState: PermissionState,
Expand All @@ -35,13 +36,13 @@ open class ExtendedSinglePermissionState(
}
}

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun rememberExtendedPermissionState(
permission: String,
requestLaunchedBefore: StateFlow<Boolean>,
saveRequestLaunched: () -> Unit,
onPermissionResult: (Boolean) -> Unit = {},
onLaunchingSuppressed: () -> Unit = {},
scope: CoroutineScope = rememberCoroutineScope()
): ExtendedSinglePermissionState {

Expand All @@ -66,7 +67,8 @@ fun rememberExtendedPermissionState(
ExtendedSinglePermissionState(
requestLaunchedBefore = requestLaunchedBefore,
permissionState = permissionState,
grantedFromRequest = grantedFromRequest
grantedFromRequest = grantedFromRequest,
onLaunchingSuppressed = onLaunchingSuppressed
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Entire file content taken from https://stackoverflow.com/a/70162451/12083276.
* Functions adopted from https://stackoverflow.com/a/70162451/12083276.
*/

package com.w2sv.composeutils
Expand All @@ -21,6 +21,7 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
Expand All @@ -35,6 +36,18 @@ import androidx.compose.ui.unit.em
import androidx.core.text.HtmlCompat
import androidx.core.text.toSpanned

/**
* @return A remembered html-styled resource text converted to an [AnnotatedString], keyed by [id] and [formatArgs].
* #
* Tested with:
* - bold: &lt;b&gt;
* - italic: &lt;i&gt;
* - underline: &lt;u&gt;
* - subscript: &lt;sub&gt;
* - superscript: &lt;sup&gt;
* - foreground color: &lt;font color="#9900FF"&gt;
* - monospace font family: &lt;font face="monospace"&gt;
*/
@Composable
fun rememberStyledTextResource(@StringRes id: Int, vararg formatArgs: Any): AnnotatedString {
val resources = LocalContext.current.resources
Expand All @@ -47,7 +60,7 @@ fun rememberStyledTextResource(@StringRes id: Int, vararg formatArgs: Any): Anno
}
}

internal fun Resources.getHtmlText(@StringRes id: Int, vararg args: Any): Spanned {
private fun Resources.getHtmlText(@StringRes id: Int, vararg args: Any): Spanned {
return HtmlCompat.fromHtml(
String.format(
getText(id).toSpanned().toHtmlWithoutParagraphs(),
Expand All @@ -68,7 +81,7 @@ private fun Spanned.toHtmlWithoutParagraphs(): String {
.substringBeforeLast("</p>")
}

internal fun spannableStringToAnnotatedString(
private fun spannableStringToAnnotatedString(
text: Spanned,
density: Density
): AnnotatedString {
Expand Down

0 comments on commit 9f7c114

Please sign in to comment.