-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59992cd
commit cde1986
Showing
5 changed files
with
174 additions
and
20 deletions.
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
core/designsystem/src/main/java/com/mifos/core/designsystem/component/MifosPermissionBox.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package com.mifos.core.designsystem.component | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import android.content.pm.PackageManager | ||
import android.net.Uri | ||
import android.provider.Settings | ||
import androidx.activity.compose.rememberLauncherForActivityResult | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.core.app.ActivityCompat | ||
import androidx.core.content.ContextCompat | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleEventObserver | ||
|
||
@Composable | ||
fun PermissionBox( | ||
requiredPermissions: List<String>, | ||
title: Int, | ||
description: Int? = null, | ||
confirmButtonText: Int, | ||
dismissButtonText: Int, | ||
onGranted: @Composable (() -> Unit)? = null, | ||
) { | ||
val context = LocalContext.current | ||
val lifecycleOwner = LocalLifecycleOwner.current | ||
|
||
var permissionGranted by remember { | ||
mutableStateOf( | ||
requiredPermissions.all { | ||
ContextCompat.checkSelfPermission( | ||
context, | ||
it | ||
) == PackageManager.PERMISSION_GRANTED | ||
} | ||
) | ||
} | ||
|
||
var shouldShowPermissionRationale = | ||
requiredPermissions.all { | ||
(context as? Activity)?.let { it1 -> | ||
ActivityCompat.shouldShowRequestPermissionRationale( | ||
it1, it | ||
) | ||
} == true | ||
} | ||
|
||
var shouldDirectUserToApplicationSettings by remember { | ||
mutableStateOf(false) | ||
} | ||
|
||
val decideCurrentPermissionStatus: (Boolean, Boolean) -> String = | ||
{ permissionGranted, shouldShowPermissionRationale -> | ||
if (permissionGranted) "Granted" | ||
else if (shouldShowPermissionRationale) "Rejected" | ||
else "Denied" | ||
} | ||
|
||
var currentPermissionStatus by remember { | ||
mutableStateOf( | ||
decideCurrentPermissionStatus( | ||
permissionGranted, | ||
shouldShowPermissionRationale | ||
) | ||
) | ||
} | ||
|
||
val multiplePermissionLauncher = rememberLauncherForActivityResult( | ||
contract = ActivityResultContracts.RequestMultiplePermissions(), | ||
onResult = { permissionResults -> | ||
val isGranted = | ||
requiredPermissions.all { permissionResults[it] ?: false } | ||
|
||
permissionGranted = isGranted | ||
|
||
if (!isGranted) { | ||
shouldShowPermissionRationale = | ||
requiredPermissions.all { | ||
ActivityCompat.shouldShowRequestPermissionRationale( | ||
context as Activity, | ||
it | ||
) | ||
} | ||
} | ||
shouldDirectUserToApplicationSettings = | ||
!shouldShowPermissionRationale && !permissionGranted | ||
currentPermissionStatus = decideCurrentPermissionStatus( | ||
permissionGranted, | ||
shouldShowPermissionRationale | ||
) | ||
}) | ||
|
||
DisposableEffect(key1 = lifecycleOwner, effect = { | ||
val observer = LifecycleEventObserver { _, event -> | ||
if (event == Lifecycle.Event.ON_START && | ||
!permissionGranted && | ||
!shouldShowPermissionRationale | ||
) { | ||
multiplePermissionLauncher.launch(requiredPermissions.toTypedArray()) | ||
} | ||
} | ||
lifecycleOwner.lifecycle.addObserver(observer) | ||
onDispose { | ||
lifecycleOwner.lifecycle.removeObserver(observer) | ||
} | ||
}) | ||
|
||
if (shouldShowPermissionRationale) { | ||
MifosDialogBox( | ||
showDialogState = shouldShowPermissionRationale, | ||
onDismiss = { shouldShowPermissionRationale = false }, | ||
title = title, | ||
message = description, | ||
confirmButtonText = confirmButtonText, | ||
onConfirm = { | ||
shouldShowPermissionRationale = false | ||
multiplePermissionLauncher.launch(requiredPermissions.toTypedArray()) | ||
}, | ||
dismissButtonText = dismissButtonText | ||
) | ||
} | ||
|
||
if (shouldDirectUserToApplicationSettings) { | ||
Intent( | ||
Settings.ACTION_APPLICATION_DETAILS_SETTINGS, | ||
Uri.fromParts("package", context.packageName, null) | ||
).also { | ||
context.startActivity(it) | ||
} | ||
} | ||
|
||
if (permissionGranted) { | ||
if (onGranted != null) { | ||
onGranted() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters