Skip to content

Commit

Permalink
[fix]better at handling permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve-Mr committed Sep 25, 2024
1 parent 7559963 commit 168ac9b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/top/maary/oblivionis/OblivionisScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fun OblivionisApp(
){
composable (route = OblivionisScreen.Welcome.name) {
WelcomeScreen (onPermissionFinished = {
actionViewModel.loadAlbums()
navController.navigate(OblivionisScreen.Entry.name) {
popUpTo(OblivionisScreen.Welcome.name) {
inclusive = true // 将 WelcomeScreen 从栈中移除
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/top/maary/oblivionis/ui/WelcomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -24,6 +25,8 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberMultiplePermissionsState

Expand All @@ -48,6 +51,8 @@ fun WelcomeScreen(onPermissionFinished: () -> Unit) {
if (!permissionHandled) {
if (storagePermissionState.allPermissionsGranted and manageMediaPermissionState.value) {
permissionHandled = true
Log.v("OBLIVIONIS", "PERMISSION GRANT")

onPermissionFinished()
} else {
Column (modifier = Modifier.fillMaxSize().padding(innerPadding),
Expand All @@ -69,27 +74,24 @@ fun WelcomeScreen(onPermissionFinished: () -> Unit) {
}
}
}
Log.v("YDNM", "1")
}



}

@Composable
fun rememberCanManageMediaState(): State<Boolean> {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current
val state by lifecycleOwner.lifecycle.currentStateFlow.collectAsState()
var canManageMedia by remember { mutableStateOf(false) }

LaunchedEffect(context) {
LaunchedEffect(state) {
canManageMedia = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// 检查应用是否可以管理媒体
MediaStore.canManageMedia(context)
} else {
true // 如果版本低于 Android 12,默认返回 true
}
}
Log.v("YDNM", "2")

return rememberUpdatedState(newValue = canManageMedia)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class ActionViewModel(
private val _albums = MutableStateFlow<List<Album>>(emptyList())
val albums: StateFlow<List<Album>> get() = _albums

private fun loadAlbums() {
fun loadAlbums() {
viewModelScope.launch(Dispatchers.IO) {
val albumList = getAlbumsFromMediaStore(getApplication<Application>().contentResolver)
_albums.value = albumList
Expand Down Expand Up @@ -404,6 +404,7 @@ class ActionViewModel(

init {
loadAlbums()
/* TODO 获取权限 */
}

fun databaseMark(image: MediaStoreImage) = viewModelScope.launch {
Expand Down

0 comments on commit 168ac9b

Please sign in to comment.