Skip to content

Instantly share code, notes, and snippets.

@Astroa7m
Created October 25, 2022 18:08
Show Gist options
  • Save Astroa7m/03d875d7c68fde71dbdc8d55a348ee52 to your computer and use it in GitHub Desktop.
Save Astroa7m/03d875d7c68fde71dbdc8d55a348ee52 to your computer and use it in GitHub Desktop.
final SharingUtil.kt
object SharingUtil {
...
// add the following two functions
@RequiresApi(Build.VERSION_CODES.S)
@Composable
fun CheckIfAppApprovedForDomain() {
val context = LocalContext.current
val domain = context.getString(R.string.host)
val lifecycleOwner = LocalLifecycleOwner.current
var launchDialog by remember { mutableStateOf(false) }
val manager = remember { context.getSystemService(DomainVerificationManager::class.java) }
DisposableEffect(key1 = lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_RESUME) {
val userState = manager.getDomainVerificationUserState(context.packageName)
val verifiedDomain =
userState?.hostToStateMap?.filterValues { it == DomainVerificationUserState.DOMAIN_STATE_SELECTED }
val selectedDomains = userState?.hostToStateMap
?.filterValues { it == DomainVerificationUserState.DOMAIN_STATE_SELECTED }
launchDialog =
(verifiedDomain?.keys?.contains(domain) != true || selectedDomains?.keys?.contains(domain) != true)
|| userState.isLinkHandlingAllowed == false
}
}
lifecycleOwner.lifecycle.addObserver(observer)
onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}
if (launchDialog) {
VerifyDomainDialog(
onDismissed = {
launchDialog = false
},
onConfirm = {
val intent = Intent(
Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS,
Uri.parse("package:${context.packageName}")
)
context.startActivity(intent)
}
)
}
}
@Composable
private fun VerifyDomainDialog(
modifier: Modifier = Modifier,
onDismissed: () -> Unit,
onConfirm: () -> Unit
) {
AlertDialog(
onDismissRequest = { /*TODO*/ },
modifier = modifier,
title = {
Text(
text = "Extra Configurations Needed!"
)
},
text = {
Text(text = "To provide the best user experience with this feature, please do the following:\n" +
"1 - Click on \"Open Settings\" below.\n" +
"2 - Make sure \"Open supported links\" is switched ON.\n" +
"3 - Make sure you checked all the links in the dialog after clicking on \"+ Add link\".\n\n" +
"Note: You could simply ignore setting the app as the default links handler. Though you won't be able to navigate to the app by clicking on external app-related links")
},
confirmButton = {
TextButton(onClick = onConfirm) {
Text(text = "Open Settings")
}
},
dismissButton = {
TextButton(onClick = onDismissed) {
Text(text = "Ignore")
}
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment