From f68d8f1626e7e11c0ab3675494268a86f4d987c8 Mon Sep 17 00:00:00 2001 From: "Markus M. May" Date: Tue, 30 Jan 2018 19:33:10 +0100 Subject: [PATCH] Cleanup Refactoring --- .../nightscout/androidaps/MainActivity.java | 4 ++ .../androidaps/db/DatabaseHelper.java | 1 - .../androidaps/plugins/Food/FoodFragment.java | 3 +- .../androidaps/plugins/Food/FoodService.java | 63 +++++++++++-------- 4 files changed, 42 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index e77be36ba6..ef09a9c7b4 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -42,6 +42,7 @@ import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.Food.FoodPlugin; import info.nightscout.androidaps.plugins.Overview.events.EventSetWakeLock; import info.nightscout.androidaps.tabs.SlidingTabLayout; import info.nightscout.androidaps.tabs.TabPageAdapter; @@ -367,6 +368,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe @Override public void onClick(DialogInterface dialog, int which) { MainApp.getDbHelper().resetDatabases(); + // should be handled by Plugin-Interface and + // additional service interface and plugin registry + MainApp.getSpecificPlugin(FoodPlugin.class).getService().resetFood(); } }) .create() diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index aabefb1307..9738192593 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -236,7 +236,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class); TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class); TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class); -// resetFood(); updateEarliestDataChange(0); } catch (SQLException e) { log.error("Unhandled exception", e); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodFragment.java index 2a3bf0788a..c5032c5299 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodFragment.java @@ -121,7 +121,8 @@ public class FoodFragment extends SubscriberFragment { } }); - RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainApp.getSpecificPlugin(FoodPlugin.class).getService().getFoodData()); + RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainApp + .getSpecificPlugin(FoodPlugin.class).getService().getFoodData()); recyclerView.setAdapter(adapter); loadData(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodService.java index bb0487dc53..617c975825 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodService.java @@ -239,11 +239,7 @@ public class FoodService extends OrmLiteBaseService { } public void createFoodFromJsonIfNotExists(Food food) { - try { - this.getDao().createOrUpdate(food); - } catch (SQLException e) { - log.error("Unhandled exception", e); - } + this.createOrUpdateByNS(food); } public void deleteNS(JSONObject json) { @@ -305,35 +301,48 @@ public class FoodService extends OrmLiteBaseService { * @return */ public boolean createOrUpdateByNS(Food food) { - try { - // find by NS _id - if (food._id != null && !food._id.equals("")) { - Food old = this.findByNSId(food._id); + // find by NS _id + if (food._id != null && !food._id.equals("")) { + Food old = this.findByNSId(food._id); - if (old != null) { - if (!old.isEqual(food)) { - this.delete(old); // need to delete/create because date may change too - old.copyFrom(food); - this.getDao().create(old); - log.debug("FOOD: Updating record by _id: " + old.toString()); - this.scheduleFoodChange(); - return true; - } else { - return false; - } + if (old != null) { + if (!old.isEqual(food)) { + this.delete(old); // need to delete/create because date may change too + old.copyFrom(food); + this.create(old); + return true; + } else { + return false; } - } else { - this.getDao().createOrUpdate(food); - log.debug("FOOD: New record: " + food.toString()); - this.scheduleFoodChange(); - return true; } - } catch (SQLException e) { - log.error("Unhandled exception", e); + } else { + this.createOrUpdate(food); + return true; } + return false; } + public void createOrUpdate(Food food) { + try { + this.getDao().createOrUpdate(food); + log.debug("FOOD: Created or Updated: " + food.toString()); + } catch (SQLException e) { + log.error("Unable to createOrUpdate Food", e); + } + this.scheduleFoodChange(); + } + + public void create(Food food) { + try { + this.getDao().create(food); + log.debug("FOOD: New record: " + food.toString()); + } catch (SQLException e) { + log.error("Unable to create Food", e); + } + this.scheduleFoodChange(); + } + /** * finds food by its NS Id. *