Cleanup Refactoring

This commit is contained in:
Markus M. May 2018-01-30 19:33:10 +01:00
parent 2a1bfe1f6c
commit f68d8f1626
4 changed files with 42 additions and 29 deletions

View file

@ -42,6 +42,7 @@ import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; 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.plugins.Overview.events.EventSetWakeLock;
import info.nightscout.androidaps.tabs.SlidingTabLayout; import info.nightscout.androidaps.tabs.SlidingTabLayout;
import info.nightscout.androidaps.tabs.TabPageAdapter; import info.nightscout.androidaps.tabs.TabPageAdapter;
@ -367,6 +368,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
MainApp.getDbHelper().resetDatabases(); MainApp.getDbHelper().resetDatabases();
// should be handled by Plugin-Interface and
// additional service interface and plugin registry
MainApp.getSpecificPlugin(FoodPlugin.class).getService().resetFood();
} }
}) })
.create() .create()

View file

@ -236,7 +236,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class); TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class);
TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class); TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class); TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class);
// resetFood();
updateEarliestDataChange(0); updateEarliestDataChange(0);
} catch (SQLException e) { } catch (SQLException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);

View file

@ -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); recyclerView.setAdapter(adapter);
loadData(); loadData();

View file

@ -239,11 +239,7 @@ public class FoodService extends OrmLiteBaseService<DatabaseHelper> {
} }
public void createFoodFromJsonIfNotExists(Food food) { public void createFoodFromJsonIfNotExists(Food food) {
try { this.createOrUpdateByNS(food);
this.getDao().createOrUpdate(food);
} catch (SQLException e) {
log.error("Unhandled exception", e);
}
} }
public void deleteNS(JSONObject json) { public void deleteNS(JSONObject json) {
@ -305,35 +301,48 @@ public class FoodService extends OrmLiteBaseService<DatabaseHelper> {
* @return * @return
*/ */
public boolean createOrUpdateByNS(Food food) { public boolean createOrUpdateByNS(Food food) {
try { // find by NS _id
// find by NS _id if (food._id != null && !food._id.equals("")) {
if (food._id != null && !food._id.equals("")) { Food old = this.findByNSId(food._id);
Food old = this.findByNSId(food._id);
if (old != null) { if (old != null) {
if (!old.isEqual(food)) { if (!old.isEqual(food)) {
this.delete(old); // need to delete/create because date may change too this.delete(old); // need to delete/create because date may change too
old.copyFrom(food); old.copyFrom(food);
this.getDao().create(old); this.create(old);
log.debug("FOOD: Updating record by _id: " + old.toString()); return true;
this.scheduleFoodChange(); } else {
return true; return false;
} else {
return false;
}
} }
} else {
this.getDao().createOrUpdate(food);
log.debug("FOOD: New record: " + food.toString());
this.scheduleFoodChange();
return true;
} }
} catch (SQLException e) { } else {
log.error("Unhandled exception", e); this.createOrUpdate(food);
return true;
} }
return false; 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. * finds food by its NS Id.
* *