lp save button

This commit is contained in:
AdrianLxM 2018-04-03 18:33:13 +02:00
parent f17deaedfb
commit 933b1acd7e
4 changed files with 70 additions and 21 deletions

View file

@ -50,6 +50,8 @@ public class LocalProfileFragment extends SubscriberFragment {
TimeListEdit targetView; TimeListEdit targetView;
Button profileswitchButton; Button profileswitchButton;
Button resetButton; Button resetButton;
Button saveButton;
TextView invalidProfile; TextView invalidProfile;
Runnable save = () -> { 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); 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); 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);
@ -147,6 +151,14 @@ public class LocalProfileFragment extends SubscriberFragment {
updateGUI(); 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) {
log.error("Unhandled exception: ", e); log.error("Unhandled exception: ", e);
@ -157,7 +169,6 @@ public class LocalProfileFragment extends SubscriberFragment {
} }
public void doEdit() { public void doEdit() {
//LocalProfilePlugin.getPlugin().storeSettings();
LocalProfilePlugin.getPlugin().setEdited(true); LocalProfilePlugin.getPlugin().setEdited(true);
updateGUI(); updateGUI();
} }
@ -184,21 +195,37 @@ public class LocalProfileFragment extends SubscriberFragment {
@Override @Override
public void run() { public void run() {
boolean isValid = LocalProfilePlugin.getPlugin().isValidEditState(); boolean isValid = LocalProfilePlugin.getPlugin().isValidEditState();
boolean isEdited = LocalProfilePlugin.getPlugin().isEdited();
if (isValid) { if (isValid) {
invalidProfile.setVisibility(View.GONE); invalidProfile.setVisibility(View.GONE); //show invalid profile
if (isEdited || !ConfigBuilderPlugin.getActivePump().isInitialized() || ConfigBuilderPlugin.getActivePump().isSuspended()) {
if (!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(isEdited){
saveButton.setVisibility(View.VISIBLE);
} else {
saveButton.setVisibility(View.GONE);
} }
else {
} else {
invalidProfile.setVisibility(View.VISIBLE); invalidProfile.setVisibility(View.VISIBLE);
profileswitchButton.setVisibility(View.GONE); 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

@ -46,7 +46,6 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface {
} }
public void setEdited(boolean edited) { public void setEdited(boolean edited) {
//TODO check if edited is a valid profile!
this.edited = edited; this.edited = edited;
} }
@ -71,7 +70,6 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface {
} }
public synchronized void storeSettings() { public synchronized void storeSettings() {
if (1==1) return;
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);
@ -183,7 +181,7 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface {
convertedProfile = createProfileStore(); convertedProfile = createProfileStore();
} }
public boolean isValidEditState() { public synchronized boolean isValidEditState() {
return createProfileStore().getDefaultProfile().isValid(MainApp.gs(R.string.localprofile)); return createProfileStore().getDefaultProfile().isValid(MainApp.gs(R.string.localprofile));
} }

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

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