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.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
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.events.EventAppExit;
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.PluginType;
import info.nightscout.androidaps.logging.L;
@ -79,9 +80,6 @@ public class MainActivity extends NoSplashAppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (L.isEnabled(L.CORE))
log.debug("onCreate");
Iconify.with(new FontAwesomeModule());
LocaleHelper.onCreate(this, "en");
@ -97,13 +95,10 @@ public class MainActivity extends NoSplashAppCompatActivity {
actionBarDrawerToggle.syncState();
// initialize screen wake lock
onEventPreferenceChange(new EventPreferenceChange(R.string.key_keep_screen_on));
processPreferenceChange(new EventPreferenceChange(R.string.key_keep_screen_on));
doMigrations();
setupTabs();
setupViews(false);
final ViewPager viewPager = findViewById(R.id.pager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
@ -143,8 +138,12 @@ public class MainActivity extends NoSplashAppCompatActivity {
@Override
protected void onResume() {
super.onResume();
setupTabs();
setupViews();
disposable.add(RxBus.INSTANCE
.toObservable(EventRefreshGui.class)
.toObservable(EventRebuildTabs.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> {
String lang = SP.getString(R.string.key_language, "en");
@ -152,12 +151,8 @@ public class MainActivity extends NoSplashAppCompatActivity {
if (event.getRecreate()) {
recreate();
} else {
try { // activity may be destroyed
setupTabs();
setupViews(false);
} catch (IllegalStateException e) {
log.error("Unhandled exception", e);
}
setupTabs();
setupViews();
}
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
.toObservable(EventPreferenceChange.class)
.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)) {
Intent intent = new Intent(this, SetupWizardActivity.class);
startActivity(intent);
@ -189,12 +181,10 @@ public class MainActivity extends NoSplashAppCompatActivity {
AndroidPermission.notifyForLocationPermissions(this);
AndroidPermission.notifyForSMSPermissions(this);
}
}
}
@Override
public void onDestroy() {
if (L.isEnabled(L.CORE))
log.debug("onDestroy");
if (mWakeLock != null)
if (mWakeLock.isHeld())
mWakeLock.release();
@ -207,7 +197,7 @@ public class MainActivity extends NoSplashAppCompatActivity {
disposable.clear();
}
public void onEventPreferenceChange(final EventPreferenceChange ev) {
public void processPreferenceChange(final EventPreferenceChange ev) {
if (ev.isChanged(R.string.key_keep_screen_on)) {
boolean keepScreenOn = SP.getBoolean(R.string.key_keep_screen_on, false);
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);
NavigationView navigationView = findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(menuItem -> {
return true;
});
navigationView.setNavigationItemSelectedListener(menuItem -> true);
Menu menu = navigationView.getMenu();
menu.clear();
for (PluginBase p : MainApp.getPluginsList()) {
@ -246,8 +234,8 @@ public class MainActivity extends NoSplashAppCompatActivity {
}
ViewPager mPager = findViewById(R.id.pager);
mPager.setAdapter(pageAdapter);
if (switchToLast)
mPager.setCurrentItem(pageAdapter.getCount() - 1, false);
//if (switchToLast)
// mPager.setCurrentItem(pageAdapter.getCount() - 1, false);
checkPluginPreferences(mPager);
}
@ -289,10 +277,10 @@ public class MainActivity extends NoSplashAppCompatActivity {
// guarantee that the unreachable threshold is at least 30 and of type String
// 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);
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 !!!";
message += "\n\nOld settings: " + oldRange;
message += "\nProfile settings: " + newRange;
OKDialog.show(this, "Target range change", message, new Runnable() {
@Override
public void run() {
SP.remove("openapsma_min_bg");
SP.remove("openapsma_max_bg");
SP.remove("openapsma_target_bg");
}
OKDialog.show(this, "Target range change", message, () -> {
SP.remove("openapsma_min_bg");
SP.remove("openapsma_max_bg");
SP.remove("openapsma_target_bg");
});
}
}
@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);
if (permissions.length != 0) {
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.plugins.bus.RxBus;
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.PluginType;
import info.nightscout.androidaps.plugins.general.careportal.CareportalPlugin;
@ -72,12 +72,12 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
if (key.equals("language")) {
String lang = sharedPreferences.getString("language", "en");
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
finish();
}
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)) {
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.R;
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.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
@ -80,7 +80,7 @@ public abstract class PluginBase {
setFragmentVisible(type, enabled);
ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(this, getType());
ConfigBuilderPlugin.getPlugin().storeSettings("CheckedCheckboxEnabled");
RxBus.INSTANCE.send(new EventRefreshGui());
RxBus.INSTANCE.send(new EventRebuildTabs());
RxBus.INSTANCE.send(new EventConfigBuilderChange());
RxBus.INSTANCE.send(new EventConfigBuilderUpdateGui());
ConfigBuilderPlugin.getPlugin().logPluginStatus();

View file

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

View file

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

View file

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

View file

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