diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java index 8a20add98e..a325f08ad2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java @@ -50,6 +50,8 @@ public class LocalProfileFragment extends SubscriberFragment { TimeListEdit targetView; Button profileswitchButton; Button resetButton; + Button saveButton; + TextView invalidProfile; Runnable save = () -> { @@ -95,6 +97,8 @@ public class LocalProfileFragment extends SubscriberFragment { targetView = new TimeListEdit(getContext(), layout, R.id.localprofile_target, MainApp.sResources.getString(R.string.nsprofileview_target_label) + ":", LocalProfilePlugin.getPlugin().targetLow, LocalProfilePlugin.getPlugin().targetHigh, 3d, 200, 0.1d, new DecimalFormat("0.0"), save); profileswitchButton = (Button) layout.findViewById(R.id.localprofile_profileswitch); resetButton = (Button) layout.findViewById(R.id.localprofile_reset); + saveButton = (Button) layout.findViewById(R.id.localprofile_save); + invalidProfile = (TextView) layout.findViewById(R.id.invalidprofile); @@ -147,6 +151,14 @@ public class LocalProfileFragment extends SubscriberFragment { updateGUI(); }); + saveButton.setOnClickListener(view -> { + if(!LocalProfilePlugin.getPlugin().isValidEditState()){ + return; //Should not happen as saveButton should not be visible if not valid + } + LocalProfilePlugin.getPlugin().storeSettings(); + updateGUI(); + }); + return layout; } catch (Exception e) { log.error("Unhandled exception: ", e); @@ -157,7 +169,6 @@ public class LocalProfileFragment extends SubscriberFragment { } public void doEdit() { - //LocalProfilePlugin.getPlugin().storeSettings(); LocalProfilePlugin.getPlugin().setEdited(true); updateGUI(); } @@ -184,21 +195,37 @@ public class LocalProfileFragment extends SubscriberFragment { @Override public void run() { boolean isValid = LocalProfilePlugin.getPlugin().isValidEditState(); + boolean isEdited = LocalProfilePlugin.getPlugin().isEdited(); if (isValid) { - invalidProfile.setVisibility(View.GONE); + invalidProfile.setVisibility(View.GONE); //show invalid profile - - if (!ConfigBuilderPlugin.getActivePump().isInitialized() || ConfigBuilderPlugin.getActivePump().isSuspended()) { + if (isEdited || !ConfigBuilderPlugin.getActivePump().isInitialized() || ConfigBuilderPlugin.getActivePump().isSuspended()) { + //edited profile -> save first + //pump not initialized -> don't update profile yet profileswitchButton.setVisibility(View.GONE); } else { profileswitchButton.setVisibility(View.VISIBLE); } + if(isEdited){ + saveButton.setVisibility(View.VISIBLE); + } else { + saveButton.setVisibility(View.GONE); - } - else { + } + + + } else { invalidProfile.setVisibility(View.VISIBLE); profileswitchButton.setVisibility(View.GONE); + saveButton.setVisibility(View.GONE); //don't save an invalid profile + } + + //Show reset button iff data was edited + if(isEdited) { + resetButton.setVisibility(View.VISIBLE); + } else { + resetButton.setVisibility(View.GONE); } } }); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java index 8d695ed719..30e0d9b064 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java @@ -46,7 +46,6 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface { } public void setEdited(boolean edited) { - //TODO check if edited is a valid profile! this.edited = edited; } @@ -71,7 +70,6 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface { } public synchronized void storeSettings() { - if (1==1) return; SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean(LOCAL_PROFILE + "mmol", mmol); @@ -183,7 +181,7 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface { convertedProfile = createProfileStore(); } - public boolean isValidEditState() { + public synchronized boolean isValidEditState() { return createProfileStore().getDefaultProfile().isValid(MainApp.gs(R.string.localprofile)); } diff --git a/app/src/main/java/info/nightscout/utils/NumberPicker.java b/app/src/main/java/info/nightscout/utils/NumberPicker.java index 53ffd90f7b..01c7dcb336 100644 --- a/app/src/main/java/info/nightscout/utils/NumberPicker.java +++ b/app/src/main/java/info/nightscout/utils/NumberPicker.java @@ -148,6 +148,9 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, } public void setParams(Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero, TextWatcher textWatcher) { + if(this.textWatcher != null) { + editText.removeTextChangedListener(this.textWatcher); + } setParams(initValue, minValue, maxValue, step, formater, allowZero); this.textWatcher = textWatcher; editText.addTextChangedListener(textWatcher); diff --git a/app/src/main/res/layout/localprofile_fragment.xml b/app/src/main/res/layout/localprofile_fragment.xml index 68d1bf81b5..d4defa4142 100644 --- a/app/src/main/res/layout/localprofile_fragment.xml +++ b/app/src/main/res/layout/localprofile_fragment.xml @@ -153,22 +153,43 @@ android:text="@string/activate_profile" android:textColor="@color/colorProfileSwitchButton" /> -