diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 94ccb45521..6eb3e03e98 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -18,6 +18,7 @@ import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.Actions.ActionsFragment; import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; +import info.nightscout.androidaps.plugins.CircadianPercentageProfile.CircadianPercentageProfileFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.DanaR.DanaRFragment; @@ -75,6 +76,7 @@ public class MainApp extends Application { if (Config.OPENAPSMAENABLED) pluginsList.add(OpenAPSMAFragment.getPlugin()); pluginsList.add(NSProfileViewerFragment.getPlugin()); pluginsList.add(SimpleProfileFragment.getPlugin()); + pluginsList.add(CircadianPercentageProfileFragment.getPlugin()); pluginsList.add(TreatmentsFragment.getPlugin()); pluginsList.add(TempBasalsFragment.getPlugin()); pluginsList.add(SafetyFragment.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfileFragment.java new file mode 100644 index 0000000000..0d518c0603 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfileFragment.java @@ -0,0 +1,341 @@ +package info.nightscout.androidaps.plugins.CircadianPercentageProfile; + + +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.Fragment; +import android.text.Editable; +import android.text.Html; +import android.text.TextWatcher; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.inputmethod.InputMethodManager; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.RadioButton; +import android.widget.TextView; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.interfaces.FragmentBase; +import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; +import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; +import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.SafeParse; +import info.nightscout.utils.ToastUtils; + +public class CircadianPercentageProfileFragment extends Fragment implements FragmentBase { + private static Logger log = LoggerFactory.getLogger(CircadianPercentageProfileFragment.class); + + private static CircadianPercentageProfilePlugin circadianPercentageProfilePlugin = new CircadianPercentageProfilePlugin(); + + public static CircadianPercentageProfilePlugin getPlugin() { + return circadianPercentageProfilePlugin; + } + + EditText diaView; + RadioButton mgdlView; + RadioButton mmolView; + EditText carView; + EditText targetlowView; + EditText targethighView; + EditText percentageView; + EditText timeshiftView; + TextView profileView; + TextView baseprofileIC; + TextView baseprofileBasal; + TextView baseprofileISF; + Button profileswitchButton; + ImageView percentageIcon; + ImageView timeIcon; + ImageView basaleditIcon; + ImageView iceditIcon; + ImageView isfeditIcon; + BasalEditDialog basalEditDialog; + + + + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View layout = inflater.inflate(R.layout.circadianpercentageprofile_fragment, container, false); + diaView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_dia); + mgdlView = (RadioButton) layout.findViewById(R.id.circadianpercentageprofile_mgdl); + mmolView = (RadioButton) layout.findViewById(R.id.circadianpercentageprofile_mmol); + carView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_car); + targetlowView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_targetlow); + targethighView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_targethigh); + percentageView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_percentage); + timeshiftView = (EditText) layout.findViewById(R.id.circadianpercentageprofile_timeshift); + profileView = (TextView) layout.findViewById(R.id.circadianpercentageprofile_profileview); + baseprofileBasal = (TextView) layout.findViewById(R.id.circadianpercentageprofile_baseprofilebasal); + baseprofileIC = (TextView) layout.findViewById(R.id.circadianpercentageprofile_baseprofileic); + baseprofileISF = (TextView) layout.findViewById(R.id.circadianpercentageprofile_baseprofileisf); + percentageIcon = (ImageView) layout.findViewById(R.id.circadianpercentageprofile_percentageicon); + timeIcon = (ImageView) layout.findViewById(R.id.circadianpercentageprofile_timeicon); + profileswitchButton = (Button) layout.findViewById(R.id.circadianpercentageprofile_profileswitch); + + basaleditIcon = (ImageView) layout.findViewById(R.id.circadianpercentageprofile_basaledit); + iceditIcon = (ImageView) layout.findViewById(R.id.circadianpercentageprofile_icedit); + isfeditIcon = (ImageView) layout.findViewById(R.id.circadianpercentageprofile_isfedit); + + + + mgdlView.setChecked(circadianPercentageProfilePlugin.mgdl); + mmolView.setChecked(circadianPercentageProfilePlugin.mmol); + diaView.setText(circadianPercentageProfilePlugin.dia.toString()); + carView.setText(circadianPercentageProfilePlugin.car.toString()); + targetlowView.setText(circadianPercentageProfilePlugin.targetLow.toString()); + targethighView.setText(circadianPercentageProfilePlugin.targetHigh.toString()); + percentageView.setText("" + circadianPercentageProfilePlugin.percentage); + timeshiftView.setText("" + circadianPercentageProfilePlugin.timeshift); + updateProfileInfo(); + + mgdlView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + circadianPercentageProfilePlugin.mgdl = mgdlView.isChecked(); + circadianPercentageProfilePlugin.mmol = !circadianPercentageProfilePlugin.mgdl; + mmolView.setChecked(circadianPercentageProfilePlugin.mmol); + circadianPercentageProfilePlugin.storeSettings(); + updateProfileInfo(); + } + }); + mmolView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + circadianPercentageProfilePlugin.mmol = mmolView.isChecked(); + circadianPercentageProfilePlugin.mgdl = !circadianPercentageProfilePlugin.mmol; + mgdlView.setChecked(circadianPercentageProfilePlugin.mgdl); + circadianPercentageProfilePlugin.storeSettings(); + updateProfileInfo(); + } + }); + + 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"); + } + }); + + timeshiftView.setOnFocusChangeListener(new View.OnFocusChangeListener() { + @Override + public void onFocusChange(View view, boolean b) { + if(b) + ToastUtils.showToastInUiThread(getContext(), getString(R.string.timeshift_hint)); + + } + }); + + percentageView.setOnFocusChangeListener(new View.OnFocusChangeListener() { + @Override + public void onFocusChange(View view, boolean b) { + if(b) + ToastUtils.showToastInUiThread(getContext(), getString(R.string.percentagefactor_hint)); + } + }); + + timeIcon.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + timeshiftView.requestFocusFromTouch(); + timeshiftView.setSelection(timeshiftView.getText().length()); + ((InputMethodManager) getContext() + .getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(timeshiftView, InputMethodManager.SHOW_IMPLICIT); + } + }); + + percentageIcon.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + percentageView.requestFocusFromTouch(); + percentageView.setSelection(percentageView.getText().length()); + ((InputMethodManager) getContext() + .getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(percentageView, InputMethodManager.SHOW_IMPLICIT); + } + }); + + basaleditIcon.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + basalEditDialog = new BasalEditDialog(); + basalEditDialog.setup(getPlugin().basebasal, getString(R.string.edit_base_basal), CircadianPercentageProfileFragment.this); + basalEditDialog.show(getFragmentManager(), getString(R.string.edit_base_basal)); + } + }); + + isfeditIcon.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + basalEditDialog = new BasalEditDialog(); + basalEditDialog.setup(getPlugin().baseisf, getString(R.string.edit_base_isf), CircadianPercentageProfileFragment.this); + basalEditDialog.show(getFragmentManager(), getString(R.string.edit_base_isf)); + } + }); + + iceditIcon.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + basalEditDialog = new BasalEditDialog(); + basalEditDialog.setup(getPlugin().baseic, getString(R.string.edit_base_ic), CircadianPercentageProfileFragment.this); + basalEditDialog.show(getFragmentManager(), getString(R.string.edit_base_ic)); + } + }); + + + 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) { + + if(SafeParse.stringToInt(percentageView.getText().toString()) == 0) { + circadianPercentageProfilePlugin.percentage = 100; + } else { + circadianPercentageProfilePlugin.percentage = SafeParse.stringToInt(percentageView.getText().toString()); + } + circadianPercentageProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString()); + circadianPercentageProfilePlugin.car = SafeParse.stringToDouble(carView.getText().toString()); + circadianPercentageProfilePlugin.targetLow = SafeParse.stringToDouble(targetlowView.getText().toString()); + circadianPercentageProfilePlugin.targetHigh = SafeParse.stringToDouble(targethighView.getText().toString()); + circadianPercentageProfilePlugin.timeshift = SafeParse.stringToInt(timeshiftView.getText().toString()); + circadianPercentageProfilePlugin.storeSettings(); + updateProfileInfo(); + } + }; + + diaView.addTextChangedListener(textWatch); + carView.addTextChangedListener(textWatch); + targetlowView.addTextChangedListener(textWatch); + targethighView.addTextChangedListener(textWatch); + percentageView.addTextChangedListener(textWatch); + timeshiftView.addTextChangedListener(textWatch); + + return layout; + } + + private void updateProfileInfo() { + StringBuilder sb = new StringBuilder(); + sb.append("

"); + sb.append(getString(R.string.nsprofileview_activeprofile_label)); + sb.append("

"); + sb.append("

"); + sb.append(getString(R.string.nsprofileview_basal_label)); + sb.append("

" + circadianPercentageProfilePlugin.basalString()); + sb.append("

"); + sb.append(getString(R.string.nsprofileview_ic_label)); + sb.append("

" + circadianPercentageProfilePlugin.icString()); + sb.append("

"); + sb.append(getString(R.string.nsprofileview_isf_label)); + sb.append("

" + circadianPercentageProfilePlugin.isfString()); + profileView.setText(Html.fromHtml(sb.toString())); + + baseprofileBasal.setText(Html.fromHtml("

" + getString(R.string.base_profile_label) + "

" + + "

" + getString(R.string.nsprofileview_basal_label) + "

" + circadianPercentageProfilePlugin.baseBasalString())); + baseprofileIC.setText(Html.fromHtml("

" + getString(R.string.nsprofileview_ic_label) + "

" + circadianPercentageProfilePlugin.baseIcString())); + baseprofileISF.setText(Html.fromHtml("

" + getString(R.string.nsprofileview_isf_label) + "

" + circadianPercentageProfilePlugin.baseIsfString())); + } + + @Override + public void onStop(){ + super.onStop(); + if(basalEditDialog != null && basalEditDialog.isVisible()){ + basalEditDialog.dismiss(); + } + basalEditDialog = null; + } + + public static class BasalEditDialog extends DialogFragment{ + + private double[] values; + private String title; + private CircadianPercentageProfileFragment fragment; + + public void setup(double[] values, String title, CircadianPercentageProfileFragment fragment){ + this.values = values; + this.title = title; + this.fragment = fragment; + } + + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + getDialog().setTitle(title); + View view = inflater.inflate(R.layout.circadianpercentageprofile_editbasal_dialog, container, false); + LinearLayout list = (LinearLayout) view.findViewById(R.id.circadianpp_editbasal_listlayout); + final EditText[] editTexts = new EditText[24]; + for (int i = 0; i < 24; i++) { + View childview = inflater.inflate(R.layout.circadianpercentageprofile_listelement, container, false); + ((TextView)childview.findViewById(R.id.basal_time_elem)).setText((i<10?"0":"") + i + ":00: "); + + ImageView copyprevbutton = (ImageView)childview.findViewById(R.id.basal_copyprev_elem); + + if(i==0){ + copyprevbutton.setVisibility(View.INVISIBLE);; + } else { + final int j = i; //needs to be final to be passed to inner class. + copyprevbutton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + editTexts[j].setText(editTexts[j-1].getText()); + } + }); + } + + editTexts[i] = ((EditText) childview.findViewById(R.id.basal_edittext_elem)); + editTexts[i].setText(DecimalFormatter.to2Decimal(values[i])); + list.addView(childview); + } + getDialog().setCancelable(true); + + view.findViewById(R.id.ok_button).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + for (int i = 0; i < 24; i++) { + if (editTexts[i].getText().length()!= 0){ + values[i]= SafeParse.stringToDouble(editTexts[i].getText().toString()) ; + } + } + fragment.updateProfileInfo(); + getPlugin().storeSettings(); + dismiss(); + } + }); + + view.findViewById(R.id.cancel_action).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + dismiss(); + } + }); + + return view; + } + + + + + +} + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfilePlugin.java new file mode 100644 index 0000000000..2caa7c0639 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/CircadianPercentageProfile/CircadianPercentageProfilePlugin.java @@ -0,0 +1,325 @@ +package info.nightscout.androidaps.plugins.CircadianPercentageProfile; + +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.OpenAPSMA.OpenAPSMAPlugin; +import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.SafeParse; +import info.nightscout.utils.ToastUtils; + +/** + * Created by Adrian on 12.11.2016. + * Based on SimpleProfile created by mike on 05.08.2016. + */ +public class CircadianPercentageProfilePlugin implements PluginBase, ProfileInterface { + public static final String SETTINGS_PREFIX = "CircadianPercentageProfile"; + public static final int MIN_PERCENTAGE = 50; + public static final int MAX_PERCENTAGE = 200; + private static Logger log = LoggerFactory.getLogger(CircadianPercentageProfilePlugin.class); + + private static boolean fragmentEnabled = true; + private static boolean fragmentVisible = true; + + private static NSProfile convertedProfile = null; + + boolean mgdl; + boolean mmol; + Double dia; + Double car; + Double targetLow; + Double targetHigh; + int percentage; + int timeshift; + double[] basebasal = new double[]{1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d, 1d}; + double[] baseisf = new double[]{35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d, 35d}; + double[] baseic = new double[]{4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d, 4d}; + + public CircadianPercentageProfilePlugin() { + loadSettings(); + } + + @Override + public String getFragmentClass() { + return CircadianPercentageProfileFragment.class.getName(); + } + + @Override + public int getType() { + return PluginBase.PROFILE; + } + + @Override + public String getName() { + return MainApp.instance().getString(R.string.circadian_percentage_profile); + } + + @Override + public boolean isEnabled(int type) { + return fragmentEnabled; + } + + @Override + public boolean isVisibleInTabs(int type) { + return fragmentVisible; + } + + @Override + public boolean canBeHidden(int type) { + return true; + } + + @Override + public void setFragmentEnabled(int type, boolean fragmentEnabled) { + this.fragmentEnabled = fragmentEnabled; + } + + @Override + public void setFragmentVisible(int type, boolean fragmentVisible) { + this.fragmentVisible = fragmentVisible; + } + + void storeSettings() { + if (Config.logPrefsChange) + log.debug("Storing settings"); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + SharedPreferences.Editor editor = settings.edit(); + editor.putBoolean(SETTINGS_PREFIX + "mmol", mmol); + editor.putBoolean(SETTINGS_PREFIX + "mgdl", mgdl); + editor.putString(SETTINGS_PREFIX + "dia", dia.toString()); + editor.putString(SETTINGS_PREFIX + "car", car.toString()); + editor.putString(SETTINGS_PREFIX + "targetlow", targetLow.toString()); + editor.putString(SETTINGS_PREFIX + "targethigh", targetHigh.toString()); + editor.putString(SETTINGS_PREFIX + "timeshift", timeshift + ""); + editor.putString(SETTINGS_PREFIX + "percentage", percentage + ""); + + + for (int i = 0; i < 24; i++) { + editor.putString(SETTINGS_PREFIX + "basebasal" + i, DecimalFormatter.to2Decimal(basebasal[i])); + editor.putString(SETTINGS_PREFIX + "baseisf" + i, DecimalFormatter.to2Decimal(baseisf[i])); + editor.putString(SETTINGS_PREFIX + "baseic" + i, DecimalFormatter.to2Decimal(baseic[i])); + } + editor.commit(); + createConvertedProfile(); + } + + void loadSettings() { + if (Config.logPrefsChange) + log.debug("Loading stored settings"); + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + + if (settings.contains(SETTINGS_PREFIX + "mgdl")) + try { + mgdl = settings.getBoolean(SETTINGS_PREFIX + "mgdl", true); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else mgdl = true; + if (settings.contains(SETTINGS_PREFIX + "mmol")) + try { + mmol = settings.getBoolean(SETTINGS_PREFIX + "mmol", false); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else mmol = false; + if (settings.contains(SETTINGS_PREFIX + "dia")) + try { + dia = SafeParse.stringToDouble(settings.getString(SETTINGS_PREFIX + "dia", "3")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else dia = 3d; + if (settings.contains(SETTINGS_PREFIX + "car")) + try { + car = SafeParse.stringToDouble(settings.getString(SETTINGS_PREFIX + "car", "20")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else car = 20d; + if (settings.contains(SETTINGS_PREFIX + "targetlow")) + try { + targetLow = SafeParse.stringToDouble(settings.getString(SETTINGS_PREFIX + "targetlow", "80")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else targetLow = 80d; + if (settings.contains(SETTINGS_PREFIX + "targethigh")) + try { + targetHigh = SafeParse.stringToDouble(settings.getString(SETTINGS_PREFIX + "targethigh", "120")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else targetHigh = 120d; + if (settings.contains(SETTINGS_PREFIX + "percentage")) + try { + percentage = SafeParse.stringToInt(settings.getString(SETTINGS_PREFIX + "percentage", "100")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else percentage = 100; + + if (settings.contains(SETTINGS_PREFIX + "timeshift")) + try { + timeshift = SafeParse.stringToInt(settings.getString(SETTINGS_PREFIX + "timeshift", "0")); + } catch (Exception e) { + log.debug(e.getMessage()); + } + else timeshift = 0; + + for (int i = 0; i < 24; i++) { + try { + basebasal[i] = SafeParse.stringToDouble(settings.getString(SETTINGS_PREFIX + "basebasal" + i, DecimalFormatter.to2Decimal(basebasal[i]))); + } catch (Exception e) { + log.debug(e.getMessage()); + } + try { + baseic[i] = SafeParse.stringToDouble(settings.getString(SETTINGS_PREFIX + "baseic" + i, DecimalFormatter.to2Decimal(baseic[i]))); + } catch (Exception e) { + log.debug(e.getMessage()); + } + try { + baseisf[i] = SafeParse.stringToDouble(settings.getString(SETTINGS_PREFIX + "baseisf" + i, DecimalFormatter.to2Decimal(baseisf[i]))); + } catch (Exception e) { + log.debug(e.getMessage()); + } + } + + + createConvertedProfile(); + } + + private void createConvertedProfile() { + JSONObject json = new JSONObject(); + JSONObject store = new JSONObject(); + JSONObject profile = new JSONObject(); + + StringBuilder stringBuilder = new StringBuilder(); + double sum = 0d; + for (int i = 0; i < 24; i++) { + sum += basebasal[i]; + } + stringBuilder.append(DecimalFormatter.to2Decimal(sum)); + stringBuilder.append("U@"); + stringBuilder.append(percentage); + stringBuilder.append("%>"); + stringBuilder.append(timeshift); + stringBuilder.append("h"); + String profileName = stringBuilder.toString(); + + try { + json.put("defaultProfile", profileName); + json.put("store", store); + profile.put("dia", dia); + + int offset = -(timeshift % 24) + 24; + + JSONArray icArray = new JSONArray(); + for (int i = 0; i < 24; i++) { + icArray.put(new JSONObject().put("timeAsSeconds", i * 60 * 60).put("value", baseic[(offset + i) % 24] * 100d / percentage)); + } + profile.put("carbratio", icArray); + + profile.put("carbs_hr", car); + + JSONArray isfArray = new JSONArray(); + for (int i = 0; i < 24; i++) { + isfArray.put(new JSONObject().put("timeAsSeconds", i * 60 * 60).put("value", baseisf[(offset + i) % 24] * 100d / percentage)); + } + profile.put("sens", isfArray); + + JSONArray basalArray = new JSONArray(); + for (int i = 0; i < 24; i++) { + basalArray.put(new JSONObject().put("timeAsSeconds", i * 60 * 60).put("value", basebasal[(offset + i) % 24] * percentage / 100d)); + } + profile.put("basal", basalArray); + + + 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(profileName, profile); + } catch (JSONException e) { + e.printStackTrace(); + } + convertedProfile = new NSProfile(json, profileName); + } + + @Override + public NSProfile getProfile() { + + performLimitCheck(); + + return convertedProfile; + } + + private void performLimitCheck() { + if (percentage < MIN_PERCENTAGE || percentage > MAX_PERCENTAGE){ + String msg = String.format(MainApp.sResources.getString(R.string.openapsma_valueoutofrange), "Profile-Percentage"); + log.error(msg); + OpenAPSMAPlugin.sendErrorToNSClient(msg); + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), msg, R.raw.error); + percentage = Math.max(percentage, MIN_PERCENTAGE); + percentage = Math.min(percentage, MAX_PERCENTAGE); + } + } + + String basalString() { + return profileString(basebasal, timeshift, percentage, true); + } + + String icString() { + return profileString(baseic, timeshift, percentage, false); + } + + String isfString() { + return profileString(baseisf, timeshift, percentage, false); + } + + String baseIcString() { + return profileString(baseic, 0, 100, false); + } + + String baseIsfString() { + return profileString(baseisf, 0, 100, false); + } + + String baseBasalString() {return profileString(basebasal, 0, 100, true);} + + + private static String profileString(double[] values, int timeshift, int percentage, boolean inc) { + timeshift = -(timeshift % 24) + 24; + StringBuilder sb = new StringBuilder(); + sb.append(""); + sb.append(0); + sb.append("h: "); + sb.append(""); + sb.append(DecimalFormatter.to2Decimal(values[(timeshift + 0) % 24] * (inc ? percentage / 100d : 100d / percentage))); + double prevVal = values[(timeshift + 0) % 24]; + for (int i = 1; i < 24; i++) { + if (prevVal != values[(timeshift + i) % 24]) { + sb.append(", "); + sb.append(""); + sb.append(i); + sb.append("h: "); + sb.append(""); + sb.append(DecimalFormatter.to2Decimal(values[(timeshift + i) % 24] * (inc ? percentage / 100d : 100d / percentage))); + prevVal = values[(timeshift + i) % 24]; + } + } + return sb.toString(); + } + +} diff --git a/app/src/main/res/layout/circadianpercentageprofile_editbasal_dialog.xml b/app/src/main/res/layout/circadianpercentageprofile_editbasal_dialog.xml new file mode 100644 index 0000000000..ac703d6536 --- /dev/null +++ b/app/src/main/res/layout/circadianpercentageprofile_editbasal_dialog.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + +