LocalProfilePlugin -> kotlin
This commit is contained in:
parent
c8c830f51c
commit
cf506a5439
5 changed files with 249 additions and 264 deletions
|
@ -189,7 +189,7 @@ public class MainApp extends Application {
|
||||||
if (Config.APS) pluginsList.add(OpenAPSSMBPlugin.getPlugin());
|
if (Config.APS) pluginsList.add(OpenAPSSMBPlugin.getPlugin());
|
||||||
pluginsList.add(NSProfilePlugin.getPlugin());
|
pluginsList.add(NSProfilePlugin.getPlugin());
|
||||||
if (Config.OTHERPROFILES) pluginsList.add(SimpleProfilePlugin.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());
|
pluginsList.add(TreatmentsPlugin.getPlugin());
|
||||||
if (Config.SAFETY) pluginsList.add(SafetyPlugin.getPlugin());
|
if (Config.SAFETY) pluginsList.add(SafetyPlugin.getPlugin());
|
||||||
if (Config.SAFETY) pluginsList.add(VersionCheckerPlugin.INSTANCE);
|
if (Config.SAFETY) pluginsList.add(VersionCheckerPlugin.INSTANCE);
|
||||||
|
|
|
@ -11,7 +11,6 @@ import androidx.fragment.app.Fragment
|
||||||
import info.nightscout.androidaps.MainApp
|
import info.nightscout.androidaps.MainApp
|
||||||
import info.nightscout.androidaps.R
|
import info.nightscout.androidaps.R
|
||||||
import info.nightscout.androidaps.events.EventInitializationChanged
|
import info.nightscout.androidaps.events.EventInitializationChanged
|
||||||
import info.nightscout.androidaps.interfaces.PumpDescription
|
|
||||||
import info.nightscout.androidaps.plugins.bus.RxBus
|
import info.nightscout.androidaps.plugins.bus.RxBus
|
||||||
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin
|
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin
|
||||||
import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment
|
import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment
|
||||||
|
@ -40,13 +39,13 @@ class LocalProfileFragment : Fragment() {
|
||||||
override fun afterTextChanged(s: Editable) {}
|
override fun afterTextChanged(s: Editable) {}
|
||||||
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
|
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
|
||||||
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: 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()
|
doEdit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sumLabel(): String {
|
private fun sumLabel(): String {
|
||||||
val profile = LocalProfilePlugin.getPlugin().createProfileStore().defaultProfile
|
val profile = LocalProfilePlugin.createProfileStore().defaultProfile
|
||||||
val sum = profile?.baseBasalSum() ?: 0.0
|
val sum = profile?.baseBasalSum() ?: 0.0
|
||||||
return " ∑" + DecimalFormatter.to2Decimal(sum) + MainApp.gs(R.string.insulin_unit_shortname)
|
return " ∑" + DecimalFormatter.to2Decimal(sum) + MainApp.gs(R.string.insulin_unit_shortname)
|
||||||
}
|
}
|
||||||
|
@ -60,30 +59,30 @@ class LocalProfileFragment : Fragment() {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
val pumpDescription = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription ?: return
|
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)
|
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.getPlugin().ic, null, 0.5, 50.0, 0.1, DecimalFormat("0.0"), save)
|
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.getPlugin().isf, null, 0.5, 500.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.getPlugin().basal, null, pumpDescription.basalMinimumRate, 10.0, 0.01, DecimalFormat("0.00"), 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.getPlugin().targetLow, LocalProfilePlugin.getPlugin().targetHigh, 3.0, 200.0, 0.1, DecimalFormat("0.0"), 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) {
|
if (!pumpDescription.isTempBasalCapable) {
|
||||||
localprofile_basal.visibility = View.GONE
|
localprofile_basal.visibility = View.GONE
|
||||||
}
|
}
|
||||||
|
|
||||||
localprofile_mgdl.isChecked = LocalProfilePlugin.getPlugin().mgdl
|
localprofile_mgdl.isChecked = LocalProfilePlugin.mgdl
|
||||||
localprofile_mmol.isChecked = LocalProfilePlugin.getPlugin().mmol
|
localprofile_mmol.isChecked = LocalProfilePlugin.mmol
|
||||||
|
|
||||||
localprofile_mgdl.setOnClickListener {
|
localprofile_mgdl.setOnClickListener {
|
||||||
LocalProfilePlugin.getPlugin().mgdl = localprofile_mgdl.isChecked
|
LocalProfilePlugin.mgdl = localprofile_mgdl.isChecked
|
||||||
LocalProfilePlugin.getPlugin().mmol = !LocalProfilePlugin.getPlugin().mgdl
|
LocalProfilePlugin.mmol = !LocalProfilePlugin.mgdl
|
||||||
localprofile_mmol.isChecked = LocalProfilePlugin.getPlugin().mmol
|
localprofile_mmol.isChecked = LocalProfilePlugin.mmol
|
||||||
doEdit()
|
doEdit()
|
||||||
}
|
}
|
||||||
localprofile_mmol.setOnClickListener {
|
localprofile_mmol.setOnClickListener {
|
||||||
LocalProfilePlugin.getPlugin().mmol = localprofile_mmol.isChecked
|
LocalProfilePlugin.mmol = localprofile_mmol.isChecked
|
||||||
LocalProfilePlugin.getPlugin().mgdl = !LocalProfilePlugin.getPlugin().mmol
|
LocalProfilePlugin.mgdl = !LocalProfilePlugin.mmol
|
||||||
localprofile_mgdl.isChecked = LocalProfilePlugin.getPlugin().mgdl
|
localprofile_mgdl.isChecked = LocalProfilePlugin.mgdl
|
||||||
doEdit()
|
doEdit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,22 +95,22 @@ class LocalProfileFragment : Fragment() {
|
||||||
}
|
}
|
||||||
|
|
||||||
localprofile_reset.setOnClickListener {
|
localprofile_reset.setOnClickListener {
|
||||||
LocalProfilePlugin.getPlugin().loadSettings()
|
LocalProfilePlugin.loadSettings()
|
||||||
localprofile_mgdl.isChecked = LocalProfilePlugin.getPlugin().mgdl
|
localprofile_mgdl.isChecked = LocalProfilePlugin.mgdl
|
||||||
localprofile_mmol.isChecked = LocalProfilePlugin.getPlugin().mmol
|
localprofile_mmol.isChecked = LocalProfilePlugin.mmol
|
||||||
localprofile_dia.setParams(LocalProfilePlugin.getPlugin().dia, MIN_DIA, 12.0, 0.1, DecimalFormat("0.0"), false, localprofile_save, textWatch)
|
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.getPlugin().ic, null, 0.5, 50.0, 0.1, DecimalFormat("0.0"), save)
|
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.getPlugin().isf, null, 0.5, 500.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.getPlugin().basal, null, pumpDescription.basalMinimumRate, 10.0, 0.01, DecimalFormat("0.00"), 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.getPlugin().targetLow, LocalProfilePlugin.getPlugin().targetHigh, 3.0, 200.0, 0.1, DecimalFormat("0.0"), 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()
|
updateGUI()
|
||||||
}
|
}
|
||||||
|
|
||||||
localprofile_save.setOnClickListener {
|
localprofile_save.setOnClickListener {
|
||||||
if (!LocalProfilePlugin.getPlugin().isValidEditState) {
|
if (!LocalProfilePlugin.isValidEditState()) {
|
||||||
return@setOnClickListener //Should not happen as saveButton should not be visible if not valid
|
return@setOnClickListener //Should not happen as saveButton should not be visible if not valid
|
||||||
}
|
}
|
||||||
LocalProfilePlugin.getPlugin().storeSettings()
|
LocalProfilePlugin.storeSettings()
|
||||||
updateGUI()
|
updateGUI()
|
||||||
}
|
}
|
||||||
updateGUI()
|
updateGUI()
|
||||||
|
@ -134,14 +133,14 @@ class LocalProfileFragment : Fragment() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun doEdit() {
|
fun doEdit() {
|
||||||
LocalProfilePlugin.getPlugin().isEdited = true
|
LocalProfilePlugin.isEdited = true
|
||||||
updateGUI()
|
updateGUI()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateGUI() {
|
fun updateGUI() {
|
||||||
if (invalidprofile == null) return
|
if (invalidprofile == null) return
|
||||||
val isValid = LocalProfilePlugin.getPlugin().isValidEditState
|
val isValid = LocalProfilePlugin.isValidEditState()
|
||||||
val isEdited = LocalProfilePlugin.getPlugin().isEdited
|
val isEdited = LocalProfilePlugin.isEdited
|
||||||
if (isValid) {
|
if (isValid) {
|
||||||
invalidprofile.visibility = View.GONE //show invalid profile
|
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
|
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) {
|
if (isEdited) {
|
||||||
localprofile_reset.visibility = View.VISIBLE
|
localprofile_reset.visibility = View.VISIBLE
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -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 ";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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 "
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -271,8 +271,8 @@ public class SWDefinition {
|
||||||
.skippable(false)
|
.skippable(false)
|
||||||
.add(new SWFragment(this)
|
.add(new SWFragment(this)
|
||||||
.add(new LocalProfileFragment()))
|
.add(new LocalProfileFragment()))
|
||||||
.validator(() -> LocalProfilePlugin.getPlugin().getProfile() != null && LocalProfilePlugin.getPlugin().getProfile().getDefaultProfile() != null && LocalProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid("StartupWizard"))
|
.validator(() -> LocalProfilePlugin.INSTANCE.getProfile() != null && LocalProfilePlugin.INSTANCE.getProfile().getDefaultProfile() != null && LocalProfilePlugin.INSTANCE.getProfile().getDefaultProfile().isValid("StartupWizard"))
|
||||||
.visibility(() -> LocalProfilePlugin.getPlugin().isEnabled(PluginType.PROFILE));
|
.visibility(() -> LocalProfilePlugin.INSTANCE.isEnabled(PluginType.PROFILE));
|
||||||
|
|
||||||
private SWScreen screenSimpleProfile = new SWScreen(R.string.simpleprofile)
|
private SWScreen screenSimpleProfile = new SWScreen(R.string.simpleprofile)
|
||||||
.skippable(false)
|
.skippable(false)
|
||||||
|
|
Loading…
Reference in a new issue