mirror of
https://github.com/TrianguloY/UrlChecker.git
synced 2024-09-19 20:02:16 +02:00
tweaked reload
This commit is contained in:
parent
146892e948
commit
205a618716
@ -10,20 +10,18 @@ import android.view.View;
|
||||
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 class MainActivity extends Activity {
|
||||
}
|
||||
|
||||
@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 class MainActivity extends Activity {
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -35,6 +35,9 @@ public class SettingsActivity extends Activity {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -18,7 +18,7 @@ public class ActivityResultInjector {
|
||||
|
||||
/* ------------------- client use ------------------- */
|
||||
|
||||
interface Listener {
|
||||
public interface Listener {
|
||||
/**
|
||||
* Called when the event fires for a particular registrar
|
||||
*/
|
||||
|
@ -9,6 +9,7 @@ import android.util.Log;
|
||||
|
||||
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 @@ public interface AndroidSettings {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user