-
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.
Merge pull request #11 from f-lab-edu/feature/9
#9 - Permission 작업
- Loading branch information
Showing
10 changed files
with
242 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,61 @@ | ||
package ny.photomap | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import com.google.accompanist.permissions.ExperimentalPermissionsApi | ||
import com.google.accompanist.permissions.rememberMultiplePermissionsState | ||
import com.google.maps.android.compose.GoogleMap | ||
import ny.photomap.permission.ReadImagePermission | ||
|
||
@OptIn(ExperimentalPermissionsApi::class) | ||
@Composable | ||
fun MapScreen(modifier: Modifier) { | ||
GoogleMap( | ||
modifier = modifier.fillMaxSize(), | ||
) | ||
Scaffold(modifier = modifier.fillMaxSize()) { padding -> | ||
|
||
val permission = ReadImagePermission | ||
val permissionState = rememberMultiplePermissionsState(permission.permission) | ||
var visualUserSelectedGranted by remember { mutableStateOf(false) } | ||
var onDenied by remember { mutableStateOf(false) } | ||
PermissionRequester( | ||
permission = permission, | ||
permissionsState = permissionState, | ||
onAllGranted = { | ||
// todo 이미지 파일 조회 | ||
}, | ||
onVisualUserSelectedGranted = { | ||
visualUserSelectedGranted = true | ||
}, | ||
onDenied = { _ -> | ||
onDenied = true | ||
}) | ||
|
||
Column(modifier = Modifier.fillMaxSize()) { | ||
if (visualUserSelectedGranted || onDenied) { | ||
val title: String = stringResource(permission.titleRes) | ||
val description: String = if (visualUserSelectedGranted) { | ||
stringResource(permission.visualUserSelectedDescriptionRes) | ||
} else { | ||
stringResource(permission.needDescriptionRes) | ||
} | ||
|
||
//todo UI 작업 | ||
} | ||
GoogleMap( | ||
modifier = modifier | ||
.padding(padding) | ||
.fillMaxSize(), | ||
) | ||
} | ||
|
||
} | ||
|
||
} |
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,68 @@ | ||
package ny.photomap | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import com.google.accompanist.permissions.ExperimentalPermissionsApi | ||
import com.google.accompanist.permissions.MultiplePermissionsState | ||
import com.google.accompanist.permissions.rememberMultiplePermissionsState | ||
import ny.photomap.permission.Permission | ||
import ny.photomap.permission.PermissionStateCheckerImpl | ||
import ny.photomap.permission.PermissionVisualUserSelected | ||
import ny.photomap.permission.PermissionVisualUserSelectedStateCheckerImpl | ||
|
||
|
||
@OptIn(ExperimentalPermissionsApi::class) | ||
@Composable | ||
fun PermissionRequester( | ||
permission: Permission, | ||
permissionsState: MultiplePermissionsState = rememberMultiplePermissionsState(permission.permission), | ||
onAllGranted: () -> Unit, | ||
onVisualUserSelectedGranted: (() -> Unit)? = null, | ||
onDenied: (shouldShowRationale: Boolean) -> Unit, | ||
) { | ||
if (permission.permission.isEmpty()) return | ||
var permissionRequested: Boolean by remember { mutableStateOf(false) } | ||
|
||
if (permission is PermissionVisualUserSelected) { | ||
val permissionChecker = PermissionVisualUserSelectedStateCheckerImpl(permission) | ||
when { | ||
permissionChecker.isGranted(permissionsState.permissions) -> onAllGranted() | ||
else -> when { | ||
permissionChecker.isVisualUserSelectedGranted(permissionsState.permissions) | ||
-> onVisualUserSelectedGranted?.invoke() | ||
|
||
permissionChecker.isDenied(permissionsState.permissions) | ||
-> if (permissionRequested) { | ||
onDenied(permissionChecker.shouldShowRationale(permissionsState.permissions)) | ||
} else { | ||
LaunchedEffect(Unit) { | ||
permissionsState.launchMultiplePermissionRequest() | ||
permissionRequested = !permissionRequested | ||
} | ||
} | ||
|
||
} | ||
} | ||
} else { | ||
val permissionChecker = PermissionStateCheckerImpl(permission) | ||
when { | ||
permissionChecker.isGranted(permissionsState.permissions) | ||
-> onAllGranted() | ||
|
||
permissionChecker.isDenied(permissionsState.permissions) | ||
-> if (!permissionRequested) { | ||
LaunchedEffect(Unit) { | ||
permissionsState.launchMultiplePermissionRequest() | ||
} | ||
permissionRequested = !permissionRequested | ||
} else { | ||
onDenied(permissionChecker.shouldShowRationale(permissionsState.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ny.photomap.permission | ||
|
||
import android.annotation.TargetApi | ||
import android.os.Build | ||
import androidx.annotation.StringRes | ||
import ny.photomap.R | ||
|
||
interface Permission { | ||
val permission: List<String> | ||
val titleRes: Int | ||
val needDescriptionRes: Int | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) | ||
interface PermissionVisualUserSelected : Permission { | ||
val visualUserSelectedDescriptionRes: Int | ||
|
||
val mainPermission: String | ||
val visualUserSelectedPermission: String | ||
get() = android.Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED | ||
} | ||
|
||
|
||
object ReadImagePermission : PermissionVisualUserSelected { | ||
override val mainPermission: String = | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) android.Manifest.permission.READ_MEDIA_IMAGES | ||
else "" | ||
|
||
override val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) | ||
listOf( | ||
mainPermission, | ||
visualUserSelectedPermission | ||
) | ||
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) | ||
listOf(mainPermission) | ||
else listOf() | ||
|
||
@StringRes | ||
override val titleRes = R.string.permission_read_image_title | ||
|
||
@StringRes | ||
override val needDescriptionRes = R.string.permission_read_image_description | ||
|
||
@StringRes | ||
override val visualUserSelectedDescriptionRes: Int = | ||
R.string.permission_read_image_visual_user_selected_description | ||
|
||
} | ||
|
||
|
||
|
49 changes: 49 additions & 0 deletions
49
app/src/main/java/ny/photomap/permission/PermissionStateChecker.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,49 @@ | ||
package ny.photomap.permission | ||
|
||
import com.google.accompanist.permissions.ExperimentalPermissionsApi | ||
import com.google.accompanist.permissions.PermissionState | ||
import com.google.accompanist.permissions.isGranted | ||
import com.google.accompanist.permissions.shouldShowRationale | ||
|
||
|
||
@OptIn(ExperimentalPermissionsApi::class) | ||
interface PermissionStateChecker { | ||
fun isGranted(permissionState: List<PermissionState>): Boolean | ||
fun isDenied(permissionState: List<PermissionState>): Boolean | ||
fun shouldShowRationale(permissionState: List<PermissionState>): Boolean | ||
} | ||
|
||
@OptIn(ExperimentalPermissionsApi::class) | ||
interface PermissionVisualUserSelectedStateChecker : PermissionStateChecker { | ||
fun isVisualUserSelectedGranted(permissionState: List<PermissionState>): Boolean | ||
} | ||
|
||
@OptIn(ExperimentalPermissionsApi::class) | ||
class PermissionVisualUserSelectedStateCheckerImpl(val permission: PermissionVisualUserSelected) : | ||
PermissionVisualUserSelectedStateChecker { | ||
override fun isVisualUserSelectedGranted(permissionState: List<PermissionState>): Boolean = | ||
permissionState.any { it.permission == permission.mainPermission && it.status.isGranted } | ||
|
||
override fun isGranted(permissionState: List<PermissionState>): Boolean = | ||
permissionState.any { it.permission == permission.visualUserSelectedPermission && it.status.isGranted } | ||
|
||
override fun isDenied(permissionState: List<PermissionState>): Boolean = | ||
permissionState.all { !it.status.isGranted } | ||
|
||
override fun shouldShowRationale(permissionState: List<PermissionState>): Boolean = | ||
permissionState.any { it.permission == permission.mainPermission && it.status.shouldShowRationale } | ||
|
||
} | ||
|
||
@OptIn(ExperimentalPermissionsApi::class) | ||
class PermissionStateCheckerImpl(val permission: Permission) : PermissionStateChecker { | ||
override fun isGranted(permissionState: List<PermissionState>): Boolean = | ||
permissionState.any { it.permission == permission.permission[0] && it.status.isGranted } | ||
|
||
override fun isDenied(permissionState: List<PermissionState>): Boolean = | ||
permissionState.all { !it.status.isGranted } | ||
|
||
override fun shouldShowRationale(permissionState: List<PermissionState>): Boolean = | ||
permissionState.any { it.permission == permission.permission[0] && it.status.shouldShowRationale } | ||
|
||
} |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
<resources> | ||
<string name="app_name">PhotoMap</string> | ||
<string name="title_activity_main2">MainActivity2</string> | ||
<string name="title_activity_main">MainActivity</string> | ||
<string name="permission_read_image_title">이미지 파일 엑세스</string> | ||
<string name="permission_read_image_description">서비스를 이용하기 위해서 이미지 파일 엑세스 권한이 필요합니다.</string> | ||
<string name="permission_read_image_visual_user_selected_description">선택한 일부 사진에만 엑세스 권한을 부여했습니다.</string> | ||
</resources> |
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