diff --git a/app/src/main/java/com/github/libretube/activities/SettingsActivity.kt b/app/src/main/java/com/github/libretube/activities/SettingsActivity.kt index da25444cd..0a9aaa7ea 100644 --- a/app/src/main/java/com/github/libretube/activities/SettingsActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/SettingsActivity.kt @@ -1,10 +1,8 @@ package com.github.libretube.activities -import android.app.NotificationManager import android.os.Build import android.os.Bundle import androidx.appcompat.app.AppCompatActivity -import androidx.core.app.ActivityCompat import com.github.libretube.R import com.github.libretube.databinding.ActivitySettingsBinding import com.github.libretube.preferences.MainSettings @@ -12,7 +10,6 @@ import com.github.libretube.util.ThemeHelper import com.google.android.material.color.DynamicColors var isCurrentViewMainSettings = true -var requireMainActivityRestart = false class SettingsActivity : AppCompatActivity() { val TAG = "SettingsActivity" @@ -50,17 +47,7 @@ class SettingsActivity : AppCompatActivity() { override fun onBackPressed() { if (isCurrentViewMainSettings) { - if (requireMainActivityRestart) { - requireMainActivityRestart = false - // kill player notification - val nManager = - this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager - nManager.cancelAll() - ThemeHelper.restartMainActivity(this) - ActivityCompat.finishAffinity(this) - } else { - super.onBackPressed() - } + super.onBackPressed() finishAndRemoveTask() } else { isCurrentViewMainSettings = true diff --git a/app/src/main/java/com/github/libretube/dialogs/DeleteAccountDialog.kt b/app/src/main/java/com/github/libretube/dialogs/DeleteAccountDialog.kt index a04ec1af8..9cb0ad48a 100644 --- a/app/src/main/java/com/github/libretube/dialogs/DeleteAccountDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/DeleteAccountDialog.kt @@ -7,7 +7,6 @@ import android.widget.Toast import androidx.fragment.app.DialogFragment import androidx.lifecycle.lifecycleScope import com.github.libretube.R -import com.github.libretube.activities.requireMainActivityRestart import com.github.libretube.databinding.DialogDeleteAccountBinding import com.github.libretube.obj.DeleteUserRequest import com.github.libretube.preferences.PreferenceHelper @@ -55,10 +54,10 @@ class DeleteAccountDialog : DialogFragment() { Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() return@launchWhenCreated } - requireMainActivityRestart = true Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() logout() - dialog?.dismiss() + val restartDialog = RequireRestartDialog() + restartDialog.show(childFragmentManager, "RequireRestartDialog") } } run() diff --git a/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt b/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt index 75983feb6..de8a2188c 100644 --- a/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt @@ -7,7 +7,6 @@ import android.widget.Toast import androidx.fragment.app.DialogFragment import androidx.lifecycle.lifecycleScope import com.github.libretube.R -import com.github.libretube.activities.requireMainActivityRestart import com.github.libretube.databinding.DialogLoginBinding import com.github.libretube.obj.Login import com.github.libretube.preferences.PreferenceHelper @@ -82,9 +81,9 @@ class LoginDialog : DialogFragment() { Toast.makeText(context, R.string.loggedIn, Toast.LENGTH_SHORT).show() PreferenceHelper.setToken(requireContext(), response.token!!) PreferenceHelper.setUsername(requireContext(), login.username!!) - requireMainActivityRestart = true + val restartDialog = RequireRestartDialog() + restartDialog.show(parentFragmentManager, "RequireRestartDialog") dialog?.dismiss() - activity?.recreate() } } } diff --git a/app/src/main/java/com/github/libretube/dialogs/LogoutDialog.kt b/app/src/main/java/com/github/libretube/dialogs/LogoutDialog.kt index a7192efd4..77520bd33 100644 --- a/app/src/main/java/com/github/libretube/dialogs/LogoutDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/LogoutDialog.kt @@ -5,7 +5,6 @@ import android.os.Bundle import android.widget.Toast import androidx.fragment.app.DialogFragment import com.github.libretube.R -import com.github.libretube.activities.requireMainActivityRestart import com.github.libretube.databinding.DialogLogoutBinding import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.util.ThemeHelper @@ -25,7 +24,6 @@ class LogoutDialog : DialogFragment() { binding.user.text = binding.user.text.toString() + " (" + user + ")" binding.logout.setOnClickListener { - requireMainActivityRestart = true Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show() PreferenceHelper.setToken(requireContext(), "") dialog?.dismiss() diff --git a/app/src/main/java/com/github/libretube/dialogs/RequireRestartDialog.kt b/app/src/main/java/com/github/libretube/dialogs/RequireRestartDialog.kt new file mode 100644 index 000000000..1030fe05b --- /dev/null +++ b/app/src/main/java/com/github/libretube/dialogs/RequireRestartDialog.kt @@ -0,0 +1,25 @@ +package com.github.libretube.dialogs + +import android.app.Dialog +import android.os.Bundle +import androidx.fragment.app.DialogFragment +import com.github.libretube.R +import com.github.libretube.util.ThemeHelper +import com.google.android.material.dialog.MaterialAlertDialogBuilder + +class RequireRestartDialog : DialogFragment() { + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + return activity?.let { + MaterialAlertDialogBuilder(requireContext()) + .setTitle(R.string.require_restart) + .setMessage(R.string.require_restart_message) + .setPositiveButton(R.string.okay) { _, _ -> + activity?.recreate() + ThemeHelper.restartMainActivity(requireContext()) + } + .setNegativeButton(R.string.cancel) { _, _ -> } + .create() + } ?: throw IllegalStateException("Activity cannot be null") + } +} diff --git a/app/src/main/java/com/github/libretube/preferences/AdvancedSettings.kt b/app/src/main/java/com/github/libretube/preferences/AdvancedSettings.kt index f80dbafd9..750fe5f9a 100644 --- a/app/src/main/java/com/github/libretube/preferences/AdvancedSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/AdvancedSettings.kt @@ -5,7 +5,7 @@ import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import com.github.libretube.R import com.github.libretube.activities.SettingsActivity -import com.github.libretube.activities.requireMainActivityRestart +import com.github.libretube.dialogs.RequireRestartDialog import com.google.android.material.dialog.MaterialAlertDialogBuilder class AdvancedSettings : PreferenceFragmentCompat() { @@ -48,8 +48,8 @@ class AdvancedSettings : PreferenceFragmentCompat() { // clear login token PreferenceHelper.setToken(requireContext(), "") - requireMainActivityRestart = true - activity?.recreate() + val restartDialog = RequireRestartDialog() + restartDialog.show(childFragmentManager, "RequireRestartDialog") } .setNegativeButton(getString(R.string.cancel)) { _, _ -> } .setTitle(R.string.reset) diff --git a/app/src/main/java/com/github/libretube/preferences/AppearanceSettings.kt b/app/src/main/java/com/github/libretube/preferences/AppearanceSettings.kt index dbec1e361..a69755fc3 100644 --- a/app/src/main/java/com/github/libretube/preferences/AppearanceSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/AppearanceSettings.kt @@ -6,7 +6,7 @@ import androidx.preference.PreferenceFragmentCompat import androidx.preference.SwitchPreference import com.github.libretube.R import com.github.libretube.activities.SettingsActivity -import com.github.libretube.activities.requireMainActivityRestart +import com.github.libretube.dialogs.RequireRestartDialog import com.github.libretube.util.ThemeHelper import com.google.android.material.color.DynamicColors @@ -20,16 +20,16 @@ class AppearanceSettings : PreferenceFragmentCompat() { val themeToggle = findPreference("theme_togglee") themeToggle?.setOnPreferenceChangeListener { _, _ -> - requireMainActivityRestart = true - activity?.recreate() + val restartDialog = RequireRestartDialog() + restartDialog.show(childFragmentManager, "RequireRestartDialog") true } val accentColor = findPreference("accent_color") updateAccentColorValues(accentColor!!) accentColor.setOnPreferenceChangeListener { _, _ -> - requireMainActivityRestart = true - activity?.recreate() + val restartDialog = RequireRestartDialog() + restartDialog.show(childFragmentManager, "RequireRestartDialog") true } @@ -41,13 +41,15 @@ class AppearanceSettings : PreferenceFragmentCompat() { val gridColumns = findPreference("grid") gridColumns?.setOnPreferenceChangeListener { _, _ -> - requireMainActivityRestart = true + val restartDialog = RequireRestartDialog() + restartDialog.show(childFragmentManager, "RequireRestartDialog") true } val hideTrending = findPreference("hide_trending_page") hideTrending?.setOnPreferenceChangeListener { _, _ -> - requireMainActivityRestart = true + val restartDialog = RequireRestartDialog() + restartDialog.show(childFragmentManager, "RequireRestartDialog") true } } diff --git a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt index fc902b685..169cf7358 100644 --- a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt @@ -21,11 +21,11 @@ import androidx.preference.PreferenceFragmentCompat import androidx.preference.SwitchPreference import com.github.libretube.R import com.github.libretube.activities.SettingsActivity -import com.github.libretube.activities.requireMainActivityRestart import com.github.libretube.dialogs.CustomInstanceDialog import com.github.libretube.dialogs.DeleteAccountDialog import com.github.libretube.dialogs.LoginDialog import com.github.libretube.dialogs.LogoutDialog +import com.github.libretube.dialogs.RequireRestartDialog import com.github.libretube.util.RetrofitInstance import org.json.JSONObject import org.json.JSONTokener @@ -119,7 +119,8 @@ class InstanceSettings : PreferenceFragmentCompat() { // fetchInstance() initCustomInstances(instance!!) instance.setOnPreferenceChangeListener { _, newValue -> - requireMainActivityRestart = true + val restartDialog = RequireRestartDialog() + restartDialog.show(childFragmentManager, "RequireRestartDialog") RetrofitInstance.url = newValue.toString() if (!PreferenceHelper.getBoolean(requireContext(), "auth_instance_toggle", false)) { RetrofitInstance.authUrl = newValue.toString() @@ -136,22 +137,24 @@ class InstanceSettings : PreferenceFragmentCompat() { authInstance.isVisible = false } authInstance.setOnPreferenceChangeListener { _, newValue -> - requireMainActivityRestart = true // save new auth url RetrofitInstance.authUrl = newValue.toString() RetrofitInstance.lazyMgr.reset() logout() + val restartDialog = RequireRestartDialog() + restartDialog.show(childFragmentManager, "RequireRestartDialog") true } val authInstanceToggle = findPreference("auth_instance_toggle") authInstanceToggle?.setOnPreferenceChangeListener { _, newValue -> - requireMainActivityRestart = true authInstance.isVisible = newValue == true logout() // either use new auth url or the normal api url if auth instance disabled RetrofitInstance.authUrl = if (newValue == false) RetrofitInstance.url else authInstance.value + val restartDialog = RequireRestartDialog() + restartDialog.show(childFragmentManager, "RequireRestartDialog") true } diff --git a/app/src/main/java/com/github/libretube/preferences/MainSettings.kt b/app/src/main/java/com/github/libretube/preferences/MainSettings.kt index 52ee90b8f..20ab72272 100644 --- a/app/src/main/java/com/github/libretube/preferences/MainSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/MainSettings.kt @@ -9,7 +9,7 @@ import androidx.preference.PreferenceFragmentCompat import com.github.libretube.BuildConfig import com.github.libretube.R import com.github.libretube.activities.isCurrentViewMainSettings -import com.github.libretube.activities.requireMainActivityRestart +import com.github.libretube.dialogs.RequireRestartDialog import com.github.libretube.util.ThemeHelper import com.github.libretube.util.checkUpdate @@ -25,7 +25,8 @@ class MainSettings : PreferenceFragmentCompat() { val region = findPreference("region") region?.setOnPreferenceChangeListener { _, _ -> - requireMainActivityRestart = true + val restartDialog = RequireRestartDialog() + restartDialog.show(childFragmentManager, "RequireRestartDialog") true } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d5863da59..6f0f77787 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -231,4 +231,6 @@ Open … Chapters Playback speed + Restart required + This change requires an app restart. Do you want to restart the app now? Otherwise the changes will be applied on the next app restart. \ No newline at end of file