Use the real name of the selected export folder rather than manipulating the URI
This commit is contained in:
@@ -15,9 +15,11 @@
|
||||
package org.fairscan.app.ui.screens.settings
|
||||
|
||||
import android.content.Context
|
||||
import androidx.core.net.toUri
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import org.fairscan.app.domain.ExportQuality
|
||||
@@ -35,6 +37,12 @@ class SettingsRepository(private val context: Context) {
|
||||
prefs[EXPORT_DIR_URI]
|
||||
}
|
||||
|
||||
val exportDirName: Flow<String?> = exportDirUri.map { uriString ->
|
||||
uriString?.let {
|
||||
DocumentFile.fromTreeUri(context, it.toUri())?.name
|
||||
}
|
||||
}
|
||||
|
||||
val exportFormat: Flow<ExportFormat> =
|
||||
context.dataStore.data.map { prefs ->
|
||||
when (prefs[EXPORT_FORMAT]) {
|
||||
|
||||
@@ -92,10 +92,7 @@ private fun SettingsContent(
|
||||
onExportQualityChanged: (ExportQuality) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val folderName = remember(uiState.exportDirUri) {
|
||||
extractFolderName(uiState.exportDirUri, context)
|
||||
}
|
||||
val folderName = uiState.exportDirName ?: stringResource(R.string.download_dirname)
|
||||
|
||||
Column(
|
||||
modifier
|
||||
@@ -111,7 +108,7 @@ private fun SettingsContent(
|
||||
|
||||
Spacer(Modifier.height(12.dp))
|
||||
|
||||
if (uiState.exportDirUri != null) {
|
||||
if (uiState.exportDirName != null) {
|
||||
OutlinedButton(
|
||||
onClick = onResetExportDirClick,
|
||||
border = BorderStroke(1.dp, MaterialTheme.colorScheme.primary),
|
||||
@@ -197,14 +194,6 @@ fun DirectorySettingItem(
|
||||
}
|
||||
}
|
||||
|
||||
private fun extractFolderName(uriString: String?, context: Context): String {
|
||||
if (uriString == null) return context.getString(R.string.download_dirname)
|
||||
return runCatching {
|
||||
val uri = uriString.toUri()
|
||||
uri.lastPathSegment?.substringAfter(':')?.substringAfter('/') ?: uriString
|
||||
}.getOrElse { uriString }
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
fun SettingsScreenPreviewWithoutDir() {
|
||||
|
||||
@@ -16,13 +16,15 @@ package org.fairscan.app.ui.screens.settings
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import org.fairscan.app.AppContainer
|
||||
import org.fairscan.app.domain.ExportQuality
|
||||
|
||||
data class SettingsUiState(
|
||||
val exportDirUri: String? = null,
|
||||
val exportDirName: String? = null,
|
||||
val exportFormat: ExportFormat = ExportFormat.PDF,
|
||||
val exportQuality: ExportQuality = ExportQuality.BALANCED,
|
||||
)
|
||||
@@ -32,12 +34,12 @@ class SettingsViewModel(container: AppContainer) : ViewModel() {
|
||||
private val repo = container.settingsRepository
|
||||
|
||||
val uiState = combine(
|
||||
repo.exportDirUri,
|
||||
repo.exportDirName,
|
||||
repo.exportFormat,
|
||||
repo.exportQuality,
|
||||
) { dir, format, quality ->
|
||||
SettingsUiState(
|
||||
exportDirUri = dir,
|
||||
exportDirName = dir,
|
||||
exportFormat = format,
|
||||
exportQuality = quality,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user