Tweak DatabaseSettings UI

This commit is contained in:
vfsfitvnm 2022-10-04 16:06:47 +02:00
parent 693542f6e3
commit 8fc5ccabc6
2 changed files with 56 additions and 54 deletions

View file

@ -13,10 +13,7 @@ import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.autoSaver import androidx.compose.runtime.saveable.autoSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import it.vfsfitvnm.vimusic.Database import it.vfsfitvnm.vimusic.Database
@ -26,7 +23,6 @@ import it.vfsfitvnm.vimusic.internal
import it.vfsfitvnm.vimusic.path import it.vfsfitvnm.vimusic.path
import it.vfsfitvnm.vimusic.query import it.vfsfitvnm.vimusic.query
import it.vfsfitvnm.vimusic.service.PlayerService import it.vfsfitvnm.vimusic.service.PlayerService
import it.vfsfitvnm.vimusic.ui.components.themed.ConfirmationDialog
import it.vfsfitvnm.vimusic.ui.components.themed.Header import it.vfsfitvnm.vimusic.ui.components.themed.Header
import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance
import it.vfsfitvnm.vimusic.utils.intent import it.vfsfitvnm.vimusic.utils.intent
@ -46,12 +42,11 @@ fun DatabaseSettings() {
val context = LocalContext.current val context = LocalContext.current
val (colorPalette) = LocalAppearance.current val (colorPalette) = LocalAppearance.current
var isShowingRestoreDialog by rememberSaveable {
mutableStateOf(false)
}
val queriesCount by produceSaveableState(initialValue = 0, stateSaver = autoSaver()) { val queriesCount by produceSaveableState(initialValue = 0, stateSaver = autoSaver()) {
Database.queriesCount().flowOn(Dispatchers.IO).distinctUntilChanged().collect { value = it } Database.queriesCount()
.flowOn(Dispatchers.IO)
.distinctUntilChanged()
.collect { value = it }
} }
val openDocumentContract = ActivityResultContracts.OpenDocument() val openDocumentContract = ActivityResultContracts.OpenDocument()
@ -62,12 +57,12 @@ fun DatabaseSettings() {
query { query {
Database.internal.checkpoint() Database.internal.checkpoint()
context.applicationContext.contentResolver.openOutputStream(uri)
?.use { outputStream -> context.applicationContext.contentResolver.openOutputStream(uri)?.use { outputStream ->
FileInputStream(Database.internal.path).use { inputStream -> FileInputStream(Database.internal.path).use { inputStream ->
inputStream.copyTo(outputStream) inputStream.copyTo(outputStream)
}
} }
}
} }
} }
@ -78,11 +73,10 @@ fun DatabaseSettings() {
Database.internal.checkpoint() Database.internal.checkpoint()
Database.internal.close() Database.internal.close()
FileOutputStream(Database.internal.path).use { outputStream -> context.applicationContext.contentResolver.openInputStream(uri)?.use { inputStream ->
context.applicationContext.contentResolver.openInputStream(uri) FileOutputStream(Database.internal.path).use { outputStream ->
?.use { inputStream -> inputStream.copyTo(outputStream)
inputStream.copyTo(outputStream) }
}
} }
context.stopService(context.intent<PlayerService>()) context.stopService(context.intent<PlayerService>())
@ -90,34 +84,6 @@ fun DatabaseSettings() {
} }
} }
if (isShowingRestoreDialog) {
ConfirmationDialog(
text = "The application will automatically close itself to avoid problems after restoring the database.",
onDismiss = {
isShowingRestoreDialog = false
},
onConfirm = {
val input = arrayOf(
"application/x-sqlite3",
"application/vnd.sqlite3",
"application/octet-stream"
)
if (openDocumentContract.createIntent(context, input)
.resolveActivity(context.packageManager) != null
) {
restoreLauncher.launch(input)
} else {
Toast.makeText(
context,
"Can't read the database from the external storage",
Toast.LENGTH_SHORT
).show()
}
},
confirmText = "Ok"
)
}
Column( Column(
modifier = Modifier modifier = Modifier
@ -164,11 +130,13 @@ fun DatabaseSettings() {
) { ) {
backupLauncher.launch(input) backupLauncher.launch(input)
} else { } else {
Toast.makeText( Toast
context, .makeText(
"Can't copy the database to the external storage", context,
Toast.LENGTH_SHORT "Can't copy the database to the external storage",
).show() Toast.LENGTH_SHORT
)
.show()
} }
} }
) )
@ -177,12 +145,30 @@ fun DatabaseSettings() {
SettingsEntryGroupText(title = "RESTORE") SettingsEntryGroupText(title = "RESTORE")
SettingsDescription(text = "Existing data will be overwritten.") ImportantSettingsDescription(text = "Existing data will be overwritten.\n${context.applicationInfo.nonLocalizedLabel} will automatically close itself after restoring the database.")
SettingsEntry( SettingsEntry(
title = "Restore", title = "Restore",
text = "Import the database from the external storage", text = "Import the database from the external storage",
onClick = { isShowingRestoreDialog = true } onClick = {
val input = arrayOf(
"application/x-sqlite3",
"application/vnd.sqlite3",
"application/octet-stream"
)
if (openDocumentContract.createIntent(context, input)
.resolveActivity(context.packageManager) != null
) {
restoreLauncher.launch(input)
} else {
Toast.makeText(
context,
"Can't read the database from the external storage",
Toast.LENGTH_SHORT
).show()
}
}
) )
} }
} }

View file

@ -210,6 +210,22 @@ fun SettingsDescription(
) )
} }
@Composable
fun ImportantSettingsDescription(
text: String,
modifier: Modifier = Modifier,
) {
val (colorPalette, typography) = LocalAppearance.current
BasicText(
text = text,
style = typography.xxs.semiBold.color(colorPalette.red),
modifier = modifier
.padding(start = 16.dp)
.padding(horizontal = 16.dp, vertical = 8.dp)
)
}
@Composable @Composable
fun SettingsEntryGroupText( fun SettingsEntryGroupText(
title: String, title: String,