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
|
package org.fairscan.app.ui.screens.settings
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import androidx.core.net.toUri
|
||||||
import androidx.datastore.preferences.core.edit
|
import androidx.datastore.preferences.core.edit
|
||||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||||
import androidx.datastore.preferences.preferencesDataStore
|
import androidx.datastore.preferences.preferencesDataStore
|
||||||
|
import androidx.documentfile.provider.DocumentFile
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import org.fairscan.app.domain.ExportQuality
|
import org.fairscan.app.domain.ExportQuality
|
||||||
@@ -35,6 +37,12 @@ class SettingsRepository(private val context: Context) {
|
|||||||
prefs[EXPORT_DIR_URI]
|
prefs[EXPORT_DIR_URI]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val exportDirName: Flow<String?> = exportDirUri.map { uriString ->
|
||||||
|
uriString?.let {
|
||||||
|
DocumentFile.fromTreeUri(context, it.toUri())?.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val exportFormat: Flow<ExportFormat> =
|
val exportFormat: Flow<ExportFormat> =
|
||||||
context.dataStore.data.map { prefs ->
|
context.dataStore.data.map { prefs ->
|
||||||
when (prefs[EXPORT_FORMAT]) {
|
when (prefs[EXPORT_FORMAT]) {
|
||||||
|
|||||||
@@ -92,10 +92,7 @@ private fun SettingsContent(
|
|||||||
onExportQualityChanged: (ExportQuality) -> Unit,
|
onExportQualityChanged: (ExportQuality) -> Unit,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
val context = LocalContext.current
|
val folderName = uiState.exportDirName ?: stringResource(R.string.download_dirname)
|
||||||
val folderName = remember(uiState.exportDirUri) {
|
|
||||||
extractFolderName(uiState.exportDirUri, context)
|
|
||||||
}
|
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
modifier
|
modifier
|
||||||
@@ -111,7 +108,7 @@ private fun SettingsContent(
|
|||||||
|
|
||||||
Spacer(Modifier.height(12.dp))
|
Spacer(Modifier.height(12.dp))
|
||||||
|
|
||||||
if (uiState.exportDirUri != null) {
|
if (uiState.exportDirName != null) {
|
||||||
OutlinedButton(
|
OutlinedButton(
|
||||||
onClick = onResetExportDirClick,
|
onClick = onResetExportDirClick,
|
||||||
border = BorderStroke(1.dp, MaterialTheme.colorScheme.primary),
|
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
|
@Preview
|
||||||
@Composable
|
@Composable
|
||||||
fun SettingsScreenPreviewWithoutDir() {
|
fun SettingsScreenPreviewWithoutDir() {
|
||||||
|
|||||||
@@ -16,13 +16,15 @@ package org.fairscan.app.ui.screens.settings
|
|||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
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 kotlinx.coroutines.launch
|
||||||
import org.fairscan.app.AppContainer
|
import org.fairscan.app.AppContainer
|
||||||
import org.fairscan.app.domain.ExportQuality
|
import org.fairscan.app.domain.ExportQuality
|
||||||
|
|
||||||
data class SettingsUiState(
|
data class SettingsUiState(
|
||||||
val exportDirUri: String? = null,
|
val exportDirName: String? = null,
|
||||||
val exportFormat: ExportFormat = ExportFormat.PDF,
|
val exportFormat: ExportFormat = ExportFormat.PDF,
|
||||||
val exportQuality: ExportQuality = ExportQuality.BALANCED,
|
val exportQuality: ExportQuality = ExportQuality.BALANCED,
|
||||||
)
|
)
|
||||||
@@ -32,12 +34,12 @@ class SettingsViewModel(container: AppContainer) : ViewModel() {
|
|||||||
private val repo = container.settingsRepository
|
private val repo = container.settingsRepository
|
||||||
|
|
||||||
val uiState = combine(
|
val uiState = combine(
|
||||||
repo.exportDirUri,
|
repo.exportDirName,
|
||||||
repo.exportFormat,
|
repo.exportFormat,
|
||||||
repo.exportQuality,
|
repo.exportQuality,
|
||||||
) { dir, format, quality ->
|
) { dir, format, quality ->
|
||||||
SettingsUiState(
|
SettingsUiState(
|
||||||
exportDirUri = dir,
|
exportDirName = dir,
|
||||||
exportFormat = format,
|
exportFormat = format,
|
||||||
exportQuality = quality,
|
exportQuality = quality,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user