From 3a2d4869769a9b6a098b6807267c9e6686bd1fce Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 3 Apr 2020 21:47:23 +0200 Subject: [PATCH] Move Overview menus to separate class --- .../historyBrowser/HistoryBrowseActivity.java | 132 +----- .../general/overview/OverviewFragment.java | 409 +---------------- .../plugins/general/overview/OverviewMenus.kt | 433 ++++++++++++++++++ 3 files changed, 467 insertions(+), 507 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewMenus.kt diff --git a/app/src/main/java/info/nightscout/androidaps/historyBrowser/HistoryBrowseActivity.java b/app/src/main/java/info/nightscout/androidaps/historyBrowser/HistoryBrowseActivity.java index 6a47e3749b..4ed1c3b8b7 100644 --- a/app/src/main/java/info/nightscout/androidaps/historyBrowser/HistoryBrowseActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/historyBrowser/HistoryBrowseActivity.java @@ -28,6 +28,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.activities.NoSplashAppCompatActivity; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.events.EventCustomCalculationFinished; +import info.nightscout.androidaps.events.EventRefreshOverview; import info.nightscout.androidaps.interfaces.ActivePluginProvider; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.AAPSLogger; @@ -35,6 +36,7 @@ import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.bus.RxBusWrapper; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction; import info.nightscout.androidaps.plugins.general.overview.OverviewFragment; +import info.nightscout.androidaps.plugins.general.overview.OverviewMenus; import info.nightscout.androidaps.plugins.general.overview.graphData.GraphData; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventIobCalculationProgress; @@ -60,6 +62,7 @@ public class HistoryBrowseActivity extends NoSplashAppCompatActivity { @Inject ActivePluginProvider activePlugin; @Inject BuildHelper buildHelper; @Inject FabricPrivacy fabricPrivacy; + @Inject OverviewMenus overviewMenus; private CompositeDisposable disposable = new CompositeDisposable(); @@ -171,7 +174,7 @@ public class HistoryBrowseActivity extends NoSplashAppCompatActivity { iobGraph.getGridLabelRenderer().setLabelVerticalWidth(50); iobGraph.getGridLabelRenderer().setNumVerticalLabels(5); - setupChartMenu(); + overviewMenus.setupChartMenu(findViewById(R.id.overview_chartMenuButton)); } @Override @@ -194,7 +197,7 @@ public class HistoryBrowseActivity extends NoSplashAppCompatActivity { updateGUI("EventAutosensCalculationFinished"); } } - }, exception -> fabricPrivacy.logException(exception)) + }, fabricPrivacy::logException) ); disposable.add(rxBus .toObservable(EventIobCalculationProgress.class) @@ -204,6 +207,11 @@ public class HistoryBrowseActivity extends NoSplashAppCompatActivity { iobCalculationProgressView.setText(event.getProgress()); }, exception -> fabricPrivacy.logException(exception)) ); + disposable.add(rxBus + .toObservable(EventRefreshOverview.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGUI("EventRefreshOverview") , fabricPrivacy::logException) + ); // set start of current day Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); @@ -367,124 +375,4 @@ public class HistoryBrowseActivity extends NoSplashAppCompatActivity { }); }).start(); } - - private void setupChartMenu() { - chartButton = findViewById(R.id.overview_chartMenuButton); - chartButton.setOnClickListener(v -> { - MenuItem item, dividerItem; - CharSequence title; - int titleMaxChars = 0; - SpannableString s; - PopupMenu popup = new PopupMenu(v.getContext(), v); - - - item = popup.getMenu().add(Menu.NONE, OverviewFragment.CHARTTYPE.BAS.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_basals)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.basal, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(showBasal); - - item = popup.getMenu().add(Menu.NONE, OverviewFragment.CHARTTYPE.ACTPRIM.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_activity)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.activity, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(showActPrim); - - dividerItem = popup.getMenu().add(""); - dividerItem.setEnabled(false); - - item = popup.getMenu().add(Menu.NONE, OverviewFragment.CHARTTYPE.IOB.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_iob)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.iob, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(showIob); - - item = popup.getMenu().add(Menu.NONE, OverviewFragment.CHARTTYPE.COB.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_cob)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.cob, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(showCob); - - item = popup.getMenu().add(Menu.NONE, OverviewFragment.CHARTTYPE.DEV.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_deviations)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.deviations, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(showDev); - - item = popup.getMenu().add(Menu.NONE, OverviewFragment.CHARTTYPE.SEN.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_sensitivity)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.ratio, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(showRat); - - item = popup.getMenu().add(Menu.NONE, OverviewFragment.CHARTTYPE.ACTSEC.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_activity)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.activity, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(showActSec); - - - if (buildHelper.isDev()) { - item = popup.getMenu().add(Menu.NONE, OverviewFragment.CHARTTYPE.DEVSLOPE.ordinal(), Menu.NONE, "Deviation slope"); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.devslopepos, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(showDevslope); - } - - // Fairly good guestimate for required divider text size... - title = new String(new char[titleMaxChars + 10]).replace("\0", "_"); - dividerItem.setTitle(title); - - popup.setOnMenuItemClickListener(item1 -> { - if (item1.getItemId() == OverviewFragment.CHARTTYPE.BAS.ordinal()) { - sp.putBoolean("hist_showbasals", !item1.isChecked()); - } else if (item1.getItemId() == OverviewFragment.CHARTTYPE.IOB.ordinal()) { - sp.putBoolean("hist_showiob", !item1.isChecked()); - } else if (item1.getItemId() == OverviewFragment.CHARTTYPE.COB.ordinal()) { - sp.putBoolean("hist_showcob", !item1.isChecked()); - } else if (item1.getItemId() == OverviewFragment.CHARTTYPE.DEV.ordinal()) { - sp.putBoolean("hist_showdeviations", !item1.isChecked()); - } else if (item1.getItemId() == OverviewFragment.CHARTTYPE.SEN.ordinal()) { - sp.putBoolean("hist_showratios", !item1.isChecked()); - } else if (item1.getItemId() == OverviewFragment.CHARTTYPE.ACTPRIM.ordinal()) { - sp.putBoolean("hist_showactivityprimary", !item1.isChecked()); - } else if (item1.getItemId() == OverviewFragment.CHARTTYPE.ACTSEC.ordinal()) { - sp.putBoolean("hist_showactivitysecondary", !item1.isChecked()); - } else if (item1.getItemId() == OverviewFragment.CHARTTYPE.DEVSLOPE.ordinal()) { - sp.putBoolean("hist_showdevslope", !item1.isChecked()); - } - updateGUI("onGraphCheckboxesCheckedChanged"); - return true; - }); - chartButton.setImageResource(R.drawable.ic_arrow_drop_up_white_24dp); - popup.setOnDismissListener(menu -> chartButton.setImageResource(R.drawable.ic_arrow_drop_down_white_24dp)); - popup.show(); - }); - } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java index a3d86716bb..d7bb8e35a5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java @@ -10,13 +10,10 @@ import android.content.pm.PackageManager; import android.graphics.Paint; import android.os.Bundle; import android.os.Handler; -import android.text.SpannableString; -import android.text.style.ForegroundColorSpan; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.ContextMenu; import android.view.LayoutInflater; -import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; @@ -25,9 +22,7 @@ import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.NonNull; -import androidx.appcompat.widget.PopupMenu; import androidx.constraintlayout.widget.ConstraintLayout; -import androidx.core.content.res.ResourcesCompat; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import androidx.recyclerview.widget.LinearLayoutManager; @@ -35,6 +30,8 @@ import androidx.recyclerview.widget.RecyclerView; import com.jjoe64.graphview.GraphView; +import org.jetbrains.annotations.NotNull; + import java.util.Calendar; import java.util.Date; import java.util.Locale; @@ -55,15 +52,11 @@ import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.ExtendedBolus; -import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.dialogs.CalibrationDialog; import info.nightscout.androidaps.dialogs.CarbsDialog; import info.nightscout.androidaps.dialogs.InsulinDialog; -import info.nightscout.androidaps.dialogs.ProfileSwitchDialog; -import info.nightscout.androidaps.dialogs.ProfileViewerDialog; -import info.nightscout.androidaps.dialogs.TempTargetDialog; import info.nightscout.androidaps.dialogs.TreatmentDialog; import info.nightscout.androidaps.dialogs.WizardDialog; import info.nightscout.androidaps.events.EventAcceptOpenLoopChange; @@ -80,7 +73,6 @@ import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.ActivePluginProvider; import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.AAPSLogger; import info.nightscout.androidaps.logging.LTag; @@ -105,7 +97,6 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventIobCa import info.nightscout.androidaps.plugins.source.DexcomPlugin; import info.nightscout.androidaps.plugins.source.XdripPlugin; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; -import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.CommandQueue; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; @@ -127,8 +118,6 @@ import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.schedulers.Schedulers; -import static info.nightscout.androidaps.utils.DateUtil.now; - public class OverviewFragment extends DaggerFragment implements View.OnClickListener, View.OnLongClickListener { @Inject HasAndroidInjector injector; @Inject AAPSLogger aapsLogger; @@ -154,6 +143,8 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList @Inject BuildHelper buildHelper; @Inject CommandQueue commandQueue; @Inject ProtectionCheck protectionCheck; + @Inject FabricPrivacy fabricPrivacy; + @Inject OverviewMenus overviewMenus; private CompositeDisposable disposable = new CompositeDisposable(); @@ -182,7 +173,6 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList LinearLayout pumpStatusLayout; GraphView bgGraph; GraphView iobGraph; - ImageButton chartButton; TextView iage; TextView cage; @@ -214,8 +204,6 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList public static boolean shorttextmode = false; - private boolean accepted; - private int rangeToDisplay = 6; // for graph Handler sLoopHandler = new Handler(); @@ -364,7 +352,7 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList return false; }); - setupChartMenu(view); + overviewMenus.setupChartMenu(view.findViewById(R.id.overview_chartMenuButton)); return view; } @@ -386,79 +374,79 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList .toObservable(EventRefreshOverview.class) .observeOn(Schedulers.io()) .subscribe(eventOpenAPSUpdateGui -> scheduleUpdateGUI(eventOpenAPSUpdateGui.getFrom()), - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); disposable.add(rxBus .toObservable(EventExtendedBolusChange.class) .observeOn(Schedulers.io()) .subscribe(event -> scheduleUpdateGUI("EventExtendedBolusChange"), - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); disposable.add(rxBus .toObservable(EventTempBasalChange.class) .observeOn(Schedulers.io()) .subscribe(event -> scheduleUpdateGUI("EventTempBasalChange"), - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); disposable.add(rxBus .toObservable(EventTreatmentChange.class) .observeOn(Schedulers.io()) .subscribe(event -> scheduleUpdateGUI("EventTreatmentChange"), - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); disposable.add(rxBus .toObservable(EventTempTargetChange.class) .observeOn(Schedulers.io()) .subscribe(event -> scheduleUpdateGUI("EventTempTargetChange"), - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); disposable.add(rxBus .toObservable(EventAcceptOpenLoopChange.class) .observeOn(Schedulers.io()) .subscribe(event -> scheduleUpdateGUI("EventAcceptOpenLoopChange"), - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); disposable.add(rxBus .toObservable(EventCareportalEventChange.class) .observeOn(Schedulers.io()) .subscribe(event -> scheduleUpdateGUI("EventCareportalEventChange"), - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); disposable.add(rxBus .toObservable(EventInitializationChanged.class) .observeOn(Schedulers.io()) .subscribe(event -> scheduleUpdateGUI("EventInitializationChanged"), - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); disposable.add(rxBus .toObservable(EventAutosensCalculationFinished.class) .observeOn(Schedulers.io()) .subscribe(event -> scheduleUpdateGUI("EventAutosensCalculationFinished"), - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); disposable.add(rxBus .toObservable(EventProfileNeedsUpdate.class) .observeOn(Schedulers.io()) .subscribe(event -> scheduleUpdateGUI("EventProfileNeedsUpdate"), - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); disposable.add(rxBus .toObservable(EventPreferenceChange.class) .observeOn(Schedulers.io()) .subscribe(event -> scheduleUpdateGUI("EventPreferenceChange"), - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); disposable.add(rxBus .toObservable(EventNewOpenLoopNotification.class) .observeOn(Schedulers.io()) .subscribe(event -> scheduleUpdateGUI("EventNewOpenLoopNotification"), - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); disposable.add(rxBus .toObservable(EventPumpStatusChanged.class) .observeOn(AndroidSchedulers.mainThread()) .subscribe(event -> updatePumpStatus(event), - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); disposable.add(rxBus .toObservable(EventIobCalculationProgress.class) @@ -467,7 +455,7 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList if (iobCalculationProgressView != null) iobCalculationProgressView.setText(event.getProgress()); }, - exception -> FabricPrivacy.getInstance().logException(exception) + fabricPrivacy::logException )); sRefreshLoop = () -> { scheduleUpdateGUI("refreshLoop"); @@ -480,367 +468,18 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList updateGUI("onResume"); } - private void setupChartMenu(View view) { - chartButton = (ImageButton) view.findViewById(R.id.overview_chartMenuButton); - chartButton.setOnClickListener(v -> { - boolean predictionsAvailable; - if (Config.APS) - predictionsAvailable = loopPlugin.lastRun != null && loopPlugin.lastRun.request.hasPredictions; - else if (Config.NSCLIENT) - predictionsAvailable = true; - else - predictionsAvailable = false; - - MenuItem item, dividerItem; - CharSequence title; - int titleMaxChars = 0; - SpannableString s; - PopupMenu popup = new PopupMenu(v.getContext(), v); - if (predictionsAvailable) { - item = popup.getMenu().add(Menu.NONE, CHARTTYPE.PRE.ordinal(), Menu.NONE, "Predictions"); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.prediction, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(sp.getBoolean("showprediction", true)); - } - - item = popup.getMenu().add(Menu.NONE, CHARTTYPE.BAS.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_basals)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.basal, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(sp.getBoolean("showbasals", true)); - - item = popup.getMenu().add(Menu.NONE, CHARTTYPE.ACTPRIM.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_activity)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.activity, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(sp.getBoolean("showactivityprimary", true)); - - dividerItem = popup.getMenu().add(""); - dividerItem.setEnabled(false); - - item = popup.getMenu().add(Menu.NONE, CHARTTYPE.IOB.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_iob)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.iob, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(sp.getBoolean("showiob", true)); - - item = popup.getMenu().add(Menu.NONE, CHARTTYPE.COB.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_cob)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.cob, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(sp.getBoolean("showcob", true)); - - item = popup.getMenu().add(Menu.NONE, CHARTTYPE.DEV.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_deviations)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.deviations, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(sp.getBoolean("showdeviations", false)); - - item = popup.getMenu().add(Menu.NONE, CHARTTYPE.SEN.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_sensitivity)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.ratio, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(sp.getBoolean("showratios", false)); - - item = popup.getMenu().add(Menu.NONE, CHARTTYPE.ACTSEC.ordinal(), Menu.NONE, resourceHelper.gs(R.string.overview_show_activity)); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.activity, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(sp.getBoolean("showactivitysecondary", true)); - - if (buildHelper.isDev()) { - item = popup.getMenu().add(Menu.NONE, CHARTTYPE.DEVSLOPE.ordinal(), Menu.NONE, "Deviation slope"); - title = item.getTitle(); - if (titleMaxChars < title.length()) titleMaxChars = title.length(); - s = new SpannableString(title); - s.setSpan(new ForegroundColorSpan(ResourcesCompat.getColor(getResources(), R.color.devslopepos, null)), 0, s.length(), 0); - item.setTitle(s); - item.setCheckable(true); - item.setChecked(sp.getBoolean("showdevslope", false)); - } - - // Fairly good guestimate for required divider text size... - title = new String(new char[titleMaxChars + 10]).replace("\0", "_"); - dividerItem.setTitle(title); - - popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { - @Override - public boolean onMenuItemClick(MenuItem item) { - if (item.getItemId() == CHARTTYPE.PRE.ordinal()) { - sp.putBoolean("showprediction", !item.isChecked()); - } else if (item.getItemId() == CHARTTYPE.BAS.ordinal()) { - sp.putBoolean("showbasals", !item.isChecked()); - } else if (item.getItemId() == CHARTTYPE.IOB.ordinal()) { - sp.putBoolean("showiob", !item.isChecked()); - } else if (item.getItemId() == CHARTTYPE.COB.ordinal()) { - sp.putBoolean("showcob", !item.isChecked()); - } else if (item.getItemId() == CHARTTYPE.DEV.ordinal()) { - sp.putBoolean("showdeviations", !item.isChecked()); - } else if (item.getItemId() == CHARTTYPE.SEN.ordinal()) { - sp.putBoolean("showratios", !item.isChecked()); - } else if (item.getItemId() == CHARTTYPE.ACTPRIM.ordinal()) { - sp.putBoolean("showactivityprimary", !item.isChecked()); - } else if (item.getItemId() == CHARTTYPE.ACTSEC.ordinal()) { - sp.putBoolean("showactivitysecondary", !item.isChecked()); - } else if (item.getItemId() == CHARTTYPE.DEVSLOPE.ordinal()) { - sp.putBoolean("showdevslope", !item.isChecked()); - } - scheduleUpdateGUI("onGraphCheckboxesCheckedChanged"); - return true; - } - }); - chartButton.setImageResource(R.drawable.ic_arrow_drop_up_white_24dp); - popup.setOnDismissListener(new PopupMenu.OnDismissListener() { - @Override - public void onDismiss(PopupMenu menu) { - chartButton.setImageResource(R.drawable.ic_arrow_drop_down_white_24dp); - } - }); - popup.show(); - }); - } - @Override - public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + public void onCreateContextMenu(@NotNull ContextMenu menu, @NotNull View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); - if (v == apsModeView) { - final PumpDescription pumpDescription = - activePlugin.getActivePump().getPumpDescription(); - if (!profileFunction.isProfileValid("ContexMenuCreation")) - return; - menu.setHeaderTitle(resourceHelper.gs(R.string.loop)); - if (loopPlugin.isEnabled(PluginType.LOOP)) { - menu.add(resourceHelper.gs(R.string.disableloop)); - if (!loopPlugin.isSuspended()) { - menu.add(resourceHelper.gs(R.string.suspendloopfor1h)); - menu.add(resourceHelper.gs(R.string.suspendloopfor2h)); - menu.add(resourceHelper.gs(R.string.suspendloopfor3h)); - menu.add(resourceHelper.gs(R.string.suspendloopfor10h)); - } else { - if (!loopPlugin.isDisconnected()) { - menu.add(resourceHelper.gs(R.string.resume)); - } - } - } - - if (!loopPlugin.isEnabled(PluginType.LOOP)) { - menu.add(resourceHelper.gs(R.string.enableloop)); - } - - if (!loopPlugin.isDisconnected()) { - showSuspendtPump(menu, pumpDescription); - } else { - menu.add(resourceHelper.gs(R.string.reconnect)); - } - - } else if (v == activeProfileView) { - menu.setHeaderTitle(resourceHelper.gs(R.string.profile)); - menu.add(resourceHelper.gs(R.string.danar_viewprofile)); - activePlugin.getActiveProfileInterface(); - if (activePlugin.getActiveProfileInterface().getProfile() != null) { - menu.add(resourceHelper.gs(R.string.careportal_profileswitch)); - } - } else if (v == tempTargetView) { - menu.setHeaderTitle(resourceHelper.gs(R.string.careportal_temporarytarget)); - menu.add(resourceHelper.gs(R.string.custom)); - menu.add(resourceHelper.gs(R.string.eatingsoon)); - menu.add(resourceHelper.gs(R.string.activity)); - menu.add(resourceHelper.gs(R.string.hypo)); - if (treatmentsPlugin.getTempTargetFromHistory() != null) { - menu.add(resourceHelper.gs(R.string.cancel)); - } - } - } - - private void showSuspendtPump(ContextMenu menu, PumpDescription pumpDescription) { - if (pumpDescription.tempDurationStep15mAllowed) - menu.add(resourceHelper.gs(R.string.disconnectpumpfor15m)); - if (pumpDescription.tempDurationStep30mAllowed) - menu.add(resourceHelper.gs(R.string.disconnectpumpfor30m)); - menu.add(resourceHelper.gs(R.string.disconnectpumpfor1h)); - menu.add(resourceHelper.gs(R.string.disconnectpumpfor2h)); - menu.add(resourceHelper.gs(R.string.disconnectpumpfor3h)); + overviewMenus.createContextMenu(menu, v); } @Override public boolean onContextItemSelected(@NonNull MenuItem item) { - final Profile profile = profileFunction.getProfile(); - if (profile == null) - return true; - if (item.getTitle().equals(resourceHelper.gs(R.string.disableloop))) { - aapsLogger.debug("USER ENTRY: LOOP DISABLED"); - loopPlugin.setPluginEnabled(PluginType.LOOP, false); - loopPlugin.setFragmentVisible(PluginType.LOOP, false); - configBuilderPlugin.storeSettings("DisablingLoop"); - updateGUI("suspendmenu"); - commandQueue.cancelTempBasal(true, new Callback() { - @Override - public void run() { - if (!result.success) { - ToastUtils.showToastInUiThread(mainApp, resourceHelper.gs(R.string.tempbasaldeliveryerror)); - } - } - }); - loopPlugin.createOfflineEvent(24 * 60); // upload 24h, we don't know real duration - return true; - } else if (item.getTitle().equals(resourceHelper.gs(R.string.enableloop))) { - aapsLogger.debug("USER ENTRY: LOOP ENABLED"); - loopPlugin.setPluginEnabled(PluginType.LOOP, true); - loopPlugin.setFragmentVisible(PluginType.LOOP, true); - configBuilderPlugin.storeSettings("EnablingLoop"); - updateGUI("suspendmenu"); - loopPlugin.createOfflineEvent(0); - return true; - } else if (item.getTitle().equals(resourceHelper.gs(R.string.resume)) || - item.getTitle().equals(resourceHelper.gs(R.string.reconnect))) { - aapsLogger.debug("USER ENTRY: RESUME"); - loopPlugin.suspendTo(0L); - updateGUI("suspendmenu"); - commandQueue.cancelTempBasal(true, new Callback() { - @Override - public void run() { - if (!result.success) { - ToastUtils.showToastInUiThread(mainApp, resourceHelper.gs(R.string.tempbasaldeliveryerror)); - } - } - }); - sp.putBoolean(R.string.key_objectiveusereconnect, true); - loopPlugin.createOfflineEvent(0); - return true; - } else if (item.getTitle().equals(resourceHelper.gs(R.string.suspendloopfor1h))) { - aapsLogger.debug("USER ENTRY: SUSPEND 1h"); - loopPlugin.suspendLoop(60); - updateGUI("suspendmenu"); - return true; - } else if (item.getTitle().equals(resourceHelper.gs(R.string.suspendloopfor2h))) { - aapsLogger.debug("USER ENTRY: SUSPEND 2h"); - loopPlugin.suspendLoop(120); - updateGUI("suspendmenu"); - return true; - } else if (item.getTitle().equals(resourceHelper.gs(R.string.suspendloopfor3h))) { - aapsLogger.debug("USER ENTRY: SUSPEND 3h"); - loopPlugin.suspendLoop(180); - updateGUI("suspendmenu"); - return true; - } else if (item.getTitle().equals(resourceHelper.gs(R.string.suspendloopfor10h))) { - aapsLogger.debug("USER ENTRY: SUSPEND 10h"); - loopPlugin.suspendLoop(600); - updateGUI("suspendmenu"); - return true; - } else if (item.getTitle().equals(resourceHelper.gs(R.string.disconnectpumpfor15m))) { - aapsLogger.debug("USER ENTRY: DISCONNECT 15m"); - loopPlugin.disconnectPump(15, profile); - updateGUI("suspendmenu"); - return true; - } else if (item.getTitle().equals(resourceHelper.gs(R.string.disconnectpumpfor30m))) { - aapsLogger.debug("USER ENTRY: DISCONNECT 30m"); - loopPlugin.disconnectPump(30, profile); - updateGUI("suspendmenu"); - return true; - } else if (item.getTitle().equals(resourceHelper.gs(R.string.disconnectpumpfor1h))) { - aapsLogger.debug("USER ENTRY: DISCONNECT 1h"); - loopPlugin.disconnectPump(60, profile); - sp.putBoolean(R.string.key_objectiveusedisconnect, true); - updateGUI("suspendmenu"); - return true; - } else if (item.getTitle().equals(resourceHelper.gs(R.string.disconnectpumpfor2h))) { - aapsLogger.debug("USER ENTRY: DISCONNECT 2h"); - loopPlugin.disconnectPump(120, profile); - updateGUI("suspendmenu"); - return true; - } else if (item.getTitle().equals(resourceHelper.gs(R.string.disconnectpumpfor3h))) { - aapsLogger.debug("USER ENTRY: DISCONNECT 3h"); - loopPlugin.disconnectPump(180, profile); - updateGUI("suspendmenu"); - return true; - } else if (item.getTitle().equals(resourceHelper.gs(R.string.careportal_profileswitch))) { - FragmentManager manager = getFragmentManager(); - if (manager != null) - new ProfileSwitchDialog().show(manager, "Overview"); - } else if (item.getTitle().equals(resourceHelper.gs(R.string.danar_viewprofile))) { - Bundle args = new Bundle(); - args.putLong("time", DateUtil.now()); - args.putInt("mode", ProfileViewerDialog.Mode.RUNNING_PROFILE.ordinal()); - ProfileViewerDialog pvd = new ProfileViewerDialog(); - pvd.setArguments(args); - FragmentManager manager = getFragmentManager(); - if (manager != null) - pvd.show(manager, "ProfileViewDialog"); - } else if (item.getTitle().equals(resourceHelper.gs(R.string.eatingsoon))) { - aapsLogger.debug("USER ENTRY: TEMP TARGET EATING SOON"); - double target = Profile.toMgdl(defaultValueHelper.determineEatingSoonTT(), profileFunction.getUnits()); - TempTarget tempTarget = new TempTarget() - .date(System.currentTimeMillis()) - .duration(defaultValueHelper.determineEatingSoonTTDuration()) - .reason(resourceHelper.gs(R.string.eatingsoon)) - .source(Source.USER) - .low(target) - .high(target); - treatmentsPlugin.addToHistoryTempTarget(tempTarget); - } else if (item.getTitle().equals(resourceHelper.gs(R.string.activity))) { - aapsLogger.debug("USER ENTRY: TEMP TARGET ACTIVITY"); - double target = Profile.toMgdl(defaultValueHelper.determineActivityTT(), profileFunction.getUnits()); - TempTarget tempTarget = new TempTarget() - .date(now()) - .duration(defaultValueHelper.determineActivityTTDuration()) - .reason(resourceHelper.gs(R.string.activity)) - .source(Source.USER) - .low(target) - .high(target); - treatmentsPlugin.addToHistoryTempTarget(tempTarget); - } else if (item.getTitle().equals(resourceHelper.gs(R.string.hypo))) { - aapsLogger.debug("USER ENTRY: TEMP TARGET HYPO"); - double target = Profile.toMgdl(defaultValueHelper.determineHypoTT(), profileFunction.getUnits()); - TempTarget tempTarget = new TempTarget() - .date(now()) - .duration(defaultValueHelper.determineHypoTTDuration()) - .reason(resourceHelper.gs(R.string.hypo)) - .source(Source.USER) - .low(target) - .high(target); - treatmentsPlugin.addToHistoryTempTarget(tempTarget); - } else if (item.getTitle().equals(resourceHelper.gs(R.string.custom))) { - FragmentManager manager = getFragmentManager(); - if (manager != null) - new TempTargetDialog().show(manager, "Overview"); - } else if (item.getTitle().equals(resourceHelper.gs(R.string.cancel))) { - aapsLogger.debug("USER ENTRY: TEMP TARGET CANCEL"); - TempTarget tempTarget = new TempTarget() - .source(Source.USER) - .date(now()) - .duration(0) - .low(0) - .high(0); - treatmentsPlugin.addToHistoryTempTarget(tempTarget); - } - - return super.onContextItemSelected(item); + FragmentManager manager = getFragmentManager(); + if (manager != null && overviewMenus.onContextItemSelected(item, manager)) return true; + else return super.onContextItemSelected(item); } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewMenus.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewMenus.kt new file mode 100644 index 0000000000..3336f62ab6 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewMenus.kt @@ -0,0 +1,433 @@ +package info.nightscout.androidaps.plugins.general.overview + +import android.content.Context +import android.content.Intent +import android.os.Bundle +import android.text.SpannableString +import android.text.style.ForegroundColorSpan +import android.view.ContextMenu +import android.view.Menu +import android.view.MenuItem +import android.view.View +import android.widget.ImageButton +import androidx.appcompat.widget.PopupMenu +import androidx.fragment.app.FragmentManager +import info.nightscout.androidaps.Config +import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.ErrorHelperActivity +import info.nightscout.androidaps.data.Profile +import info.nightscout.androidaps.db.Source +import info.nightscout.androidaps.db.TempTarget +import info.nightscout.androidaps.dialogs.ProfileSwitchDialog +import info.nightscout.androidaps.dialogs.ProfileViewerDialog +import info.nightscout.androidaps.dialogs.TempTargetDialog +import info.nightscout.androidaps.events.EventRefreshOverview +import info.nightscout.androidaps.interfaces.ActivePluginProvider +import info.nightscout.androidaps.interfaces.CommandQueueProvider +import info.nightscout.androidaps.interfaces.PluginType +import info.nightscout.androidaps.interfaces.PumpDescription +import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin +import info.nightscout.androidaps.plugins.bus.RxBusWrapper +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction +import info.nightscout.androidaps.plugins.general.overview.OverviewFragment.CHARTTYPE +import info.nightscout.androidaps.queue.Callback +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.DefaultValueHelper +import info.nightscout.androidaps.utils.ToastUtils +import info.nightscout.androidaps.utils.buildHelper.BuildHelper +import info.nightscout.androidaps.utils.resources.ResourceHelper +import info.nightscout.androidaps.utils.sharedPreferences.SP +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class OverviewMenus @Inject constructor( + private val aapsLogger: AAPSLogger, + private val resourceHelper: ResourceHelper, + private val sp: SP, + private val rxBus: RxBusWrapper, + private val context: Context, + private val buildHelper: BuildHelper, + private val defaultValueHelper: DefaultValueHelper, + private val activePlugin: ActivePluginProvider, + private val profileFunction: ProfileFunction, + private val commandQueue: CommandQueueProvider, + private val configBuilderPlugin: ConfigBuilderPlugin, + private val loopPlugin: LoopPlugin +) { + + fun setupChartMenu(chartButton: ImageButton) { + chartButton.setOnClickListener { v: View -> + val predictionsAvailable: Boolean = when { + Config.APS -> loopPlugin.lastRun?.request?.hasPredictions ?: false + Config.NSCLIENT -> true + else -> false + } + //var item: MenuItem + val dividerItem: MenuItem + //var title: CharSequence + var titleMaxChars = 0 + //var s: SpannableString + val popup = PopupMenu(v.context, v) + if (predictionsAvailable) { + val item = popup.menu.add(Menu.NONE, CHARTTYPE.PRE.ordinal, Menu.NONE, "Predictions") + val title = item.title + if (titleMaxChars < title.length) titleMaxChars = title.length + val s = SpannableString(title) + s.setSpan(ForegroundColorSpan(resourceHelper.gc(R.color.prediction)), 0, s.length, 0) + item.title = s + item.isCheckable = true + item.isChecked = sp.getBoolean("showprediction", true) + } + run { + val item = popup.menu.add(Menu.NONE, CHARTTYPE.BAS.ordinal, Menu.NONE, resourceHelper.gs(R.string.overview_show_basals)) + val title = item.title + if (titleMaxChars < title.length) titleMaxChars = title.length + val s = SpannableString(title) + s.setSpan(ForegroundColorSpan(resourceHelper.gc(R.color.basal)), 0, s.length, 0) + item.title = s + item.isCheckable = true + item.isChecked = sp.getBoolean("showbasals", true) + } + run { + val item = popup.menu.add(Menu.NONE, CHARTTYPE.ACTPRIM.ordinal, Menu.NONE, resourceHelper.gs(R.string.overview_show_activity)) + val title = item.title + if (titleMaxChars < title.length) titleMaxChars = title.length + val s = SpannableString(title) + s.setSpan(ForegroundColorSpan(resourceHelper.gc(R.color.activity)), 0, s.length, 0) + item.title = s + item.isCheckable = true + item.isChecked = sp.getBoolean("showactivityprimary", true) + dividerItem = popup.menu.add("") + dividerItem.isEnabled = false + } + run { + val item = popup.menu.add(Menu.NONE, CHARTTYPE.IOB.ordinal, Menu.NONE, resourceHelper.gs(R.string.overview_show_iob)) + val title = item.title + if (titleMaxChars < title.length) titleMaxChars = title.length + val s = SpannableString(title) + s.setSpan(ForegroundColorSpan(resourceHelper.gc(R.color.iob)), 0, s.length, 0) + item.title = s + item.isCheckable = true + item.isChecked = sp.getBoolean("showiob", true) + } + run { + val item = popup.menu.add(Menu.NONE, CHARTTYPE.COB.ordinal, Menu.NONE, resourceHelper.gs(R.string.overview_show_cob)) + val title = item.title + if (titleMaxChars < title.length) titleMaxChars = title.length + val s = SpannableString(title) + s.setSpan(ForegroundColorSpan(resourceHelper.gc(R.color.cob)), 0, s.length, 0) + item.title = s + item.isCheckable = true + item.isChecked = sp.getBoolean("showcob", true) + } + run { + val item = popup.menu.add(Menu.NONE, CHARTTYPE.DEV.ordinal, Menu.NONE, resourceHelper.gs(R.string.overview_show_deviations)) + val title = item.title + if (titleMaxChars < title.length) titleMaxChars = title.length + val s = SpannableString(title) + s.setSpan(ForegroundColorSpan(resourceHelper.gc(R.color.deviations)), 0, s.length, 0) + item.title = s + item.isCheckable = true + item.isChecked = sp.getBoolean("showdeviations", false) + } + run { + val item = popup.menu.add(Menu.NONE, CHARTTYPE.SEN.ordinal, Menu.NONE, resourceHelper.gs(R.string.overview_show_sensitivity)) + val title = item.title + if (titleMaxChars < title.length) titleMaxChars = title.length + val s = SpannableString(title) + s.setSpan(ForegroundColorSpan(resourceHelper.gc(R.color.ratio)), 0, s.length, 0) + item.title = s + item.isCheckable = true + item.isChecked = sp.getBoolean("showratios", false) + } + run { + val item = popup.menu.add(Menu.NONE, CHARTTYPE.ACTSEC.ordinal, Menu.NONE, resourceHelper.gs(R.string.overview_show_activity)) + val title = item.title + if (titleMaxChars < title.length) titleMaxChars = title.length + val s = SpannableString(title) + s.setSpan(ForegroundColorSpan(resourceHelper.gc(R.color.activity)), 0, s.length, 0) + item.title = s + item.isCheckable = true + item.isChecked = sp.getBoolean("showactivitysecondary", true) + } + if (buildHelper.isDev()) { + val item = popup.menu.add(Menu.NONE, CHARTTYPE.DEVSLOPE.ordinal, Menu.NONE, "Deviation slope") + val title = item.title + if (titleMaxChars < title.length) titleMaxChars = title.length + val s = SpannableString(title) + s.setSpan(ForegroundColorSpan(resourceHelper.gc(R.color.devslopepos)), 0, s.length, 0) + item.title = s + item.isCheckable = true + item.isChecked = sp.getBoolean("showdevslope", false) + } + + // Fairly good estimate for required divider text size... + dividerItem.title = String(CharArray(titleMaxChars + 10)).replace("\u0000", "_") + popup.setOnMenuItemClickListener { + when (it.itemId) { + CHARTTYPE.PRE.ordinal -> sp.putBoolean("showprediction", !it.isChecked) + CHARTTYPE.BAS.ordinal -> sp.putBoolean("showbasals", !it.isChecked) + CHARTTYPE.IOB.ordinal -> sp.putBoolean("showiob", !it.isChecked) + CHARTTYPE.COB.ordinal -> sp.putBoolean("showcob", !it.isChecked) + CHARTTYPE.DEV.ordinal -> sp.putBoolean("showdeviations", !it.isChecked) + CHARTTYPE.SEN.ordinal -> sp.putBoolean("showratios", !it.isChecked) + CHARTTYPE.ACTPRIM.ordinal -> sp.putBoolean("showactivityprimary", !it.isChecked) + CHARTTYPE.ACTSEC.ordinal -> sp.putBoolean("showactivitysecondary", !it.isChecked) + CHARTTYPE.DEVSLOPE.ordinal -> sp.putBoolean("showdevslope", !it.isChecked) + } + rxBus.send(EventRefreshOverview("OnMenuItemClickListener")) + return@setOnMenuItemClickListener true + } + chartButton.setImageResource(R.drawable.ic_arrow_drop_up_white_24dp) + popup.setOnDismissListener { chartButton.setImageResource(R.drawable.ic_arrow_drop_down_white_24dp) } + popup.show() + } + } + + fun createContextMenu(menu: ContextMenu, v: View) { + when (v.id) { + R.id.overview_apsmode -> { + val pumpDescription: PumpDescription = activePlugin.activePump.pumpDescription + if (!profileFunction.isProfileValid("ContextMenuCreation")) return + menu.setHeaderTitle(resourceHelper.gs(R.string.loop)) + if (loopPlugin.isEnabled(PluginType.LOOP)) { + menu.add(resourceHelper.gs(R.string.disableloop)) + if (!loopPlugin.isSuspended) { + menu.add(resourceHelper.gs(R.string.suspendloopfor1h)) + menu.add(resourceHelper.gs(R.string.suspendloopfor2h)) + menu.add(resourceHelper.gs(R.string.suspendloopfor3h)) + menu.add(resourceHelper.gs(R.string.suspendloopfor10h)) + } else { + if (!loopPlugin.isDisconnected) { + menu.add(resourceHelper.gs(R.string.resume)) + } + } + } + if (!loopPlugin.isEnabled(PluginType.LOOP)) { + menu.add(resourceHelper.gs(R.string.enableloop)) + } + if (!loopPlugin.isDisconnected) { + showSuspendPump(menu, pumpDescription) + } else { + menu.add(resourceHelper.gs(R.string.reconnect)) + } + } + + R.id.overview_activeprofile -> { + menu.setHeaderTitle(resourceHelper.gs(R.string.profile)) + menu.add(resourceHelper.gs(R.string.danar_viewprofile)) + if (activePlugin.activeProfileInterface.profile != null) { + menu.add(resourceHelper.gs(R.string.careportal_profileswitch)) + } + } + + R.id.overview_temptarget -> { + menu.setHeaderTitle(resourceHelper.gs(R.string.careportal_temporarytarget)) + menu.add(resourceHelper.gs(R.string.custom)) + menu.add(resourceHelper.gs(R.string.eatingsoon)) + menu.add(resourceHelper.gs(R.string.activity)) + menu.add(resourceHelper.gs(R.string.hypo)) + if (activePlugin.activeTreatments.tempTargetFromHistory != null) { + menu.add(resourceHelper.gs(R.string.cancel)) + } + } + } + } + + private fun showSuspendPump(menu: ContextMenu, pumpDescription: PumpDescription) { + if (pumpDescription.tempDurationStep15mAllowed) menu.add(resourceHelper.gs(R.string.disconnectpumpfor15m)) + if (pumpDescription.tempDurationStep30mAllowed) menu.add(resourceHelper.gs(R.string.disconnectpumpfor30m)) + menu.add(resourceHelper.gs(R.string.disconnectpumpfor1h)) + menu.add(resourceHelper.gs(R.string.disconnectpumpfor2h)) + menu.add(resourceHelper.gs(R.string.disconnectpumpfor3h)) + } + + fun onContextItemSelected(item: MenuItem, manager: FragmentManager): Boolean { + val profile = profileFunction.getProfile() ?: return true + when (item.title) { + resourceHelper.gs(R.string.disableloop) -> { + aapsLogger.debug("USER ENTRY: LOOP DISABLED") + loopPlugin.setPluginEnabled(PluginType.LOOP, false) + loopPlugin.setFragmentVisible(PluginType.LOOP, false) + configBuilderPlugin.storeSettings("DisablingLoop") + rxBus.send(EventRefreshOverview("suspendmenu")) + commandQueue.cancelTempBasal(true, object : Callback() { + override fun run() { + if (!result.success) { + ToastUtils.showToastInUiThread(context, resourceHelper.gs(R.string.tempbasaldeliveryerror)) + } + } + }) + loopPlugin.createOfflineEvent(24 * 60) // upload 24h, we don't know real duration + return true + } + + resourceHelper.gs(R.string.enableloop) -> { + aapsLogger.debug("USER ENTRY: LOOP ENABLED") + loopPlugin.setPluginEnabled(PluginType.LOOP, true) + loopPlugin.setFragmentVisible(PluginType.LOOP, true) + configBuilderPlugin.storeSettings("EnablingLoop") + rxBus.send(EventRefreshOverview("suspendmenu")) + loopPlugin.createOfflineEvent(0) + return true + } + + resourceHelper.gs(R.string.resume), resourceHelper.gs(R.string.reconnect) -> { + aapsLogger.debug("USER ENTRY: RESUME") + loopPlugin.suspendTo(0L) + rxBus.send(EventRefreshOverview("suspendmenu")) + commandQueue.cancelTempBasal(true, object : Callback() { + override fun run() { + if (!result.success) { + val i = Intent(context, ErrorHelperActivity::class.java) + i.putExtra("soundid", R.raw.boluserror) + i.putExtra("status", result.comment) + i.putExtra("title", resourceHelper.gs(R.string.tempbasaldeliveryerror)) + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + context.startActivity(i) + } + } + }) + sp.putBoolean(R.string.key_objectiveusereconnect, true) + loopPlugin.createOfflineEvent(0) + return true + } + + resourceHelper.gs(R.string.suspendloopfor1h) -> { + aapsLogger.debug("USER ENTRY: SUSPEND 1h") + loopPlugin.suspendLoop(60) + rxBus.send(EventRefreshOverview("suspendmenu")) + return true + } + + resourceHelper.gs(R.string.suspendloopfor2h) -> { + aapsLogger.debug("USER ENTRY: SUSPEND 2h") + loopPlugin.suspendLoop(120) + rxBus.send(EventRefreshOverview("suspendmenu")) + return true + } + + resourceHelper.gs(R.string.suspendloopfor3h) -> { + aapsLogger.debug("USER ENTRY: SUSPEND 3h") + loopPlugin.suspendLoop(180) + rxBus.send(EventRefreshOverview("suspendmenu")) + return true + } + + resourceHelper.gs(R.string.suspendloopfor10h) -> { + aapsLogger.debug("USER ENTRY: SUSPEND 10h") + loopPlugin.suspendLoop(600) + rxBus.send(EventRefreshOverview("suspendmenu")) + return true + } + + resourceHelper.gs(R.string.disconnectpumpfor15m) -> { + aapsLogger.debug("USER ENTRY: DISCONNECT 15m") + loopPlugin.disconnectPump(15, profile) + rxBus.send(EventRefreshOverview("suspendmenu")) + return true + } + + resourceHelper.gs(R.string.disconnectpumpfor30m) -> { + aapsLogger.debug("USER ENTRY: DISCONNECT 30m") + loopPlugin.disconnectPump(30, profile) + rxBus.send(EventRefreshOverview("suspendmenu")) + return true + } + + resourceHelper.gs(R.string.disconnectpumpfor1h) -> { + aapsLogger.debug("USER ENTRY: DISCONNECT 1h") + loopPlugin.disconnectPump(60, profile) + sp.putBoolean(R.string.key_objectiveusedisconnect, true) + rxBus.send(EventRefreshOverview("suspendmenu")) + return true + } + + resourceHelper.gs(R.string.disconnectpumpfor2h) -> { + aapsLogger.debug("USER ENTRY: DISCONNECT 2h") + loopPlugin.disconnectPump(120, profile) + rxBus.send(EventRefreshOverview("suspendmenu")) + return true + } + + resourceHelper.gs(R.string.disconnectpumpfor3h) -> { + aapsLogger.debug("USER ENTRY: DISCONNECT 3h") + loopPlugin.disconnectPump(180, profile) + rxBus.send(EventRefreshOverview("suspendmenu")) + return true + } + + resourceHelper.gs(R.string.careportal_profileswitch) -> { + ProfileSwitchDialog().show(manager, "Overview") + } + + resourceHelper.gs(R.string.danar_viewprofile) -> { + val args = Bundle() + args.putLong("time", DateUtil.now()) + args.putInt("mode", ProfileViewerDialog.Mode.RUNNING_PROFILE.ordinal) + val pvd = ProfileViewerDialog() + pvd.arguments = args + pvd.show(manager, "ProfileViewDialog") + } + + resourceHelper.gs(R.string.eatingsoon) -> { + aapsLogger.debug("USER ENTRY: TEMP TARGET EATING SOON") + val target = Profile.toMgdl(defaultValueHelper.determineEatingSoonTT(), profileFunction.getUnits()) + val tempTarget = TempTarget() + .date(System.currentTimeMillis()) + .duration(defaultValueHelper.determineEatingSoonTTDuration()) + .reason(resourceHelper.gs(R.string.eatingsoon)) + .source(Source.USER) + .low(target) + .high(target) + activePlugin.activeTreatments.addToHistoryTempTarget(tempTarget) + } + + resourceHelper.gs(R.string.activity) -> { + aapsLogger.debug("USER ENTRY: TEMP TARGET ACTIVITY") + val target = Profile.toMgdl(defaultValueHelper.determineActivityTT(), profileFunction.getUnits()) + val tempTarget = TempTarget() + .date(DateUtil.now()) + .duration(defaultValueHelper.determineActivityTTDuration()) + .reason(resourceHelper.gs(R.string.activity)) + .source(Source.USER) + .low(target) + .high(target) + activePlugin.activeTreatments.addToHistoryTempTarget(tempTarget) + } + + resourceHelper.gs(R.string.hypo) -> { + aapsLogger.debug("USER ENTRY: TEMP TARGET HYPO") + val target = Profile.toMgdl(defaultValueHelper.determineHypoTT(), profileFunction.getUnits()) + val tempTarget = TempTarget() + .date(DateUtil.now()) + .duration(defaultValueHelper.determineHypoTTDuration()) + .reason(resourceHelper.gs(R.string.hypo)) + .source(Source.USER) + .low(target) + .high(target) + activePlugin.activeTreatments.addToHistoryTempTarget(tempTarget) + } + + resourceHelper.gs(R.string.custom) -> { + TempTargetDialog().show(manager, "Overview") + } + + resourceHelper.gs(R.string.cancel) -> { + aapsLogger.debug("USER ENTRY: TEMP TARGET CANCEL") + val tempTarget = TempTarget() + .source(Source.USER) + .date(DateUtil.now()) + .duration(0) + .low(0.0) + .high(0.0) + activePlugin.activeTreatments.addToHistoryTempTarget(tempTarget) + } + } + return false + } + +} \ No newline at end of file