Events to RxBus
This commit is contained in:
parent
30cf4f77da
commit
4ed8f0a1e1
12 changed files with 199 additions and 229 deletions
|
@ -1,8 +0,0 @@
|
||||||
package info.nightscout.androidaps.events;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by mike on 20.09.2017.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class EventFoodDatabaseChanged extends Event {
|
|
||||||
}
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
package info.nightscout.androidaps.events
|
||||||
|
|
||||||
|
class EventFoodDatabaseChanged : Event()
|
|
@ -1,35 +0,0 @@
|
||||||
package info.nightscout.androidaps.events;
|
|
||||||
|
|
||||||
import android.os.Bundle;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Event which is published with data fetched from NightScout specific for the
|
|
||||||
* Food-class.
|
|
||||||
*
|
|
||||||
* Payload is the from NS retrieved JSON-String which should be handled by all
|
|
||||||
* subscriber.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class EventNsFood extends Event {
|
|
||||||
|
|
||||||
public static final int ADD = 0;
|
|
||||||
public static final int UPDATE = 1;
|
|
||||||
public static final int REMOVE = 2;
|
|
||||||
|
|
||||||
private final int mode;
|
|
||||||
|
|
||||||
private final Bundle payload;
|
|
||||||
|
|
||||||
public EventNsFood(int mode, Bundle payload) {
|
|
||||||
this.mode = mode;
|
|
||||||
this.payload = payload;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getMode() {
|
|
||||||
return mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Bundle getPayload() {
|
|
||||||
return payload;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
package info.nightscout.androidaps.events
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event which is published with data fetched from NightScout specific for the
|
||||||
|
* Food-class.
|
||||||
|
*
|
||||||
|
* Payload is the from NS retrieved JSON-String which should be handled by all
|
||||||
|
* subscriber.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class EventNsFood(val mode: Int, val payload: Bundle) : Event() {
|
||||||
|
companion object {
|
||||||
|
val ADD = 0
|
||||||
|
val UPDATE = 1
|
||||||
|
val REMOVE = 2
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,36 +0,0 @@
|
||||||
package info.nightscout.androidaps.events;
|
|
||||||
|
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Event which is published with data fetched from NightScout specific for the
|
|
||||||
* Treatment-class.
|
|
||||||
* <p>
|
|
||||||
* Payload is the from NS retrieved JSON-String which should be handled by all
|
|
||||||
* subscriber.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class EventNsTreatment extends Event {
|
|
||||||
|
|
||||||
public static final int ADD = 0;
|
|
||||||
public static final int UPDATE = 1;
|
|
||||||
public static final int REMOVE = 2;
|
|
||||||
|
|
||||||
private final int mode;
|
|
||||||
|
|
||||||
private final JSONObject payload;
|
|
||||||
|
|
||||||
public EventNsTreatment(int mode, JSONObject payload) {
|
|
||||||
this.mode = mode;
|
|
||||||
this.payload = payload;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getMode() {
|
|
||||||
return mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JSONObject getPayload() {
|
|
||||||
return payload;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
package info.nightscout.androidaps.events
|
||||||
|
|
||||||
|
import org.json.JSONObject
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event which is published with data fetched from NightScout specific for the
|
||||||
|
* Treatment-class.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Payload is the from NS retrieved JSON-String which should be handled by all
|
||||||
|
* subscriber.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class EventNsTreatment(val mode: Int, val payload: JSONObject) : Event() {
|
||||||
|
companion object {
|
||||||
|
val ADD = 0
|
||||||
|
val UPDATE = 1
|
||||||
|
val REMOVE = 2
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +0,0 @@
|
||||||
package info.nightscout.androidaps.events;
|
|
||||||
|
|
||||||
/** Base class for events to update the UI, mostly a specific tab. */
|
|
||||||
public abstract class EventUpdateGui extends Event {
|
|
||||||
}
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
package info.nightscout.androidaps.events
|
||||||
|
|
||||||
|
/** Base class for events to update the UI, mostly a specific tab. */
|
||||||
|
abstract class EventUpdateGui : Event()
|
|
@ -1,12 +1,8 @@
|
||||||
package info.nightscout.androidaps.plugins.general.food;
|
package info.nightscout.androidaps.plugins.general.food;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import androidx.appcompat.app.AlertDialog;
|
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
|
||||||
import android.text.Editable;
|
import android.text.Editable;
|
||||||
import android.text.TextWatcher;
|
import android.text.TextWatcher;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
|
@ -18,7 +14,10 @@ import android.widget.EditText;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.squareup.otto.Subscribe;
|
import androidx.appcompat.app.AlertDialog;
|
||||||
|
import androidx.fragment.app.Fragment;
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -31,17 +30,20 @@ import java.util.Set;
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
import info.nightscout.androidaps.R;
|
import info.nightscout.androidaps.R;
|
||||||
import info.nightscout.androidaps.events.EventFoodDatabaseChanged;
|
import info.nightscout.androidaps.events.EventFoodDatabaseChanged;
|
||||||
import info.nightscout.androidaps.plugins.common.SubscriberFragment;
|
import info.nightscout.androidaps.plugins.bus.RxBus;
|
||||||
import info.nightscout.androidaps.utils.FabricPrivacy;
|
|
||||||
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
|
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
|
||||||
|
import info.nightscout.androidaps.utils.FabricPrivacy;
|
||||||
import info.nightscout.androidaps.utils.SpinnerHelper;
|
import info.nightscout.androidaps.utils.SpinnerHelper;
|
||||||
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||||
|
import io.reactivex.disposables.CompositeDisposable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by mike on 16.10.2017.
|
* Created by mike on 16.10.2017.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class FoodFragment extends SubscriberFragment {
|
public class FoodFragment extends Fragment {
|
||||||
private static Logger log = LoggerFactory.getLogger(FoodFragment.class);
|
private static Logger log = LoggerFactory.getLogger(FoodFragment.class);
|
||||||
|
private CompositeDisposable disposable = new CompositeDisposable();
|
||||||
|
|
||||||
EditText filter;
|
EditText filter;
|
||||||
ImageView clearFilter;
|
ImageView clearFilter;
|
||||||
|
@ -59,89 +61,93 @@ public class FoodFragment extends SubscriberFragment {
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
try {
|
View view = inflater.inflate(R.layout.food_fragment, container, false);
|
||||||
View view = inflater.inflate(R.layout.food_fragment, container, false);
|
filter = (EditText) view.findViewById(R.id.food_filter);
|
||||||
filter = (EditText) view.findViewById(R.id.food_filter);
|
clearFilter = (ImageView) view.findViewById(R.id.food_clearfilter);
|
||||||
clearFilter = (ImageView) view.findViewById(R.id.food_clearfilter);
|
category = new SpinnerHelper(view.findViewById(R.id.food_category));
|
||||||
category = new SpinnerHelper(view.findViewById(R.id.food_category));
|
subcategory = new SpinnerHelper(view.findViewById(R.id.food_subcategory));
|
||||||
subcategory = new SpinnerHelper(view.findViewById(R.id.food_subcategory));
|
recyclerView = (RecyclerView) view.findViewById(R.id.food_recyclerview);
|
||||||
recyclerView = (RecyclerView) view.findViewById(R.id.food_recyclerview);
|
recyclerView.setHasFixedSize(true);
|
||||||
recyclerView.setHasFixedSize(true);
|
LinearLayoutManager llm = new LinearLayoutManager(view.getContext());
|
||||||
LinearLayoutManager llm = new LinearLayoutManager(view.getContext());
|
recyclerView.setLayoutManager(llm);
|
||||||
recyclerView.setLayoutManager(llm);
|
|
||||||
|
|
||||||
clearFilter.setOnClickListener(new View.OnClickListener() {
|
clearFilter.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
filter.setText("");
|
filter.setText("");
|
||||||
category.setSelection(0);
|
category.setSelection(0);
|
||||||
subcategory.setSelection(0);
|
subcategory.setSelection(0);
|
||||||
filterData();
|
filterData();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||||
fillSubcategories();
|
fillSubcategories();
|
||||||
filterData();
|
filterData();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNothingSelected(AdapterView<?> parent) {
|
public void onNothingSelected(AdapterView<?> parent) {
|
||||||
fillSubcategories();
|
fillSubcategories();
|
||||||
filterData();
|
filterData();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
subcategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
subcategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||||
filterData();
|
filterData();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNothingSelected(AdapterView<?> parent) {
|
public void onNothingSelected(AdapterView<?> parent) {
|
||||||
filterData();
|
filterData();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
filter.addTextChangedListener(new TextWatcher() {
|
filter.addTextChangedListener(new TextWatcher() {
|
||||||
@Override
|
@Override
|
||||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||||
filterData();
|
filterData();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterTextChanged(Editable s) {
|
public void afterTextChanged(Editable s) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainApp
|
RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainApp
|
||||||
.getSpecificPlugin(FoodPlugin.class).getService().getFoodData());
|
.getSpecificPlugin(FoodPlugin.class).getService().getFoodData());
|
||||||
recyclerView.setAdapter(adapter);
|
recyclerView.setAdapter(adapter);
|
||||||
|
|
||||||
loadData();
|
loadData();
|
||||||
fillCategories();
|
fillCategories();
|
||||||
fillSubcategories();
|
fillSubcategories();
|
||||||
filterData();
|
filterData();
|
||||||
return view;
|
return view;
|
||||||
} catch (Exception e) {
|
|
||||||
FabricPrivacy.logException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Override
|
||||||
@SuppressWarnings("unused")
|
public synchronized void onResume() {
|
||||||
public void onStatusEvent(final EventFoodDatabaseChanged ev) {
|
super.onResume();
|
||||||
loadData();
|
disposable.add(RxBus.INSTANCE
|
||||||
filterData();
|
.toObservable(EventFoodDatabaseChanged.class)
|
||||||
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.subscribe(event -> updateGui(), FabricPrivacy::logException)
|
||||||
|
);
|
||||||
|
updateGui();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
disposable.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadData() {
|
void loadData() {
|
||||||
|
@ -207,19 +213,11 @@ public class FoodFragment extends SubscriberFragment {
|
||||||
filtered.add(f);
|
filtered.add(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateGUI();
|
updateGui();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
protected void updateGui() {
|
||||||
protected void updateGUI() {
|
recyclerView.swapAdapter(new FoodFragment.RecyclerViewAdapter(filtered), true);
|
||||||
Activity activity = getActivity();
|
|
||||||
if (activity != null)
|
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
recyclerView.swapAdapter(new FoodFragment.RecyclerViewAdapter(filtered), true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.FoodsViewHolder> {
|
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.FoodsViewHolder> {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.general.food;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
import com.j256.ormlite.android.apptools.OpenHelperManager;
|
import com.j256.ormlite.android.apptools.OpenHelperManager;
|
||||||
|
@ -11,7 +12,6 @@ import com.j256.ormlite.dao.Dao;
|
||||||
import com.j256.ormlite.dao.DaoManager;
|
import com.j256.ormlite.dao.DaoManager;
|
||||||
import com.j256.ormlite.support.ConnectionSource;
|
import com.j256.ormlite.support.ConnectionSource;
|
||||||
import com.j256.ormlite.table.TableUtils;
|
import com.j256.ormlite.table.TableUtils;
|
||||||
import com.squareup.otto.Subscribe;
|
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
|
@ -34,6 +34,10 @@ import info.nightscout.androidaps.events.Event;
|
||||||
import info.nightscout.androidaps.events.EventFoodDatabaseChanged;
|
import info.nightscout.androidaps.events.EventFoodDatabaseChanged;
|
||||||
import info.nightscout.androidaps.events.EventNsFood;
|
import info.nightscout.androidaps.events.EventNsFood;
|
||||||
import info.nightscout.androidaps.logging.L;
|
import info.nightscout.androidaps.logging.L;
|
||||||
|
import info.nightscout.androidaps.plugins.bus.RxBus;
|
||||||
|
import info.nightscout.androidaps.utils.FabricPrivacy;
|
||||||
|
import io.reactivex.disposables.CompositeDisposable;
|
||||||
|
import io.reactivex.schedulers.Schedulers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by mike on 24.09.2017.
|
* Created by mike on 24.09.2017.
|
||||||
|
@ -41,6 +45,7 @@ import info.nightscout.androidaps.logging.L;
|
||||||
|
|
||||||
public class FoodService extends OrmLiteBaseService<DatabaseHelper> {
|
public class FoodService extends OrmLiteBaseService<DatabaseHelper> {
|
||||||
private Logger log = LoggerFactory.getLogger(L.DATAFOOD);
|
private Logger log = LoggerFactory.getLogger(L.DATAFOOD);
|
||||||
|
private CompositeDisposable disposable = new CompositeDisposable();
|
||||||
|
|
||||||
private static final ScheduledExecutorService foodEventWorker = Executors.newSingleThreadScheduledExecutor();
|
private static final ScheduledExecutorService foodEventWorker = Executors.newSingleThreadScheduledExecutor();
|
||||||
private static ScheduledFuture<?> scheduledFoodEventPost = null;
|
private static ScheduledFuture<?> scheduledFoodEventPost = null;
|
||||||
|
@ -48,7 +53,34 @@ public class FoodService extends OrmLiteBaseService<DatabaseHelper> {
|
||||||
public FoodService() {
|
public FoodService() {
|
||||||
onCreate();
|
onCreate();
|
||||||
dbInitialize();
|
dbInitialize();
|
||||||
MainApp.bus().register(this);
|
disposable.add(RxBus.INSTANCE
|
||||||
|
.toObservable(EventNsFood.class)
|
||||||
|
.observeOn(Schedulers.io())
|
||||||
|
.subscribe(event -> {
|
||||||
|
int mode = event.getMode();
|
||||||
|
Bundle payload = event.getPayload();
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (payload.containsKey("food")) {
|
||||||
|
JSONObject json = new JSONObject(payload.getString("food"));
|
||||||
|
if (mode == EventNsFood.Companion.getADD() || mode == EventNsFood.Companion.getUPDATE())
|
||||||
|
this.createFoodFromJsonIfNotExists(json);
|
||||||
|
else
|
||||||
|
this.deleteNS(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (payload.containsKey("foods")) {
|
||||||
|
JSONArray array = new JSONArray(payload.getString("foods"));
|
||||||
|
if (mode == EventNsFood.Companion.getADD() || mode == EventNsFood.Companion.getUPDATE())
|
||||||
|
this.createFoodFromJsonIfNotExists(array);
|
||||||
|
else
|
||||||
|
this.deleteNS(array);
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
log.error("Unhandled Exception", e);
|
||||||
|
}
|
||||||
|
}, FabricPrivacy::logException)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,34 +111,6 @@ public class FoodService extends OrmLiteBaseService<DatabaseHelper> {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
|
||||||
public void handleNsEvent(EventNsFood event) {
|
|
||||||
int mode = event.getMode();
|
|
||||||
Bundle payload = event.getPayload();
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (payload.containsKey("food")) {
|
|
||||||
JSONObject json = new JSONObject(payload.getString("food"));
|
|
||||||
if (mode == EventNsFood.ADD || mode == EventNsFood.UPDATE) {
|
|
||||||
this.createFoodFromJsonIfNotExists(json);
|
|
||||||
} else {
|
|
||||||
this.deleteNS(json);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (payload.containsKey("foods")) {
|
|
||||||
JSONArray array = new JSONArray(payload.getString("foods"));
|
|
||||||
if (mode == EventNsFood.ADD || mode == EventNsFood.UPDATE) {
|
|
||||||
this.createFoodFromJsonIfNotExists(array);
|
|
||||||
} else {
|
|
||||||
this.deleteNS(array);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
|
||||||
log.error("Unhandled Exception", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
@ -162,7 +166,7 @@ public class FoodService extends OrmLiteBaseService<DatabaseHelper> {
|
||||||
public void run() {
|
public void run() {
|
||||||
if (L.isEnabled(L.DATAFOOD))
|
if (L.isEnabled(L.DATAFOOD))
|
||||||
log.debug("Firing EventFoodChange");
|
log.debug("Firing EventFoodChange");
|
||||||
MainApp.bus().post(event);
|
RxBus.INSTANCE.send(event);
|
||||||
callback.setPost(null);
|
callback.setPost(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,13 +35,17 @@ import info.nightscout.androidaps.db.ICallback;
|
||||||
import info.nightscout.androidaps.db.Source;
|
import info.nightscout.androidaps.db.Source;
|
||||||
import info.nightscout.androidaps.events.Event;
|
import info.nightscout.androidaps.events.Event;
|
||||||
import info.nightscout.androidaps.events.EventNsTreatment;
|
import info.nightscout.androidaps.events.EventNsTreatment;
|
||||||
|
import info.nightscout.androidaps.events.EventPreferenceChange;
|
||||||
import info.nightscout.androidaps.events.EventReloadTreatmentData;
|
import info.nightscout.androidaps.events.EventReloadTreatmentData;
|
||||||
import info.nightscout.androidaps.events.EventTreatmentChange;
|
import info.nightscout.androidaps.events.EventTreatmentChange;
|
||||||
import info.nightscout.androidaps.logging.L;
|
import info.nightscout.androidaps.logging.L;
|
||||||
import info.nightscout.androidaps.plugins.bus.RxBus;
|
import info.nightscout.androidaps.plugins.bus.RxBus;
|
||||||
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventNewHistoryData;
|
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventNewHistoryData;
|
||||||
import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil;
|
import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil;
|
||||||
|
import info.nightscout.androidaps.utils.FabricPrivacy;
|
||||||
import info.nightscout.androidaps.utils.JsonHelper;
|
import info.nightscout.androidaps.utils.JsonHelper;
|
||||||
|
import io.reactivex.disposables.CompositeDisposable;
|
||||||
|
import io.reactivex.schedulers.Schedulers;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,6 +54,7 @@ import info.nightscout.androidaps.utils.JsonHelper;
|
||||||
|
|
||||||
public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
|
public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
|
||||||
private static Logger log = LoggerFactory.getLogger(L.DATATREATMENTS);
|
private static Logger log = LoggerFactory.getLogger(L.DATATREATMENTS);
|
||||||
|
private CompositeDisposable disposable = new CompositeDisposable();
|
||||||
|
|
||||||
private static final ScheduledExecutorService treatmentEventWorker = Executors.newSingleThreadScheduledExecutor();
|
private static final ScheduledExecutorService treatmentEventWorker = Executors.newSingleThreadScheduledExecutor();
|
||||||
private static ScheduledFuture<?> scheduledTreatmentEventPost = null;
|
private static ScheduledFuture<?> scheduledTreatmentEventPost = null;
|
||||||
|
@ -57,7 +62,20 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
|
||||||
public TreatmentService() {
|
public TreatmentService() {
|
||||||
onCreate();
|
onCreate();
|
||||||
dbInitialize();
|
dbInitialize();
|
||||||
MainApp.bus().register(this);
|
disposable.add(RxBus.INSTANCE
|
||||||
|
.toObservable(EventNsTreatment.class)
|
||||||
|
.observeOn(Schedulers.io())
|
||||||
|
.subscribe(event -> {
|
||||||
|
int mode = event.getMode();
|
||||||
|
JSONObject payload = event.getPayload();
|
||||||
|
|
||||||
|
if (mode == EventNsTreatment.Companion.getADD() || mode == EventNsTreatment.Companion.getUPDATE()) {
|
||||||
|
this.createTreatmentFromJsonIfNotExists(payload);
|
||||||
|
} else { // EventNsTreatment.REMOVE
|
||||||
|
this.deleteNS(payload);
|
||||||
|
}
|
||||||
|
}, FabricPrivacy::logException)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -88,19 +106,6 @@ public class TreatmentService extends OrmLiteBaseService<DatabaseHelper> {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public void handleNsEvent(EventNsTreatment event) {
|
|
||||||
int mode = event.getMode();
|
|
||||||
JSONObject payload = event.getPayload();
|
|
||||||
|
|
||||||
if (mode == EventNsTreatment.ADD || mode == EventNsTreatment.UPDATE) {
|
|
||||||
this.createTreatmentFromJsonIfNotExists(payload);
|
|
||||||
} else { // EventNsTreatment.REMOVE
|
|
||||||
this.deleteNS(payload);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
|
|
@ -87,14 +87,14 @@ public class DataService extends IntentService {
|
||||||
} else if (Intents.ACTION_NEW_STATUS.equals(action)) {
|
} else if (Intents.ACTION_NEW_STATUS.equals(action)) {
|
||||||
NSSettingsStatus.getInstance().handleNewData(intent);
|
NSSettingsStatus.getInstance().handleNewData(intent);
|
||||||
} else if (Intents.ACTION_NEW_FOOD.equals(action)) {
|
} else if (Intents.ACTION_NEW_FOOD.equals(action)) {
|
||||||
EventNsFood evt = new EventNsFood(EventNsFood.ADD, bundles);
|
EventNsFood evt = new EventNsFood(EventNsFood.Companion.getADD(), bundles);
|
||||||
MainApp.bus().post(evt);
|
RxBus.INSTANCE.send(evt);
|
||||||
} else if (Intents.ACTION_CHANGED_FOOD.equals(action)) {
|
} else if (Intents.ACTION_CHANGED_FOOD.equals(action)) {
|
||||||
EventNsFood evt = new EventNsFood(EventNsFood.UPDATE, bundles);
|
EventNsFood evt = new EventNsFood(EventNsFood.Companion.getUPDATE(), bundles);
|
||||||
MainApp.bus().post(evt);
|
RxBus.INSTANCE.send(evt);
|
||||||
} else if (Intents.ACTION_REMOVED_FOOD.equals(action)) {
|
} else if (Intents.ACTION_REMOVED_FOOD.equals(action)) {
|
||||||
EventNsFood evt = new EventNsFood(EventNsFood.REMOVE, bundles);
|
EventNsFood evt = new EventNsFood(EventNsFood.Companion.getREMOVE(), bundles);
|
||||||
MainApp.bus().post(evt);
|
RxBus.INSTANCE.send(evt);
|
||||||
} else if (acceptNSData &&
|
} else if (acceptNSData &&
|
||||||
(Intents.ACTION_NEW_TREATMENT.equals(action) ||
|
(Intents.ACTION_NEW_TREATMENT.equals(action) ||
|
||||||
Intents.ACTION_CHANGED_TREATMENT.equals(action) ||
|
Intents.ACTION_CHANGED_TREATMENT.equals(action) ||
|
||||||
|
@ -198,8 +198,8 @@ public class DataService extends IntentService {
|
||||||
|
|
||||||
private void handleRemovedTreatmentFromNS(JSONObject json) {
|
private void handleRemovedTreatmentFromNS(JSONObject json) {
|
||||||
// new DB model
|
// new DB model
|
||||||
EventNsTreatment evtTreatment = new EventNsTreatment(EventNsTreatment.REMOVE, json);
|
EventNsTreatment evtTreatment = new EventNsTreatment(EventNsTreatment.Companion.getREMOVE(), json);
|
||||||
MainApp.bus().post(evtTreatment);
|
RxBus.INSTANCE.send(evtTreatment);
|
||||||
// old DB model
|
// old DB model
|
||||||
String _id = JsonHelper.safeGetString(json, "_id");
|
String _id = JsonHelper.safeGetString(json, "_id");
|
||||||
MainApp.getDbHelper().deleteTempTargetById(_id);
|
MainApp.getDbHelper().deleteTempTargetById(_id);
|
||||||
|
@ -211,7 +211,7 @@ public class DataService extends IntentService {
|
||||||
|
|
||||||
private void handleTreatmentFromNS(JSONObject json, Intent intent) {
|
private void handleTreatmentFromNS(JSONObject json, Intent intent) {
|
||||||
// new DB model
|
// new DB model
|
||||||
int mode = Intents.ACTION_NEW_TREATMENT.equals(intent.getAction()) ? EventNsTreatment.ADD : EventNsTreatment.UPDATE;
|
int mode = Intents.ACTION_NEW_TREATMENT.equals(intent.getAction()) ? EventNsTreatment.Companion.getADD() : EventNsTreatment.Companion.getUPDATE();
|
||||||
double insulin = JsonHelper.safeGetDouble(json, "insulin");
|
double insulin = JsonHelper.safeGetDouble(json, "insulin");
|
||||||
double carbs = JsonHelper.safeGetDouble(json, "carbs");
|
double carbs = JsonHelper.safeGetDouble(json, "carbs");
|
||||||
String eventType = JsonHelper.safeGetString(json, "eventType");
|
String eventType = JsonHelper.safeGetString(json, "eventType");
|
||||||
|
@ -221,7 +221,7 @@ public class DataService extends IntentService {
|
||||||
}
|
}
|
||||||
if (insulin > 0 || carbs > 0) {
|
if (insulin > 0 || carbs > 0) {
|
||||||
EventNsTreatment evtTreatment = new EventNsTreatment(mode, json);
|
EventNsTreatment evtTreatment = new EventNsTreatment(mode, json);
|
||||||
MainApp.bus().post(evtTreatment);
|
RxBus.INSTANCE.send(evtTreatment);
|
||||||
} else if (json.has(DanaRNSHistorySync.DANARSIGNATURE)) {
|
} else if (json.has(DanaRNSHistorySync.DANARSIGNATURE)) {
|
||||||
// old DB model
|
// old DB model
|
||||||
MainApp.getDbHelper().updateDanaRHistoryRecordId(json);
|
MainApp.getDbHelper().updateDanaRHistoryRecordId(json);
|
||||||
|
|
Loading…
Reference in a new issue