|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2020-2026 NewPipe contributors <https://newpipe.net> |
| 3 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +package org.schabi.newpipe.settings.migration |
| 7 | + |
| 8 | +import android.content.Context |
| 9 | +import androidx.appcompat.app.AlertDialog |
| 10 | +import org.schabi.newpipe.R |
| 11 | +import org.schabi.newpipe.error.ErrorUtil.Companion.showUiErrorSnackbar |
| 12 | + |
| 13 | +/** |
| 14 | + * MigrationManager is responsible for running migrations and showing the user information about |
| 15 | + * the migrations that were applied. |
| 16 | + */ |
| 17 | +object MigrationManager { |
| 18 | + private val TAG: String = MigrationManager::class.java.getSimpleName() |
| 19 | + |
| 20 | + /** |
| 21 | + * List of UI actions that are performed after the UI is initialized (e.g. showing alert |
| 22 | + * dialogs) to inform the user about changes that were applied by migrations. |
| 23 | + */ |
| 24 | + private val MIGRATION_INFO: MutableList<(Context) -> Unit> = ArrayList() |
| 25 | + |
| 26 | + /** |
| 27 | + * Run all migrations that are needed for the current version of NewPipe. |
| 28 | + * This method should be called at the start of the application, before any other operations |
| 29 | + * that depend on the settings. |
| 30 | + * |
| 31 | + * @param context Context that can be used to run migrations |
| 32 | + */ |
| 33 | + @JvmStatic |
| 34 | + fun runMigrationsIfNeeded(context: Context) { |
| 35 | + SettingMigrations.runMigrationsIfNeeded(context) |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Perform UI actions informing about migrations that took place if they are present. |
| 40 | + * @param context Context that can be used to show dialogs/snackbars/toasts |
| 41 | + */ |
| 42 | + @JvmStatic |
| 43 | + fun showUserInfoIfPresent(context: Context) { |
| 44 | + if (MIGRATION_INFO.isEmpty()) { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + try { |
| 49 | + MIGRATION_INFO[0](context) |
| 50 | + } catch (e: Exception) { |
| 51 | + showUiErrorSnackbar(context, "Showing migration info to the user", e) |
| 52 | + // Remove the migration that caused the error and continue with the next one |
| 53 | + MIGRATION_INFO.removeAt(0) |
| 54 | + showUserInfoIfPresent(context) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Add a migration info action that will be executed after the UI is initialized. |
| 60 | + * This can be used to show dialogs/snackbars/toasts to inform the user about changes that |
| 61 | + * were applied by migrations. |
| 62 | + * |
| 63 | + * @param info the action to be executed |
| 64 | + */ |
| 65 | + @JvmStatic |
| 66 | + fun addMigrationInfo(info: (Context) -> Unit) { |
| 67 | + MIGRATION_INFO.add(info) |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * This method should be called when the user dismisses the migration info |
| 72 | + * to check if there are any more migration info actions to be shown. |
| 73 | + * @param context Context that can be used to show dialogs/snackbars/toasts |
| 74 | + */ |
| 75 | + @JvmStatic |
| 76 | + fun onMigrationInfoDismissed(context: Context) { |
| 77 | + MIGRATION_INFO.removeAt(0) |
| 78 | + showUserInfoIfPresent(context) |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Creates a dialog to inform the user about the migration. |
| 83 | + * @param uiContext Context that can be used to show dialogs/snackbars/toasts |
| 84 | + * @param title the title of the dialog |
| 85 | + * @param message the message of the dialog |
| 86 | + * @return the dialog that can be shown to the user with a custom dismiss listener |
| 87 | + */ |
| 88 | + @JvmStatic |
| 89 | + fun createMigrationInfoDialog(uiContext: Context, title: String, message: String): AlertDialog { |
| 90 | + return AlertDialog.Builder(uiContext) |
| 91 | + .setTitle(title) |
| 92 | + .setMessage(message) |
| 93 | + .setPositiveButton(R.string.ok, null) |
| 94 | + .setOnDismissListener { onMigrationInfoDismissed(uiContext) } |
| 95 | + .setCancelable(false) // prevents the dialog from being dismissed accidentally |
| 96 | + .create() |
| 97 | + } |
| 98 | +} |
0 commit comments