From 5c9ab721ce10b81e2b21848ab1214acecbb930b7 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 19 Dec 2019 20:30:35 +0100 Subject: [PATCH] Careportal actions to Actions plugin --- .../general/actions/ActionsFragment.kt | 49 ++-- .../plugins/general/actions/ActionsPlugin.kt | 3 + .../general/actions/dialogs/CareDialog.kt | 143 +++++++++ .../general/careportal/CareportalPlugin.java | 9 +- .../Dialogs/NewNSTreatmentDialog.java | 17 +- .../main/res/layout/actions_care_dialog.xml | 140 +++++++++ app/src/main/res/layout/actions_fragment.xml | 272 ++++++++++++------ .../main/res/layout/careportal_fragment.xml | 1 - app/src/main/res/values/strings.xml | 1 + 9 files changed, 524 insertions(+), 111 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/actions/dialogs/CareDialog.kt create mode 100644 app/src/main/res/layout/actions_care_dialog.xml diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.kt index f30c0178ad..24c05db48d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.kt @@ -19,9 +19,11 @@ 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.actions.defs.CustomAction +import info.nightscout.androidaps.plugins.general.actions.dialogs.CareDialog import info.nightscout.androidaps.plugins.general.actions.dialogs.FillDialog import info.nightscout.androidaps.plugins.general.actions.dialogs.NewExtendedBolusDialog import info.nightscout.androidaps.plugins.general.actions.dialogs.NewTempBasalDialog +import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment import info.nightscout.androidaps.plugins.general.overview.dialogs.ProfileSwitchDialog import info.nightscout.androidaps.plugins.general.overview.dialogs.TempTargetDialog import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin @@ -30,6 +32,7 @@ import info.nightscout.androidaps.utils.* import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import kotlinx.android.synthetic.main.actions_fragment.* +import kotlinx.android.synthetic.main.careportal_stats_fragment.* import java.util.* class ActionsFragment : Fragment() { @@ -80,6 +83,15 @@ class ActionsFragment : Fragment() { actions_fill.setOnClickListener { fragmentManager?.let { FillDialog().show(it, "FillDialog") } } actions_historybrowser.setOnClickListener { startActivity(Intent(context, HistoryBrowseActivity::class.java)) } actions_tddstats.setOnClickListener { startActivity(Intent(context, TDDStatsActivity::class.java)) } + actions_bgcheck.setOnClickListener { + fragmentManager?.let { CareDialog().setOptions(CareDialog.EventType.BGCHECK, R.string.careportal_bgcheck).show(it, "Actions") } + } + actions_cgmsensorinsert.setOnClickListener { + fragmentManager?.let { CareDialog().setOptions(CareDialog.EventType.SENSOR_INSERT, R.string.careportal_cgmsensorinsert).show(it, "Actions") } + } + actions_pumpbatterychange.setOnClickListener { + fragmentManager?.let { CareDialog().setOptions(CareDialog.EventType.BATTERY_CHANGE, R.string.careportal_pumpbatterychange).show(it, "Actions") } + } SP.putBoolean(R.string.key_objectiveuseactions, true) } @@ -90,43 +102,27 @@ class ActionsFragment : Fragment() { disposable += RxBus .toObservable(EventInitializationChanged::class.java) .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ - updateGui() - }, { - FabricPrivacy.logException(it) - }) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) disposable += RxBus .toObservable(EventRefreshOverview::class.java) .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ - updateGui() - }, { - FabricPrivacy.logException(it) - }) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) disposable += RxBus .toObservable(EventExtendedBolusChange::class.java) .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ - updateGui() - }, { - FabricPrivacy.logException(it) - }) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) disposable += RxBus .toObservable(EventTempBasalChange::class.java) .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ - updateGui() - }, { - FabricPrivacy.logException(it) - }) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) disposable += RxBus .toObservable(EventCustomActionsChanged::class.java) .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ - updateGui() - }, { - FabricPrivacy.logException(it) - }) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) + disposable += RxBus + .toObservable(EventCareportalEventChange::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) updateGui() } @@ -193,6 +189,9 @@ class ActionsFragment : Fragment() { actions_temptarget?.visibility = if (!Config.APS) View.GONE else View.VISIBLE actions_tddstats?.visibility = if (!pump.pumpDescription.supportsTDDs) View.GONE else View.VISIBLE + activity?.let { activity -> + CareportalFragment.updateAge(activity, careportal_sensorage, careportal_insulinage, careportal_canulaage, careportal_pbage) + } checkPumpCustomActions() } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsPlugin.kt index 04afb826a2..e534fefa26 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsPlugin.kt @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.actions +import info.nightscout.androidaps.Config import info.nightscout.androidaps.R import info.nightscout.androidaps.interfaces.PluginBase import info.nightscout.androidaps.interfaces.PluginDescription @@ -8,6 +9,8 @@ import info.nightscout.androidaps.interfaces.PluginType object ActionsPlugin : PluginBase(PluginDescription() .mainType(PluginType.GENERAL) .fragmentClass(ActionsFragment::class.qualifiedName) + .enableByDefault(Config.APS || Config.PUMPCONTROL) + .visibleByDefault(Config.APS || Config.PUMPCONTROL) .pluginName(R.string.actions) .shortName(R.string.actions_shortname) .description(R.string.description_actions)) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/dialogs/CareDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/dialogs/CareDialog.kt new file mode 100644 index 0000000000..27651710ac --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/dialogs/CareDialog.kt @@ -0,0 +1,143 @@ +package info.nightscout.androidaps.plugins.general.actions.dialogs + +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.annotation.StringRes +import com.google.common.base.Joiner +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.db.CareportalEvent +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.general.nsclient.NSUpload +import info.nightscout.androidaps.plugins.general.overview.dialogs.DialogFragmentWithDate +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus +import info.nightscout.androidaps.utils.* +import kotlinx.android.synthetic.main.actions_care_dialog.* +import kotlinx.android.synthetic.main.okcancel.* +import org.json.JSONObject +import java.text.DecimalFormat +import java.util.* + +class CareDialog : DialogFragmentWithDate() { + + enum class EventType { + BGCHECK, + SENSOR_INSERT, + BATTERY_CHANGE + } + + private var options: EventType = EventType.BGCHECK + @StringRes + private var event: Int = R.string.none + + fun setOptions(options: EventType, @StringRes event: Int): CareDialog { + this.options = options + this.event = event + return this + } + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putDouble("actions_care_bg", actions_care_bg.value) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + onCreateView() + return inflater.inflate(R.layout.actions_care_dialog, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + actions_care_icon.setImageResource(when (options) { + EventType.BGCHECK -> R.drawable.icon_cp_bgcheck + EventType.SENSOR_INSERT -> R.drawable.icon_cp_cgm_insert + EventType.BATTERY_CHANGE -> R.drawable.icon_cp_pump_battery + }) + actions_care_title.text = MainApp.gs(when (options) { + EventType.BGCHECK -> R.string.careportal_bgcheck + EventType.SENSOR_INSERT -> R.string.careportal_cgmsensorinsert + EventType.BATTERY_CHANGE -> R.string.careportal_pumpbatterychange + }) + + when (options) { + EventType.SENSOR_INSERT, + EventType.BATTERY_CHANGE -> { + action_care_bg_layout.visibility = View.GONE + actions_care_bgsource.visibility = View.GONE + } + else -> { + } + } + + val bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData()?.glucose + ?: 0.0, ProfileFunctions.getSystemUnits()) + val bgTextWatcher: TextWatcher = 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) { + if (actions_care_sensor.isChecked) actions_care_meter.isChecked = true + } + } + + if (ProfileFunctions.getSystemUnits() == Constants.MMOL) + actions_care_bg.setParams(savedInstanceState?.getDouble("actions_care_bg") + ?: bg, 0.0, 30.0, 0.1, DecimalFormat("0.0"), false, ok, bgTextWatcher) + else + actions_care_bg.setParams(savedInstanceState?.getDouble("actions_care_bg") + ?: bg, 0.0, 500.0, 1.0, DecimalFormat("0"), false, ok, bgTextWatcher) + } + + override fun submit() { + val enteredBy = SP.getString("careportal_enteredby", "") + val unitResId = if (ProfileFunctions.getSystemUnits() == Constants.MGDL) R.string.mgdl else R.string.mmol + + val json = JSONObject() + val actions: LinkedList = LinkedList() + actions.add("" + MainApp.gs(event) + "") + if (options == EventType.BGCHECK) { + val type = + when { + actions_care_meter.isChecked -> "Finger" + actions_care_sensor.isChecked -> "Sensor" + else -> "Manual" + } + actions.add(MainApp.gs(R.string.careportal_newnstreatment_glucosetype) + ": " + Translator.translate(type)) + actions.add(MainApp.gs(R.string.treatments_wizard_bg_label) + ": " + Profile.toCurrentUnitsString(actions_care_bg.value) + " " + MainApp.gs(unitResId)) + json.put("glucose", actions_care_bg.value) + json.put("glucoseType", type) + } + val notes = actions_care_notes.text.toString() + if (notes.isNotEmpty()) { + actions.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes) + json.put("notes", notes) + } + if (eventTimeChanged) { + actions.add(MainApp.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(eventTime)) + } + + json.put("created_at", DateUtil.toISOString(eventTime)) + json.put("eventType", when (options) { + EventType.BGCHECK -> CareportalEvent.BGCHECK + EventType.SENSOR_INSERT -> CareportalEvent.SENSORCHANGE + EventType.BATTERY_CHANGE -> CareportalEvent.PUMPBATTERYCHANGE + }) + json.put("units", ProfileFunctions.getSystemUnits()) + if (enteredBy.isNotEmpty()) + json.put("enteredBy", enteredBy) + + activity?.let { activity -> + OKDialog.showConfirmation(activity, HtmlHelper.fromHtml(Joiner.on("
").join(actions))) { + MainApp.getDbHelper().createCareportalEventFromJsonIfNotExists(json) + NSUpload.uploadCareportalEntryToNS(json) + } + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalPlugin.java index fa8f87ef46..fc28d23188 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalPlugin.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.careportal; +import info.nightscout.androidaps.Config; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; @@ -22,10 +23,14 @@ public class CareportalPlugin extends PluginBase { .fragmentClass(CareportalFragment.class.getName()) .pluginName(R.string.careportal) .shortName(R.string.careportal_shortname) - .visibleByDefault(true) - .enableByDefault(true) + .visibleByDefault(Config.NSCLIENT) + .enableByDefault(Config.NSCLIENT) .description(R.string.description_careportal) ); } + @Override + public boolean specialEnableCondition() { + return Config.NSCLIENT; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/Dialogs/NewNSTreatmentDialog.java index 1fb0a68037..eedaf22b0b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/Dialogs/NewNSTreatmentDialog.java @@ -9,6 +9,8 @@ import android.text.format.DateFormat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; @@ -103,9 +105,10 @@ public class NewNSTreatmentDialog extends AppCompatDialogFragment implements Vie private static Integer seconds = null; - public void setOptions(OptionsToShow options, int event) { + public NewNSTreatmentDialog setOptions(OptionsToShow options, int event) { this.options = options; this.event = MainApp.gs(event); + return this; } public NewNSTreatmentDialog() { @@ -120,7 +123,10 @@ public class NewNSTreatmentDialog extends AppCompatDialogFragment implements Vie public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (options == null) return null; - getDialog().setTitle(MainApp.gs(options.eventName)); + getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); + getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); + setCancelable(true); + getDialog().setCanceledOnTouchOutside(false); setStyle(DialogFragment.STYLE_NORMAL, getTheme()); View view = inflater.inflate(R.layout.careportal_newnstreatment_dialog, container, false); @@ -392,6 +398,13 @@ public class NewNSTreatmentDialog extends AppCompatDialogFragment implements Vie return view; } + @Override + public void onResume() { + super.onResume(); + getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); + + } + @Override public void onClick(View view) { Calendar calendar = Calendar.getInstance(); diff --git a/app/src/main/res/layout/actions_care_dialog.xml b/app/src/main/res/layout/actions_care_dialog.xml new file mode 100644 index 0000000000..9fb33578a9 --- /dev/null +++ b/app/src/main/res/layout/actions_care_dialog.xml @@ -0,0 +1,140 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/actions_fragment.xml b/app/src/main/res/layout/actions_fragment.xml index 3e809b3b02..f9796c6d7e 100644 --- a/app/src/main/res/layout/actions_fragment.xml +++ b/app/src/main/res/layout/actions_fragment.xml @@ -1,138 +1,248 @@ - - + android:layout_height="wrap_content" + android:orientation="vertical"> - + android:layout_marginTop="15dp" + android:paddingStart="15dp" + android:paddingEnd="15dp" + android:text="@string/actions" /> + + + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/careportal_profileswitch" + + app:layout_column="0" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="0" /> + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/careportal_temporarytarget" + + app:layout_column="1" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="0" /> + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/overview_tempbasal_button" + + app:layout_column="0" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="1" /> + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/canceltemp" + android:visibility="gone" + + app:layout_column="0" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="1" /> + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/overview_extendedbolus_button" + + app:layout_column="1" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="1" /> + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/overview_extendedbolus_cancel_button" + android:visibility="gone" + + app:layout_column="1" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="1" /> + + + + + + + + + + + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/primefill" + + app:layout_column="1" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="2" /> + + + + + + + + + + + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/nav_historybrowser" + app:layout_column="0" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="4" /> + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/danar_stats_tdd" - + app:layout_column="1" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="4" /> - + - + + + diff --git a/app/src/main/res/layout/careportal_fragment.xml b/app/src/main/res/layout/careportal_fragment.xml index 66f79ef688..7f642c6fab 100644 --- a/app/src/main/res/layout/careportal_fragment.xml +++ b/app/src/main/res/layout/careportal_fragment.xml @@ -48,7 +48,6 @@ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 045d44f772..acb7a439b5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1683,5 +1683,6 @@ Random BG Generate random BG data (Demo mode only) BG + Tools