From 858759873af64d95cb30c522625b1cfc210b6f59 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 7 Aug 2019 18:45:33 +0200 Subject: [PATCH] MedtronicFragment remove static variables --- .../ResetRileyLinkConfigurationTask.java | 6 ++- .../service/tasks/WakeAndTuneTask.java | 7 +-- .../pump/medtronic/MedtronicFragment.java | 44 ++++++------------- .../pump/medtronic/MedtronicPumpPlugin.java | 3 +- .../events/EventRefreshButtonState.kt | 5 +++ 5 files changed, 28 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/events/EventRefreshButtonState.kt diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ResetRileyLinkConfigurationTask.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ResetRileyLinkConfigurationTask.java index ae8b4dcefa..62dcaa0f5f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ResetRileyLinkConfigurationTask.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/ResetRileyLinkConfigurationTask.java @@ -1,8 +1,10 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks; +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data.ServiceTransport; import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicFragment; import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicPumpPlugin; +import info.nightscout.androidaps.plugins.pump.medtronic.events.EventRefreshButtonState; import info.nightscout.androidaps.plugins.pump.medtronic.service.RileyLinkMedtronicService; /** @@ -24,11 +26,11 @@ public class ResetRileyLinkConfigurationTask extends PumpTask { @Override public void run() { - MedtronicFragment.refreshButtonEnabled(false); + MainApp.bus().post(new EventRefreshButtonState(false)); MedtronicPumpPlugin.isBusy = true; RileyLinkMedtronicService.getInstance().resetRileyLinkConfiguration(); MedtronicPumpPlugin.isBusy = false; - MedtronicFragment.refreshButtonEnabled(true); + MainApp.bus().post(new EventRefreshButtonState(true)); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/WakeAndTuneTask.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/WakeAndTuneTask.java index 13ffba516a..3dec2cb212 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/WakeAndTuneTask.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/WakeAndTuneTask.java @@ -1,8 +1,10 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks; +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data.ServiceTransport; import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicFragment; import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicPumpPlugin; +import info.nightscout.androidaps.plugins.pump.medtronic.events.EventRefreshButtonState; import info.nightscout.androidaps.plugins.pump.medtronic.service.RileyLinkMedtronicService; /** @@ -24,11 +26,10 @@ public class WakeAndTuneTask extends PumpTask { @Override public void run() { - MedtronicFragment.refreshButtonEnabled(false); + MainApp.bus().post(new EventRefreshButtonState(false)); MedtronicPumpPlugin.isBusy = true; RileyLinkMedtronicService.getInstance().doTuneUpDevice(); MedtronicPumpPlugin.isBusy = false; - MedtronicFragment.refreshButtonEnabled(true); + MainApp.bus().post(new EventRefreshButtonState(true)); } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicFragment.java index 9bb7b09601..894c010476 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicFragment.java @@ -46,6 +46,7 @@ import info.nightscout.androidaps.plugins.pump.medtronic.driver.MedtronicPumpSta import info.nightscout.androidaps.plugins.pump.medtronic.events.EventMedtronicDeviceStatusChange; import info.nightscout.androidaps.plugins.pump.medtronic.events.EventMedtronicPumpConfigurationChanged; import info.nightscout.androidaps.plugins.pump.medtronic.events.EventMedtronicPumpValuesChanged; +import info.nightscout.androidaps.plugins.pump.medtronic.events.EventRefreshButtonState; import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.queue.Callback; @@ -65,8 +66,6 @@ public class MedtronicFragment extends SubscriberFragment { @BindView(R.id.medtronic_basabasalrate) TextView basaBasalRateView; - // @BindView(R.id.medtronic_btconnection) - // TextView btConnectionView; @BindView(R.id.medtronic_tempbasal) TextView tempBasalView; @BindView(R.id.medtronic_pumpstate_battery) @@ -88,9 +87,6 @@ public class MedtronicFragment extends SubscriberFragment { @BindView(R.id.medtronic_refresh) Button refreshButton; private Handler loopHandler = new Handler(); - private static Activity localActivity; - - static Button refreshButtonStatic; private Runnable refreshLoop = new Runnable() { @@ -126,7 +122,7 @@ public class MedtronicFragment extends SubscriberFragment { View view = inflater.inflate(R.layout.medtronic_fragment, container, false); unbinder = ButterKnife.bind(this, view); - overviewPumpMedtronicView.setBackgroundColor(MainApp.sResources.getColor(R.color.colorInitializingBorder)); + overviewPumpMedtronicView.setBackgroundColor(MainApp.gc(R.color.colorInitializingBorder)); rileyLinkStatus.setText(getTranslation(RileyLinkServiceState.NotStarted.getResourceId(getTargetDevice()))); rileyLinkStatus.setTextSize(14); @@ -135,8 +131,6 @@ public class MedtronicFragment extends SubscriberFragment { pumpStatusIconView.setTextSize(14); pumpStatusIconView.setText("{fa-bed}"); - refreshButtonStatic = refreshButton; - return view; } catch (Exception e) { Crashlytics.logException(e); @@ -164,8 +158,8 @@ public class MedtronicFragment extends SubscriberFragment { return; } - if (refreshButtonStatic != null) - refreshButtonStatic.setEnabled(false); + if (refreshButton != null) + refreshButton.setEnabled(false); MedtronicPumpPlugin.getPlugin().resetStatusState(); @@ -177,8 +171,8 @@ public class MedtronicFragment extends SubscriberFragment { if (activity != null) { activity.runOnUiThread(() -> { - if (refreshButtonStatic != null) - refreshButtonStatic.setEnabled(true); + if (refreshButton != null) + refreshButton.setEnabled(true); }); } } @@ -202,22 +196,12 @@ public class MedtronicFragment extends SubscriberFragment { } - public static void refreshButtonEnabled(boolean enable) { - if (localActivity != null) { - localActivity.runOnUiThread(() -> { - if (refreshButtonStatic != null) { - refreshButtonStatic.setEnabled(enable); - } - }); - } + @Subscribe + public void onStatusEvent(final EventRefreshButtonState e) { + if (getActivity() != null) + getActivity().runOnUiThread(() -> refreshButton.setEnabled(e.getNewState())); } - - public static Activity getCustomActivity() { - return localActivity; - } - - @Subscribe public void onStatusEvent(final EventMedtronicDeviceStatusChange eventStatusChange) { if (isLogEnabled()) @@ -225,7 +209,6 @@ public class MedtronicFragment extends SubscriberFragment { Activity activity = getActivity(); if (activity != null) { - localActivity = activity; activity.runOnUiThread(() -> { MedtronicPumpStatus pumpStatus = MedtronicUtil.getPumpStatus(); setDeviceStatus(pumpStatus); @@ -363,12 +346,12 @@ public class MedtronicFragment extends SubscriberFragment { } - public RileyLinkTargetDevice getTargetDevice() { + private RileyLinkTargetDevice getTargetDevice() { return RileyLinkTargetDevice.MedtronicPump; } - public String getTranslation(int resourceId) { + private String getTranslation(int resourceId) { return MainApp.gs(resourceId); } @@ -417,8 +400,7 @@ public class MedtronicFragment extends SubscriberFragment { if (lastConnectionView == null) // ui not yet initialized return; - localActivity = activity; - MedtronicPumpPlugin plugin = (MedtronicPumpPlugin) MedtronicPumpPlugin.getPlugin(); + MedtronicPumpPlugin plugin = MedtronicPumpPlugin.getPlugin(); MedtronicPumpStatus pumpStatus = MedtronicUtil.getPumpStatus(); setDeviceStatus(pumpStatus); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java index 595e546a93..6b8e641418 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java @@ -73,6 +73,7 @@ import info.nightscout.androidaps.plugins.pump.medtronic.defs.MedtronicStatusRef import info.nightscout.androidaps.plugins.pump.medtronic.defs.MedtronicUIResponseType; import info.nightscout.androidaps.plugins.pump.medtronic.driver.MedtronicPumpStatus; import info.nightscout.androidaps.plugins.pump.medtronic.events.EventMedtronicPumpValuesChanged; +import info.nightscout.androidaps.plugins.pump.medtronic.events.EventRefreshButtonState; import info.nightscout.androidaps.plugins.pump.medtronic.service.RileyLinkMedtronicService; import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicConst; import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil; @@ -494,7 +495,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter private void setRefreshButtonEnabled(boolean enabled) { - MedtronicFragment.refreshButtonEnabled(enabled); + MainApp.bus().post(new EventRefreshButtonState(enabled)); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/events/EventRefreshButtonState.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/events/EventRefreshButtonState.kt new file mode 100644 index 0000000000..81b9af4ff3 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/events/EventRefreshButtonState.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.pump.medtronic.events + +import info.nightscout.androidaps.events.Event + +class EventRefreshButtonState (val newState : Boolean): Event() \ No newline at end of file