|
| 1 | +package com.threegap.bitnagil.presentation.common.premission |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.net.Uri |
| 5 | +import android.provider.Settings |
| 6 | +import androidx.compose.runtime.Composable |
| 7 | +import androidx.compose.runtime.getValue |
| 8 | +import androidx.compose.runtime.mutableIntStateOf |
| 9 | +import androidx.compose.runtime.mutableStateOf |
| 10 | +import androidx.compose.runtime.remember |
| 11 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 12 | +import androidx.compose.runtime.setValue |
| 13 | +import androidx.compose.ui.platform.LocalContext |
| 14 | +import com.google.accompanist.permissions.ExperimentalPermissionsApi |
| 15 | +import com.google.accompanist.permissions.isGranted |
| 16 | +import com.google.accompanist.permissions.rememberPermissionState |
| 17 | +import com.google.accompanist.permissions.shouldShowRationale |
| 18 | +import com.threegap.bitnagil.designsystem.component.block.BitnagilAlertDialog |
| 19 | + |
| 20 | +@OptIn(ExperimentalPermissionsApi::class) |
| 21 | +@Composable |
| 22 | +fun rememberPermissionHandler( |
| 23 | + permission: String, |
| 24 | + dialogDescription: String, |
| 25 | + onGranted: () -> Unit, |
| 26 | +): PermissionHandler { |
| 27 | + val context = LocalContext.current |
| 28 | + var showSettingsDialog by remember { mutableStateOf(false) } |
| 29 | + var permissionRequestCount by rememberSaveable { mutableIntStateOf(0) } |
| 30 | + |
| 31 | + val permissionState = rememberPermissionState( |
| 32 | + permission = permission, |
| 33 | + onPermissionResult = { isGranted -> |
| 34 | + if (isGranted) { |
| 35 | + permissionRequestCount = 0 |
| 36 | + onGranted() |
| 37 | + } else { |
| 38 | + permissionRequestCount++ |
| 39 | + } |
| 40 | + }, |
| 41 | + ) |
| 42 | + |
| 43 | + val handler = remember(context) { |
| 44 | + object : PermissionHandler { |
| 45 | + override val isGranted: Boolean |
| 46 | + get() = permissionState.status.isGranted |
| 47 | + |
| 48 | + override fun requestPermission() { |
| 49 | + when { |
| 50 | + permissionState.status.isGranted -> { |
| 51 | + onGranted() |
| 52 | + } |
| 53 | + |
| 54 | + permissionState.status.shouldShowRationale -> { |
| 55 | + permissionState.launchPermissionRequest() |
| 56 | + } |
| 57 | + |
| 58 | + else -> { |
| 59 | + if (permissionRequestCount == 0) { |
| 60 | + permissionState.launchPermissionRequest() |
| 61 | + } else { |
| 62 | + showSettingsDialog = true |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Composable |
| 69 | + override fun PermissionDialogs() { |
| 70 | + if (showSettingsDialog) { |
| 71 | + BitnagilAlertDialog( |
| 72 | + title = "권한 설정이 필요합니다.", |
| 73 | + description = dialogDescription, |
| 74 | + dismissButtonText = "닫기", |
| 75 | + confirmButtonText = "설정으로 이동", |
| 76 | + onConfirm = { |
| 77 | + showSettingsDialog = false |
| 78 | + val intent = Intent( |
| 79 | + Settings.ACTION_APPLICATION_DETAILS_SETTINGS, |
| 80 | + Uri.fromParts("package", context.packageName, null), |
| 81 | + ) |
| 82 | + context.startActivity(intent) |
| 83 | + }, |
| 84 | + onDismiss = { showSettingsDialog = false }, |
| 85 | + ) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return handler |
| 92 | +} |
0 commit comments