Cleanup Refactoring
This commit is contained in:
parent
2a1bfe1f6c
commit
f68d8f1626
4 changed files with 42 additions and 29 deletions
|
@ -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()
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -239,11 +239,7 @@ public class FoodService extends OrmLiteBaseService<DatabaseHelper> {
|
|||
}
|
||||
|
||||
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<DatabaseHelper> {
|
|||
* @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.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue