diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index b647e9b6d1..8f9d7606e8 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -24,6 +24,7 @@ import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.DanaR.DanaRFragment; import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanFragment; +import info.nightscout.androidaps.plugins.LocalProfile.LocalProfileFragment; import info.nightscout.androidaps.plugins.Loop.LoopFragment; import info.nightscout.androidaps.plugins.MDI.MDIFragment; import info.nightscout.androidaps.plugins.NSProfile.NSProfileFragment; @@ -83,6 +84,7 @@ public class MainApp extends Application { if (Config.OPENAPSMAENABLED) pluginsList.add(OpenAPSMAFragment.getPlugin()); pluginsList.add(NSProfileFragment.getPlugin()); pluginsList.add(SimpleProfileFragment.getPlugin()); + pluginsList.add(LocalProfileFragment.getPlugin()); pluginsList.add(CircadianPercentageProfileFragment.getPlugin()); pluginsList.add(TreatmentsFragment.getPlugin()); pluginsList.add(TempBasalsFragment.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfileFragment.java new file mode 100644 index 0000000000..8c9a156d33 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfileFragment.java @@ -0,0 +1,174 @@ +package info.nightscout.androidaps.plugins.LocalProfile; + + +import android.app.Activity; +import android.os.Bundle; +import android.support.v4.app.Fragment; +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 com.squareup.otto.Subscribe; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.events.EventInitializationChanged; +import info.nightscout.androidaps.interfaces.FragmentBase; +import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; +import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; +import info.nightscout.androidaps.plugins.SimpleProfile.SimpleProfilePlugin; +import info.nightscout.utils.SafeParse; + +public class LocalProfileFragment extends Fragment implements FragmentBase { + private static Logger log = LoggerFactory.getLogger(LocalProfileFragment.class); + + private static LocalProfilePlugin localProfilePlugin = new LocalProfilePlugin(); + + public static LocalProfilePlugin getPlugin() { + return localProfilePlugin; + } + + EditText diaView; + RadioButton mgdlView; + RadioButton mmolView; + EditText icView; + EditText isfView; + EditText carView; + EditText basalView; + EditText targetlowView; + EditText targethighView; + Button profileswitchButton; + + @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); + carView = (EditText) layout.findViewById(R.id.simpleprofile_car); + 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); + + onStatusEvent(null); + + mgdlView.setChecked(localProfilePlugin.mgdl); + mmolView.setChecked(localProfilePlugin.mmol); + diaView.setText(localProfilePlugin.dia.toString()); + icView.setText(localProfilePlugin.ic.toString()); + isfView.setText(localProfilePlugin.isf.toString()); + carView.setText(localProfilePlugin.car.toString()); + basalView.setText(localProfilePlugin.basal.toString()); + targetlowView.setText(localProfilePlugin.targetLow.toString()); + targethighView.setText(localProfilePlugin.targetHigh.toString()); + + mgdlView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + localProfilePlugin.mgdl = mgdlView.isChecked(); + localProfilePlugin.mmol = !localProfilePlugin.mgdl; + mmolView.setChecked(localProfilePlugin.mmol); + localProfilePlugin.storeSettings(); + } + }); + mmolView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + localProfilePlugin.mmol = mmolView.isChecked(); + localProfilePlugin.mgdl = !localProfilePlugin.mmol; + mgdlView.setChecked(localProfilePlugin.mgdl); + localProfilePlugin.storeSettings(); + } + }); + + profileswitchButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); + final OptionsToShow profileswitch = new OptionsToShow(R.id.careportal_profileswitch, R.string.careportal_profileswitch, true, false, false, false, false, false, false, true, false); + profileswitch.executeProfileSwitch = true; + newDialog.setOptions(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) { + localProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString()); + localProfilePlugin.ic = SafeParse.stringToDouble(icView.getText().toString()); + localProfilePlugin.isf = SafeParse.stringToDouble(isfView.getText().toString()); + localProfilePlugin.car = SafeParse.stringToDouble(carView.getText().toString()); + localProfilePlugin.basal = SafeParse.stringToDouble(basalView.getText().toString()); + localProfilePlugin.targetLow = SafeParse.stringToDouble(targetlowView.getText().toString()); + localProfilePlugin.targetHigh = SafeParse.stringToDouble(targethighView.getText().toString()); + localProfilePlugin.storeSettings(); + } + }; + + diaView.addTextChangedListener(textWatch); + icView.addTextChangedListener(textWatch); + isfView.addTextChangedListener(textWatch); + carView.addTextChangedListener(textWatch); + basalView.addTextChangedListener(textWatch); + targetlowView.addTextChangedListener(textWatch); + targethighView.addTextChangedListener(textWatch); + + onStatusEvent(null); + + return layout; + } + + @Override + public void onPause() { + super.onPause(); + MainApp.bus().unregister(this); + } + + @Override + public void onResume() { + super.onResume(); + MainApp.bus().register(this); + onStatusEvent(null); + } + + @Subscribe + public void onStatusEvent(final EventInitializationChanged e) { + Activity activity = getActivity(); + if (activity != null) + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + if (!MainApp.getConfigBuilder().isInitialized() || !MainApp.getConfigBuilder().getPumpDescription().isSetBasalProfileCapable) { + profileswitchButton.setVisibility(View.GONE); + } else { + profileswitchButton.setVisibility(View.VISIBLE); + } + } + }); + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java new file mode 100644 index 0000000000..37daeac883 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/LocalProfile/LocalProfilePlugin.java @@ -0,0 +1,243 @@ +package info.nightscout.androidaps.plugins.LocalProfile; + +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.Config; +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.PluginBase; +import info.nightscout.androidaps.interfaces.ProfileInterface; +import info.nightscout.androidaps.plugins.SimpleProfile.SimpleProfileFragment; +import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.SafeParse; + +/** + * Created by mike on 05.08.2016. + */ +public class LocalProfilePlugin implements PluginBase, ProfileInterface { + private static Logger log = LoggerFactory.getLogger(LocalProfilePlugin.class); + + private static boolean fragmentEnabled = true; + private static boolean fragmentVisible = true; + + private static NSProfile convertedProfile = null; + + boolean mgdl; + boolean mmol; + Double dia; + Double ic; + Double isf; + Double car; + Double basal; + Double targetLow; + Double targetHigh; + + public LocalProfilePlugin() { + loadSettings(); + } + + @Override + public String getFragmentClass() { + return LocalProfileFragment.class.getName(); + } + + @Override + public int getType() { + return PluginBase.PROFILE; + } + + @Override + public String getName() { + return MainApp.instance().getString(R.string.localprofile); + } + + @Override + public boolean isEnabled(int type) { + return type == PROFILE && fragmentEnabled; + } + + @Override + public boolean isVisibleInTabs(int type) { + return type == PROFILE && fragmentVisible; + } + + @Override + public boolean canBeHidden(int type) { + return true; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + if (type == PROFILE) this.fragmentEnabled = fragmentEnabled; + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + if (type == PROFILE) this.fragmentVisible = fragmentVisible; + } + + public void storeSettings() { + if (Config.logPrefsChange) + 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" + "car", car.toString()); + editor.putString("SimpleProfile" + "basal", basal.toString()); + editor.putString("SimpleProfile" + "targetlow", targetLow.toString()); + editor.putString("SimpleProfile" + "targethigh", targetHigh.toString()); + + editor.commit(); + createConvertedProfile(); + } + + private void loadSettings() { + if (Config.logPrefsChange) + log.debug("Loading stored settings"); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + + if (settings.contains("SimpleProfile" + "mgdl")) + try { + mgdl = settings.getBoolean("SimpleProfile" + "mgdl", true); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else mgdl = true; + if (settings.contains("SimpleProfile" + "mmol")) + try { + mmol = settings.getBoolean("SimpleProfile" + "mmol", false); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else mmol = false; + if (settings.contains("SimpleProfile" + "dia")) + try { + dia = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "dia", "3")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else dia = 3d; + if (settings.contains("SimpleProfile" + "ic")) + try { + ic = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "ic", "20")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else ic = 20d; + if (settings.contains("SimpleProfile" + "isf")) + try { + isf = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "isf", "200")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else isf = 200d; + if (settings.contains("SimpleProfile" + "car")) + try { + car = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "car", "20")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else car = 20d; + if (settings.contains("SimpleProfile" + "basal")) + try { + basal = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "basal", "1")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else basal = 1d; + if (settings.contains("SimpleProfile" + "targetlow")) + try { + targetLow = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "targetlow", "80")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else targetLow = 80d; + if (settings.contains("SimpleProfile" + "targethigh")) + try { + targetHigh = SafeParse.stringToDouble(settings.getString("SimpleProfile" + "targethigh", "120")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else targetHigh = 120d; + createConvertedProfile(); + } + + /* + { + "_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" + } + */ + 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("timeAsSeconds", 0).put("value", ic))); + profile.put("carbs_hr", car); + profile.put("sens", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", isf))); + profile.put("basal", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", basal))); + profile.put("target_low", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetLow))); + profile.put("target_high", new JSONArray().put(new JSONObject().put("timeAsSeconds", 0).put("value", targetHigh))); + profile.put("units", mgdl ? Constants.MGDL : Constants.MMOL); + store.put("SimpleProfile", profile); + } catch (JSONException e) { + e.printStackTrace(); + } + convertedProfile = new NSProfile(json, "SimpleProfile"); + } + + @Override + public NSProfile getProfile() { + return convertedProfile; + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java index 090c2542e2..1d0381ce3b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java @@ -52,9 +52,6 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase { EditText targetlowView; EditText targethighView; Button profileswitchButton; - TimeListEdit test; - JSONArray data1 = new JSONArray(); - JSONArray data2 = new JSONArray(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -70,7 +67,6 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase { targetlowView = (EditText) layout.findViewById(R.id.simpleprofile_targetlow); targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh); profileswitchButton = (Button) layout.findViewById(R.id.simpleprofile_profileswitch); - test = new TimeListEdit(getContext(), layout, R.id.simpleprofile_test, "Test", data1, data2, new DecimalFormat("0.00")); onStatusEvent(null); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b147ca2fbc..9359821c78 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -395,4 +395,5 @@ OLD DATA %dmin ago %dmin ago + Local Profile