From 52e4496add447eea216d1d66b2ef6f3c56d744cc Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 22 Jan 2018 21:49:01 +0100 Subject: [PATCH] Careportal DB browser --- .../nightscout/androidaps/data/Profile.java | 1 + .../androidaps/db/CareportalEvent.java | 11 + .../androidaps/db/DatabaseHelper.java | 16 +- .../Treatments/TreatmentsFragment.java | 9 + .../TreatmentsCareportalFragment.java | 192 ++++++++++++++++++ .../layout/treatments_careportal_fragment.xml | 28 +++ .../res/layout/treatments_careportal_item.xml | 104 ++++++++++ .../main/res/layout/treatments_fragment.xml | 10 + 8 files changed, 370 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsCareportalFragment.java create mode 100644 app/src/main/res/layout/treatments_careportal_fragment.xml create mode 100644 app/src/main/res/layout/treatments_careportal_item.xml diff --git a/app/src/main/java/info/nightscout/androidaps/data/Profile.java b/app/src/main/java/info/nightscout/androidaps/data/Profile.java index 5235545362..7bb693ef23 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/Profile.java +++ b/app/src/main/java/info/nightscout/androidaps/data/Profile.java @@ -239,6 +239,7 @@ public class Profile { // if pump not available (at start) // do not store converted array basal_v = null; + isValidated = false; } } diff --git a/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java b/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java index b80f4f932e..8c55034c9f 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java +++ b/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java @@ -191,6 +191,17 @@ public class CareportalEvent implements DataPointWithLabelInterface { return Translator.translate(eventType); } + public String getNotes() { + try { + JSONObject object = new JSONObject(json); + if (object.has("notes")) + return object.getString("notes"); + } catch (JSONException e) { + log.error("Unhandled exception", e); + } + return ""; + } + @Override public long getDuration() { try { 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 69bc542dae..8613f18ab5 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -1473,7 +1473,21 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { } catch (SQLException e) { log.error("Unhandled exception", e); } - return new ArrayList(); + return new ArrayList<>(); + } + + public List getCareportalEventsFromTime(boolean ascending) { + try { + List careportalEvents; + QueryBuilder queryBuilder = getDaoCareportalEvents().queryBuilder(); + queryBuilder.orderBy("date", ascending); + PreparedQuery preparedQuery = queryBuilder.prepare(); + careportalEvents = getDaoCareportalEvents().query(preparedQuery); + return careportalEvents; + } catch (SQLException e) { + log.error("Unhandled exception", e); + } + return new ArrayList<>(); } public void deleteCareportalEventById(String _id) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java index 6fcbcfe951..b10ad96697 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java @@ -20,6 +20,7 @@ import info.nightscout.androidaps.events.EventExtendedBolusChange; import info.nightscout.androidaps.plugins.Common.SubscriberFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Treatments.fragments.TreatmentsBolusFragment; +import info.nightscout.androidaps.plugins.Treatments.fragments.TreatmentsCareportalFragment; import info.nightscout.androidaps.plugins.Treatments.fragments.TreatmentsExtendedBolusesFragment; import info.nightscout.androidaps.plugins.Treatments.fragments.TreatmentsProfileSwitchFragment; import info.nightscout.androidaps.plugins.Treatments.fragments.TreatmentsTempTargetFragment; @@ -33,6 +34,7 @@ public class TreatmentsFragment extends SubscriberFragment implements View.OnCli TextView tempBasalsTab; TextView tempTargetTab; TextView profileSwitchTab; + TextView careportalTab; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -45,11 +47,13 @@ public class TreatmentsFragment extends SubscriberFragment implements View.OnCli tempBasalsTab = (TextView) view.findViewById(R.id.treatments_tempbasals); tempTargetTab = (TextView) view.findViewById(R.id.treatments_temptargets); profileSwitchTab = (TextView) view.findViewById(R.id.treatments_profileswitches); + careportalTab = (TextView) view.findViewById(R.id.treatments_careportal); treatmentsTab.setOnClickListener(this); extendedBolusesTab.setOnClickListener(this); tempBasalsTab.setOnClickListener(this); tempTargetTab.setOnClickListener(this); profileSwitchTab.setOnClickListener(this); + careportalTab.setOnClickListener(this); setFragment(new TreatmentsBolusFragment()); setBackgroundColorOnSelected(treatmentsTab); @@ -87,6 +91,10 @@ public class TreatmentsFragment extends SubscriberFragment implements View.OnCli setFragment(new TreatmentsProfileSwitchFragment()); setBackgroundColorOnSelected(profileSwitchTab); break; + case R.id.treatments_careportal: + setFragment(new TreatmentsCareportalFragment()); + setBackgroundColorOnSelected(careportalTab); + break; } } @@ -104,6 +112,7 @@ public class TreatmentsFragment extends SubscriberFragment implements View.OnCli tempBasalsTab.setBackgroundColor(MainApp.sResources.getColor(R.color.defaultbackground)); tempTargetTab.setBackgroundColor(MainApp.sResources.getColor(R.color.defaultbackground)); profileSwitchTab.setBackgroundColor(MainApp.sResources.getColor(R.color.defaultbackground)); + careportalTab.setBackgroundColor(MainApp.sResources.getColor(R.color.defaultbackground)); selected.setBackgroundColor(MainApp.sResources.getColor(R.color.tabBgColorSelected)); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsCareportalFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsCareportalFragment.java new file mode 100644 index 0000000000..7218198b43 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsCareportalFragment.java @@ -0,0 +1,192 @@ +package info.nightscout.androidaps.plugins.Treatments.fragments; + +import android.app.Activity; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.graphics.Paint; +import android.os.Bundle; +import android.support.v7.app.AlertDialog; +import android.support.v7.widget.CardView; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.TextView; + +import com.squareup.otto.Subscribe; + +import java.util.List; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.Services.Intents; +import info.nightscout.androidaps.db.CareportalEvent; +import info.nightscout.androidaps.events.EventCareportalEventChange; +import info.nightscout.androidaps.plugins.Common.SubscriberFragment; +import info.nightscout.androidaps.plugins.NSClientInternal.UploadQueue; +import info.nightscout.utils.DateUtil; +import info.nightscout.utils.NSUpload; +import info.nightscout.utils.SP; +import info.nightscout.utils.Translator; + +/** + * Created by mike on 13/01/17. + */ + +public class TreatmentsCareportalFragment extends SubscriberFragment implements View.OnClickListener { + + RecyclerView recyclerView; + LinearLayoutManager llm; + Button refreshFromNS; + + Context context; + + public class RecyclerViewAdapter extends RecyclerView.Adapter { + + List careportalEventList; + + RecyclerViewAdapter(List careportalEventList) { + this.careportalEventList = careportalEventList; + } + + @Override + public CareportalEventsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { + View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.treatments_careportal_item, viewGroup, false); + CareportalEventsViewHolder CareportalEventsViewHolder = new CareportalEventsViewHolder(v); + return CareportalEventsViewHolder; + } + + @Override + public void onBindViewHolder(CareportalEventsViewHolder holder, int position) { + CareportalEvent careportalEvent = careportalEventList.get(position); + holder.ns.setVisibility(NSUpload.isIdValid(careportalEvent._id) ? View.VISIBLE : View.GONE); + holder.date.setText(DateUtil.dateAndTimeString(careportalEvent.date)); + holder.note.setText(careportalEvent.getNotes()); + holder.type.setText(Translator.translate(careportalEvent.eventType)); + holder.remove.setTag(careportalEvent); + } + + @Override + public int getItemCount() { + return careportalEventList.size(); + } + + @Override + public void onAttachedToRecyclerView(RecyclerView recyclerView) { + super.onAttachedToRecyclerView(recyclerView); + } + + public class CareportalEventsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { + CardView cv; + TextView date; + TextView type; + TextView note; + TextView remove; + TextView ns; + + CareportalEventsViewHolder(View itemView) { + super(itemView); + cv = (CardView) itemView.findViewById(R.id.careportal_cardview); + date = (TextView) itemView.findViewById(R.id.careportal_date); + type = (TextView) itemView.findViewById(R.id.careportal_type); + note = (TextView) itemView.findViewById(R.id.careportal_note); + ns = (TextView) itemView.findViewById(R.id.ns_sign); + remove = (TextView) itemView.findViewById(R.id.careportal_remove); + remove.setOnClickListener(this); + remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + } + + @Override + public void onClick(View v) { + final CareportalEvent careportalEvent = (CareportalEvent) v.getTag(); + switch (v.getId()) { + case R.id.careportal_remove: + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(MainApp.sResources.getString(R.string.confirmation)); + builder.setMessage(MainApp.sResources.getString(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(careportalEvent.date)); + builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + final String _id = careportalEvent._id; + if (NSUpload.isIdValid(_id)) { + NSUpload.removeCareportalEntryFromNS(_id); + } else { + UploadQueue.removeID("dbAdd", _id); + } + MainApp.getDbHelper().delete(careportalEvent); + } + }); + builder.setNegativeButton(MainApp.sResources.getString(R.string.cancel), null); + builder.show(); + break; + } + } + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.treatments_careportal_fragment, container, false); + + recyclerView = (RecyclerView) view.findViewById(R.id.careportal_recyclerview); + recyclerView.setHasFixedSize(true); + llm = new LinearLayoutManager(view.getContext()); + recyclerView.setLayoutManager(llm); + + RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEventsFromTime(false)); + recyclerView.setAdapter(adapter); + + refreshFromNS = (Button) view.findViewById(R.id.careportal_refreshfromnightscout); + refreshFromNS.setOnClickListener(this); + + context = getContext(); + + boolean nsUploadOnly = SP.getBoolean(R.string.key_ns_upload_only, false); + if (nsUploadOnly) + refreshFromNS.setVisibility(View.GONE); + + updateGUI(); + return view; + } + + @Override + public void onClick(View view) { + switch (view.getId()) { + case R.id.careportal_refreshfromnightscout: + AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext()); + builder.setTitle(this.getContext().getString(R.string.confirmation)); + builder.setMessage(this.getContext().getString(R.string.refresheventsfromnightscout) + " ?"); + builder.setPositiveButton(this.getContext().getString(R.string.ok), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + MainApp.getDbHelper().resetCareportalEvents(); + Intent restartNSClient = new Intent(Intents.ACTION_RESTART); + MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient); + } + }); + builder.setNegativeButton(this.getContext().getString(R.string.cancel), null); + builder.show(); + break; + } + + } + + @Subscribe + public void onStatusEvent(final EventCareportalEventChange ev) { + updateGUI(); + } + + @Override + protected void updateGUI() { + Activity activity = getActivity(); + if (activity != null) + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEventsFromTime(false)), false); + } + }); + } +} diff --git a/app/src/main/res/layout/treatments_careportal_fragment.xml b/app/src/main/res/layout/treatments_careportal_fragment.xml new file mode 100644 index 0000000000..710afed7bb --- /dev/null +++ b/app/src/main/res/layout/treatments_careportal_fragment.xml @@ -0,0 +1,28 @@ + + + + +