forked from gkd-kit/gkd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: permission, refresh appList, divider
- Loading branch information
Showing
23 changed files
with
822 additions
and
610 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -226,4 +226,5 @@ dependencies { | |
implementation(libs.exp4j) | ||
|
||
implementation(libs.toaster) | ||
implementation(libs.permissions) | ||
} |
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
97 changes: 97 additions & 0 deletions
97
app/src/main/kotlin/li/songe/gkd/permission/PermissionDialog.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,97 @@ | ||
package li.songe.gkd.permission | ||
|
||
import android.app.Activity | ||
import androidx.compose.material3.AlertDialog | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextButton | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import com.hjq.permissions.OnPermissionCallback | ||
import com.hjq.permissions.XXPermissions | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
data class AuthReason( | ||
val text: String, | ||
val confirm: () -> Unit | ||
) | ||
|
||
val authReasonFlow = MutableStateFlow<AuthReason?>(null) | ||
|
||
@Composable | ||
fun AuthDialog() { | ||
val authAction = authReasonFlow.collectAsState().value | ||
if (authAction != null) { | ||
AlertDialog( | ||
title = { | ||
Text(text = "权限请求") | ||
}, | ||
text = { | ||
Text(text = authAction.text) | ||
}, | ||
onDismissRequest = { authReasonFlow.value = null }, | ||
confirmButton = { | ||
TextButton(onClick = { | ||
authReasonFlow.value = null | ||
authAction.confirm() | ||
}) { | ||
Text(text = "确认") | ||
} | ||
}, | ||
dismissButton = { | ||
TextButton(onClick = { authReasonFlow.value = null }) { | ||
Text(text = "取消") | ||
} | ||
} | ||
) | ||
} | ||
} | ||
|
||
sealed class PermissionResult { | ||
data object Granted : PermissionResult() | ||
data class Denied(val doNotAskAgain: Boolean) : PermissionResult() | ||
} | ||
|
||
suspend fun asyncRequestPermission( | ||
context: Activity, | ||
permission: String, | ||
): PermissionResult { | ||
if (XXPermissions.isGranted(context, permission)) { | ||
return PermissionResult.Granted | ||
} | ||
return suspendCoroutine { continuation -> | ||
XXPermissions.with(context) | ||
.unchecked() | ||
.permission(permission) | ||
.request(object : OnPermissionCallback { | ||
override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) { | ||
if (allGranted) { | ||
continuation.resume(PermissionResult.Granted) | ||
} else { | ||
continuation.resume(PermissionResult.Denied(false)) | ||
} | ||
} | ||
|
||
override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) { | ||
continuation.resume(PermissionResult.Denied(doNotAskAgain)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
suspend fun checkOrRequestPermission( | ||
context: Activity, | ||
permissionState: PermissionState | ||
): Boolean { | ||
if (!permissionState.updateAndGet()) { | ||
val result = permissionState.request?.let { it(context) } ?: return false | ||
if (result is PermissionResult.Denied) { | ||
if (result.doNotAskAgain) { | ||
authReasonFlow.value = permissionState.reason | ||
} | ||
return false | ||
} | ||
} | ||
return true | ||
} |
Oops, something went wrong.