Skip to content

Commit

Permalink
tweaked reload
Browse files Browse the repository at this point in the history
  • Loading branch information
TrianguloY committed Jan 29, 2024
1 parent 146892e commit 205a618
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 ------------------- */
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ActivityResultInjector {

/* ------------------- client use ------------------- */

interface Listener {
public interface Listener {
/**
* Called when the event fires for a particular registrar
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 205a618

Please sign in to comment.