fix recreating tabs

This commit is contained in:
Milos Kozak 2019-10-15 20:44:03 +02:00
parent 8d5fbeb66d
commit 6f03743ebc
9 changed files with 41 additions and 57 deletions

View file

@ -21,6 +21,7 @@ import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
@ -45,7 +46,7 @@ import info.nightscout.androidaps.activities.SingleFragmentActivity;
import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.events.EventRebuildTabs;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
@ -79,9 +80,6 @@ public class MainActivity extends NoSplashAppCompatActivity {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (L.isEnabled(L.CORE))
log.debug("onCreate");
Iconify.with(new FontAwesomeModule()); Iconify.with(new FontAwesomeModule());
LocaleHelper.onCreate(this, "en"); LocaleHelper.onCreate(this, "en");
@ -97,13 +95,10 @@ public class MainActivity extends NoSplashAppCompatActivity {
actionBarDrawerToggle.syncState(); actionBarDrawerToggle.syncState();
// initialize screen wake lock // initialize screen wake lock
onEventPreferenceChange(new EventPreferenceChange(R.string.key_keep_screen_on)); processPreferenceChange(new EventPreferenceChange(R.string.key_keep_screen_on));
doMigrations(); doMigrations();
setupTabs();
setupViews(false);
final ViewPager viewPager = findViewById(R.id.pager); final ViewPager viewPager = findViewById(R.id.pager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override @Override
@ -143,8 +138,12 @@ public class MainActivity extends NoSplashAppCompatActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
setupTabs();
setupViews();
disposable.add(RxBus.INSTANCE disposable.add(RxBus.INSTANCE
.toObservable(EventRefreshGui.class) .toObservable(EventRebuildTabs.class)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> { .subscribe(event -> {
String lang = SP.getString(R.string.key_language, "en"); String lang = SP.getString(R.string.key_language, "en");
@ -152,12 +151,8 @@ public class MainActivity extends NoSplashAppCompatActivity {
if (event.getRecreate()) { if (event.getRecreate()) {
recreate(); recreate();
} else { } else {
try { // activity may be destroyed setupTabs();
setupTabs(); setupViews();
setupViews(false);
} catch (IllegalStateException e) {
log.error("Unhandled exception", e);
}
} }
boolean keepScreenOn = Config.NSCLIENT && SP.getBoolean(R.string.key_keep_screen_on, false); boolean keepScreenOn = Config.NSCLIENT && SP.getBoolean(R.string.key_keep_screen_on, false);
@ -170,12 +165,9 @@ public class MainActivity extends NoSplashAppCompatActivity {
disposable.add(RxBus.INSTANCE disposable.add(RxBus.INSTANCE
.toObservable(EventPreferenceChange.class) .toObservable(EventPreferenceChange.class)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> onEventPreferenceChange(event), FabricPrivacy::logException) .subscribe(this::processPreferenceChange, FabricPrivacy::logException)
); );
if (L.isEnabled(L.CORE))
log.debug("onResume");
if (!SP.getBoolean(R.string.key_setupwizard_processed, false)) { if (!SP.getBoolean(R.string.key_setupwizard_processed, false)) {
Intent intent = new Intent(this, SetupWizardActivity.class); Intent intent = new Intent(this, SetupWizardActivity.class);
startActivity(intent); startActivity(intent);
@ -189,12 +181,10 @@ public class MainActivity extends NoSplashAppCompatActivity {
AndroidPermission.notifyForLocationPermissions(this); AndroidPermission.notifyForLocationPermissions(this);
AndroidPermission.notifyForSMSPermissions(this); AndroidPermission.notifyForSMSPermissions(this);
} }
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
if (L.isEnabled(L.CORE))
log.debug("onDestroy");
if (mWakeLock != null) if (mWakeLock != null)
if (mWakeLock.isHeld()) if (mWakeLock.isHeld())
mWakeLock.release(); mWakeLock.release();
@ -207,7 +197,7 @@ public class MainActivity extends NoSplashAppCompatActivity {
disposable.clear(); disposable.clear();
} }
public void onEventPreferenceChange(final EventPreferenceChange ev) { public void processPreferenceChange(final EventPreferenceChange ev) {
if (ev.isChanged(R.string.key_keep_screen_on)) { if (ev.isChanged(R.string.key_keep_screen_on)) {
boolean keepScreenOn = SP.getBoolean(R.string.key_keep_screen_on, false); boolean keepScreenOn = SP.getBoolean(R.string.key_keep_screen_on, false);
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
@ -222,12 +212,10 @@ public class MainActivity extends NoSplashAppCompatActivity {
} }
} }
private void setupViews(boolean switchToLast) { private void setupViews() {
TabPageAdapter pageAdapter = new TabPageAdapter(getSupportFragmentManager(), this); TabPageAdapter pageAdapter = new TabPageAdapter(getSupportFragmentManager(), this);
NavigationView navigationView = findViewById(R.id.navigation_view); NavigationView navigationView = findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(menuItem -> { navigationView.setNavigationItemSelectedListener(menuItem -> true);
return true;
});
Menu menu = navigationView.getMenu(); Menu menu = navigationView.getMenu();
menu.clear(); menu.clear();
for (PluginBase p : MainApp.getPluginsList()) { for (PluginBase p : MainApp.getPluginsList()) {
@ -246,8 +234,8 @@ public class MainActivity extends NoSplashAppCompatActivity {
} }
ViewPager mPager = findViewById(R.id.pager); ViewPager mPager = findViewById(R.id.pager);
mPager.setAdapter(pageAdapter); mPager.setAdapter(pageAdapter);
if (switchToLast) //if (switchToLast)
mPager.setCurrentItem(pageAdapter.getCount() - 1, false); // mPager.setCurrentItem(pageAdapter.getCount() - 1, false);
checkPluginPreferences(mPager); checkPluginPreferences(mPager);
} }
@ -289,10 +277,10 @@ public class MainActivity extends NoSplashAppCompatActivity {
// guarantee that the unreachable threshold is at least 30 and of type String // guarantee that the unreachable threshold is at least 30 and of type String
// Added in 1.57 at 21.01.2018 // Added in 1.57 at 21.01.2018
Integer unreachable_threshold = SP.getInt(R.string.key_pump_unreachable_threshold, 30); int unreachable_threshold = SP.getInt(R.string.key_pump_unreachable_threshold, 30);
SP.remove(R.string.key_pump_unreachable_threshold); SP.remove(R.string.key_pump_unreachable_threshold);
if (unreachable_threshold < 30) unreachable_threshold = 30; if (unreachable_threshold < 30) unreachable_threshold = 30;
SP.putString(R.string.key_pump_unreachable_threshold, unreachable_threshold.toString()); SP.putString(R.string.key_pump_unreachable_threshold, Integer.toString(unreachable_threshold));
} }
@ -308,19 +296,16 @@ public class MainActivity extends NoSplashAppCompatActivity {
String message = "Target range is changed in current version.\n\nIt's not taken from preferences but from profile.\n\n!!! REVIEW YOUR SETTINGS !!!"; String message = "Target range is changed in current version.\n\nIt's not taken from preferences but from profile.\n\n!!! REVIEW YOUR SETTINGS !!!";
message += "\n\nOld settings: " + oldRange; message += "\n\nOld settings: " + oldRange;
message += "\nProfile settings: " + newRange; message += "\nProfile settings: " + newRange;
OKDialog.show(this, "Target range change", message, new Runnable() { OKDialog.show(this, "Target range change", message, () -> {
@Override SP.remove("openapsma_min_bg");
public void run() { SP.remove("openapsma_max_bg");
SP.remove("openapsma_min_bg"); SP.remove("openapsma_target_bg");
SP.remove("openapsma_max_bg");
SP.remove("openapsma_target_bg");
}
}); });
} }
} }
@Override @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults); super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (permissions.length != 0) { if (permissions.length != 0) {
if (ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED) {

View file

@ -17,7 +17,7 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.events.EventRebuildTabs;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.plugins.general.careportal.CareportalPlugin; import info.nightscout.androidaps.plugins.general.careportal.CareportalPlugin;
@ -72,12 +72,12 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
if (key.equals("language")) { if (key.equals("language")) {
String lang = sharedPreferences.getString("language", "en"); String lang = sharedPreferences.getString("language", "en");
LocaleHelper.setLocale(getApplicationContext(), lang); LocaleHelper.setLocale(getApplicationContext(), lang);
RxBus.INSTANCE.send(new EventRefreshGui(true)); RxBus.INSTANCE.send(new EventRebuildTabs(true));
//recreate() does not update language so better close settings //recreate() does not update language so better close settings
finish(); finish();
} }
if (key.equals("short_tabtitles")) { if (key.equals("short_tabtitles")) {
RxBus.INSTANCE.send(new EventRefreshGui()); RxBus.INSTANCE.send(new EventRebuildTabs());
} }
if (key.equals(MainApp.gs(R.string.key_openapsama_useautosens)) && SP.getBoolean(R.string.key_openapsama_useautosens, false)) { if (key.equals(MainApp.gs(R.string.key_openapsama_useautosens)) && SP.getBoolean(R.string.key_openapsama_useautosens, false)) {
OKDialog.show(this, MainApp.gs(R.string.configbuilder_sensitivity), MainApp.gs(R.string.sensitivity_warning), null); OKDialog.show(this, MainApp.gs(R.string.configbuilder_sensitivity), MainApp.gs(R.string.sensitivity_warning), null);

View file

@ -0,0 +1,3 @@
package info.nightscout.androidaps.events
class EventRebuildTabs @JvmOverloads constructor(var recreate: Boolean = false) : Event()

View file

@ -1,3 +0,0 @@
package info.nightscout.androidaps.events
class EventRefreshGui @JvmOverloads constructor(var recreate: Boolean = false) : Event()

View file

@ -11,7 +11,7 @@ import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventConfigBuilderChange; import info.nightscout.androidaps.events.EventConfigBuilderChange;
import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.events.EventRebuildTabs;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
@ -80,7 +80,7 @@ public abstract class PluginBase {
setFragmentVisible(type, enabled); setFragmentVisible(type, enabled);
ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(this, getType()); ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(this, getType());
ConfigBuilderPlugin.getPlugin().storeSettings("CheckedCheckboxEnabled"); ConfigBuilderPlugin.getPlugin().storeSettings("CheckedCheckboxEnabled");
RxBus.INSTANCE.send(new EventRefreshGui()); RxBus.INSTANCE.send(new EventRebuildTabs());
RxBus.INSTANCE.send(new EventConfigBuilderChange()); RxBus.INSTANCE.send(new EventConfigBuilderChange());
RxBus.INSTANCE.send(new EventConfigBuilderUpdateGui()); RxBus.INSTANCE.send(new EventConfigBuilderUpdateGui());
ConfigBuilderPlugin.getPlugin().logPluginStatus(); ConfigBuilderPlugin.getPlugin().logPluginStatus();

View file

@ -3,10 +3,9 @@ package info.nightscout.androidaps.plugins.configBuilder
import android.content.Intent import android.content.Intent
import android.view.View import android.view.View
import android.widget.* import android.widget.*
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.PreferencesActivity import info.nightscout.androidaps.activities.PreferencesActivity
import info.nightscout.androidaps.events.EventRefreshGui import info.nightscout.androidaps.events.EventRebuildTabs
import info.nightscout.androidaps.interfaces.PluginBase import info.nightscout.androidaps.interfaces.PluginBase
import info.nightscout.androidaps.interfaces.PluginType import info.nightscout.androidaps.interfaces.PluginType
import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.bus.RxBus
@ -35,7 +34,7 @@ class PluginViewHolder internal constructor(private val fragment: ConfigBuilderF
pluginVisibility.setOnClickListener { pluginVisibility.setOnClickListener {
plugin.setFragmentVisible(pluginType, pluginVisibility.isChecked) plugin.setFragmentVisible(pluginType, pluginVisibility.isChecked)
ConfigBuilderPlugin.getPlugin().storeSettings("CheckedCheckboxVisible") ConfigBuilderPlugin.getPlugin().storeSettings("CheckedCheckboxVisible")
RxBus.send(EventRefreshGui()) RxBus.send(EventRebuildTabs())
ConfigBuilderPlugin.getPlugin().logPluginStatus() ConfigBuilderPlugin.getPlugin().logPluginStatus()
} }

View file

@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.events.EventRebuildTabs;
import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.bus.RxBus;
@ -47,7 +47,7 @@ public class MsgInitConnStatusTime extends MessageBase {
} }
ConfigBuilderPlugin.getPlugin().storeSettings("ChangingDanaDriver"); ConfigBuilderPlugin.getPlugin().storeSettings("ChangingDanaDriver");
RxBus.INSTANCE.send(new EventRefreshGui()); RxBus.INSTANCE.send(new EventRebuildTabs());
ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("PumpDriverChange", null); // force new connection ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("PumpDriverChange", null); // force new connection
failed = false; failed = false;
return; return;

View file

@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.events.EventRebuildTabs;
import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.bus.RxBus;
@ -49,7 +49,7 @@ public class MsgInitConnStatusTime_k extends MessageBase {
} }
ConfigBuilderPlugin.getPlugin().storeSettings("ChangingKoreanDanaDriver"); ConfigBuilderPlugin.getPlugin().storeSettings("ChangingKoreanDanaDriver");
RxBus.INSTANCE.send(new EventRefreshGui()); RxBus.INSTANCE.send(new EventRebuildTabs());
ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("PumpDriverChange", null); // force new connection ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("PumpDriverChange", null); // force new connection
return; return;
} }

View file

@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.events.EventRebuildTabs;
import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.bus.RxBus;
@ -61,7 +61,7 @@ public class MsgCheckValue_v2 extends MessageBase {
} }
ConfigBuilderPlugin.getPlugin().storeSettings("ChangingDanaRv2Driver"); ConfigBuilderPlugin.getPlugin().storeSettings("ChangingDanaRv2Driver");
RxBus.INSTANCE.send(new EventRefreshGui()); RxBus.INSTANCE.send(new EventRebuildTabs());
ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("PumpDriverChange", null); // force new connection ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("PumpDriverChange", null); // force new connection
return; return;
} }
@ -84,7 +84,7 @@ public class MsgCheckValue_v2 extends MessageBase {
} }
ConfigBuilderPlugin.getPlugin().storeSettings("ChangingDanaRv2Driver"); ConfigBuilderPlugin.getPlugin().storeSettings("ChangingDanaRv2Driver");
RxBus.INSTANCE.send(new EventRefreshGui()); RxBus.INSTANCE.send(new EventRebuildTabs());
ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("PumpDriverChange", null); // force new connection ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("PumpDriverChange", null); // force new connection
return; return;
} }