fix: Properly take persistable uri for open document tree

This commit is contained in:
Myzel394 2023-11-03 21:43:23 +01:00
parent 8f8376cd16
commit 6948e11fca
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B
3 changed files with 37 additions and 11 deletions

View File

@ -129,4 +129,6 @@ dependencies {
implementation 'com.maxkeppeler.sheets-compose-dialogs:duration:1.2.0'
implementation 'com.maxkeppeler.sheets-compose-dialogs:list:1.2.0'
implementation 'com.maxkeppeler.sheets-compose-dialogs:input:1.2.0'
implementation 'androidx.activity:activity-ktx:1.8.0'
}

View File

@ -1,12 +1,11 @@
package app.myzel394.alibi.ui.components.SettingsScreen.atoms
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.background
import android.content.Intent
import android.net.Uri
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
@ -29,19 +28,15 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.core.net.toFile
import app.myzel394.alibi.R
import app.myzel394.alibi.dataStore
import app.myzel394.alibi.db.AppSettings
import app.myzel394.alibi.helpers.AudioRecorderExporter
import app.myzel394.alibi.ui.components.atoms.SettingsTile
import app.myzel394.alibi.ui.utils.rememberFolderSelectorDialog
import com.maxkeppeker.sheets.core.models.base.rememberUseCaseState
import kotlinx.coroutines.launch
@Composable
@ -53,6 +48,15 @@ fun SaveFolderTile(
val dataStore = context.dataStore
fun updateValue(path: String?) {
if (settings.audioRecorderSettings.saveFolder != null) {
runCatching {
context.contentResolver.releasePersistableUriPermission(
Uri.parse(settings.audioRecorderSettings.saveFolder),
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
}
}
scope.launch {
dataStore.updateData {
it.setAudioRecorderSettings(
@ -67,6 +71,11 @@ fun SaveFolderTile(
return@rememberFolderSelectorDialog
}
context.contentResolver.takePersistableUriPermission(
folder,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
updateValue(folder.toString())
}

View File

@ -1,9 +1,12 @@
package app.myzel394.alibi.ui.utils
import android.app.Activity
import android.content.Intent
import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
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
@ -63,11 +66,23 @@ fun rememberFolderSelectorDialog(
): (() -> Unit) {
val launcher =
rememberLauncherForActivityResult(
ActivityResultContracts.OpenDocumentTree(),
callback,
)
ActivityResultContracts.StartActivityForResult()
) {
if (it.resultCode == Activity.RESULT_OK) {
val uri = it.data?.data
callback(uri)
}
}
return {
launcher.launch(null)
launcher.launch(Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
addFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION
or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
or Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
or Intent.FLAG_GRANT_PREFIX_URI_PERMISSION
)
})
}
}