From 205a6187164921260b9154278ff2e328bb068f4e Mon Sep 17 00:00:00 2001 From: TrianguloY Date: Mon, 29 Jan 2024 23:16:40 +0100 Subject: [PATCH] tweaked reload --- .../urlchecker/activities/MainActivity.java | 26 ++++++------- .../activities/SettingsActivity.java | 3 ++ .../fragments/ActivityResultInjector.java | 2 +- .../urlchecker/utilities/AndroidSettings.java | 38 ++++++++++++++++++- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/trianguloy/urlchecker/activities/MainActivity.java b/app/src/main/java/com/trianguloy/urlchecker/activities/MainActivity.java index ca00041f..9c897c87 100644 --- a/app/src/main/java/com/trianguloy/urlchecker/activities/MainActivity.java +++ b/app/src/main/java/com/trianguloy/urlchecker/activities/MainActivity.java @@ -10,20 +10,18 @@ import android.widget.Toast; import com.trianguloy.urlchecker.R; +import com.trianguloy.urlchecker.fragments.ActivityResultInjector; import com.trianguloy.urlchecker.modules.companions.VersionManager; import com.trianguloy.urlchecker.utilities.AndroidSettings; import com.trianguloy.urlchecker.utilities.methods.AndroidUtils; import com.trianguloy.urlchecker.utilities.methods.PackageUtils; -import java.util.Objects; - /** * The activity to show when clicking the desktop shortcut (when 'opening' the app) */ public class MainActivity extends Activity { - private AndroidSettings.Theme previousTheme; - private String previousLocale; + private final ActivityResultInjector activityResultInjector = new ActivityResultInjector(); @Override protected void onCreate(Bundle savedInstanceState) { @@ -58,16 +56,9 @@ public boolean onOptionsItemSelected(MenuItem item) { } @Override - protected void onResume() { - super.onResume(); - // check if the theme was changed, if so reload to apply - var currentTheme = AndroidSettings.THEME_PREF(this).get(); - if (previousTheme == null) previousTheme = currentTheme; - if (previousTheme != currentTheme) AndroidSettings.reload(this); - // check if the locale was changed, if so reload to apply - var currentLocale = AndroidSettings.LOCALE_PREF(this).get(); - if (previousLocale == null) previousLocale = currentLocale; - if (!Objects.equals(previousLocale, currentLocale)) AndroidSettings.reload(this); + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (!activityResultInjector.onActivityResult(requestCode, resultCode, data)) + super.onActivityResult(requestCode, resultCode, data); } /* ------------------- button clicks ------------------- */ @@ -77,7 +68,12 @@ public void openModulesActivity(View view) { } public void openSettings(View view) { - PackageUtils.startActivity(new Intent(this, SettingsActivity.class), R.string.toast_noApp, this); + PackageUtils.startActivityForResult( + new Intent(this, SettingsActivity.class), + AndroidSettings.registerForReloading(activityResultInjector, this), + R.string.toast_noApp, + this + ); } public void openAbout(View view) { diff --git a/app/src/main/java/com/trianguloy/urlchecker/activities/SettingsActivity.java b/app/src/main/java/com/trianguloy/urlchecker/activities/SettingsActivity.java index 85d70558..62a3e64c 100644 --- a/app/src/main/java/com/trianguloy/urlchecker/activities/SettingsActivity.java +++ b/app/src/main/java/com/trianguloy/urlchecker/activities/SettingsActivity.java @@ -35,6 +35,9 @@ protected void onCreate(Bundle savedInstanceState) { configureBrowserButtons(); configureDayNight(); configureLocale(); + + // if this app was reloaded, some settings may have changed, so reload previous one too + if (AndroidSettings.wasReloaded(this)) AndroidSettings.markForReloading(this); } diff --git a/app/src/main/java/com/trianguloy/urlchecker/fragments/ActivityResultInjector.java b/app/src/main/java/com/trianguloy/urlchecker/fragments/ActivityResultInjector.java index a990d382..7a104c43 100644 --- a/app/src/main/java/com/trianguloy/urlchecker/fragments/ActivityResultInjector.java +++ b/app/src/main/java/com/trianguloy/urlchecker/fragments/ActivityResultInjector.java @@ -18,7 +18,7 @@ public class ActivityResultInjector { /* ------------------- client use ------------------- */ - interface Listener { + public interface Listener { /** * Called when the event fires for a particular registrar */ diff --git a/app/src/main/java/com/trianguloy/urlchecker/utilities/AndroidSettings.java b/app/src/main/java/com/trianguloy/urlchecker/utilities/AndroidSettings.java index f43ed00f..11bd97db 100644 --- a/app/src/main/java/com/trianguloy/urlchecker/utilities/AndroidSettings.java +++ b/app/src/main/java/com/trianguloy/urlchecker/utilities/AndroidSettings.java @@ -9,6 +9,7 @@ import com.trianguloy.urlchecker.BuildConfig; import com.trianguloy.urlchecker.R; +import com.trianguloy.urlchecker.fragments.ActivityResultInjector; import com.trianguloy.urlchecker.utilities.generics.GenericPref; import java.util.ArrayList; @@ -192,13 +193,46 @@ static void setTheme(Context activity, boolean dialog) { activity.setTheme(style); } - /* ------------------- general ------------------- */ + /* ------------------- reloading ------------------- */ + + String RELOAD_EXTRA = "reloaded"; + int RELOAD_RESULT_CODE = Activity.RESULT_FIRST_USER; /** - * destroys and recreates the activity (to apply changes) + * destroys and recreates the activity (to apply changes) and marks it */ static void reload(Activity cntx) { Log.d("SETTINGS", "reloading"); + cntx.getIntent().putExtra(RELOAD_EXTRA, true); // keep data cntx.recreate(); } + + /** + * Returns true if the activity was reloaded (with {@link AndroidSettings#reload}) and clears the flag + */ + static boolean wasReloaded(Activity cntx) { + var intent = cntx.getIntent(); + var reloaded = intent.getBooleanExtra(RELOAD_EXTRA, false); + intent.removeExtra(RELOAD_EXTRA); + return reloaded; + } + + /** + * Registers an activity result to reload if the launched activity is marked as reloading using{@link AndroidSettings#markForReloading(Activity)} + */ + static int registerForReloading(ActivityResultInjector activityResultInjector, Activity cntx) { + return activityResultInjector.register((resultCode, data) -> { + if (resultCode == RELOAD_RESULT_CODE) { + AndroidSettings.reload(cntx); + } + }); + } + + /** + * Makes the activity that launched this one to reload, if registered with {@link AndroidSettings#registerForReloading(ActivityResultInjector, Activity)} + */ + static void markForReloading(Activity cntx) { + cntx.setResult(RELOAD_RESULT_CODE); + } + }