From c8c830f51c79ddf7659c747c22fd904190dfeb84 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 15 Sep 2019 17:53:30 +0200 Subject: [PATCH 01/26] LocalProfileFragment -> kotlin --- .../profile/local/LocalProfileFragment.java | 212 ------------------ .../profile/local/LocalProfileFragment.kt | 169 ++++++++++++++ 2 files changed, 169 insertions(+), 212 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.java deleted file mode 100644 index f8969e9594..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.java +++ /dev/null @@ -1,212 +0,0 @@ -package info.nightscout.androidaps.plugins.profile.local; - - -import android.app.Activity; -import android.os.Bundle; -import android.text.Editable; -import android.text.TextWatcher; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Button; -import android.widget.RadioButton; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; - -import java.text.DecimalFormat; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.ProfileStore; -import info.nightscout.androidaps.events.EventInitializationChanged; -import info.nightscout.androidaps.interfaces.PumpDescription; -import info.nightscout.androidaps.plugins.bus.RxBus; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment; -import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog; -import info.nightscout.androidaps.plugins.general.careportal.OptionsToShow; -import info.nightscout.androidaps.utils.DecimalFormatter; -import info.nightscout.androidaps.utils.FabricPrivacy; -import info.nightscout.androidaps.utils.NumberPicker; -import info.nightscout.androidaps.utils.SafeParse; -import info.nightscout.androidaps.utils.TimeListEdit; -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.disposables.CompositeDisposable; - -import static info.nightscout.androidaps.plugins.insulin.InsulinOrefBasePlugin.MIN_DIA; - -public class LocalProfileFragment extends Fragment { - private CompositeDisposable disposable = new CompositeDisposable(); - - private NumberPicker diaView; - private RadioButton mgdlView; - private RadioButton mmolView; - private TimeListEdit basalView; - private Button profileswitchButton; - private Button resetButton; - private Button saveButton; - - private TextView invalidProfile; - - private Runnable save = () -> { - doEdit(); - if (basalView != null) { - basalView.updateLabel(MainApp.gs(R.string.nsprofileview_basal_label) + ": " + getSumLabel()); - } - }; - - private TextWatcher textWatch = new TextWatcher() { - - @Override - public void afterTextChanged(Editable s) { - } - - @Override - public void beforeTextChanged(CharSequence s, int start, - int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, - int before, int count) { - LocalProfilePlugin.getPlugin().dia = SafeParse.stringToDouble(diaView.getText().toString()); - doEdit(); - } - }; - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - PumpDescription pumpDescription = ConfigBuilderPlugin.getPlugin().getActivePump().getPumpDescription(); - View layout = inflater.inflate(R.layout.localprofile_fragment, container, false); - diaView = layout.findViewById(R.id.localprofile_dia); - diaView.setParams(LocalProfilePlugin.getPlugin().dia, MIN_DIA, 12d, 0.1d, new DecimalFormat("0.0"), false, layout.findViewById(R.id.localprofile_save), textWatch); - mgdlView = layout.findViewById(R.id.localprofile_mgdl); - mmolView = layout.findViewById(R.id.localprofile_mmol); - new TimeListEdit(getContext(), layout, R.id.localprofile_ic, MainApp.gs(R.string.nsprofileview_ic_label) + ":", LocalProfilePlugin.getPlugin().ic, null, 0.5, 50d, 0.1d, new DecimalFormat("0.0"), save); - new TimeListEdit(getContext(), layout, R.id.localprofile_isf, MainApp.gs(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.gs(R.string.nsprofileview_basal_label) + ": " + getSumLabel(), LocalProfilePlugin.getPlugin().basal, null, pumpDescription.basalMinimumRate, 10, 0.01d, new DecimalFormat("0.00"), save); - new TimeListEdit(getContext(), layout, R.id.localprofile_target, MainApp.gs(R.string.nsprofileview_target_label) + ":", LocalProfilePlugin.getPlugin().targetLow, LocalProfilePlugin.getPlugin().targetHigh, 3d, 200, 0.1d, new DecimalFormat("0.0"), save); - profileswitchButton = layout.findViewById(R.id.localprofile_profileswitch); - resetButton = layout.findViewById(R.id.localprofile_reset); - saveButton = layout.findViewById(R.id.localprofile_save); - - - invalidProfile = layout.findViewById(R.id.invalidprofile); - - if (!ConfigBuilderPlugin.getPlugin().getActivePump().getPumpDescription().isTempBasalCapable) { - layout.findViewById(R.id.localprofile_basal).setVisibility(View.GONE); - } - - mgdlView.setChecked(LocalProfilePlugin.getPlugin().mgdl); - mmolView.setChecked(LocalProfilePlugin.getPlugin().mmol); - - mgdlView.setOnClickListener(v -> { - LocalProfilePlugin.getPlugin().mgdl = mgdlView.isChecked(); - LocalProfilePlugin.getPlugin().mmol = !LocalProfilePlugin.getPlugin().mgdl; - mmolView.setChecked(LocalProfilePlugin.getPlugin().mmol); - doEdit(); - }); - mmolView.setOnClickListener(v -> { - LocalProfilePlugin.getPlugin().mmol = mmolView.isChecked(); - LocalProfilePlugin.getPlugin().mgdl = !LocalProfilePlugin.getPlugin().mmol; - mgdlView.setChecked(LocalProfilePlugin.getPlugin().mgdl); - doEdit(); - }); - - profileswitchButton.setOnClickListener(view -> { - NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); - final OptionsToShow profileswitch = CareportalFragment.PROFILESWITCHDIRECT; - profileswitch.executeProfileSwitch = true; - newDialog.setOptions(profileswitch, R.string.careportal_profileswitch); - newDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); - }); - - resetButton.setOnClickListener(view -> { - LocalProfilePlugin.getPlugin().loadSettings(); - mgdlView.setChecked(LocalProfilePlugin.getPlugin().mgdl); - mmolView.setChecked(LocalProfilePlugin.getPlugin().mmol); - diaView.setParams(LocalProfilePlugin.getPlugin().dia, MIN_DIA, 12d, 0.1d, new DecimalFormat("0.0"), false, view.findViewById(R.id.localprofile_save), textWatch); - new TimeListEdit(getContext(), layout, R.id.localprofile_ic, MainApp.gs(R.string.nsprofileview_ic_label) + ":", LocalProfilePlugin.getPlugin().ic, null, 0.5, 50d, 0.1d, new DecimalFormat("0.0"), save); - new TimeListEdit(getContext(), layout, R.id.localprofile_isf, MainApp.gs(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.gs(R.string.nsprofileview_basal_label) + ": " + getSumLabel(), LocalProfilePlugin.getPlugin().basal, null, pumpDescription.basalMinimumRate, 10, 0.01d, new DecimalFormat("0.00"), save); - new TimeListEdit(getContext(), layout, R.id.localprofile_target, MainApp.gs(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; - } - - @Override - public synchronized void onResume() { - super.onResume(); - disposable.add(RxBus.INSTANCE - .toObservable(EventInitializationChanged.class) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(event -> updateGUI(), FabricPrivacy::logException) - ); - updateGUI(); - } - - @Override - public synchronized void onPause() { - super.onPause(); - disposable.clear(); - } - - public void doEdit() { - LocalProfilePlugin.getPlugin().setEdited(true); - updateGUI(); - } - - @NonNull - public String getSumLabel() { - ProfileStore profile = LocalProfilePlugin.getPlugin().createProfileStore(); - if (profile != null) - return " ∑" + DecimalFormatter.to2Decimal(profile.getDefaultProfile().baseBasalSum()) + MainApp.gs(R.string.insulin_unit_shortname); - else - return MainApp.gs(R.string.localprofile); - } - - protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - boolean isValid = LocalProfilePlugin.getPlugin().isValidEditState(); - boolean isEdited = LocalProfilePlugin.getPlugin().isEdited(); - if (isValid) { - invalidProfile.setVisibility(View.GONE); //show invalid profile - - if (isEdited) { - //edited profile -> save first - profileswitchButton.setVisibility(View.GONE); - saveButton.setVisibility(View.VISIBLE); - } else { - profileswitchButton.setVisibility(View.VISIBLE); - saveButton.setVisibility(View.GONE); - } - } 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/profile/local/LocalProfileFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt new file mode 100644 index 0000000000..9824933616 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt @@ -0,0 +1,169 @@ +package info.nightscout.androidaps.plugins.profile.local + + +import android.os.Bundle +import android.text.Editable +import android.text.TextWatcher +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.events.EventInitializationChanged +import info.nightscout.androidaps.interfaces.PumpDescription +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment +import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog +import info.nightscout.androidaps.plugins.insulin.InsulinOrefBasePlugin.MIN_DIA +import info.nightscout.androidaps.utils.DecimalFormatter +import info.nightscout.androidaps.utils.FabricPrivacy +import info.nightscout.androidaps.utils.SafeParse +import info.nightscout.androidaps.utils.TimeListEdit +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.CompositeDisposable +import kotlinx.android.synthetic.main.localprofile_fragment.* +import java.text.DecimalFormat + +class LocalProfileFragment : Fragment() { + private var disposable: CompositeDisposable = CompositeDisposable() + + private var basalView: TimeListEdit? = null + + private val save = Runnable { + doEdit() + basalView?.updateLabel(MainApp.gs(R.string.nsprofileview_basal_label) + ": " + sumLabel()) + } + + private val textWatch = object : TextWatcher { + override fun afterTextChanged(s: Editable) {} + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { + LocalProfilePlugin.getPlugin().dia = SafeParse.stringToDouble(localprofile_dia.text.toString()) + doEdit() + } + } + + private fun sumLabel(): String { + val profile = LocalProfilePlugin.getPlugin().createProfileStore().defaultProfile + val sum = profile?.baseBasalSum() ?: 0.0 + return " ∑" + DecimalFormatter.to2Decimal(sum) + MainApp.gs(R.string.insulin_unit_shortname) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + return inflater.inflate(R.layout.localprofile_fragment, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + val pumpDescription = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription ?: return + + localprofile_dia.setParams(LocalProfilePlugin.getPlugin().dia, MIN_DIA, 12.0, 0.1, DecimalFormat("0.0"), false, localprofile_save, textWatch) + TimeListEdit(context, view, R.id.localprofile_ic, MainApp.gs(R.string.nsprofileview_ic_label) + ":", LocalProfilePlugin.getPlugin().ic, null, 0.5, 50.0, 0.1, DecimalFormat("0.0"), save) + TimeListEdit(context, view, R.id.localprofile_isf, MainApp.gs(R.string.nsprofileview_isf_label) + ":", LocalProfilePlugin.getPlugin().isf, null, 0.5, 500.0, 0.1, DecimalFormat("0.0"), save) + basalView = TimeListEdit(context, view, R.id.localprofile_basal, MainApp.gs(R.string.nsprofileview_basal_label) + ": " + sumLabel(), LocalProfilePlugin.getPlugin().basal, null, pumpDescription.basalMinimumRate, 10.0, 0.01, DecimalFormat("0.00"), save) + TimeListEdit(context, view, R.id.localprofile_target, MainApp.gs(R.string.nsprofileview_target_label) + ":", LocalProfilePlugin.getPlugin().targetLow, LocalProfilePlugin.getPlugin().targetHigh, 3.0, 200.0, 0.1, DecimalFormat("0.0"), save) + + + if (!pumpDescription.isTempBasalCapable) { + localprofile_basal.visibility = View.GONE + } + + localprofile_mgdl.isChecked = LocalProfilePlugin.getPlugin().mgdl + localprofile_mmol.isChecked = LocalProfilePlugin.getPlugin().mmol + + localprofile_mgdl.setOnClickListener { + LocalProfilePlugin.getPlugin().mgdl = localprofile_mgdl.isChecked + LocalProfilePlugin.getPlugin().mmol = !LocalProfilePlugin.getPlugin().mgdl + localprofile_mmol.isChecked = LocalProfilePlugin.getPlugin().mmol + doEdit() + } + localprofile_mmol.setOnClickListener { + LocalProfilePlugin.getPlugin().mmol = localprofile_mmol.isChecked + LocalProfilePlugin.getPlugin().mgdl = !LocalProfilePlugin.getPlugin().mmol + localprofile_mgdl.isChecked = LocalProfilePlugin.getPlugin().mgdl + doEdit() + } + + localprofile_profileswitch.setOnClickListener { + val newDialog = NewNSTreatmentDialog() + val profileSwitch = CareportalFragment.PROFILESWITCHDIRECT + profileSwitch.executeProfileSwitch = true + newDialog.setOptions(profileSwitch, R.string.careportal_profileswitch) + fragmentManager?.let { newDialog.show(it, "NewNSTreatmentDialog") } + } + + localprofile_reset.setOnClickListener { + LocalProfilePlugin.getPlugin().loadSettings() + localprofile_mgdl.isChecked = LocalProfilePlugin.getPlugin().mgdl + localprofile_mmol.isChecked = LocalProfilePlugin.getPlugin().mmol + localprofile_dia.setParams(LocalProfilePlugin.getPlugin().dia, MIN_DIA, 12.0, 0.1, DecimalFormat("0.0"), false, localprofile_save, textWatch) + TimeListEdit(context, view, R.id.localprofile_ic, MainApp.gs(R.string.nsprofileview_ic_label) + ":", LocalProfilePlugin.getPlugin().ic, null, 0.5, 50.0, 0.1, DecimalFormat("0.0"), save) + TimeListEdit(context, view, R.id.localprofile_isf, MainApp.gs(R.string.nsprofileview_isf_label) + ":", LocalProfilePlugin.getPlugin().isf, null, 0.5, 500.0, 0.1, DecimalFormat("0.0"), save) + basalView = TimeListEdit(context, view, R.id.localprofile_basal, MainApp.gs(R.string.nsprofileview_basal_label) + ": " + sumLabel(), LocalProfilePlugin.getPlugin().basal, null, pumpDescription.basalMinimumRate, 10.0, 0.01, DecimalFormat("0.00"), save) + TimeListEdit(context, view, R.id.localprofile_target, MainApp.gs(R.string.nsprofileview_target_label) + ":", LocalProfilePlugin.getPlugin().targetLow, LocalProfilePlugin.getPlugin().targetHigh, 3.0, 200.0, 0.1, DecimalFormat("0.0"), save) + updateGUI() + } + + localprofile_save.setOnClickListener { + if (!LocalProfilePlugin.getPlugin().isValidEditState) { + return@setOnClickListener //Should not happen as saveButton should not be visible if not valid + } + LocalProfilePlugin.getPlugin().storeSettings() + updateGUI() + } + updateGUI() + } + + @Synchronized + override fun onResume() { + super.onResume() + disposable.add(RxBus + .toObservable(EventInitializationChanged::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGUI() }, { FabricPrivacy.logException(it) }) + ) + } + + @Synchronized + override fun onPause() { + super.onPause() + disposable.clear() + } + + fun doEdit() { + LocalProfilePlugin.getPlugin().isEdited = true + updateGUI() + } + + fun updateGUI() { + if (invalidprofile == null) return + val isValid = LocalProfilePlugin.getPlugin().isValidEditState + val isEdited = LocalProfilePlugin.getPlugin().isEdited + if (isValid) { + invalidprofile.visibility = View.GONE //show invalid profile + + if (isEdited) { + //edited profile -> save first + localprofile_profileswitch.visibility = View.GONE + localprofile_save.visibility = View.VISIBLE + } else { + localprofile_profileswitch.visibility = View.VISIBLE + localprofile_save.visibility = View.GONE + } + } else { + invalidprofile.visibility = View.VISIBLE + localprofile_profileswitch.visibility = View.GONE + localprofile_save.visibility = View.GONE //don't save an invalid profile + } + + //Show reset button iff data was edited + if (isEdited) { + localprofile_reset.visibility = View.VISIBLE + } else { + localprofile_reset.visibility = View.GONE + } + } +} From cf506a5439d876dbdeb4fdfdbdc18373c42bf552 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 15 Sep 2019 18:19:36 +0200 Subject: [PATCH 02/26] LocalProfilePlugin -> kotlin --- .../info/nightscout/androidaps/MainApp.java | 2 +- .../profile/local/LocalProfileFragment.kt | 59 +++-- .../profile/local/LocalProfilePlugin.java | 231 ------------------ .../profile/local/LocalProfilePlugin.kt | 217 ++++++++++++++++ .../androidaps/setupwizard/SWDefinition.java | 4 +- 5 files changed, 249 insertions(+), 264 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index a1300f59bc..89f08b908e 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -189,7 +189,7 @@ public class MainApp extends Application { if (Config.APS) pluginsList.add(OpenAPSSMBPlugin.getPlugin()); pluginsList.add(NSProfilePlugin.getPlugin()); if (Config.OTHERPROFILES) pluginsList.add(SimpleProfilePlugin.getPlugin()); - if (Config.OTHERPROFILES) pluginsList.add(LocalProfilePlugin.getPlugin()); + if (Config.OTHERPROFILES) pluginsList.add(LocalProfilePlugin.INSTANCE); pluginsList.add(TreatmentsPlugin.getPlugin()); if (Config.SAFETY) pluginsList.add(SafetyPlugin.getPlugin()); if (Config.SAFETY) pluginsList.add(VersionCheckerPlugin.INSTANCE); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt index 9824933616..54cd41ee45 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt @@ -11,7 +11,6 @@ import androidx.fragment.app.Fragment import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R import info.nightscout.androidaps.events.EventInitializationChanged -import info.nightscout.androidaps.interfaces.PumpDescription import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment @@ -40,13 +39,13 @@ class LocalProfileFragment : Fragment() { override fun afterTextChanged(s: Editable) {} override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { - LocalProfilePlugin.getPlugin().dia = SafeParse.stringToDouble(localprofile_dia.text.toString()) + LocalProfilePlugin.dia = SafeParse.stringToDouble(localprofile_dia.text.toString()) doEdit() } } private fun sumLabel(): String { - val profile = LocalProfilePlugin.getPlugin().createProfileStore().defaultProfile + val profile = LocalProfilePlugin.createProfileStore().defaultProfile val sum = profile?.baseBasalSum() ?: 0.0 return " ∑" + DecimalFormatter.to2Decimal(sum) + MainApp.gs(R.string.insulin_unit_shortname) } @@ -60,30 +59,30 @@ class LocalProfileFragment : Fragment() { super.onViewCreated(view, savedInstanceState) val pumpDescription = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription ?: return - localprofile_dia.setParams(LocalProfilePlugin.getPlugin().dia, MIN_DIA, 12.0, 0.1, DecimalFormat("0.0"), false, localprofile_save, textWatch) - TimeListEdit(context, view, R.id.localprofile_ic, MainApp.gs(R.string.nsprofileview_ic_label) + ":", LocalProfilePlugin.getPlugin().ic, null, 0.5, 50.0, 0.1, DecimalFormat("0.0"), save) - TimeListEdit(context, view, R.id.localprofile_isf, MainApp.gs(R.string.nsprofileview_isf_label) + ":", LocalProfilePlugin.getPlugin().isf, null, 0.5, 500.0, 0.1, DecimalFormat("0.0"), save) - basalView = TimeListEdit(context, view, R.id.localprofile_basal, MainApp.gs(R.string.nsprofileview_basal_label) + ": " + sumLabel(), LocalProfilePlugin.getPlugin().basal, null, pumpDescription.basalMinimumRate, 10.0, 0.01, DecimalFormat("0.00"), save) - TimeListEdit(context, view, R.id.localprofile_target, MainApp.gs(R.string.nsprofileview_target_label) + ":", LocalProfilePlugin.getPlugin().targetLow, LocalProfilePlugin.getPlugin().targetHigh, 3.0, 200.0, 0.1, DecimalFormat("0.0"), save) + localprofile_dia.setParams(LocalProfilePlugin.dia, MIN_DIA, 12.0, 0.1, DecimalFormat("0.0"), false, localprofile_save, textWatch) + TimeListEdit(context, view, R.id.localprofile_ic, MainApp.gs(R.string.nsprofileview_ic_label) + ":", LocalProfilePlugin.ic, null, 0.5, 50.0, 0.1, DecimalFormat("0.0"), save) + TimeListEdit(context, view, R.id.localprofile_isf, MainApp.gs(R.string.nsprofileview_isf_label) + ":", LocalProfilePlugin.isf, null, 0.5, 500.0, 0.1, DecimalFormat("0.0"), save) + basalView = TimeListEdit(context, view, R.id.localprofile_basal, MainApp.gs(R.string.nsprofileview_basal_label) + ": " + sumLabel(), LocalProfilePlugin.basal, null, pumpDescription.basalMinimumRate, 10.0, 0.01, DecimalFormat("0.00"), save) + TimeListEdit(context, view, R.id.localprofile_target, MainApp.gs(R.string.nsprofileview_target_label) + ":", LocalProfilePlugin.targetLow, LocalProfilePlugin.targetHigh, 3.0, 200.0, 0.1, DecimalFormat("0.0"), save) if (!pumpDescription.isTempBasalCapable) { localprofile_basal.visibility = View.GONE } - localprofile_mgdl.isChecked = LocalProfilePlugin.getPlugin().mgdl - localprofile_mmol.isChecked = LocalProfilePlugin.getPlugin().mmol + localprofile_mgdl.isChecked = LocalProfilePlugin.mgdl + localprofile_mmol.isChecked = LocalProfilePlugin.mmol localprofile_mgdl.setOnClickListener { - LocalProfilePlugin.getPlugin().mgdl = localprofile_mgdl.isChecked - LocalProfilePlugin.getPlugin().mmol = !LocalProfilePlugin.getPlugin().mgdl - localprofile_mmol.isChecked = LocalProfilePlugin.getPlugin().mmol + LocalProfilePlugin.mgdl = localprofile_mgdl.isChecked + LocalProfilePlugin.mmol = !LocalProfilePlugin.mgdl + localprofile_mmol.isChecked = LocalProfilePlugin.mmol doEdit() } localprofile_mmol.setOnClickListener { - LocalProfilePlugin.getPlugin().mmol = localprofile_mmol.isChecked - LocalProfilePlugin.getPlugin().mgdl = !LocalProfilePlugin.getPlugin().mmol - localprofile_mgdl.isChecked = LocalProfilePlugin.getPlugin().mgdl + LocalProfilePlugin.mmol = localprofile_mmol.isChecked + LocalProfilePlugin.mgdl = !LocalProfilePlugin.mmol + localprofile_mgdl.isChecked = LocalProfilePlugin.mgdl doEdit() } @@ -96,22 +95,22 @@ class LocalProfileFragment : Fragment() { } localprofile_reset.setOnClickListener { - LocalProfilePlugin.getPlugin().loadSettings() - localprofile_mgdl.isChecked = LocalProfilePlugin.getPlugin().mgdl - localprofile_mmol.isChecked = LocalProfilePlugin.getPlugin().mmol - localprofile_dia.setParams(LocalProfilePlugin.getPlugin().dia, MIN_DIA, 12.0, 0.1, DecimalFormat("0.0"), false, localprofile_save, textWatch) - TimeListEdit(context, view, R.id.localprofile_ic, MainApp.gs(R.string.nsprofileview_ic_label) + ":", LocalProfilePlugin.getPlugin().ic, null, 0.5, 50.0, 0.1, DecimalFormat("0.0"), save) - TimeListEdit(context, view, R.id.localprofile_isf, MainApp.gs(R.string.nsprofileview_isf_label) + ":", LocalProfilePlugin.getPlugin().isf, null, 0.5, 500.0, 0.1, DecimalFormat("0.0"), save) - basalView = TimeListEdit(context, view, R.id.localprofile_basal, MainApp.gs(R.string.nsprofileview_basal_label) + ": " + sumLabel(), LocalProfilePlugin.getPlugin().basal, null, pumpDescription.basalMinimumRate, 10.0, 0.01, DecimalFormat("0.00"), save) - TimeListEdit(context, view, R.id.localprofile_target, MainApp.gs(R.string.nsprofileview_target_label) + ":", LocalProfilePlugin.getPlugin().targetLow, LocalProfilePlugin.getPlugin().targetHigh, 3.0, 200.0, 0.1, DecimalFormat("0.0"), save) + LocalProfilePlugin.loadSettings() + localprofile_mgdl.isChecked = LocalProfilePlugin.mgdl + localprofile_mmol.isChecked = LocalProfilePlugin.mmol + localprofile_dia.setParams(LocalProfilePlugin.dia, MIN_DIA, 12.0, 0.1, DecimalFormat("0.0"), false, localprofile_save, textWatch) + TimeListEdit(context, view, R.id.localprofile_ic, MainApp.gs(R.string.nsprofileview_ic_label) + ":", LocalProfilePlugin.ic, null, 0.5, 50.0, 0.1, DecimalFormat("0.0"), save) + TimeListEdit(context, view, R.id.localprofile_isf, MainApp.gs(R.string.nsprofileview_isf_label) + ":", LocalProfilePlugin.isf, null, 0.5, 500.0, 0.1, DecimalFormat("0.0"), save) + basalView = TimeListEdit(context, view, R.id.localprofile_basal, MainApp.gs(R.string.nsprofileview_basal_label) + ": " + sumLabel(), LocalProfilePlugin.basal, null, pumpDescription.basalMinimumRate, 10.0, 0.01, DecimalFormat("0.00"), save) + TimeListEdit(context, view, R.id.localprofile_target, MainApp.gs(R.string.nsprofileview_target_label) + ":", LocalProfilePlugin.targetLow, LocalProfilePlugin.targetHigh, 3.0, 200.0, 0.1, DecimalFormat("0.0"), save) updateGUI() } localprofile_save.setOnClickListener { - if (!LocalProfilePlugin.getPlugin().isValidEditState) { + if (!LocalProfilePlugin.isValidEditState()) { return@setOnClickListener //Should not happen as saveButton should not be visible if not valid } - LocalProfilePlugin.getPlugin().storeSettings() + LocalProfilePlugin.storeSettings() updateGUI() } updateGUI() @@ -134,14 +133,14 @@ class LocalProfileFragment : Fragment() { } fun doEdit() { - LocalProfilePlugin.getPlugin().isEdited = true + LocalProfilePlugin.isEdited = true updateGUI() } fun updateGUI() { if (invalidprofile == null) return - val isValid = LocalProfilePlugin.getPlugin().isValidEditState - val isEdited = LocalProfilePlugin.getPlugin().isEdited + val isValid = LocalProfilePlugin.isValidEditState() + val isEdited = LocalProfilePlugin.isEdited if (isValid) { invalidprofile.visibility = View.GONE //show invalid profile @@ -159,7 +158,7 @@ class LocalProfileFragment : Fragment() { localprofile_save.visibility = View.GONE //don't save an invalid profile } - //Show reset button iff data was edited + //Show reset button if data was edited if (isEdited) { localprofile_reset.visibility = View.VISIBLE } else { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.java deleted file mode 100644 index e3e068c1cb..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.java +++ /dev/null @@ -1,231 +0,0 @@ -package info.nightscout.androidaps.plugins.profile.local; - -import androidx.annotation.NonNull; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.ProfileStore; -import info.nightscout.androidaps.events.EventProfileStoreChanged; -import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.PluginDescription; -import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.interfaces.ProfileInterface; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.plugins.bus.RxBus; -import info.nightscout.androidaps.utils.DecimalFormatter; -import info.nightscout.androidaps.utils.SP; - -/** - * Created by mike on 05.08.2016. - */ -public class LocalProfilePlugin extends PluginBase implements ProfileInterface { - public static final String LOCAL_PROFILE = "LocalProfile"; - private static Logger log = LoggerFactory.getLogger(L.PROFILE); - - private static LocalProfilePlugin localProfilePlugin; - - public static LocalProfilePlugin getPlugin() { - if (localProfilePlugin == null) - localProfilePlugin = new LocalProfilePlugin(); - return localProfilePlugin; - } - - private ProfileStore convertedProfile = null; - - 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 mmol; - Double dia; - JSONArray ic; - JSONArray isf; - JSONArray basal; - JSONArray targetLow; - JSONArray targetHigh; - - public LocalProfilePlugin() { - super(new PluginDescription() - .mainType(PluginType.PROFILE) - .fragmentClass(LocalProfileFragment.class.getName()) - .pluginName(R.string.localprofile) - .shortName(R.string.localprofile_shortname) - .description(R.string.description_profile_local) - ); - loadSettings(); - } - - public synchronized void storeSettings() { - SP.putBoolean(LOCAL_PROFILE + "mmol", mmol); - SP.putBoolean(LOCAL_PROFILE + "mgdl", mgdl); - SP.putString(LOCAL_PROFILE + "dia", dia.toString()); - SP.putString(LOCAL_PROFILE + "ic", ic.toString()); - SP.putString(LOCAL_PROFILE + "isf", isf.toString()); - SP.putString(LOCAL_PROFILE + "basal", basal.toString()); - SP.putString(LOCAL_PROFILE + "targetlow", targetLow.toString()); - SP.putString(LOCAL_PROFILE + "targethigh", targetHigh.toString()); - - createAndStoreConvertedProfile(); - edited = false; - if (L.isEnabled(L.PROFILE)) - log.debug("Storing settings: " + getRawProfile().getData().toString()); - RxBus.INSTANCE.send(new EventProfileStoreChanged()); - } - - public synchronized void loadSettings() { - if (L.isEnabled(L.PROFILE)) - log.debug("Loading stored settings"); - - mgdl = SP.getBoolean(LOCAL_PROFILE + "mgdl", false); - mmol = SP.getBoolean(LOCAL_PROFILE + "mmol", true); - dia = SP.getDouble(LOCAL_PROFILE + "dia", Constants.defaultDIA); - try { - ic = new JSONArray(SP.getString(LOCAL_PROFILE + "ic", DEFAULTARRAY)); - } catch (JSONException e1) { - try { - ic = new JSONArray(DEFAULTARRAY); - } catch (JSONException ignored) { - } - } - try { - isf = new JSONArray(SP.getString(LOCAL_PROFILE + "isf", DEFAULTARRAY)); - } catch (JSONException e1) { - try { - isf = new JSONArray(DEFAULTARRAY); - } catch (JSONException ignored) { - } - } - try { - basal = new JSONArray(SP.getString(LOCAL_PROFILE + "basal", DEFAULTARRAY)); - } catch (JSONException e1) { - try { - basal = new JSONArray(DEFAULTARRAY); - } catch (JSONException ignored) { - } - } - try { - targetLow = new JSONArray(SP.getString(LOCAL_PROFILE + "targetlow", DEFAULTARRAY)); - } catch (JSONException e1) { - try { - targetLow = new JSONArray(DEFAULTARRAY); - } catch (JSONException ignored) { - } - } - try { - targetHigh = new JSONArray(SP.getString(LOCAL_PROFILE + "targethigh", DEFAULTARRAY)); - } catch (JSONException e1) { - try { - targetHigh = new JSONArray(DEFAULTARRAY); - } catch (JSONException ignored) { - } - } - edited = false; - createAndStoreConvertedProfile(); - } - - /* - { - "_id": "576264a12771b7500d7ad184", - "startDate": "2016-06-16T08:35:00.000Z", - "defaultProfile": "Default", - "store": { - "Default": { - "dia": "3", - "carbratio": [{ - "time": "00:00", - "value": "30" - }], - "carbs_hr": "20", - "delay": "20", - "sens": [{ - "time": "00:00", - "value": "100" - }], - "timezone": "UTC", - "basal": [{ - "time": "00:00", - "value": "0.1" - }], - "target_low": [{ - "time": "00:00", - "value": "0" - }], - "target_high": [{ - "time": "00:00", - "value": "0" - }], - "startDate": "1970-01-01T00:00:00.000Z", - "units": "mmol" - } - }, - "created_at": "2016-06-16T08:34:41.256Z" - } - */ - private void createAndStoreConvertedProfile() { - convertedProfile = createProfileStore(); - } - - public synchronized boolean isValidEditState() { - return createProfileStore().getDefaultProfile().isValid(MainApp.gs(R.string.localprofile), false); - } - - @NonNull - public ProfileStore createProfileStore() { - JSONObject json = new JSONObject(); - JSONObject store = new JSONObject(); - JSONObject profile = new JSONObject(); - - try { - json.put("defaultProfile", LOCAL_PROFILE); - json.put("store", store); - profile.put("dia", dia); - profile.put("carbratio", ic); - profile.put("sens", isf); - profile.put("basal", basal); - profile.put("target_low", targetLow); - profile.put("target_high", targetHigh); - profile.put("units", mgdl ? Constants.MGDL : Constants.MMOL); - store.put(LOCAL_PROFILE, profile); - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - return new ProfileStore(json); - } - - @Override - public ProfileStore getProfile() { - if (!convertedProfile.getDefaultProfile().isValid(MainApp.gs(R.string.localprofile))) - return null; - return convertedProfile; - } - - public ProfileStore getRawProfile() { - return convertedProfile; - } - - @Override - public String getUnits() { - return mgdl ? Constants.MGDL : Constants.MMOL; - } - - @Override - public String getProfileName() { - return DecimalFormatter.to2Decimal(convertedProfile.getDefaultProfile().percentageBasalSum()) + "U "; - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt new file mode 100644 index 0000000000..e9ead3e4d0 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt @@ -0,0 +1,217 @@ +package info.nightscout.androidaps.plugins.profile.local + +import info.nightscout.androidaps.Constants +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.data.ProfileStore +import info.nightscout.androidaps.events.EventProfileStoreChanged +import info.nightscout.androidaps.interfaces.PluginBase +import info.nightscout.androidaps.interfaces.PluginDescription +import info.nightscout.androidaps.interfaces.PluginType +import info.nightscout.androidaps.interfaces.ProfileInterface +import info.nightscout.androidaps.logging.L +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.utils.DecimalFormatter +import info.nightscout.androidaps.utils.SP +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject +import org.slf4j.LoggerFactory + +/** + * Created by mike on 05.08.2016. + */ +object LocalProfilePlugin : PluginBase(PluginDescription() + .mainType(PluginType.PROFILE) + .fragmentClass(LocalProfileFragment::class.java.name) + .pluginName(R.string.localprofile) + .shortName(R.string.localprofile_shortname) + .description(R.string.description_profile_local)), ProfileInterface { + + private val log = LoggerFactory.getLogger(L.PROFILE) + + private var rawProfile: ProfileStore? = null + + const val LOCAL_PROFILE = "LocalProfile" + + private const val DEFAULTARRAY = "[{\"time\":\"00:00\",\"timeAsSeconds\":0,\"value\":0}]" + + var isEdited: Boolean = false + internal var mgdl: Boolean = false + internal var mmol: Boolean = false + internal var dia: Double? = null + internal var ic: JSONArray? = null + internal var isf: JSONArray? = null + internal var basal: JSONArray? = null + internal var targetLow: JSONArray? = null + internal var targetHigh: JSONArray? = null + + @Synchronized + fun isValidEditState(): Boolean { + return createProfileStore().defaultProfile?.isValid(MainApp.gs(R.string.localprofile), false) + ?: false + } + + init { + loadSettings() + } + + @Synchronized + fun storeSettings() { + SP.putBoolean(LOCAL_PROFILE + "mmol", mmol) + SP.putBoolean(LOCAL_PROFILE + "mgdl", mgdl) + SP.putString(LOCAL_PROFILE + "dia", dia.toString()) + SP.putString(LOCAL_PROFILE + "ic", ic.toString()) + SP.putString(LOCAL_PROFILE + "isf", isf.toString()) + SP.putString(LOCAL_PROFILE + "basal", basal.toString()) + SP.putString(LOCAL_PROFILE + "targetlow", targetLow.toString()) + SP.putString(LOCAL_PROFILE + "targethigh", targetHigh.toString()) + + createAndStoreConvertedProfile() + isEdited = false + if (L.isEnabled(L.PROFILE)) + log.debug("Storing settings: " + rawProfile?.data.toString()) + RxBus.send(EventProfileStoreChanged()) + } + + @Synchronized + fun loadSettings() { + if (L.isEnabled(L.PROFILE)) + log.debug("Loading stored settings") + + mgdl = SP.getBoolean(LOCAL_PROFILE + "mgdl", false) + mmol = SP.getBoolean(LOCAL_PROFILE + "mmol", true) + dia = SP.getDouble(LOCAL_PROFILE + "dia", Constants.defaultDIA) + try { + ic = JSONArray(SP.getString(LOCAL_PROFILE + "ic", DEFAULTARRAY)) + } catch (e1: JSONException) { + try { + ic = JSONArray(DEFAULTARRAY) + } catch (ignored: JSONException) { + } + + } + + try { + isf = JSONArray(SP.getString(LOCAL_PROFILE + "isf", DEFAULTARRAY)) + } catch (e1: JSONException) { + try { + isf = JSONArray(DEFAULTARRAY) + } catch (ignored: JSONException) { + } + + } + + try { + basal = JSONArray(SP.getString(LOCAL_PROFILE + "basal", DEFAULTARRAY)) + } catch (e1: JSONException) { + try { + basal = JSONArray(DEFAULTARRAY) + } catch (ignored: JSONException) { + } + + } + + try { + targetLow = JSONArray(SP.getString(LOCAL_PROFILE + "targetlow", DEFAULTARRAY)) + } catch (e1: JSONException) { + try { + targetLow = JSONArray(DEFAULTARRAY) + } catch (ignored: JSONException) { + } + + } + + try { + targetHigh = JSONArray(SP.getString(LOCAL_PROFILE + "targethigh", DEFAULTARRAY)) + } catch (e1: JSONException) { + try { + targetHigh = JSONArray(DEFAULTARRAY) + } catch (ignored: JSONException) { + } + + } + + isEdited = false + createAndStoreConvertedProfile() + } + + /* + { + "_id": "576264a12771b7500d7ad184", + "startDate": "2016-06-16T08:35:00.000Z", + "defaultProfile": "Default", + "store": { + "Default": { + "dia": "3", + "carbratio": [{ + "time": "00:00", + "value": "30" + }], + "carbs_hr": "20", + "delay": "20", + "sens": [{ + "time": "00:00", + "value": "100" + }], + "timezone": "UTC", + "basal": [{ + "time": "00:00", + "value": "0.1" + }], + "target_low": [{ + "time": "00:00", + "value": "0" + }], + "target_high": [{ + "time": "00:00", + "value": "0" + }], + "startDate": "1970-01-01T00:00:00.000Z", + "units": "mmol" + } + }, + "created_at": "2016-06-16T08:34:41.256Z" + } + */ + private fun createAndStoreConvertedProfile() { + rawProfile = createProfileStore() + } + + fun createProfileStore(): ProfileStore { + val json = JSONObject() + val store = JSONObject() + val profile = JSONObject() + + try { + json.put("defaultProfile", LOCAL_PROFILE) + json.put("store", store) + profile.put("dia", dia) + profile.put("carbratio", ic) + profile.put("sens", isf) + profile.put("basal", basal) + profile.put("target_low", targetLow) + profile.put("target_high", targetHigh) + profile.put("units", if (mgdl) Constants.MGDL else Constants.MMOL) + store.put(LOCAL_PROFILE, profile) + } catch (e: JSONException) { + log.error("Unhandled exception", e) + } + + return ProfileStore(json) + } + + override fun getProfile(): ProfileStore? { + return if (rawProfile?.defaultProfile?.isValid(MainApp.gs(R.string.localprofile)) != true) null else rawProfile + } + + override fun getUnits(): String { + return if (mgdl) Constants.MGDL else Constants.MMOL + } + + override fun getProfileName(): String { + return DecimalFormatter.to2Decimal(rawProfile?.defaultProfile?.percentageBasalSum() + ?: 0.0) + "U " + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java index a8b09e7428..5ebfcefcbf 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java @@ -271,8 +271,8 @@ public class SWDefinition { .skippable(false) .add(new SWFragment(this) .add(new LocalProfileFragment())) - .validator(() -> LocalProfilePlugin.getPlugin().getProfile() != null && LocalProfilePlugin.getPlugin().getProfile().getDefaultProfile() != null && LocalProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid("StartupWizard")) - .visibility(() -> LocalProfilePlugin.getPlugin().isEnabled(PluginType.PROFILE)); + .validator(() -> LocalProfilePlugin.INSTANCE.getProfile() != null && LocalProfilePlugin.INSTANCE.getProfile().getDefaultProfile() != null && LocalProfilePlugin.INSTANCE.getProfile().getDefaultProfile().isValid("StartupWizard")) + .visibility(() -> LocalProfilePlugin.INSTANCE.isEnabled(PluginType.PROFILE)); private SWScreen screenSimpleProfile = new SWScreen(R.string.simpleprofile) .skippable(false) From 60a113bda630a4536cebd2f88b217c559e842ec7 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 16 Sep 2019 09:20:23 +0200 Subject: [PATCH 03/26] fragment redesign --- .../main/res/layout/localprofile_fragment.xml | 148 +++++++++++++----- app/src/main/res/values/strings.xml | 2 + 2 files changed, 107 insertions(+), 43 deletions(-) diff --git a/app/src/main/res/layout/localprofile_fragment.xml b/app/src/main/res/layout/localprofile_fragment.xml index 1edeeb0b31..49fd413115 100644 --- a/app/src/main/res/layout/localprofile_fragment.xml +++ b/app/src/main/res/layout/localprofile_fragment.xml @@ -19,20 +19,99 @@ android:textColor="@android:color/holo_red_light" android:textStyle="bold" /> - + android:layout_height="match_parent" + android:gravity="start" + android:orientation="horizontal"> + + + + + + + + + + + + + + + + + + + + + + - - - + android:paddingTop="5dp"> - - + android:layout_gravity="center_vertical" + android:layout_marginStart="10dp" + android:text="@string/dia" + android:textAppearance="?android:attr/textAppearanceMedium" /> + + + android:layout_marginEnd="10dp" + android:layout_marginBottom="10dp" /> - - + android:text="@string/hours" + android:textAppearance="?android:attr/textAppearanceSmall"/> - - @@ -136,7 +198,7 @@ android:id="@+id/localprofile_profileswitch" style="?android:attr/buttonStyle" android:layout_width="wrap_content" - android:layout_height="wrap_content" + android:layout_height="0dp" android:layout_gravity="center_horizontal" android:layout_marginBottom="3dp" android:layout_marginLeft="10dp" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3139b71a38..f12ac4b06b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1604,5 +1604,7 @@ %1$.1fg IC: %2$.1f Bolus wizard min + Profile name: + Selected: From da447b8afee5e4fb76dc89a4a653846a0151b458 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 16 Sep 2019 15:49:45 +0200 Subject: [PATCH 04/26] LocalProfile allow more profiles --- .../activities/PreferencesActivity.java | 2 +- .../configBuilder/ProfileFunctions.java | 4 + .../profile/local/LocalProfileFragment.kt | 105 +++++--- .../profile/local/LocalProfilePlugin.kt | 246 ++++++++++++++---- .../main/res/layout/localprofile_fragment.xml | 15 +- app/src/main/res/values/arrays.xml | 9 + app/src/main/res/values/strings.xml | 3 + .../{pref_language.xml => pref_general.xml} | 10 + 8 files changed, 298 insertions(+), 96 deletions(-) rename app/src/main/res/xml/{pref_language.xml => pref_general.xml} (65%) diff --git a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java index 941366f8f0..91093f5bf8 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java @@ -145,8 +145,8 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre if (!Config.NSCLIENT) { addPreferencesFromResource(R.xml.pref_password); } + addPreferencesFromResource(R.xml.pref_general); addPreferencesFromResource(R.xml.pref_age); - addPreferencesFromResource(R.xml.pref_language); addPreferencesFromResource(R.xml.pref_overview); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java index 025d8dac1e..93767bc662 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java @@ -116,6 +116,10 @@ public class ProfileFunctions { return getProfile(System.currentTimeMillis()); } + public static String getSystemUnits() { + return SP.getString(R.string.key_units, Constants.MGDL); + } + public String getProfileUnits() { Profile profile = getProfile(); return profile != null ? profile.getUnits() : Constants.MGDL; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt index 54cd41ee45..a47726c033 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt @@ -7,19 +7,21 @@ import android.text.TextWatcher import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.AdapterView +import android.widget.ArrayAdapter import androidx.fragment.app.Fragment +import info.nightscout.androidaps.Constants import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R +import info.nightscout.androidaps.data.Profile import info.nightscout.androidaps.events.EventInitializationChanged import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog import info.nightscout.androidaps.plugins.insulin.InsulinOrefBasePlugin.MIN_DIA -import info.nightscout.androidaps.utils.DecimalFormatter -import info.nightscout.androidaps.utils.FabricPrivacy -import info.nightscout.androidaps.utils.SafeParse -import info.nightscout.androidaps.utils.TimeListEdit +import info.nightscout.androidaps.utils.* import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import kotlinx.android.synthetic.main.localprofile_fragment.* @@ -29,6 +31,7 @@ class LocalProfileFragment : Fragment() { private var disposable: CompositeDisposable = CompositeDisposable() private var basalView: TimeListEdit? = null + private var spinner: SpinnerHelper? = null private val save = Runnable { doEdit() @@ -39,7 +42,7 @@ class LocalProfileFragment : Fragment() { override fun afterTextChanged(s: Editable) {} override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { - LocalProfilePlugin.dia = SafeParse.stringToDouble(localprofile_dia.text.toString()) + LocalProfilePlugin.currentProfile().dia = SafeParse.stringToDouble(localprofile_dia.text.toString()) doEdit() } } @@ -57,35 +60,71 @@ class LocalProfileFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + build() + } + + fun build() { val pumpDescription = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription ?: return + val units = ProfileFunctions.getSystemUnits() - localprofile_dia.setParams(LocalProfilePlugin.dia, MIN_DIA, 12.0, 0.1, DecimalFormat("0.0"), false, localprofile_save, textWatch) - TimeListEdit(context, view, R.id.localprofile_ic, MainApp.gs(R.string.nsprofileview_ic_label) + ":", LocalProfilePlugin.ic, null, 0.5, 50.0, 0.1, DecimalFormat("0.0"), save) - TimeListEdit(context, view, R.id.localprofile_isf, MainApp.gs(R.string.nsprofileview_isf_label) + ":", LocalProfilePlugin.isf, null, 0.5, 500.0, 0.1, DecimalFormat("0.0"), save) - basalView = TimeListEdit(context, view, R.id.localprofile_basal, MainApp.gs(R.string.nsprofileview_basal_label) + ": " + sumLabel(), LocalProfilePlugin.basal, null, pumpDescription.basalMinimumRate, 10.0, 0.01, DecimalFormat("0.00"), save) - TimeListEdit(context, view, R.id.localprofile_target, MainApp.gs(R.string.nsprofileview_target_label) + ":", LocalProfilePlugin.targetLow, LocalProfilePlugin.targetHigh, 3.0, 200.0, 0.1, DecimalFormat("0.0"), save) - - - if (!pumpDescription.isTempBasalCapable) { - localprofile_basal.visibility = View.GONE + localprofile_name.setText(LocalProfilePlugin.currentProfile().name) + localprofile_dia.setParams(LocalProfilePlugin.currentProfile().dia, HardLimits.MINDIA, HardLimits.MAXDIA, 0.1, DecimalFormat("0.0"), false, localprofile_save, textWatch) + TimeListEdit(context, view, R.id.localprofile_ic, MainApp.gs(R.string.nsprofileview_ic_label) + ":", LocalProfilePlugin.currentProfile().ic, null, HardLimits.MINIC, HardLimits.MAXIC, 0.1, DecimalFormat("0.0"), save) + basalView = TimeListEdit(context, view, R.id.localprofile_basal, MainApp.gs(R.string.nsprofileview_basal_label) + ": " + sumLabel(), LocalProfilePlugin.currentProfile().basal, null, pumpDescription.basalMinimumRate, 10.0, 0.01, DecimalFormat("0.00"), save) + if (units == Constants.MGDL) { + TimeListEdit(context, view, R.id.localprofile_isf, MainApp.gs(R.string.nsprofileview_isf_label) + ":", LocalProfilePlugin.currentProfile().isf, null, HardLimits.MINISF, HardLimits.MAXISF, 1.0, DecimalFormat("0"), save) + TimeListEdit(context, view, R.id.localprofile_target, MainApp.gs(R.string.nsprofileview_target_label) + ":", LocalProfilePlugin.currentProfile().targetLow, LocalProfilePlugin.currentProfile().targetHigh, HardLimits.VERY_HARD_LIMIT_TARGET_BG[0].toDouble(), HardLimits.VERY_HARD_LIMIT_TARGET_BG[1].toDouble(), 1.0, DecimalFormat("0"), save) + } else { + TimeListEdit(context, view, R.id.localprofile_isf, MainApp.gs(R.string.nsprofileview_isf_label) + ":", LocalProfilePlugin.currentProfile().isf, null, Profile.fromMgdlToUnits(HardLimits.MINISF, Constants.MMOL), Profile.fromMgdlToUnits(HardLimits.MAXISF, Constants.MMOL), 0.1, DecimalFormat("0.0"), save) + TimeListEdit(context, view, R.id.localprofile_target, MainApp.gs(R.string.nsprofileview_target_label) + ":", LocalProfilePlugin.currentProfile().targetLow, LocalProfilePlugin.currentProfile().targetHigh, Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_TARGET_BG[0].toDouble(), Constants.MMOL), Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_TARGET_BG[1].toDouble(), Constants.MMOL), 0.1, DecimalFormat("0.0"), save) } - localprofile_mgdl.isChecked = LocalProfilePlugin.mgdl - localprofile_mmol.isChecked = LocalProfilePlugin.mmol + // Spinner + spinner = SpinnerHelper(view?.findViewById(R.id.localprofile_spinner)) + val profileList: ArrayList = LocalProfilePlugin.profile?.profileList + ?: ArrayList() + context?.let { context -> + val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList) + spinner?.adapter = adapter + spinner?.setSelection(LocalProfilePlugin.currentProfileIndex) + } ?: return + spinner?.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener { + override fun onNothingSelected(parent: AdapterView<*>?) { + } - localprofile_mgdl.setOnClickListener { - LocalProfilePlugin.mgdl = localprofile_mgdl.isChecked - LocalProfilePlugin.mmol = !LocalProfilePlugin.mgdl - localprofile_mmol.isChecked = LocalProfilePlugin.mmol - doEdit() + override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { + if (LocalProfilePlugin.isEdited) { + context?.let { context -> + OKDialog.show(context, MainApp.gs(R.string.confirmation), MainApp.gs(R.string.doyouwantswitchprofile)) { + LocalProfilePlugin.currentProfileIndex = position + build() + } + } + } else { + LocalProfilePlugin.currentProfileIndex = position + build() + } + } + }) + + localprofile_profile_add.setOnClickListener { + LocalProfilePlugin.addNewProfile() + build() } - localprofile_mmol.setOnClickListener { - LocalProfilePlugin.mmol = localprofile_mmol.isChecked - LocalProfilePlugin.mgdl = !LocalProfilePlugin.mmol - localprofile_mgdl.isChecked = LocalProfilePlugin.mgdl - doEdit() + + localprofile_profile_remove.setOnClickListener { + LocalProfilePlugin.removeCurrentProfile() + build() } + // this is probably not possible because it leads to invalid profile + // if (!pumpDescription.isTempBasalCapable) localprofile_basal.visibility = View.GONE + + localprofile_mgdl.isChecked = LocalProfilePlugin.currentProfile().mgdl + localprofile_mmol.isChecked = !LocalProfilePlugin.currentProfile().mgdl + localprofile_mgdl.isEnabled = false + localprofile_mmol.isEnabled = false + localprofile_profileswitch.setOnClickListener { val newDialog = NewNSTreatmentDialog() val profileSwitch = CareportalFragment.PROFILESWITCHDIRECT @@ -96,13 +135,13 @@ class LocalProfileFragment : Fragment() { localprofile_reset.setOnClickListener { LocalProfilePlugin.loadSettings() - localprofile_mgdl.isChecked = LocalProfilePlugin.mgdl - localprofile_mmol.isChecked = LocalProfilePlugin.mmol - localprofile_dia.setParams(LocalProfilePlugin.dia, MIN_DIA, 12.0, 0.1, DecimalFormat("0.0"), false, localprofile_save, textWatch) - TimeListEdit(context, view, R.id.localprofile_ic, MainApp.gs(R.string.nsprofileview_ic_label) + ":", LocalProfilePlugin.ic, null, 0.5, 50.0, 0.1, DecimalFormat("0.0"), save) - TimeListEdit(context, view, R.id.localprofile_isf, MainApp.gs(R.string.nsprofileview_isf_label) + ":", LocalProfilePlugin.isf, null, 0.5, 500.0, 0.1, DecimalFormat("0.0"), save) - basalView = TimeListEdit(context, view, R.id.localprofile_basal, MainApp.gs(R.string.nsprofileview_basal_label) + ": " + sumLabel(), LocalProfilePlugin.basal, null, pumpDescription.basalMinimumRate, 10.0, 0.01, DecimalFormat("0.00"), save) - TimeListEdit(context, view, R.id.localprofile_target, MainApp.gs(R.string.nsprofileview_target_label) + ":", LocalProfilePlugin.targetLow, LocalProfilePlugin.targetHigh, 3.0, 200.0, 0.1, DecimalFormat("0.0"), save) + localprofile_mgdl.isChecked = LocalProfilePlugin.currentProfile().mgdl + localprofile_mmol.isChecked = !LocalProfilePlugin.currentProfile().mgdl + localprofile_dia.setParams(LocalProfilePlugin.currentProfile().dia, MIN_DIA, 12.0, 0.1, DecimalFormat("0.0"), false, localprofile_save, textWatch) + TimeListEdit(context, view, R.id.localprofile_ic, MainApp.gs(R.string.nsprofileview_ic_label) + ":", LocalProfilePlugin.currentProfile().ic, null, 0.5, 50.0, 0.1, DecimalFormat("0.0"), save) + TimeListEdit(context, view, R.id.localprofile_isf, MainApp.gs(R.string.nsprofileview_isf_label) + ":", LocalProfilePlugin.currentProfile().isf, null, 0.5, 500.0, 0.1, DecimalFormat("0.0"), save) + basalView = TimeListEdit(context, view, R.id.localprofile_basal, MainApp.gs(R.string.nsprofileview_basal_label) + ": " + sumLabel(), LocalProfilePlugin.currentProfile().basal, null, pumpDescription.basalMinimumRate, 10.0, 0.01, DecimalFormat("0.00"), save) + TimeListEdit(context, view, R.id.localprofile_target, MainApp.gs(R.string.nsprofileview_target_label) + ":", LocalProfilePlugin.currentProfile().targetLow, LocalProfilePlugin.currentProfile().targetHigh, 3.0, 200.0, 0.1, DecimalFormat("0.0"), save) updateGUI() } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt index e9ead3e4d0..859154ca6a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt @@ -11,16 +11,17 @@ import info.nightscout.androidaps.interfaces.PluginType import info.nightscout.androidaps.interfaces.ProfileInterface import info.nightscout.androidaps.logging.L import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DecimalFormatter import info.nightscout.androidaps.utils.SP import org.json.JSONArray import org.json.JSONException import org.json.JSONObject import org.slf4j.LoggerFactory +import java.util.* +import kotlin.collections.ArrayList -/** - * Created by mike on 05.08.2016. - */ object LocalProfilePlugin : PluginBase(PluginDescription() .mainType(PluginType.PROFILE) .fragmentClass(LocalProfileFragment::class.java.name) @@ -36,15 +37,28 @@ object LocalProfilePlugin : PluginBase(PluginDescription() private const val DEFAULTARRAY = "[{\"time\":\"00:00\",\"timeAsSeconds\":0,\"value\":0}]" + class SingleProfile { + internal var name: String? = null + internal var mgdl: Boolean = false + internal var dia: Double = Constants.defaultDIA + internal var ic: JSONArray? = null + internal var isf: JSONArray? = null + internal var basal: JSONArray? = null + internal var targetLow: JSONArray? = null + internal var targetHigh: JSONArray? = null + } + var isEdited: Boolean = false - internal var mgdl: Boolean = false - internal var mmol: Boolean = false - internal var dia: Double? = null - internal var ic: JSONArray? = null - internal var isf: JSONArray? = null - internal var basal: JSONArray? = null - internal var targetLow: JSONArray? = null - internal var targetHigh: JSONArray? = null + var profiles: ArrayList = ArrayList() + + internal var numOfProfiles = 0 + internal var currentProfileIndex = 0 + + init { + loadSettings() + } + + fun currentProfile() = profiles[currentProfileIndex] @Synchronized fun isValidEditState(): Boolean { @@ -52,20 +66,22 @@ object LocalProfilePlugin : PluginBase(PluginDescription() ?: false } - init { - loadSettings() - } - @Synchronized fun storeSettings() { - SP.putBoolean(LOCAL_PROFILE + "mmol", mmol) - SP.putBoolean(LOCAL_PROFILE + "mgdl", mgdl) - SP.putString(LOCAL_PROFILE + "dia", dia.toString()) - SP.putString(LOCAL_PROFILE + "ic", ic.toString()) - SP.putString(LOCAL_PROFILE + "isf", isf.toString()) - SP.putString(LOCAL_PROFILE + "basal", basal.toString()) - SP.putString(LOCAL_PROFILE + "targetlow", targetLow.toString()) - SP.putString(LOCAL_PROFILE + "targethigh", targetHigh.toString()) + for (i in 0 until numOfProfiles) { + profiles[i].run { + val LOCAL_PROFILE_NUMBERED = LOCAL_PROFILE + "_" + i + "_" + SP.putString(LOCAL_PROFILE_NUMBERED + "name", name) + SP.putBoolean(LOCAL_PROFILE_NUMBERED + "mgdl", mgdl) + SP.putDouble(LOCAL_PROFILE_NUMBERED + "dia", dia) + SP.putString(LOCAL_PROFILE_NUMBERED + "ic", ic.toString()) + SP.putString(LOCAL_PROFILE_NUMBERED + "isf", isf.toString()) + SP.putString(LOCAL_PROFILE_NUMBERED + "basal", basal.toString()) + SP.putString(LOCAL_PROFILE_NUMBERED + "targetlow", targetLow.toString()) + SP.putString(LOCAL_PROFILE_NUMBERED + "targethigh", targetHigh.toString()) + } + } + SP.putInt(LOCAL_PROFILE + "_profiles", numOfProfiles) createAndStoreConvertedProfile() isEdited = false @@ -76,61 +92,146 @@ object LocalProfilePlugin : PluginBase(PluginDescription() @Synchronized fun loadSettings() { + if (SP.contains(LOCAL_PROFILE + "mgdl")) { + doConversion() + return + } + + numOfProfiles = SP.getInt(LOCAL_PROFILE + "_profiles", 0) + profiles.clear() + numOfProfiles = Math.max(numOfProfiles, 1) // create at least one default profile if none exists + + for (i in 0 until numOfProfiles) { + val p = SingleProfile() + val LOCAL_PROFILE_NUMBERED = LOCAL_PROFILE + "_" + i + "_" + + p.name = SP.getString(LOCAL_PROFILE_NUMBERED + "name", LOCAL_PROFILE + i) + p.mgdl = SP.getBoolean(LOCAL_PROFILE_NUMBERED + "mgdl", false) + p.dia = SP.getDouble(LOCAL_PROFILE_NUMBERED + "dia", Constants.defaultDIA) + try { + p.ic = JSONArray(SP.getString(LOCAL_PROFILE_NUMBERED + "ic", DEFAULTARRAY)) + } catch (e1: JSONException) { + try { + p.ic = JSONArray(DEFAULTARRAY) + } catch (ignored: JSONException) { + } + log.error("Exception", e1) + } + + try { + p.isf = JSONArray(SP.getString(LOCAL_PROFILE_NUMBERED + "isf", DEFAULTARRAY)) + } catch (e1: JSONException) { + try { + p.isf = JSONArray(DEFAULTARRAY) + } catch (ignored: JSONException) { + } + log.error("Exception", e1) + } + + try { + p.basal = JSONArray(SP.getString(LOCAL_PROFILE_NUMBERED + "basal", DEFAULTARRAY)) + } catch (e1: JSONException) { + try { + p.basal = JSONArray(DEFAULTARRAY) + } catch (ignored: JSONException) { + } + log.error("Exception", e1) + } + + try { + p.targetLow = JSONArray(SP.getString(LOCAL_PROFILE_NUMBERED + "targetlow", DEFAULTARRAY)) + } catch (e1: JSONException) { + try { + p.targetLow = JSONArray(DEFAULTARRAY) + } catch (ignored: JSONException) { + } + log.error("Exception", e1) + } + + try { + p.targetHigh = JSONArray(SP.getString(LOCAL_PROFILE_NUMBERED + "targethigh", DEFAULTARRAY)) + } catch (e1: JSONException) { + try { + p.targetHigh = JSONArray(DEFAULTARRAY) + } catch (ignored: JSONException) { + } + log.error("Exception", e1) + } + + profiles.add(p) + } + isEdited = false + createAndStoreConvertedProfile() + } + + @Synchronized + private fun doConversion() { // conversion from 2.3 to 2.4 format if (L.isEnabled(L.PROFILE)) log.debug("Loading stored settings") + val p = SingleProfile() - mgdl = SP.getBoolean(LOCAL_PROFILE + "mgdl", false) - mmol = SP.getBoolean(LOCAL_PROFILE + "mmol", true) - dia = SP.getDouble(LOCAL_PROFILE + "dia", Constants.defaultDIA) + p.mgdl = SP.getBoolean(LOCAL_PROFILE + "mgdl", ProfileFunctions.getSystemUnits() == Constants.MGDL) + p.dia = SP.getDouble(LOCAL_PROFILE + "dia", Constants.defaultDIA) try { - ic = JSONArray(SP.getString(LOCAL_PROFILE + "ic", DEFAULTARRAY)) + p.ic = JSONArray(SP.getString(LOCAL_PROFILE + "ic", DEFAULTARRAY)) } catch (e1: JSONException) { try { - ic = JSONArray(DEFAULTARRAY) + p.ic = JSONArray(DEFAULTARRAY) } catch (ignored: JSONException) { } - } try { - isf = JSONArray(SP.getString(LOCAL_PROFILE + "isf", DEFAULTARRAY)) + p.isf = JSONArray(SP.getString(LOCAL_PROFILE + "isf", DEFAULTARRAY)) } catch (e1: JSONException) { try { - isf = JSONArray(DEFAULTARRAY) + p.isf = JSONArray(DEFAULTARRAY) } catch (ignored: JSONException) { } - } try { - basal = JSONArray(SP.getString(LOCAL_PROFILE + "basal", DEFAULTARRAY)) + p.basal = JSONArray(SP.getString(LOCAL_PROFILE + "basal", DEFAULTARRAY)) } catch (e1: JSONException) { try { - basal = JSONArray(DEFAULTARRAY) + p.basal = JSONArray(DEFAULTARRAY) } catch (ignored: JSONException) { } - } try { - targetLow = JSONArray(SP.getString(LOCAL_PROFILE + "targetlow", DEFAULTARRAY)) + p.targetLow = JSONArray(SP.getString(LOCAL_PROFILE + "targetlow", DEFAULTARRAY)) } catch (e1: JSONException) { try { - targetLow = JSONArray(DEFAULTARRAY) + p.targetLow = JSONArray(DEFAULTARRAY) } catch (ignored: JSONException) { } - } try { - targetHigh = JSONArray(SP.getString(LOCAL_PROFILE + "targethigh", DEFAULTARRAY)) + p.targetHigh = JSONArray(SP.getString(LOCAL_PROFILE + "targethigh", DEFAULTARRAY)) } catch (e1: JSONException) { try { - targetHigh = JSONArray(DEFAULTARRAY) + p.targetHigh = JSONArray(DEFAULTARRAY) } catch (ignored: JSONException) { } - } + p.name = LOCAL_PROFILE + + SP.remove(LOCAL_PROFILE + "mgdl") + SP.remove(LOCAL_PROFILE + "mmol") + SP.remove(LOCAL_PROFILE + "dia") + SP.remove(LOCAL_PROFILE + "ic") + SP.remove(LOCAL_PROFILE + "isf") + SP.remove(LOCAL_PROFILE + "basal") + SP.remove(LOCAL_PROFILE + "targetlow") + SP.remove(LOCAL_PROFILE + "targethigh") + + currentProfileIndex = 0 + numOfProfiles = 1 + profiles.clear() + profiles.add(p) + storeSettings() isEdited = false createAndStoreConvertedProfile() @@ -178,22 +279,61 @@ object LocalProfilePlugin : PluginBase(PluginDescription() rawProfile = createProfileStore() } + fun addNewProfile() { + var free = 0 + for (i in 1..10000) { + if (rawProfile?.getSpecificProfile(LOCAL_PROFILE + i) == null) { + free = i; + break + } + } + val p = SingleProfile() + p.name = LOCAL_PROFILE + free + p.mgdl = ProfileFunctions.getSystemUnits() == Constants.MGDL + p.dia = Constants.defaultDIA + p.ic = JSONArray(DEFAULTARRAY) + p.isf = JSONArray(DEFAULTARRAY) + p.basal = JSONArray(DEFAULTARRAY) + p.targetLow = JSONArray(DEFAULTARRAY) + p.targetHigh = JSONArray(DEFAULTARRAY) + profiles.add(p) + currentProfileIndex = profiles.size - 1 + numOfProfiles++ + createAndStoreConvertedProfile() + storeSettings() + } + + fun removeCurrentProfile() { + profiles.removeAt(currentProfileIndex) + numOfProfiles-- + if (profiles.size == 0) addNewProfile() + currentProfileIndex = 0 + createAndStoreConvertedProfile() + storeSettings() + } + fun createProfileStore(): ProfileStore { val json = JSONObject() val store = JSONObject() - val profile = JSONObject() try { - json.put("defaultProfile", LOCAL_PROFILE) + for (i in 0 until numOfProfiles) { + profiles[i].run { + val profile = JSONObject() + profile.put("dia", dia) + profile.put("carbratio", ic) + profile.put("sens", isf) + profile.put("basal", basal) + profile.put("target_low", targetLow) + profile.put("target_high", targetHigh) + profile.put("units", if (mgdl) Constants.MGDL else Constants.MMOL) + profile.put("timezone", TimeZone.getDefault().id) + store.put(name, profile) + } + } + json.put("defaultProfile", currentProfile().name) + json.put("startDate", DateUtil.toISOAsUTC(DateUtil.now())) json.put("store", store) - profile.put("dia", dia) - profile.put("carbratio", ic) - profile.put("sens", isf) - profile.put("basal", basal) - profile.put("target_low", targetLow) - profile.put("target_high", targetHigh) - profile.put("units", if (mgdl) Constants.MGDL else Constants.MMOL) - store.put(LOCAL_PROFILE, profile) } catch (e: JSONException) { log.error("Unhandled exception", e) } @@ -202,11 +342,11 @@ object LocalProfilePlugin : PluginBase(PluginDescription() } override fun getProfile(): ProfileStore? { - return if (rawProfile?.defaultProfile?.isValid(MainApp.gs(R.string.localprofile)) != true) null else rawProfile + return rawProfile } override fun getUnits(): String { - return if (mgdl) Constants.MGDL else Constants.MMOL + return if (currentProfile().mgdl) Constants.MGDL else Constants.MMOL } override fun getProfileName(): String { diff --git a/app/src/main/res/layout/localprofile_fragment.xml b/app/src/main/res/layout/localprofile_fragment.xml index 49fd413115..19770a7257 100644 --- a/app/src/main/res/layout/localprofile_fragment.xml +++ b/app/src/main/res/layout/localprofile_fragment.xml @@ -35,32 +35,29 @@ android:textAppearance="?android:attr/textAppearanceMedium" /> diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 864835bd87..1184f37e54 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -10,6 +10,15 @@ open + + mg/dL + mmol/L + + + mg/dl + mmol + + @string/en_lang @string/af_lang diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f12ac4b06b..3570604990 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1606,5 +1606,8 @@ min Profile name: Selected: + Units + units + Do you want to switch profile and discard changes made to current profile? diff --git a/app/src/main/res/xml/pref_language.xml b/app/src/main/res/xml/pref_general.xml similarity index 65% rename from app/src/main/res/xml/pref_language.xml rename to app/src/main/res/xml/pref_general.xml index 981de2b346..f1bcbdb858 100644 --- a/app/src/main/res/xml/pref_language.xml +++ b/app/src/main/res/xml/pref_general.xml @@ -1,13 +1,23 @@ + + + + + \ No newline at end of file From 339765932030c53b22da41e9e8402805070a5855 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 15 Oct 2019 21:58:48 +0200 Subject: [PATCH 05/26] LocalProfile tweaking switching profiles --- .../profile/local/LocalProfileFragment.kt | 18 +++++++++++------ .../nightscout/androidaps/utils/OKDialog.java | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt index a47726c033..1dc9ed1a8b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt @@ -65,7 +65,7 @@ class LocalProfileFragment : Fragment() { fun build() { val pumpDescription = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription ?: return - val units = ProfileFunctions.getSystemUnits() + val units = if (LocalProfilePlugin.currentProfile().mgdl) Constants.MGDL else Constants.MMOL localprofile_name.setText(LocalProfilePlugin.currentProfile().name) localprofile_dia.setParams(LocalProfilePlugin.currentProfile().dia, HardLimits.MINDIA, HardLimits.MAXDIA, 0.1, DecimalFormat("0.0"), false, localprofile_save, textWatch) @@ -94,11 +94,13 @@ class LocalProfileFragment : Fragment() { override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { if (LocalProfilePlugin.isEdited) { - context?.let { context -> - OKDialog.show(context, MainApp.gs(R.string.confirmation), MainApp.gs(R.string.doyouwantswitchprofile)) { + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.doyouwantswitchprofile), { LocalProfilePlugin.currentProfileIndex = position build() - } + }, { + spinner?.setSelection(LocalProfilePlugin.currentProfileIndex) + }) } } else { LocalProfilePlugin.currentProfileIndex = position @@ -113,8 +115,12 @@ class LocalProfileFragment : Fragment() { } localprofile_profile_remove.setOnClickListener { - LocalProfilePlugin.removeCurrentProfile() - build() + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.doyouwantswitchprofile), { + LocalProfilePlugin.removeCurrentProfile() + build() + }, null) + } } // this is probably not possible because it leads to invalid profile diff --git a/app/src/main/java/info/nightscout/androidaps/utils/OKDialog.java b/app/src/main/java/info/nightscout/androidaps/utils/OKDialog.java index a45d329d63..cee3b7ea08 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/OKDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/OKDialog.java @@ -83,4 +83,24 @@ public class OKDialog { .show(); } + public static void showConfirmation(final Activity activity, String message, final Runnable ok, final Runnable cancel) { + AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.AppTheme)) + .setMessage(message) + .setPositiveButton(android.R.string.ok, (dialog, which) -> { + dialog.dismiss(); + if (ok != null) { + SystemClock.sleep(100); + activity.runOnUiThread(ok); + } + }) + .setNegativeButton(android.R.string.cancel, (dialog, which) -> { + dialog.dismiss(); + if (cancel != null) { + SystemClock.sleep(100); + activity.runOnUiThread(cancel); + } + }) + .show(); + } + } From 49b2aee320c6edb040be60c510ccfa460ebd2514 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 15 Oct 2019 22:35:04 +0200 Subject: [PATCH 06/26] TODO --- .../androidaps/plugins/profile/local/LocalProfileFragment.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt index 1dc9ed1a8b..ddc33ca4fb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt @@ -132,6 +132,7 @@ class LocalProfileFragment : Fragment() { localprofile_mmol.isEnabled = false localprofile_profileswitch.setOnClickListener { + // TODO: select in dialog LocalProfilePlugin.currentProfileIndex val newDialog = NewNSTreatmentDialog() val profileSwitch = CareportalFragment.PROFILESWITCHDIRECT profileSwitch.executeProfileSwitch = true From 8fa4c49060de6d9aee6ef091d15321805c026489 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 11 Nov 2019 16:42:29 +0100 Subject: [PATCH 07/26] remove simple profile --- .../info/nightscout/androidaps/MainApp.java | 2 - .../profile/simple/SimpleProfileFragment.java | 159 --------------- .../profile/simple/SimpleProfilePlugin.java | 181 ------------------ .../res/layout/simpleprofile_fragment.xml | 145 -------------- 4 files changed, 487 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfileFragment.java delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfilePlugin.java delete mode 100644 app/src/main/res/layout/simpleprofile_fragment.xml diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 7899937a04..b8dbdd7ad3 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -59,7 +59,6 @@ import info.nightscout.androidaps.plugins.insulin.InsulinOrefUltraRapidActingPlu import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin; import info.nightscout.androidaps.plugins.profile.ns.NSProfilePlugin; -import info.nightscout.androidaps.plugins.profile.simple.SimpleProfilePlugin; import info.nightscout.androidaps.plugins.pump.combo.ComboPlugin; import info.nightscout.androidaps.plugins.pump.danaR.DanaRPlugin; import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin; @@ -188,7 +187,6 @@ public class MainApp extends Application { if (Config.APS) pluginsList.add(OpenAPSAMAPlugin.getPlugin()); if (Config.APS) pluginsList.add(OpenAPSSMBPlugin.getPlugin()); pluginsList.add(NSProfilePlugin.getPlugin()); - if (!Config.NSCLIENT) pluginsList.add(SimpleProfilePlugin.getPlugin()); if (!Config.NSCLIENT) pluginsList.add(LocalProfilePlugin.INSTANCE); pluginsList.add(TreatmentsPlugin.getPlugin()); if (!Config.NSCLIENT) pluginsList.add(SafetyPlugin.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfileFragment.java deleted file mode 100644 index ecf4fc423b..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfileFragment.java +++ /dev/null @@ -1,159 +0,0 @@ -package info.nightscout.androidaps.plugins.profile.simple; - - -import android.os.Bundle; -import android.text.Editable; -import android.text.TextWatcher; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Button; -import android.widget.EditText; -import android.widget.RadioButton; -import android.widget.TextView; - -import androidx.fragment.app.Fragment; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.events.EventInitializationChanged; -import info.nightscout.androidaps.plugins.bus.RxBus; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment; -import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog; -import info.nightscout.androidaps.plugins.general.careportal.OptionsToShow; -import info.nightscout.androidaps.utils.FabricPrivacy; -import info.nightscout.androidaps.utils.SafeParse; -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.disposables.CompositeDisposable; - -public class SimpleProfileFragment extends Fragment { - private CompositeDisposable disposable = new CompositeDisposable(); - - EditText diaView; - RadioButton mgdlView; - RadioButton mmolView; - EditText icView; - EditText isfView; - EditText basalView; - EditText targetlowView; - EditText targethighView; - Button profileswitchButton; - TextView invalidProfile; - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View layout = inflater.inflate(R.layout.simpleprofile_fragment, container, false); - diaView = (EditText) layout.findViewById(R.id.simpleprofile_dia); - mgdlView = (RadioButton) layout.findViewById(R.id.simpleprofile_mgdl); - mmolView = (RadioButton) layout.findViewById(R.id.simpleprofile_mmol); - icView = (EditText) layout.findViewById(R.id.simpleprofile_ic); - isfView = (EditText) layout.findViewById(R.id.simpleprofile_isf); - basalView = (EditText) layout.findViewById(R.id.simpleprofile_basalrate); - targetlowView = (EditText) layout.findViewById(R.id.simpleprofile_targetlow); - targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh); - profileswitchButton = (Button) layout.findViewById(R.id.simpleprofile_profileswitch); - invalidProfile = (TextView) layout.findViewById(R.id.invalidprofile); - - if (!ConfigBuilderPlugin.getPlugin().getActivePump().getPumpDescription().isTempBasalCapable) { - layout.findViewById(R.id.simpleprofile_basalrate).setVisibility(View.GONE); - layout.findViewById(R.id.simpleprofile_basalrate_label).setVisibility(View.GONE); - } - - mgdlView.setChecked(SimpleProfilePlugin.getPlugin().mgdl); - mmolView.setChecked(SimpleProfilePlugin.getPlugin().mmol); - diaView.setText(SimpleProfilePlugin.getPlugin().dia.toString()); - icView.setText(SimpleProfilePlugin.getPlugin().ic.toString()); - isfView.setText(SimpleProfilePlugin.getPlugin().isf.toString()); - basalView.setText(SimpleProfilePlugin.getPlugin().basal.toString()); - targetlowView.setText(SimpleProfilePlugin.getPlugin().targetLow.toString()); - targethighView.setText(SimpleProfilePlugin.getPlugin().targetHigh.toString()); - - mgdlView.setOnClickListener(v -> { - SimpleProfilePlugin.getPlugin().mgdl = mgdlView.isChecked(); - SimpleProfilePlugin.getPlugin().mmol = !SimpleProfilePlugin.getPlugin().mgdl; - mmolView.setChecked(SimpleProfilePlugin.getPlugin().mmol); - SimpleProfilePlugin.getPlugin().storeSettings(); - }); - mmolView.setOnClickListener(v -> { - SimpleProfilePlugin.getPlugin().mmol = mmolView.isChecked(); - SimpleProfilePlugin.getPlugin().mgdl = !SimpleProfilePlugin.getPlugin().mmol; - mgdlView.setChecked(SimpleProfilePlugin.getPlugin().mgdl); - SimpleProfilePlugin.getPlugin().storeSettings(); - }); - - profileswitchButton.setOnClickListener(view -> { - NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); - final OptionsToShow profileswitch = CareportalFragment.PROFILESWITCH; - profileswitch.executeProfileSwitch = true; - newDialog.setOptions(profileswitch, R.string.careportal_profileswitch); - newDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); - }); - - TextWatcher textWatch = new TextWatcher() { - - @Override - public void afterTextChanged(Editable s) { - } - - @Override - public void beforeTextChanged(CharSequence s, int start, - int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, - int before, int count) { - SimpleProfilePlugin.getPlugin().dia = SafeParse.stringToDouble(diaView.getText().toString()); - SimpleProfilePlugin.getPlugin().ic = SafeParse.stringToDouble(icView.getText().toString()); - SimpleProfilePlugin.getPlugin().isf = SafeParse.stringToDouble(isfView.getText().toString()); - SimpleProfilePlugin.getPlugin().basal = SafeParse.stringToDouble(basalView.getText().toString()); - SimpleProfilePlugin.getPlugin().targetLow = SafeParse.stringToDouble(targetlowView.getText().toString()); - SimpleProfilePlugin.getPlugin().targetHigh = SafeParse.stringToDouble(targethighView.getText().toString()); - SimpleProfilePlugin.getPlugin().storeSettings(); - updateGUI(); - } - }; - - diaView.addTextChangedListener(textWatch); - icView.addTextChangedListener(textWatch); - isfView.addTextChangedListener(textWatch); - basalView.addTextChangedListener(textWatch); - targetlowView.addTextChangedListener(textWatch); - targethighView.addTextChangedListener(textWatch); - - return layout; - } - - @Override - public synchronized void onResume() { - super.onResume(); - disposable.add(RxBus.INSTANCE - .toObservable(EventInitializationChanged.class) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(event -> updateGUI(), FabricPrivacy::logException) - ); - updateGUI(); - } - - @Override - public synchronized void onPause() { - super.onPause(); - disposable.clear(); - } - - protected void updateGUI() { - boolean isValid = SimpleProfilePlugin.getPlugin().getProfile() != null && SimpleProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid(MainApp.gs(R.string.simpleprofile)); - if (!ConfigBuilderPlugin.getPlugin().getActivePump().isInitialized() || ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended() || !isValid) { - profileswitchButton.setVisibility(View.GONE); - } else { - profileswitchButton.setVisibility(View.VISIBLE); - } - if (isValid) - invalidProfile.setVisibility(View.GONE); - else - invalidProfile.setVisibility(View.VISIBLE); - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfilePlugin.java deleted file mode 100644 index 61ca403f55..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfilePlugin.java +++ /dev/null @@ -1,181 +0,0 @@ -package info.nightscout.androidaps.plugins.profile.simple; - -import android.content.SharedPreferences; -import android.preference.PreferenceManager; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.ProfileStore; -import info.nightscout.androidaps.events.EventProfileStoreChanged; -import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.PluginDescription; -import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.interfaces.ProfileInterface; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.plugins.bus.RxBus; -import info.nightscout.androidaps.utils.SP; - -/** - * Created by mike on 05.08.2016. - */ -public class SimpleProfilePlugin extends PluginBase implements ProfileInterface { - private static Logger log = LoggerFactory.getLogger(L.PROFILE); - - private static SimpleProfilePlugin simpleProfilePlugin; - - public static SimpleProfilePlugin getPlugin() { - if (simpleProfilePlugin == null) - simpleProfilePlugin = new SimpleProfilePlugin(); - return simpleProfilePlugin; - } - - private static ProfileStore convertedProfile = null; - - boolean mgdl; - boolean mmol; - Double dia; - Double ic; - Double isf; - Double basal; - Double targetLow; - Double targetHigh; - - private SimpleProfilePlugin() { - super(new PluginDescription() - .mainType(PluginType.PROFILE) - .fragmentClass(SimpleProfileFragment.class.getName()) - .pluginName(R.string.simpleprofile) - .shortName(R.string.simpleprofile_shortname) - .description(R.string.description_profile_simple) - ); - loadSettings(); - } - - public void storeSettings() { - if (L.isEnabled(L.PROFILE)) - log.debug("Storing settings"); - SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - SharedPreferences.Editor editor = settings.edit(); - editor.putBoolean("SimpleProfile" + "mmol", mmol); - editor.putBoolean("SimpleProfile" + "mgdl", mgdl); - editor.putString("SimpleProfile" + "dia", dia.toString()); - editor.putString("SimpleProfile" + "ic", ic.toString()); - editor.putString("SimpleProfile" + "isf", isf.toString()); - editor.putString("SimpleProfile" + "basal", basal.toString()); - editor.putString("SimpleProfile" + "targetlow", targetLow.toString()); - editor.putString("SimpleProfile" + "targethigh", targetHigh.toString()); - - editor.apply(); - createConvertedProfile(); - if (L.isEnabled(L.PROFILE)) - log.debug("Storing settings: " + getRawProfile().getData().toString()); - RxBus.INSTANCE.send(new EventProfileStoreChanged()); - } - - private void loadSettings() { - if (L.isEnabled(L.PROFILE)) - log.debug("Loading stored settings"); - - mgdl = SP.getBoolean("SimpleProfile" + "mgdl", true); - mmol = SP.getBoolean("SimpleProfile" + "mmol", false); - dia = SP.getDouble("SimpleProfile" + "dia", Constants.defaultDIA); - ic = SP.getDouble("SimpleProfile" + "ic", 0d); - isf = SP.getDouble("SimpleProfile" + "isf", 0d); - basal = SP.getDouble("SimpleProfile" + "basal", 0d); - targetLow = SP.getDouble("SimpleProfile" + "targetlow", 0d); - targetHigh = SP.getDouble("SimpleProfile" + "targethigh", 0d); - } - - /* - { - "_id": "576264a12771b7500d7ad184", - "startDate": "2016-06-16T08:35:00.000Z", - "defaultProfile": "Default", - "store": { - "Default": { - "dia": "3", - "carbratio": [{ - "time": "00:00", - "value": "30" - }], - "carbs_hr": "20", - "delay": "20", - "sens": [{ - "time": "00:00", - "value": "100" - }], - "timezone": "UTC", - "basal": [{ - "time": "00:00", - "value": "0.1" - }], - "target_low": [{ - "time": "00:00", - "value": "0" - }], - "target_high": [{ - "time": "00:00", - "value": "0" - }], - "startDate": "1970-01-01T00:00:00.000Z", - "units": "mmol" - } - }, - "created_at": "2016-06-16T08:34:41.256Z" - } - */ - private void createConvertedProfile() { - JSONObject json = new JSONObject(); - JSONObject store = new JSONObject(); - JSONObject profile = new JSONObject(); - - try { - json.put("defaultProfile", "SimpleProfile"); - json.put("store", store); - profile.put("dia", dia); - profile.put("carbratio", new JSONArray().put(new JSONObject().put("time", "00:00").put("timeAsSeconds", 0).put("value", ic))); - profile.put("sens", new JSONArray().put(new JSONObject().put("time", "00:00").put("timeAsSeconds", 0).put("value", isf))); - profile.put("basal", new JSONArray().put(new JSONObject().put("time", "00:00").put("timeAsSeconds", 0).put("value", basal))); - profile.put("target_low", new JSONArray().put(new JSONObject().put("time", "00:00").put("timeAsSeconds", 0).put("value", targetLow))); - profile.put("target_high", new JSONArray().put(new JSONObject().put("time", "00:00").put("timeAsSeconds", 0).put("value", targetHigh))); - profile.put("units", mgdl ? Constants.MGDL : Constants.MMOL); - store.put("SimpleProfile", profile); - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - convertedProfile = new ProfileStore(json); - } - - @Override - public ProfileStore getProfile() { - if (convertedProfile == null) - createConvertedProfile(); - if (!convertedProfile.getDefaultProfile().isValid(MainApp.gs(R.string.simpleprofile))) - return null; - return convertedProfile; - } - - public ProfileStore getRawProfile() { - if (convertedProfile == null) - createConvertedProfile(); - return convertedProfile; - } - - @Override - public String getUnits() { - return mgdl ? Constants.MGDL : Constants.MMOL; - } - - @Override - public String getProfileName() { - return "SimpleProfile"; - } - -} diff --git a/app/src/main/res/layout/simpleprofile_fragment.xml b/app/src/main/res/layout/simpleprofile_fragment.xml deleted file mode 100644 index 5a896f22a0..0000000000 --- a/app/src/main/res/layout/simpleprofile_fragment.xml +++ /dev/null @@ -1,145 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -