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 d4a8b87309..8a20add98e 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 @@ -57,7 +57,6 @@ public class LocalProfileFragment extends SubscriberFragment { if (basalView != null) { basalView.updateLabel(MainApp.sResources.getString(R.string.nsprofileview_basal_label) + ": " + getSumLabel()); } - updateGUI(); }; TextWatcher textWatch = new TextWatcher() { @@ -76,7 +75,6 @@ public class LocalProfileFragment extends SubscriberFragment { int before, int count) { LocalProfilePlugin.getPlugin().dia = SafeParse.stringToDouble(diaView.getText().toString()); doEdit(); - updateGUI(); } }; @@ -146,6 +144,7 @@ public class LocalProfileFragment extends SubscriberFragment { isfView = new TimeListEdit(getContext(), layout, R.id.localprofile_isf, MainApp.sResources.getString(R.string.nsprofileview_isf_label) + ":", LocalProfilePlugin.getPlugin().isf, null, 0.5, 500d, 0.1d, new DecimalFormat("0.0"), save); basalView = new TimeListEdit(getContext(), layout, R.id.localprofile_basal, MainApp.sResources.getString(R.string.nsprofileview_basal_label) + ": " + getSumLabel(), LocalProfilePlugin.getPlugin().basal, null, pumpDescription.basalMinimumRate, 10, 0.01d, new DecimalFormat("0.00"), save); 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); + updateGUI(); }); return layout; @@ -158,12 +157,14 @@ public class LocalProfileFragment extends SubscriberFragment { } public void doEdit() { - LocalProfilePlugin.getPlugin().storeSettings(); + //LocalProfilePlugin.getPlugin().storeSettings(); + LocalProfilePlugin.getPlugin().setEdited(true); + updateGUI(); } @NonNull public String getSumLabel() { - ProfileStore profile = LocalProfilePlugin.getPlugin().getProfile(); + ProfileStore profile = LocalProfilePlugin.getPlugin().createProfileStore(); if (profile != null) return " ∑" + DecimalFormatter.to2Decimal(profile.getDefaultProfile().baseBasalSum()) + "U"; else @@ -182,16 +183,23 @@ public class LocalProfileFragment extends SubscriberFragment { activity.runOnUiThread(new Runnable() { @Override public void run() { - boolean isValid = LocalProfilePlugin.getPlugin().getProfile() != null && LocalProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid(MainApp.gs(R.string.localprofile)); - if (!ConfigBuilderPlugin.getActivePump().isInitialized() || ConfigBuilderPlugin.getActivePump().isSuspended() || !isValid) { - profileswitchButton.setVisibility(View.GONE); - } else { - profileswitchButton.setVisibility(View.VISIBLE); - } - if (isValid) + boolean isValid = LocalProfilePlugin.getPlugin().isValidEditState(); + if (isValid) { invalidProfile.setVisibility(View.GONE); - else + + + if (!ConfigBuilderPlugin.getActivePump().isInitialized() || ConfigBuilderPlugin.getActivePump().isSuspended()) { + profileswitchButton.setVisibility(View.GONE); + } else { + profileswitchButton.setVisibility(View.VISIBLE); + } + + + } + else { invalidProfile.setVisibility(View.VISIBLE); + profileswitchButton.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 eaeb5d388e..8d695ed719 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 @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.ProfileLocal; import android.content.SharedPreferences; import android.preference.PreferenceManager; +import android.support.annotation.NonNull; import org.json.JSONArray; import org.json.JSONException; @@ -69,7 +70,7 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface { loadSettings(); } - public void storeSettings() { + public synchronized void storeSettings() { if (1==1) return; SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); SharedPreferences.Editor editor = settings.edit(); @@ -83,12 +84,13 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface { editor.putString(LOCAL_PROFILE + "targethigh", targetHigh.toString()); editor.apply(); - createConvertedProfile(); + createAndStoreConvertedProfile(); + edited = false; if (Config.logPrefsChange) log.debug("Storing settings: " + getRawProfile().getData().toString()); } - public void loadSettings() { + public synchronized void loadSettings() { if (Config.logPrefsChange) log.debug("Loading stored settings"); @@ -135,7 +137,8 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface { } catch (JSONException ignored) { } } - createConvertedProfile(); + edited = false; + createAndStoreConvertedProfile(); } /* @@ -176,7 +179,16 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface { "created_at": "2016-06-16T08:34:41.256Z" } */ - private void createConvertedProfile() { + private void createAndStoreConvertedProfile() { + convertedProfile = createProfileStore(); + } + + public boolean isValidEditState() { + return createProfileStore().getDefaultProfile().isValid(MainApp.gs(R.string.localprofile)); + } + + @NonNull + public ProfileStore createProfileStore() { JSONObject json = new JSONObject(); JSONObject store = new JSONObject(); JSONObject profile = new JSONObject(); @@ -195,7 +207,7 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface { } catch (JSONException e) { log.error("Unhandled exception", e); } - convertedProfile = new ProfileStore(json); + return new ProfileStore(json); } @Override