Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* SPDX-FileCopyrightText: 2020-2026 NewPipe contributors <https://newpipe.net>
* SPDX-License-Identifier: GPL-3.0-or-later
*/

package org.schabi.newpipe.settings.migration

import android.content.Context
import androidx.appcompat.app.AlertDialog
import org.schabi.newpipe.R
import org.schabi.newpipe.error.ErrorUtil.Companion.showUiErrorSnackbar

/**
* MigrationManager is responsible for running migrations and showing the user information about
* the migrations that were applied.
*/
object MigrationManager {
private val TAG: String = MigrationManager::class.java.getSimpleName()

/**
* List of UI actions that are performed after the UI is initialized (e.g. showing alert
* dialogs) to inform the user about changes that were applied by migrations.
*/
private val MIGRATION_INFO: MutableList<(Context) -> Unit> = ArrayList()

/**
* Run all migrations that are needed for the current version of NewPipe.
* This method should be called at the start of the application, before any other operations
* that depend on the settings.
*
* @param context Context that can be used to run migrations
*/
@JvmStatic
fun runMigrationsIfNeeded(context: Context) {
SettingMigrations.runMigrationsIfNeeded(context)
}

/**
* Perform UI actions informing about migrations that took place if they are present.
* @param context Context that can be used to show dialogs/snackbars/toasts
*/
@JvmStatic
fun showUserInfoIfPresent(context: Context) {
if (MIGRATION_INFO.isEmpty()) {
return
}

try {
MIGRATION_INFO[0](context)
} catch (e: Exception) {
showUiErrorSnackbar(context, "Showing migration info to the user", e)
// Remove the migration that caused the error and continue with the next one
MIGRATION_INFO.removeAt(0)
showUserInfoIfPresent(context)
}
}

/**
* Add a migration info action that will be executed after the UI is initialized.
* This can be used to show dialogs/snackbars/toasts to inform the user about changes that
* were applied by migrations.
*
* @param info the action to be executed
*/
@JvmStatic
fun addMigrationInfo(info: (Context) -> Unit) {
MIGRATION_INFO.add(info)
}

/**
* This method should be called when the user dismisses the migration info
* to check if there are any more migration info actions to be shown.
* @param context Context that can be used to show dialogs/snackbars/toasts
*/
@JvmStatic
fun onMigrationInfoDismissed(context: Context) {
MIGRATION_INFO.removeAt(0)
showUserInfoIfPresent(context)
}

/**
* Creates a dialog to inform the user about the migration.
* @param uiContext Context that can be used to show dialogs/snackbars/toasts
* @param title the title of the dialog
* @param message the message of the dialog
* @return the dialog that can be shown to the user with a custom dismiss listener
*/
@JvmStatic
fun createMigrationInfoDialog(uiContext: Context, title: String, message: String): AlertDialog {
return AlertDialog.Builder(uiContext)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.ok, null)
.setOnDismissListener { onMigrationInfoDismissed(uiContext) }
.setCancelable(false) // prevents the dialog from being dismissed accidentally
.create()
}
}
Loading