Preview themes before importing and catch errors
This commit is contained in:
parent
0b95157714
commit
365c006a7b
@ -99,10 +99,12 @@ fun ImportThemeSheet(
|
||||
modifier = Modifier
|
||||
.padding(it)
|
||||
.fillMaxWidth()
|
||||
.aspectRatio(1f),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
LargeMessage(
|
||||
icon = Icons.Rounded.ErrorOutline,
|
||||
text = "Theme could not be read. Is the file corrupted?"
|
||||
text = stringResource(R.string.import_theme_error),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package de.mm20.launcher2.ui.settings.colorscheme
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.background
|
||||
@ -46,6 +47,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import de.mm20.launcher2.themes.Theme
|
||||
import de.mm20.launcher2.ui.R
|
||||
import de.mm20.launcher2.ui.common.ImportThemeSheet
|
||||
import de.mm20.launcher2.ui.component.preferences.Preference
|
||||
import de.mm20.launcher2.ui.component.preferences.PreferenceCategory
|
||||
import de.mm20.launcher2.ui.component.preferences.PreferenceScreen
|
||||
@ -65,8 +67,10 @@ fun ThemesSettingsScreen() {
|
||||
|
||||
var deleteTheme by remember { mutableStateOf<Theme?>(null) }
|
||||
|
||||
var importThemeUri by remember { mutableStateOf<Uri?>(null) }
|
||||
|
||||
val importIntentLauncher = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) {
|
||||
viewModel.importTheme(context, it)
|
||||
importThemeUri = it
|
||||
}
|
||||
|
||||
PreferenceScreen(
|
||||
@ -188,6 +192,10 @@ fun ThemesSettingsScreen() {
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (importThemeUri != null) {
|
||||
ImportThemeSheet(uri = importThemeUri!!, onDismiss = { importThemeUri = null })
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
||||
@ -88,16 +88,6 @@ class ThemesSettingsScreenVM : ViewModel(), KoinComponent {
|
||||
}
|
||||
}
|
||||
|
||||
fun importTheme(context: Context, uri: Uri?) {
|
||||
uri ?: return
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
context.contentResolver.openInputStream(uri)?.use {
|
||||
val theme = Theme.fromJson(it.readBytes().toString(Charsets.UTF_8))
|
||||
themeRepository.createTheme(theme.copy(id = UUID.randomUUID()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun createNew(context: Context) {
|
||||
themeRepository.createTheme(
|
||||
Theme(
|
||||
|
||||
@ -841,4 +841,5 @@
|
||||
<string name="theme_color_scheme_custom_color">Custom</string>
|
||||
<string name="preference_restore_default">Restore default</string>
|
||||
<string name="import_theme_apply">Apply theme</string>
|
||||
<string name="import_theme_error">The selected file could not be read. Please make sure that you selected a valid theme file (*.kvtheme), and that the file is not corrupt.</string>
|
||||
</resources>
|
||||
Loading…
x
Reference in New Issue
Block a user