mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2026-11-23 15:47:20 +00:00
add backup settings screen and test
This commit is contained in:
1 parent
6a6b8d1f3c
commit
65963a2f0d
10 files changed
+611
-8
No files matched your search
@@ -46,7 +46,6 @@ import org.schabi.newpipe.util.ZipHelper
|
||||
/**
|
||||
* Host-side backup and restore for the Compose backup and restore settings screen
|
||||
*/
|
||||
@Suppress("unused")
|
||||
class ComposeBackupRestoreHost(private val context: Context) : BackupRestoreHost {
|
||||
|
||||
private val manager = ImportExportManager(BackupFileLocator(context))
|
||||
@@ -87,7 +86,6 @@ class ComposeBackupRestoreHost(private val context: Context) : BackupRestoreHost
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION") // Old backups can only be recognised through the deprecated format
|
||||
override suspend fun importDatabase(file: String): DataImportResult = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val backup = zipFile(file)
|
||||
@@ -109,7 +107,6 @@ class ComposeBackupRestoreHost(private val context: Context) : BackupRestoreHost
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION") // Old backups can only be read through the deprecated format
|
||||
override suspend fun importSettings(file: String, format: SettingsFormat) = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val backup = zipFile(file)
|
||||
@@ -150,10 +147,6 @@ class ComposeBackupRestoreHost(private val context: Context) : BackupRestoreHost
|
||||
.enqueueUniqueWork(SubscriptionImportWorker.WORK_NAME, ExistingWorkPolicy.APPEND_OR_REPLACE, request)
|
||||
}
|
||||
|
||||
/**
|
||||
* Media tunneling is switched off automatically on devices known to break it, so that choice
|
||||
* must not travel to another device with the imported settings
|
||||
*/
|
||||
private fun resetDeviceSpecificSettings() {
|
||||
val tunnelingKey = context.getString(R.string.disable_media_tunneling_key)
|
||||
val automaticTunnelingKey = context.getString(R.string.disabled_media_tunneling_automatically_key)
|
||||
|
||||
@@ -14,6 +14,9 @@ import kotlinx.serialization.Serializable
|
||||
@Serializable
|
||||
sealed interface Destination : NavKey {
|
||||
|
||||
@Serializable
|
||||
data object BackupRestoreSettings : Destination
|
||||
|
||||
@Serializable
|
||||
data object UpdateSettings : Destination
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import androidx.compose.runtime.mutableStateListOf
|
||||
import co.touchlab.kermit.Logger
|
||||
import net.newpipe.app.screen.about.AboutScreen
|
||||
import net.newpipe.app.screen.settings.AppearanceSettingsScreen
|
||||
import net.newpipe.app.screen.settings.BackupRestoreSettingsScreen
|
||||
import net.newpipe.app.screen.settings.ContentSettingsScreen
|
||||
import net.newpipe.app.screen.settings.DebugSettingsScreen
|
||||
import net.newpipe.app.screen.settings.DownloadSettingsScreen
|
||||
@@ -77,6 +78,10 @@ fun navModule() = module {
|
||||
navigation<Destination.UpdateSettings> {
|
||||
UpdateSettingsScreen()
|
||||
}
|
||||
|
||||
navigation<Destination.BackupRestoreSettings> {
|
||||
BackupRestoreSettingsScreen()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+362
@@ -0,0 +1,362 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package net.newpipe.app.screen.settings
|
||||
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.WindowInsets
|
||||
import androidx.compose.foundation.layout.asPaddingValues
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.navigationBars
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.SnackbarDuration
|
||||
import androidx.compose.material3.SnackbarHost
|
||||
import androidx.compose.material3.SnackbarHostState
|
||||
import androidx.compose.material3.SnackbarResult
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.tooling.preview.PreviewLightDark
|
||||
import androidx.compose.ui.tooling.preview.PreviewWrapper
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import kotlinx.coroutines.launch
|
||||
import net.newpipe.app.composable.PreferenceRow
|
||||
import net.newpipe.app.composable.TopAppBar
|
||||
import net.newpipe.app.composable.noFileManagerMessage
|
||||
import net.newpipe.app.model.BackupFile
|
||||
import net.newpipe.app.model.SettingsFormat
|
||||
import net.newpipe.app.navigation.Navigator
|
||||
import net.newpipe.app.platform.BackupRestoreActions
|
||||
import net.newpipe.app.platform.DownloadStorageActions
|
||||
import net.newpipe.app.preview.ThemePreviewProvider
|
||||
import net.newpipe.app.screen.settings.model.BackupRestoreConfirmation
|
||||
import net.newpipe.app.screen.settings.model.BackupRestoreMessage
|
||||
import net.newpipe.app.screen.settings.model.SettingsImport
|
||||
import net.newpipe.app.viewmodel.settings.BackupRestoreSettingsViewModel
|
||||
import newpipe.shared.generated.resources.Res
|
||||
import newpipe.shared.generated.resources.cancel
|
||||
import newpipe.shared.generated.resources.could_not_import_all_files
|
||||
import newpipe.shared.generated.resources.error_snackbar_action
|
||||
import newpipe.shared.generated.resources.export_complete_toast
|
||||
import newpipe.shared.generated.resources.export_data_summary
|
||||
import newpipe.shared.generated.resources.export_data_title
|
||||
import newpipe.shared.generated.resources.export_subscriptions_summary
|
||||
import newpipe.shared.generated.resources.export_subscriptions_title
|
||||
import newpipe.shared.generated.resources.import_data_summary
|
||||
import newpipe.shared.generated.resources.import_data_title
|
||||
import newpipe.shared.generated.resources.import_network_expensive_warning
|
||||
import newpipe.shared.generated.resources.import_settings
|
||||
import newpipe.shared.generated.resources.import_settings_vulnerable_format
|
||||
import newpipe.shared.generated.resources.import_subscriptions_summary
|
||||
import newpipe.shared.generated.resources.import_subscriptions_title
|
||||
import newpipe.shared.generated.resources.no_app_to_open_intent
|
||||
import newpipe.shared.generated.resources.no_valid_zip_file
|
||||
import newpipe.shared.generated.resources.ok
|
||||
import newpipe.shared.generated.resources.override_current_data
|
||||
import newpipe.shared.generated.resources.reset_all_settings
|
||||
import newpipe.shared.generated.resources.reset_settings_summary
|
||||
import newpipe.shared.generated.resources.reset_settings_title
|
||||
import newpipe.shared.generated.resources.settings_category_backup_restore_title
|
||||
import org.jetbrains.compose.resources.stringResource
|
||||
import org.koin.compose.koinInject
|
||||
import org.koin.compose.viewmodel.koinViewModel
|
||||
|
||||
@Composable
|
||||
fun BackupRestoreSettingsScreen(
|
||||
navigator: Navigator = koinInject(),
|
||||
viewModel: BackupRestoreSettingsViewModel = koinViewModel(),
|
||||
backupRestoreActions: BackupRestoreActions = koinInject(),
|
||||
storage: DownloadStorageActions = koinInject()
|
||||
) {
|
||||
val message by viewModel.message.collectAsStateWithLifecycle()
|
||||
val pendingSettingsImport by viewModel.pendingSettingsImport.collectAsStateWithLifecycle()
|
||||
val storageAccessFrameworkConfigurable = remember { storage.isStorageAccessFrameworkConfigurable }
|
||||
|
||||
var pendingConfirmation by remember { mutableStateOf<BackupRestoreConfirmation?>(null) }
|
||||
var noFileManager by remember { mutableStateOf(false) }
|
||||
val showNoFileManager = { noFileManager = true }
|
||||
|
||||
val pickDataImport = backupRestoreActions.rememberFilePicker(
|
||||
BackupFile.DATA_IMPORT,
|
||||
showNoFileManager
|
||||
) { file ->
|
||||
pendingConfirmation = BackupRestoreConfirmation.DataImport(file)
|
||||
}
|
||||
val pickDataExport = backupRestoreActions.rememberFilePicker(
|
||||
BackupFile.DATA_EXPORT,
|
||||
showNoFileManager,
|
||||
viewModel::exportData
|
||||
)
|
||||
val pickSubscriptionsExport = backupRestoreActions.rememberFilePicker(
|
||||
BackupFile.SUBSCRIPTIONS_EXPORT,
|
||||
showNoFileManager,
|
||||
backupRestoreActions::exportSubscriptions
|
||||
)
|
||||
val pickSubscriptionsImport = backupRestoreActions.rememberFilePicker(
|
||||
BackupFile.SUBSCRIPTIONS_IMPORT,
|
||||
showNoFileManager
|
||||
) { file ->
|
||||
pendingConfirmation = BackupRestoreConfirmation.SubscriptionsImport(file)
|
||||
}
|
||||
|
||||
BackupRestoreSettingsScreenContent(
|
||||
message = message,
|
||||
pendingConfirmation = pendingConfirmation,
|
||||
pendingSettingsImport = pendingSettingsImport,
|
||||
noFileManager = noFileManager,
|
||||
storageAccessFrameworkConfigurable = storageAccessFrameworkConfigurable,
|
||||
onImportData = pickDataImport,
|
||||
onExportData = pickDataExport,
|
||||
onResetSettings = { pendingConfirmation = BackupRestoreConfirmation.ResetSettings },
|
||||
onExportSubscriptions = pickSubscriptionsExport,
|
||||
onImportSubscriptions = pickSubscriptionsImport,
|
||||
onConfirm = { confirmation ->
|
||||
when (confirmation) {
|
||||
BackupRestoreConfirmation.ResetSettings -> backupRestoreActions.resetSettings()
|
||||
is BackupRestoreConfirmation.DataImport -> viewModel.importData(confirmation.file)
|
||||
is BackupRestoreConfirmation.SubscriptionsImport -> backupRestoreActions.importSubscriptions(confirmation.file)
|
||||
}
|
||||
},
|
||||
onDismissConfirmation = { pendingConfirmation = null },
|
||||
onImportSettings = viewModel::importSettings,
|
||||
onSkipSettingsImport = viewModel::skipSettingsImport,
|
||||
onDismissNoFileManager = { noFileManager = false },
|
||||
onMessageShown = viewModel::onMessageShown,
|
||||
onNavigateUp = { navigator.navigateUp() }
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun BackupRestoreSettingsScreenContent(
|
||||
message: BackupRestoreMessage? = null,
|
||||
pendingConfirmation: BackupRestoreConfirmation? = null,
|
||||
pendingSettingsImport: SettingsImport? = null,
|
||||
noFileManager: Boolean = false,
|
||||
storageAccessFrameworkConfigurable: Boolean = false,
|
||||
onImportData: () -> Unit = {},
|
||||
onExportData: () -> Unit = {},
|
||||
onResetSettings: () -> Unit = {},
|
||||
onExportSubscriptions: () -> Unit = {},
|
||||
onImportSubscriptions: () -> Unit = {},
|
||||
onConfirm: (BackupRestoreConfirmation) -> Unit = {},
|
||||
onDismissConfirmation: () -> Unit = {},
|
||||
onImportSettings: (SettingsImport) -> Unit = {},
|
||||
onSkipSettingsImport: (SettingsImport) -> Unit = {},
|
||||
onDismissNoFileManager: () -> Unit = {},
|
||||
onMessageShown: () -> Unit = {},
|
||||
onNavigateUp: () -> Unit = {}
|
||||
) {
|
||||
val scope = rememberCoroutineScope()
|
||||
val snackbarHostState = remember { SnackbarHostState() }
|
||||
val reportText = stringResource(Res.string.error_snackbar_action)
|
||||
|
||||
val messageText = message?.let { pending ->
|
||||
when (pending) {
|
||||
BackupRestoreMessage.ExportComplete -> {
|
||||
stringResource(Res.string.export_complete_toast)
|
||||
}
|
||||
|
||||
BackupRestoreMessage.InvalidFile -> {
|
||||
stringResource(Res.string.no_valid_zip_file)
|
||||
}
|
||||
|
||||
BackupRestoreMessage.PartialImport -> {
|
||||
stringResource(Res.string.could_not_import_all_files)
|
||||
}
|
||||
|
||||
is BackupRestoreMessage.Failed -> {
|
||||
pending.error.message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(message) {
|
||||
if (message == null || messageText == null) {
|
||||
return@LaunchedEffect
|
||||
}
|
||||
val error = (message as? BackupRestoreMessage.Failed)?.error
|
||||
// The old screen showed these as long toasts and snackbars, and the rest as short toasts
|
||||
val duration = when (message) {
|
||||
BackupRestoreMessage.PartialImport,
|
||||
is BackupRestoreMessage.Failed -> SnackbarDuration.Long
|
||||
|
||||
else -> SnackbarDuration.Short
|
||||
}
|
||||
// Showing the snackbar from the surrounding scope instead of this effect, so that marking
|
||||
// the message as seen below cannot cancel it by restarting the effect
|
||||
scope.launch {
|
||||
val result = snackbarHostState.showSnackbar(
|
||||
message = messageText,
|
||||
actionLabel = error?.let { reportText },
|
||||
duration = duration
|
||||
)
|
||||
if (result == SnackbarResult.ActionPerformed) {
|
||||
error?.open()
|
||||
}
|
||||
}
|
||||
onMessageShown()
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
title = stringResource(Res.string.settings_category_backup_restore_title),
|
||||
onNavigateUp = onNavigateUp
|
||||
)
|
||||
},
|
||||
snackbarHost = { SnackbarHost(snackbarHostState) }
|
||||
) { paddingValues ->
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(top = paddingValues.calculateTopPadding())
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(WindowInsets.navigationBars.asPaddingValues())
|
||||
) {
|
||||
PreferenceRow(
|
||||
title = stringResource(Res.string.import_data_title),
|
||||
summary = stringResource(Res.string.import_data_summary),
|
||||
onClick = onImportData
|
||||
)
|
||||
PreferenceRow(
|
||||
title = stringResource(Res.string.export_data_title),
|
||||
summary = stringResource(Res.string.export_data_summary),
|
||||
onClick = onExportData
|
||||
)
|
||||
PreferenceRow(
|
||||
title = stringResource(Res.string.reset_settings_title),
|
||||
summary = stringResource(Res.string.reset_settings_summary),
|
||||
onClick = onResetSettings
|
||||
)
|
||||
PreferenceRow(
|
||||
title = stringResource(Res.string.export_subscriptions_title),
|
||||
summary = stringResource(Res.string.export_subscriptions_summary),
|
||||
onClick = onExportSubscriptions
|
||||
)
|
||||
PreferenceRow(
|
||||
title = stringResource(Res.string.import_subscriptions_title),
|
||||
summary = stringResource(Res.string.import_subscriptions_summary),
|
||||
onClick = onImportSubscriptions
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (pendingConfirmation != null) {
|
||||
ConfirmationDialog(
|
||||
confirmation = pendingConfirmation,
|
||||
onConfirm = {
|
||||
onConfirm(pendingConfirmation)
|
||||
onDismissConfirmation()
|
||||
},
|
||||
onDismiss = onDismissConfirmation
|
||||
)
|
||||
}
|
||||
|
||||
if (pendingSettingsImport != null) {
|
||||
SettingsImportDialog(
|
||||
format = pendingSettingsImport.format,
|
||||
onConfirm = { onImportSettings(pendingSettingsImport) },
|
||||
onDismiss = { onSkipSettingsImport(pendingSettingsImport) }
|
||||
)
|
||||
}
|
||||
|
||||
if (noFileManager) {
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismissNoFileManager,
|
||||
title = { Text(text = stringResource(Res.string.no_app_to_open_intent)) },
|
||||
text = { Text(text = noFileManagerMessage(storageAccessFrameworkConfigurable)) },
|
||||
confirmButton = {
|
||||
TextButton(onClick = onDismissNoFileManager) {
|
||||
Text(text = stringResource(Res.string.ok))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ConfirmationDialog(
|
||||
confirmation: BackupRestoreConfirmation,
|
||||
onConfirm: () -> Unit,
|
||||
onDismiss: () -> Unit
|
||||
) {
|
||||
val message = when (confirmation) {
|
||||
BackupRestoreConfirmation.ResetSettings -> {
|
||||
stringResource(Res.string.reset_all_settings)
|
||||
}
|
||||
|
||||
is BackupRestoreConfirmation.DataImport -> {
|
||||
stringResource(Res.string.override_current_data)
|
||||
}
|
||||
|
||||
is BackupRestoreConfirmation.SubscriptionsImport -> {
|
||||
stringResource(Res.string.import_network_expensive_warning)
|
||||
}
|
||||
}
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
text = { Text(text = message) },
|
||||
confirmButton = {
|
||||
TextButton(onClick = onConfirm) {
|
||||
Text(text = stringResource(Res.string.ok))
|
||||
}
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = onDismiss) {
|
||||
Text(text = stringResource(Res.string.cancel))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun SettingsImportDialog(
|
||||
format: SettingsFormat,
|
||||
onConfirm: () -> Unit,
|
||||
onDismiss: () -> Unit
|
||||
) {
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = { Text(text = stringResource(Res.string.import_settings)) },
|
||||
text = when (format) {
|
||||
SettingsFormat.JSON -> {
|
||||
null
|
||||
}
|
||||
|
||||
SettingsFormat.SERIALIZED -> {
|
||||
{ Text(text = stringResource(Res.string.import_settings_vulnerable_format)) }
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(onClick = onConfirm) {
|
||||
Text(text = stringResource(Res.string.ok))
|
||||
}
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = onDismiss) {
|
||||
Text(text = stringResource(Res.string.cancel))
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@PreviewWrapper(ThemePreviewProvider::class)
|
||||
@PreviewLightDark
|
||||
@Composable
|
||||
private fun BackupRestoreSettingsScreenPreview() {
|
||||
BackupRestoreSettingsScreenContent()
|
||||
}
|
||||
@@ -84,7 +84,10 @@ fun SettingsHomeScreen(
|
||||
Destination.UpdateSettings
|
||||
)
|
||||
|
||||
// TODO: wire remaining sub-screens as they are migrated
|
||||
SettingsCategoryType.BACKUP_RESTORE -> navigator.navigateTo(
|
||||
Destination.BackupRestoreSettings
|
||||
)
|
||||
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package net.newpipe.app.screen.settings.model
|
||||
|
||||
sealed interface BackupRestoreConfirmation {
|
||||
|
||||
data object ResetSettings : BackupRestoreConfirmation
|
||||
|
||||
data class DataImport(val file: String) : BackupRestoreConfirmation
|
||||
|
||||
data class SubscriptionsImport(val file: String) : BackupRestoreConfirmation
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package net.newpipe.app.screen.settings.model
|
||||
|
||||
import net.newpipe.app.model.ErrorReport
|
||||
|
||||
sealed interface BackupRestoreMessage {
|
||||
|
||||
data object ExportComplete : BackupRestoreMessage
|
||||
|
||||
data object InvalidFile : BackupRestoreMessage
|
||||
|
||||
data object PartialImport : BackupRestoreMessage
|
||||
|
||||
class Failed(val error: ErrorReport) : BackupRestoreMessage
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package net.newpipe.app.screen.settings.model
|
||||
|
||||
import net.newpipe.app.model.SettingsFormat
|
||||
|
||||
data class SettingsImport(val file: String, val format: SettingsFormat)
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package net.newpipe.app.viewmodel.settings
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import net.newpipe.app.model.DataExportResult
|
||||
import net.newpipe.app.model.DataImportResult
|
||||
import net.newpipe.app.platform.BackupRestoreActions
|
||||
import net.newpipe.app.screen.settings.model.BackupRestoreMessage
|
||||
import net.newpipe.app.screen.settings.model.SettingsImport
|
||||
import org.koin.core.annotation.KoinViewModel
|
||||
|
||||
@KoinViewModel
|
||||
class BackupRestoreSettingsViewModel(
|
||||
private val backupRestoreActions: BackupRestoreActions
|
||||
) : ViewModel() {
|
||||
|
||||
val message: StateFlow<BackupRestoreMessage?>
|
||||
field = MutableStateFlow<BackupRestoreMessage?>(null)
|
||||
|
||||
val pendingSettingsImport: StateFlow<SettingsImport?>
|
||||
field = MutableStateFlow<SettingsImport?>(null)
|
||||
|
||||
fun exportData(file: String) {
|
||||
viewModelScope.launch {
|
||||
message.value = when (val result = backupRestoreActions.exportData(file)) {
|
||||
DataExportResult.Success -> BackupRestoreMessage.ExportComplete
|
||||
is DataExportResult.Failed -> BackupRestoreMessage.Failed(result.error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun importData(file: String) {
|
||||
viewModelScope.launch {
|
||||
when (val result = backupRestoreActions.importDatabase(file)) {
|
||||
DataImportResult.InvalidFile -> {
|
||||
message.value = BackupRestoreMessage.InvalidFile
|
||||
}
|
||||
|
||||
is DataImportResult.Failed -> {
|
||||
message.value = BackupRestoreMessage.Failed(result.error)
|
||||
}
|
||||
|
||||
is DataImportResult.Imported -> {
|
||||
onDatabaseImported(file, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun importSettings(settingsImport: SettingsImport) {
|
||||
pendingSettingsImport.value = null
|
||||
viewModelScope.launch {
|
||||
backupRestoreActions.importSettings(settingsImport.file, settingsImport.format)
|
||||
backupRestoreActions.finishImport(settingsImport.file)
|
||||
}
|
||||
}
|
||||
|
||||
fun skipSettingsImport(settingsImport: SettingsImport) {
|
||||
pendingSettingsImport.value = null
|
||||
backupRestoreActions.finishImport(settingsImport.file)
|
||||
}
|
||||
|
||||
fun onMessageShown() {
|
||||
message.value = null
|
||||
}
|
||||
|
||||
private fun onDatabaseImported(file: String, result: DataImportResult.Imported) {
|
||||
if (!result.allFilesImported) {
|
||||
message.value = BackupRestoreMessage.PartialImport
|
||||
}
|
||||
val format = result.settingsFormat
|
||||
if (format == null) {
|
||||
backupRestoreActions.finishImport(file)
|
||||
} else {
|
||||
pendingSettingsImport.value = SettingsImport(file, format)
|
||||
}
|
||||
}
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package net.newpipe.app.screen.settings
|
||||
|
||||
import androidx.compose.ui.test.ExperimentalTestApi
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import androidx.compose.ui.test.performClick
|
||||
import androidx.compose.ui.test.v2.runComposeUiTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
import net.newpipe.app.extensions.settingsModule
|
||||
import net.newpipe.app.extensions.withKoin
|
||||
import net.newpipe.app.model.ErrorReport
|
||||
import net.newpipe.app.model.SettingsFormat
|
||||
import net.newpipe.app.screen.settings.model.BackupRestoreConfirmation
|
||||
import net.newpipe.app.screen.settings.model.BackupRestoreMessage
|
||||
import net.newpipe.app.screen.settings.model.SettingsImport
|
||||
import newpipe.shared.generated.resources.Res
|
||||
import newpipe.shared.generated.resources.cancel
|
||||
import newpipe.shared.generated.resources.error_snackbar_action
|
||||
import newpipe.shared.generated.resources.import_settings_vulnerable_format
|
||||
import newpipe.shared.generated.resources.no_appropriate_file_manager_message_android_10
|
||||
import newpipe.shared.generated.resources.ok
|
||||
import newpipe.shared.generated.resources.reset_all_settings
|
||||
import org.jetbrains.compose.resources.getString
|
||||
|
||||
@OptIn(ExperimentalTestApi::class)
|
||||
class BackupRestoreSettingsScreenTest {
|
||||
|
||||
@Test
|
||||
fun confirmingTheResetPassesItOnAndClosesTheDialog() = runComposeUiTest {
|
||||
var confirmed: BackupRestoreConfirmation? = null
|
||||
var dismissed = false
|
||||
withKoin(
|
||||
modules = listOf(settingsModule()),
|
||||
content = {
|
||||
BackupRestoreSettingsScreenContent(
|
||||
pendingConfirmation = BackupRestoreConfirmation.ResetSettings,
|
||||
onConfirm = { confirmation -> confirmed = confirmation },
|
||||
onDismissConfirmation = { dismissed = true }
|
||||
)
|
||||
},
|
||||
onContent = {
|
||||
onNodeWithText(getString(Res.string.reset_all_settings)).assertIsDisplayed()
|
||||
onNodeWithText(getString(Res.string.ok)).performClick()
|
||||
assertEquals(BackupRestoreConfirmation.ResetSettings, confirmed)
|
||||
assertTrue(dismissed)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun oldSettingsFormatWarnsAndCancellingStillFinishesTheImport() = runComposeUiTest {
|
||||
val settingsImport = SettingsImport(file = "backup.zip", format = SettingsFormat.SERIALIZED)
|
||||
var skipped: SettingsImport? = null
|
||||
withKoin(
|
||||
modules = listOf(settingsModule()),
|
||||
content = {
|
||||
BackupRestoreSettingsScreenContent(
|
||||
pendingSettingsImport = settingsImport,
|
||||
onSkipSettingsImport = { skip -> skipped = skip }
|
||||
)
|
||||
},
|
||||
onContent = {
|
||||
onNodeWithText(getString(Res.string.import_settings_vulnerable_format)).assertIsDisplayed()
|
||||
onNodeWithText(getString(Res.string.cancel)).performClick()
|
||||
assertEquals(settingsImport, skipped)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun failuresOfferToReportTheError() = runComposeUiTest {
|
||||
var reported = false
|
||||
withKoin(
|
||||
modules = listOf(settingsModule()),
|
||||
content = {
|
||||
BackupRestoreSettingsScreenContent(
|
||||
message = BackupRestoreMessage.Failed(ErrorReport(message = "Boom", open = { reported = true }))
|
||||
)
|
||||
},
|
||||
onContent = {
|
||||
onNodeWithText("Boom").assertIsDisplayed()
|
||||
onNodeWithText(getString(Res.string.error_snackbar_action)).performClick()
|
||||
waitForIdle()
|
||||
assertTrue(reported)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun missingFileManagerExplainsWhatToInstall() = runComposeUiTest {
|
||||
withKoin(
|
||||
modules = listOf(settingsModule()),
|
||||
content = { BackupRestoreSettingsScreenContent(noFileManager = true) },
|
||||
onContent = {
|
||||
onNodeWithText(getString(Res.string.no_appropriate_file_manager_message_android_10))
|
||||
.assertIsDisplayed()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user