Ask restart when theme is changed

This commit is contained in:
Thomas 2022-11-14 10:37:53 +01:00
parent f66fda968e
commit e9b62b71d1
3 changed files with 30 additions and 2 deletions

View file

@ -16,15 +16,18 @@ package app.fedilab.android.activities
import android.os.Bundle import android.os.Bundle
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AlertDialog
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController import androidx.navigation.ui.setupActionBarWithNavController
import app.fedilab.android.R import app.fedilab.android.R
import app.fedilab.android.databinding.ActivitySettingsBinding import app.fedilab.android.databinding.ActivitySettingsBinding
import app.fedilab.android.helper.Helper
import app.fedilab.android.helper.ThemeHelper import app.fedilab.android.helper.ThemeHelper
import app.fedilab.android.ui.fragment.settings.FragmentThemingSettings
class SettingsActivity : BaseActivity() { class SettingsActivity : BaseActivity(), FragmentThemingSettings.ActionTheming {
private lateinit var binding: ActivitySettingsBinding private lateinit var binding: ActivitySettingsBinding
private lateinit var appBarConfiguration: AppBarConfiguration private lateinit var appBarConfiguration: AppBarConfiguration
@ -34,7 +37,6 @@ class SettingsActivity : BaseActivity() {
binding = ActivitySettingsBinding.inflate(layoutInflater) binding = ActivitySettingsBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)
val navController = findNavController(R.id.fragment_container) val navController = findNavController(R.id.fragment_container)
appBarConfiguration = AppBarConfiguration.Builder().build() appBarConfiguration = AppBarConfiguration.Builder().build()
setupActionBarWithNavController(navController, appBarConfiguration) setupActionBarWithNavController(navController, appBarConfiguration)
@ -53,4 +55,16 @@ class SettingsActivity : BaseActivity() {
} }
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
override fun restart() {
val restartBuilder = AlertDialog.Builder(this, Helper.dialogStyle())
restartBuilder.setTitle(getString(R.string.restart_the_app))
restartBuilder.setMessage(getString(R.string.restart_the_app_theme))
restartBuilder.setNegativeButton(R.string.no) { dialog, which -> dialog.dismiss() }
restartBuilder.setPositiveButton(R.string.restart) { dialog, which ->
dialog.dismiss()
Helper.restart(this)
}
restartBuilder.create().show()
}
} }

View file

@ -63,6 +63,7 @@ import java.util.List;
import app.fedilab.android.R; import app.fedilab.android.R;
import app.fedilab.android.activities.ComposeActivity; import app.fedilab.android.activities.ComposeActivity;
import app.fedilab.android.activities.SettingsActivity;
import app.fedilab.android.databinding.PopupStatusThemeBinding; import app.fedilab.android.databinding.PopupStatusThemeBinding;
import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.ThemeHelper; import app.fedilab.android.helper.ThemeHelper;
@ -75,6 +76,8 @@ public class FragmentThemingSettings extends PreferenceFragmentCompat implements
private List<LinkedHashMap<String, String>> listOfThemes; private List<LinkedHashMap<String, String>> listOfThemes;
private SharedPreferences appPref; private SharedPreferences appPref;
private SharedPreferences cyneaPref; private SharedPreferences cyneaPref;
public ActionTheming actionTheming;
private boolean shouldRestart;
@Override @Override
public void onCreatePreferences(Bundle bundle, String s) { public void onCreatePreferences(Bundle bundle, String s) {
@ -82,6 +85,8 @@ public class FragmentThemingSettings extends PreferenceFragmentCompat implements
appPref = PreferenceManager.getDefaultSharedPreferences(requireActivity()); appPref = PreferenceManager.getDefaultSharedPreferences(requireActivity());
createPref(); createPref();
listOfThemes = ThemeHelper.getContributorsTheme(requireActivity()); listOfThemes = ThemeHelper.getContributorsTheme(requireActivity());
shouldRestart = false;
actionTheming = (SettingsActivity) requireActivity();
} }
@ -101,6 +106,9 @@ public class FragmentThemingSettings extends PreferenceFragmentCompat implements
getPreferenceScreen().getSharedPreferences() getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this); .unregisterOnSharedPreferenceChangeListener(this);
} }
if (shouldRestart && actionTheming != null) {
actionTheming.restart();
}
} }
@Override @Override
@ -109,9 +117,14 @@ public class FragmentThemingSettings extends PreferenceFragmentCompat implements
if (key.equals("use_custom_theme")) { if (key.equals("use_custom_theme")) {
createPref(); createPref();
} }
shouldRestart = true;
Helper.recreateMainActivity(requireActivity()); Helper.recreateMainActivity(requireActivity());
} }
public interface ActionTheming {
void restart();
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@SuppressLint("ApplySharedPref") @SuppressLint("ApplySharedPref")

View file

@ -1599,4 +1599,5 @@
<string name="state">State</string> <string name="state">State</string>
<string name="restart_the_app">Restart the app?</string> <string name="restart_the_app">Restart the app?</string>
<string name="restart">Restart</string> <string name="restart">Restart</string>
<string name="restart_the_app_theme">You should restart the application to apply changes.</string>
</resources> </resources>