fix: Fix save folder check

Signed-off-by: Myzel394 <50424412+Myzel394@users.noreply.github.com>
This commit is contained in:
Myzel394 2024-08-20 22:25:02 +02:00
parent ffd1405a61
commit 7f644c4dc6
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
2 changed files with 78 additions and 7 deletions

View File

@ -572,19 +572,23 @@ abstract class BatchesFolder(
} }
fun canAccessFolder(context: Context, uri: Uri): Boolean { fun canAccessFolder(context: Context, uri: Uri): Boolean {
// This always returns false for some reason, let's just assume it's true
return true
/*
return try { return try {
// Create temp file // Create temp file
val tempFile = DocumentFile.fromSingleUri(context, uri)!!.createFile( val docFile = DocumentFile.fromSingleUri(context, uri)!!
"application/octet-stream",
"temp"
)!!
tempFile.delete()
true return docFile.canWrite().also {
println("Can write? ${it}")
} && docFile.canRead().also {
println("Can read? ${it}")
}
} catch (error: RuntimeException) { } catch (error: RuntimeException) {
error.printStackTrace() error.printStackTrace()
false false
} }
*/
} }
} }
} }

View File

@ -1,6 +1,7 @@
package app.myzel394.alibi.ui.components.WelcomeScreen.pages package app.myzel394.alibi.ui.components.WelcomeScreen.pages
import android.Manifest import android.Manifest
import android.content.Intent
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
@ -19,6 +20,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.InsertDriveFile import androidx.compose.material.icons.automirrored.filled.InsertDriveFile
import androidx.compose.material.icons.filled.ChevronLeft import androidx.compose.material.icons.filled.ChevronLeft
import androidx.compose.material.icons.filled.ChevronRight import androidx.compose.material.icons.filled.ChevronRight
import androidx.compose.material.icons.filled.Error
import androidx.compose.material.icons.filled.Folder import androidx.compose.material.icons.filled.Folder
import androidx.compose.material3.AlertDialog import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button import androidx.compose.material3.Button
@ -156,6 +158,17 @@ fun SaveFolderPage(
contentDescription = null, contentDescription = null,
) )
} }
var showError by rememberSaveable { mutableStateOf(false) }
if (showError) {
_FolderInaccessibleDialog(
onClose = {
showError = false
}
)
}
PermissionRequester( PermissionRequester(
permission = Manifest.permission.WRITE_EXTERNAL_STORAGE, permission = Manifest.permission.WRITE_EXTERNAL_STORAGE,
icon = Icons.AutoMirrored.Filled.InsertDriveFile, icon = Icons.AutoMirrored.Filled.InsertDriveFile,
@ -166,7 +179,23 @@ fun SaveFolderPage(
return@rememberFolderSelectorDialog return@rememberFolderSelectorDialog
} }
onContinue(saveFolder) context.contentResolver.takePersistableUriPermission(
folder,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
if (BatchesFolder.canAccessFolder(context, folder)) {
onContinue(folder.toString())
} else {
showError = true
runCatching {
context.contentResolver.releasePersistableUriPermission(
folder,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
}
}
} }
var showCustomFolderHint by rememberSaveable { mutableStateOf(false) } var showCustomFolderHint by rememberSaveable { mutableStateOf(false) }
@ -216,6 +245,44 @@ fun SaveFolderPage(
} }
} }
@Composable
fun _FolderInaccessibleDialog(
onClose: () -> Unit,
) {
AlertDialog(
onDismissRequest = onClose,
icon = {
Icon(
Icons.Default.Error,
contentDescription = null,
)
},
title = {
Text(stringResource(R.string.ui_error_occurred_title))
},
confirmButton = {
Button(onClick = onClose) {
Text(stringResource(R.string.dialog_close_neutral_label))
}
},
text = {
Column(
modifier = Modifier
.fillMaxWidth()
.verticalScroll(rememberScrollState()),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(32.dp),
) {
Text(
stringResource(R.string.ui_settings_option_saveFolder_batchesFolderInaccessible_error),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurface,
)
}
}
)
}
@Composable @Composable
fun _CustomFolderDialog( fun _CustomFolderDialog(
onAbort: () -> Unit, onAbort: () -> Unit,