From c1109a5b415c322bfa77304f4b84a9c0f8968047 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 1 Oct 2019 15:00:19 +0200 Subject: [PATCH] QuickWizard cleanup, -> kotlin --- .../androidaps/data/QuickWizard.java | 86 ------------------- .../nightscout/androidaps/data/QuickWizard.kt | 52 +++++++++++ .../general/overview/OverviewFragment.java | 5 +- .../general/overview/OverviewPlugin.kt | 14 --- .../activities/QuickWizardListActivity.java | 44 +++++----- .../dialogs/EditQuickWizardDialog.java | 5 +- .../androidaps/data/QuickWizardTest.java | 53 ++++++++---- 7 files changed, 111 insertions(+), 148 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/data/QuickWizard.java create mode 100644 app/src/main/java/info/nightscout/androidaps/data/QuickWizard.kt diff --git a/app/src/main/java/info/nightscout/androidaps/data/QuickWizard.java b/app/src/main/java/info/nightscout/androidaps/data/QuickWizard.java deleted file mode 100644 index 80a6e1b457..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/data/QuickWizard.java +++ /dev/null @@ -1,86 +0,0 @@ -package info.nightscout.androidaps.data; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import info.nightscout.androidaps.utils.SP; - -/** - * Created by mike on 12.10.2016. - */ - -public class QuickWizard { - private static Logger log = LoggerFactory.getLogger(QuickWizard.class); - - private JSONArray storage = new JSONArray(); - - public void setData(JSONArray newData) { - storage = newData; - } - - public void save() { - SP.putString("QuickWizard", storage.toString()); - } - - public int size() { - return storage.length(); - } - - public QuickWizardEntry get(int position) { - try { - return new QuickWizardEntry((JSONObject) storage.get(position), position); - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - return null; - } - - public Boolean isActive() { - for (int i = 0; i < storage.length(); i++) { - try { - if (new QuickWizardEntry((JSONObject) storage.get(i), i).isActive()) return true; - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - } - return false; - } - - public QuickWizardEntry getActive() { - for (int i = 0; i < storage.length(); i++) { - QuickWizardEntry entry; - try { - entry = new QuickWizardEntry((JSONObject) storage.get(i), i); - } catch (JSONException e) { - continue; - } - if (entry.isActive()) return entry; - } - return null; - } - - public QuickWizardEntry newEmptyItem() { - return new QuickWizardEntry(); - } - - public void addOrUpdate(QuickWizardEntry newItem) { - if (newItem.position == -1) - storage.put(newItem.storage); - else { - try { - storage.put(newItem.position, newItem.storage); - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - } - save(); - } - - public void remove(int position) { - storage.remove(position); - save(); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/data/QuickWizard.kt b/app/src/main/java/info/nightscout/androidaps/data/QuickWizard.kt new file mode 100644 index 0000000000..f8bca8c450 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/data/QuickWizard.kt @@ -0,0 +1,52 @@ +package info.nightscout.androidaps.data + +import info.nightscout.androidaps.utils.SP +import org.json.JSONArray +import org.json.JSONObject + +object QuickWizard { + private var storage = JSONArray() + + init { + setData(JSONArray(SP.getString("QuickWizard", "[]"))) + } + + fun getActive(): QuickWizardEntry? { + for (i in 0 until storage.length()) { + val entry = QuickWizardEntry(storage.get(i) as JSONObject, i) + if (entry.isActive) return entry + } + return null + } + + fun setData(newData: JSONArray) { + storage = newData + } + + fun save() { + SP.putString("QuickWizard", storage.toString()) + } + + fun size(): Int = storage.length() + + operator fun get(position: Int): QuickWizardEntry = + QuickWizardEntry(storage.get(position) as JSONObject, position) + + + fun newEmptyItem(): QuickWizardEntry { + return QuickWizardEntry() + } + + fun addOrUpdate(newItem: QuickWizardEntry) { + if (newItem.position == -1) + storage.put(newItem.storage) + else + storage.put(newItem.position, newItem.storage) + save() + } + + fun remove(position: Int) { + storage.remove(position) + save() + } +} 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 467d1c813c..b7d7cae4a0 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 @@ -56,6 +56,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.data.QuickWizard; import info.nightscout.androidaps.data.QuickWizardEntry; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.DatabaseHelper; @@ -827,7 +828,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, final String profileName = ProfileFunctions.getInstance().getProfileName(); final PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); - final QuickWizardEntry quickWizardEntry = OverviewPlugin.INSTANCE.getQuickWizard().getActive(); + final QuickWizardEntry quickWizardEntry = QuickWizard.INSTANCE.getActive(); if (quickWizardEntry != null && actualBg != null && profile != null && pump != null) { quickWizardButton.setVisibility(View.VISIBLE); final BolusWizard wizard = quickWizardEntry.doCalc(profile, profileName, actualBg, true); @@ -1221,7 +1222,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } // QuickWizard button - QuickWizardEntry quickWizardEntry = OverviewPlugin.INSTANCE.getQuickWizard().getActive(); + QuickWizardEntry quickWizardEntry = QuickWizard.INSTANCE.getActive(); if (quickWizardEntry != null && lastBG != null && pump.isInitialized() && !pump.isSuspended()) { quickWizardButton.setVisibility(View.VISIBLE); String text = quickWizardEntry.buttonText() + "\n" + DecimalFormatter.to0Decimal(quickWizardEntry.carbs()) + "g"; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt index 708f7f1c7d..0c5b3b4370 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt @@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.general.overview import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R import info.nightscout.androidaps.data.Profile -import info.nightscout.androidaps.data.QuickWizard import info.nightscout.androidaps.events.EventRefreshOverview import info.nightscout.androidaps.interfaces.PluginBase import info.nightscout.androidaps.interfaces.PluginDescription @@ -19,8 +18,6 @@ import info.nightscout.androidaps.utils.SP import info.nightscout.androidaps.utils.plusAssign import io.reactivex.disposables.CompositeDisposable import io.reactivex.schedulers.Schedulers -import org.json.JSONArray -import org.json.JSONException import org.slf4j.LoggerFactory object OverviewPlugin : PluginBase(PluginDescription() @@ -39,19 +36,8 @@ object OverviewPlugin : PluginBase(PluginDescription() var bgTargetLow = 80.0 var bgTargetHigh = 180.0 - var quickWizard = QuickWizard() - var notificationStore = NotificationStore() - init { - val storedData = SP.getString("QuickWizard", "[]") - try { - quickWizard.setData(JSONArray(storedData)) - } catch (e: JSONException) { - log.error("Unhandled exception", e) - } - } - override fun onStart() { super.onStart() disposable += RxBus diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/activities/QuickWizardListActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/activities/QuickWizardListActivity.java index f4c0d5d997..cef5e6c77f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/activities/QuickWizardListActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/activities/QuickWizardListActivity.java @@ -8,6 +8,7 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.cardview.widget.CardView; import androidx.fragment.app.FragmentManager; import androidx.recyclerview.widget.LinearLayoutManager; @@ -19,7 +20,6 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.activities.NoSplashAppCompatActivity; import info.nightscout.androidaps.data.QuickWizard; -import info.nightscout.androidaps.plugins.general.overview.OverviewPlugin; import info.nightscout.androidaps.plugins.general.overview.dialogs.EditQuickWizardDialog; import info.nightscout.androidaps.plugins.general.overview.events.EventQuickWizardChange; import info.nightscout.androidaps.utils.DateUtil; @@ -42,6 +42,7 @@ public class QuickWizardListActivity extends NoSplashAppCompatActivity implement this.fragmentManager = fragmentManager; } + @NonNull @Override public QuickWizardEntryViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.overview_quickwizardlist_item, viewGroup, false); @@ -63,7 +64,7 @@ public class QuickWizardListActivity extends NoSplashAppCompatActivity implement } @Override - public void onAttachedToRecyclerView(RecyclerView recyclerView) { + public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } @@ -80,13 +81,13 @@ public class QuickWizardListActivity extends NoSplashAppCompatActivity implement QuickWizardEntryViewHolder(View itemView, FragmentManager fragmentManager, QuickWizard qvData) { super(itemView); - cv = (CardView) itemView.findViewById(R.id.overview_quickwizard_cardview); - buttonText = (TextView) itemView.findViewById(R.id.overview_quickwizard_item_buttonText); - carbs = (TextView) itemView.findViewById(R.id.overview_quickwizard_item_carbs); - from = (TextView) itemView.findViewById(R.id.overview_quickwizard_item_from); - to = (TextView) itemView.findViewById(R.id.overview_quickwizard_item_to); - editButton = (Button) itemView.findViewById(R.id.overview_quickwizard_item_edit_button); - removeButton = (Button) itemView.findViewById(R.id.overview_quickwizard_item_remove_button); + cv = itemView.findViewById(R.id.overview_quickwizard_cardview); + buttonText = itemView.findViewById(R.id.overview_quickwizard_item_buttonText); + carbs = itemView.findViewById(R.id.overview_quickwizard_item_carbs); + from = itemView.findViewById(R.id.overview_quickwizard_item_from); + to = itemView.findViewById(R.id.overview_quickwizard_item_to); + editButton = itemView.findViewById(R.id.overview_quickwizard_item_edit_button); + removeButton = itemView.findViewById(R.id.overview_quickwizard_item_remove_button); editButton.setOnClickListener(this); removeButton.setOnClickListener(this); this.fragmentManager = fragmentManager; @@ -118,15 +119,15 @@ public class QuickWizardListActivity extends NoSplashAppCompatActivity implement super.onCreate(savedInstanceState); setContentView(R.layout.overview_quickwizardlist_activity); - recyclerView = (RecyclerView) findViewById(R.id.overview_quickwizardactivity_recyclerview); + recyclerView = findViewById(R.id.overview_quickwizardactivity_recyclerview); recyclerView.setHasFixedSize(true); llm = new LinearLayoutManager(this); recyclerView.setLayoutManager(llm); - RecyclerViewAdapter adapter = new RecyclerViewAdapter(OverviewPlugin.INSTANCE.getQuickWizard(), getSupportFragmentManager()); + RecyclerViewAdapter adapter = new RecyclerViewAdapter(QuickWizard.INSTANCE, getSupportFragmentManager()); recyclerView.setAdapter(adapter); - adButton = (Button) findViewById(R.id.overview_quickwizardactivity_add_button); + adButton = findViewById(R.id.overview_quickwizardactivity_add_button); adButton.setOnClickListener(this); } @@ -144,12 +145,10 @@ public class QuickWizardListActivity extends NoSplashAppCompatActivity implement @Override public void onClick(View v) { - switch (v.getId()) { - case R.id.overview_quickwizardactivity_add_button: - FragmentManager manager = getSupportFragmentManager(); - EditQuickWizardDialog editQuickWizardDialog = new EditQuickWizardDialog(); - editQuickWizardDialog.show(manager, "EditQuickWizardDialog"); - break; + if (v.getId() == R.id.overview_quickwizardactivity_add_button) { + FragmentManager manager = getSupportFragmentManager(); + EditQuickWizardDialog editQuickWizardDialog = new EditQuickWizardDialog(); + editQuickWizardDialog.show(manager, "EditQuickWizardDialog"); } } @@ -161,12 +160,9 @@ public class QuickWizardListActivity extends NoSplashAppCompatActivity implement public void updateGUI() { Activity activity = this; if (activity != null && recyclerView != null) { - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainApp.getSpecificPlugin(OverviewPlugin.class).getQuickWizard(), getSupportFragmentManager()); - recyclerView.swapAdapter(adapter, false); - } + activity.runOnUiThread(() -> { + RecyclerViewAdapter adapter = new RecyclerViewAdapter(QuickWizard.INSTANCE, getSupportFragmentManager()); + recyclerView.swapAdapter(adapter, false); }); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.java index 2522f5af05..e995031bfb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/EditQuickWizardDialog.java @@ -31,8 +31,7 @@ import info.nightscout.androidaps.utils.SafeParse; public class EditQuickWizardDialog extends DialogFragment implements View.OnClickListener { private static Logger log = LoggerFactory.getLogger(EditQuickWizardDialog.class); - QuickWizardEntry entry = new QuickWizard().newEmptyItem(); - QuickWizard quickWizard = MainApp.getSpecificPlugin(OverviewPlugin.class).getQuickWizard(); + QuickWizardEntry entry = QuickWizard.INSTANCE.newEmptyItem(); EditText buttonEdit; EditText carbsEdit; @@ -151,7 +150,7 @@ public class EditQuickWizardDialog extends DialogFragment implements View.OnClic } catch (JSONException e) { log.error("Unhandled exception", e); } - quickWizard.addOrUpdate(entry); + QuickWizard.INSTANCE.addOrUpdate(entry); dismiss(); MainApp.bus().post(new EventQuickWizardChange()); break; diff --git a/app/src/test/java/info/nightscout/androidaps/data/QuickWizardTest.java b/app/src/test/java/info/nightscout/androidaps/data/QuickWizardTest.java index 7bf5bbfe70..bda66cc121 100644 --- a/app/src/test/java/info/nightscout/androidaps/data/QuickWizardTest.java +++ b/app/src/test/java/info/nightscout/androidaps/data/QuickWizardTest.java @@ -1,12 +1,12 @@ package info.nightscout.androidaps.data; -import junit.framework.Assert; - import org.json.JSONArray; import org.json.JSONException; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -16,16 +16,15 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.utils.SP; @RunWith(PowerMockRunner.class) -@PrepareForTest({SP.class, MainApp.class}) +@PrepareForTest({SP.class, MainApp.class, Profile.class}) public class QuickWizardTest { - String data1 = "{\"buttonText\":\"Meal\",\"carbs\":36,\"validFrom\":0,\"validTo\":18000," + + private String data1 = "{\"buttonText\":\"Meal\",\"carbs\":36,\"validFrom\":0,\"validTo\":18000," + "\"useBG\":0,\"useCOB\":0,\"useBolusIOB\":0,\"useBasalIOB\":0,\"useTrend\":0,\"useSuperBolus\":0,\"useTemptarget\":0}"; - String data2 = "{\"buttonText\":\"Lunch\",\"carbs\":18,\"validFrom\":36000,\"validTo\":39600," + + private String data2 = "{\"buttonText\":\"Lunch\",\"carbs\":18,\"validFrom\":36000,\"validTo\":39600," + "\"useBG\":0,\"useCOB\":0,\"useBolusIOB\":1,\"useBasalIOB\":2,\"useTrend\":0,\"useSuperBolus\":0,\"useTemptarget\":0}"; - JSONArray array; - QuickWizard qv = new QuickWizard(); + private JSONArray array; public QuickWizardTest() { try { @@ -36,48 +35,64 @@ public class QuickWizardTest { } @Before - public void mock() { + public void mock() throws Exception { AAPSMocker.mockMainApp(); SPMocker.prepareMock(); + PowerMockito.mockStatic(Profile.class); + PowerMockito.when(Profile.class, "secondsFromMidnight").thenReturn(0); + } @Test public void setDataTest() { - qv.setData(array); - Assert.assertEquals(2, qv.size()); + QuickWizard.INSTANCE.setData(array); + Assert.assertEquals(2, QuickWizard.INSTANCE.size()); } @Test public void saveTest() { - qv.setData(array); - qv.save(); + QuickWizard.INSTANCE.setData(array); + QuickWizard.INSTANCE.save(); Assert.assertEquals("[{\"useBolusIOB\":0,\"buttonText\":\"Meal\",\"useTrend\":0,\"carbs\":36,\"useCOB\":0,\"useBasalIOB\":0,\"useTemptarget\":0,\"useBG\":0,\"validFrom\":0,\"useSuperBolus\":0,\"validTo\":18000},{\"useBolusIOB\":1,\"buttonText\":\"Lunch\",\"useTrend\":0,\"carbs\":18,\"useCOB\":0,\"useBasalIOB\":2,\"useTemptarget\":0,\"useBG\":0,\"validFrom\":36000,\"useSuperBolus\":0,\"validTo\":39600}]", SP.getString("QuickWizard", "d")); } @Test public void getTest() { - qv.setData(array); - Assert.assertEquals("Lunch", qv.get(1).buttonText()); - } - - @Test - public void isActive() { + QuickWizard.INSTANCE.setData(array); + Assert.assertEquals("Lunch", QuickWizard.INSTANCE.get(1).buttonText()); } @Test public void getActive() { + QuickWizard.INSTANCE.setData(array); + QuickWizardEntry e = QuickWizard.INSTANCE.getActive(); + Assert.assertEquals(36d, e.carbs(), 0.01d); + QuickWizard.INSTANCE.remove(0); + QuickWizard.INSTANCE.remove(0); + Assert.assertNull(QuickWizard.INSTANCE.getActive()); } @Test public void newEmptyItemTest() { - Assert.assertNotNull(qv.newEmptyItem()); + Assert.assertNotNull(QuickWizard.INSTANCE.newEmptyItem()); } @Test public void addOrUpdate() { + QuickWizard.INSTANCE.setData(array); + Assert.assertEquals(2, QuickWizard.INSTANCE.size()); + QuickWizard.INSTANCE.addOrUpdate(QuickWizard.INSTANCE.newEmptyItem()); + Assert.assertEquals(3, QuickWizard.INSTANCE.size()); + QuickWizardEntry q = QuickWizard.INSTANCE.newEmptyItem(); + q.position = 0; + QuickWizard.INSTANCE.addOrUpdate(q); + Assert.assertEquals(3, QuickWizard.INSTANCE.size()); } @Test public void remove() { + QuickWizard.INSTANCE.setData(array); + QuickWizard.INSTANCE.remove(0); + Assert.assertEquals(1, QuickWizard.INSTANCE.size()); } } \ No newline at end of file