Merge pull request #832 from MilosKozak/lp-edit

LocalProfile reset/save button
This commit is contained in:
AdrianLxM 2018-04-09 14:08:22 +02:00 committed by GitHub
commit d1d46849ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 161 additions and 53 deletions

View file

@ -4,6 +4,7 @@ package info.nightscout.androidaps.plugins.ProfileLocal;
import android.app.Activity; import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -48,21 +49,16 @@ public class LocalProfileFragment extends SubscriberFragment {
TimeListEdit basalView; TimeListEdit basalView;
TimeListEdit targetView; TimeListEdit targetView;
Button profileswitchButton; Button profileswitchButton;
Button resetButton;
Button saveButton;
TextView invalidProfile; TextView invalidProfile;
@Override Runnable save = () -> {
public View onCreateView(LayoutInflater inflater, ViewGroup container, doEdit();
Bundle savedInstanceState) {
try {
Runnable save = new Runnable() {
@Override
public void run() {
LocalProfilePlugin.getPlugin().storeSettings();
if (basalView != null) { if (basalView != null) {
basalView.updateLabel(MainApp.sResources.getString(R.string.nsprofileview_basal_label) + ": " + getSumLabel()); basalView.updateLabel(MainApp.sResources.getString(R.string.nsprofileview_basal_label) + ": " + getSumLabel());
} }
updateGUI();
}
}; };
TextWatcher textWatch = new TextWatcher() { TextWatcher textWatch = new TextWatcher() {
@ -80,13 +76,16 @@ public class LocalProfileFragment extends SubscriberFragment {
public void onTextChanged(CharSequence s, int start, public void onTextChanged(CharSequence s, int start,
int before, int count) { int before, int count) {
LocalProfilePlugin.getPlugin().dia = SafeParse.stringToDouble(diaView.getText().toString()); LocalProfilePlugin.getPlugin().dia = SafeParse.stringToDouble(diaView.getText().toString());
LocalProfilePlugin.getPlugin().storeSettings(); doEdit();
updateGUI();
} }
}; };
PumpDescription pumpDescription = ConfigBuilderPlugin.getActivePump().getPumpDescription(); @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try {
PumpDescription pumpDescription = ConfigBuilderPlugin.getActivePump().getPumpDescription();
View layout = inflater.inflate(R.layout.localprofile_fragment, container, false); View layout = inflater.inflate(R.layout.localprofile_fragment, container, false);
diaView = (NumberPicker) layout.findViewById(R.id.localprofile_dia); diaView = (NumberPicker) layout.findViewById(R.id.localprofile_dia);
diaView.setParams(LocalProfilePlugin.getPlugin().dia, 2d, 48d, 0.1d, new DecimalFormat("0.0"), false, textWatch); diaView.setParams(LocalProfilePlugin.getPlugin().dia, 2d, 48d, 0.1d, new DecimalFormat("0.0"), false, textWatch);
@ -97,6 +96,10 @@ public class LocalProfileFragment extends SubscriberFragment {
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); 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); 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); 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); invalidProfile = (TextView) layout.findViewById(R.id.invalidprofile);
if (!ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable) { if (!ConfigBuilderPlugin.getActivePump().getPumpDescription().isTempBasalCapable) {
@ -112,7 +115,7 @@ public class LocalProfileFragment extends SubscriberFragment {
LocalProfilePlugin.getPlugin().mgdl = mgdlView.isChecked(); LocalProfilePlugin.getPlugin().mgdl = mgdlView.isChecked();
LocalProfilePlugin.getPlugin().mmol = !LocalProfilePlugin.getPlugin().mgdl; LocalProfilePlugin.getPlugin().mmol = !LocalProfilePlugin.getPlugin().mgdl;
mmolView.setChecked(LocalProfilePlugin.getPlugin().mmol); mmolView.setChecked(LocalProfilePlugin.getPlugin().mmol);
LocalProfilePlugin.getPlugin().storeSettings(); doEdit();
} }
}); });
mmolView.setOnClickListener(new View.OnClickListener() { mmolView.setOnClickListener(new View.OnClickListener() {
@ -121,7 +124,7 @@ public class LocalProfileFragment extends SubscriberFragment {
LocalProfilePlugin.getPlugin().mmol = mmolView.isChecked(); LocalProfilePlugin.getPlugin().mmol = mmolView.isChecked();
LocalProfilePlugin.getPlugin().mgdl = !LocalProfilePlugin.getPlugin().mmol; LocalProfilePlugin.getPlugin().mgdl = !LocalProfilePlugin.getPlugin().mmol;
mgdlView.setChecked(LocalProfilePlugin.getPlugin().mgdl); mgdlView.setChecked(LocalProfilePlugin.getPlugin().mgdl);
LocalProfilePlugin.getPlugin().storeSettings(); doEdit();
} }
}); });
@ -136,6 +139,25 @@ public class LocalProfileFragment extends SubscriberFragment {
} }
}); });
resetButton.setOnClickListener(view -> {
LocalProfilePlugin.getPlugin().loadSettings();
mgdlView.setChecked(LocalProfilePlugin.getPlugin().mgdl);
mmolView.setChecked(LocalProfilePlugin.getPlugin().mmol);
diaView.setParams(LocalProfilePlugin.getPlugin().dia, 2d, 48d, 0.1d, new DecimalFormat("0.0"), false, textWatch);
icView = new TimeListEdit(getContext(), layout, R.id.localprofile_ic, MainApp.sResources.getString(R.string.nsprofileview_ic_label) + ":", LocalProfilePlugin.getPlugin().ic, null, 0.5, 50d, 0.1d, new DecimalFormat("0.0"), save);
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();
});
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; return layout;
} catch (Exception e) { } catch (Exception e) {
@ -146,9 +168,14 @@ public class LocalProfileFragment extends SubscriberFragment {
return null; return null;
} }
public void doEdit() {
LocalProfilePlugin.getPlugin().setEdited(true);
updateGUI();
}
@NonNull @NonNull
public String getSumLabel() { public String getSumLabel() {
ProfileStore profile = LocalProfilePlugin.getPlugin().getProfile(); ProfileStore profile = LocalProfilePlugin.getPlugin().createProfileStore();
if (profile != null) if (profile != null)
return "" + DecimalFormatter.to2Decimal(profile.getDefaultProfile().baseBasalSum()) + "U"; return "" + DecimalFormatter.to2Decimal(profile.getDefaultProfile().baseBasalSum()) + "U";
else else
@ -167,16 +194,39 @@ public class LocalProfileFragment extends SubscriberFragment {
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
boolean isValid = LocalProfilePlugin.getPlugin().getProfile() != null && LocalProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid(MainApp.gs(R.string.localprofile)); boolean isValid = LocalProfilePlugin.getPlugin().isValidEditState();
if (!ConfigBuilderPlugin.getActivePump().isInitialized() || ConfigBuilderPlugin.getActivePump().isSuspended() || !isValid) { boolean isEdited = LocalProfilePlugin.getPlugin().isEdited();
if (isValid) {
invalidProfile.setVisibility(View.GONE); //show invalid profile
if (isEdited || !ConfigBuilderPlugin.getActivePump().isInitialized() || ConfigBuilderPlugin.getActivePump().isSuspended()) {
//edited profile -> save first
//pump not initialized -> don't update profile yet
profileswitchButton.setVisibility(View.GONE); profileswitchButton.setVisibility(View.GONE);
} else { } else {
profileswitchButton.setVisibility(View.VISIBLE); profileswitchButton.setVisibility(View.VISIBLE);
} }
if (isValid)
invalidProfile.setVisibility(View.GONE); if(isEdited){
else saveButton.setVisibility(View.VISIBLE);
} else {
saveButton.setVisibility(View.GONE);
}
} else {
invalidProfile.setVisibility(View.VISIBLE); 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);
}
} }
}); });
} }

View file

@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.ProfileLocal;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
@ -40,6 +41,15 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface {
private static final String DEFAULTARRAY = "[{\"time\":\"00:00\",\"timeAsSeconds\":0,\"value\":0}]"; private static final String DEFAULTARRAY = "[{\"time\":\"00:00\",\"timeAsSeconds\":0,\"value\":0}]";
public boolean isEdited() {
return edited;
}
public void setEdited(boolean edited) {
this.edited = edited;
}
boolean edited;
boolean mgdl; boolean mgdl;
boolean mmol; boolean mmol;
Double dia; Double dia;
@ -59,7 +69,7 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface {
loadSettings(); loadSettings();
} }
public void storeSettings() { public synchronized void storeSettings() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(LOCAL_PROFILE + "mmol", mmol); editor.putBoolean(LOCAL_PROFILE + "mmol", mmol);
@ -72,12 +82,13 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface {
editor.putString(LOCAL_PROFILE + "targethigh", targetHigh.toString()); editor.putString(LOCAL_PROFILE + "targethigh", targetHigh.toString());
editor.apply(); editor.apply();
createConvertedProfile(); createAndStoreConvertedProfile();
edited = false;
if (Config.logPrefsChange) if (Config.logPrefsChange)
log.debug("Storing settings: " + getRawProfile().getData().toString()); log.debug("Storing settings: " + getRawProfile().getData().toString());
} }
public void loadSettings() { public synchronized void loadSettings() {
if (Config.logPrefsChange) if (Config.logPrefsChange)
log.debug("Loading stored settings"); log.debug("Loading stored settings");
@ -124,6 +135,8 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface {
} catch (JSONException ignored) { } catch (JSONException ignored) {
} }
} }
edited = false;
createAndStoreConvertedProfile();
} }
/* /*
@ -164,7 +177,16 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface {
"created_at": "2016-06-16T08:34:41.256Z" "created_at": "2016-06-16T08:34:41.256Z"
} }
*/ */
private void createConvertedProfile() { private void createAndStoreConvertedProfile() {
convertedProfile = createProfileStore();
}
public synchronized boolean isValidEditState() {
return createProfileStore().getDefaultProfile().isValid(MainApp.gs(R.string.localprofile));
}
@NonNull
public ProfileStore createProfileStore() {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
JSONObject store = new JSONObject(); JSONObject store = new JSONObject();
JSONObject profile = new JSONObject(); JSONObject profile = new JSONObject();
@ -183,21 +205,17 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface {
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }
convertedProfile = new ProfileStore(json); return new ProfileStore(json);
} }
@Override @Override
public ProfileStore getProfile() { public ProfileStore getProfile() {
if (convertedProfile == null)
createConvertedProfile();
if (!convertedProfile.getDefaultProfile().isValid(MainApp.gs(R.string.localprofile))) if (!convertedProfile.getDefaultProfile().isValid(MainApp.gs(R.string.localprofile)))
return null; return null;
return convertedProfile; return convertedProfile;
} }
public ProfileStore getRawProfile() { public ProfileStore getRawProfile() {
if (convertedProfile == null)
createConvertedProfile();
return convertedProfile; return convertedProfile;
} }
@ -208,8 +226,6 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface {
@Override @Override
public String getProfileName() { public String getProfileName() {
if (convertedProfile == null)
createConvertedProfile();
return DecimalFormatter.to2Decimal(convertedProfile.getDefaultProfile().percentageBasalSum()) + "U "; return DecimalFormatter.to2Decimal(convertedProfile.getDefaultProfile().percentageBasalSum()) + "U ";
} }

View file

@ -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) { 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); setParams(initValue, minValue, maxValue, step, formater, allowZero);
this.textWatcher = textWatcher; this.textWatcher = textWatcher;
editText.addTextChangedListener(textWatcher); editText.addTextChangedListener(textWatcher);

View file

@ -78,6 +78,7 @@ public class TimeListEdit {
private void buildView() { private void buildView() {
layout = (LinearLayout) view.findViewById(resLayoutId); layout = (LinearLayout) view.findViewById(resLayoutId);
layout.removeAllViews();
textlabel = new TextView(context); textlabel = new TextView(context);
textlabel.setText(label); textlabel.setText(label);

View file

@ -153,6 +153,43 @@
android:text="@string/activate_profile" android:text="@string/activate_profile"
android:textColor="@color/colorProfileSwitchButton" /> android:textColor="@color/colorProfileSwitchButton" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/localprofile_reset"
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/reset"
android:textColor="@color/colorProfileSwitchButton" />
<Button
android:id="@+id/localprofile_save"
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/save"
android:textColor="@color/colorProfileSwitchButton" />
</LinearLayout>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>

View file

@ -987,6 +987,7 @@
<string name="smbnotallowedinopenloopmode">SMB not allowed in open loop mode</string> <string name="smbnotallowedinopenloopmode">SMB not allowed in open loop mode</string>
<string name="food_short">Food</string> <string name="food_short">Food</string>
<string name="iobcobcalculator" translatable="false">IobCobCalculator</string> <string name="iobcobcalculator" translatable="false">IobCobCalculator</string>
<string name="reset">reset</string>
<string name="waitingfortimesynchronization">Waiting for time synchronization (%d sec)</string> <string name="waitingfortimesynchronization">Waiting for time synchronization (%d sec)</string>
<string name="loopdisconnectedfor">Disconnected (%d m)</string> <string name="loopdisconnectedfor">Disconnected (%d m)</string>
<string name="automatic_careportal_events">Automatic careportal events</string> <string name="automatic_careportal_events">Automatic careportal events</string>