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 e2ace23845..47b8aa222f 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -230,6 +230,16 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { scheduleTreatmentChange(); } + public void delete(Treatment treatment) { + try { + getDaoTreatments().delete(treatment); + latestTreatmentChange = treatment.getTimeIndex(); + } catch (SQLException e) { + e.printStackTrace(); + } + scheduleTreatmentChange(); + } + public int delete(String _id) { Treatment stored = findTreatmentById(_id); int removed = 0; 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 fed1191f38..38a4904b56 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 @@ -1,9 +1,11 @@ package info.nightscout.androidaps.plugins.Treatments; import android.app.Activity; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.Paint; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; @@ -55,7 +57,9 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener TextView activityTotal; Button refreshFromNS; - public static class RecyclerViewAdapter extends RecyclerView.Adapter { + Context context; + + public class RecyclerViewAdapter extends RecyclerView.Adapter { List treatments; @@ -88,6 +92,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.colorAffectingIOB)); else holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.cardColorBackground)); + holder.remove.setTag(treatments.get(position)); } @Override @@ -100,7 +105,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener super.onAttachedToRecyclerView(recyclerView); } - public static class TreatmentsViewHolder extends RecyclerView.ViewHolder { + public class TreatmentsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { CardView cv; TextView date; TextView insulin; @@ -109,6 +114,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener TextView activity; TextView mealOrCorrection; LinearLayout dateLinearLayout; + TextView remove; TreatmentsViewHolder(View itemView) { super(itemView); @@ -120,6 +126,35 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener activity = (TextView) itemView.findViewById(R.id.treatments_activity); mealOrCorrection = (TextView) itemView.findViewById(R.id.treatments_mealorcorrection); dateLinearLayout = (LinearLayout) itemView.findViewById(R.id.treatments_datelinearlayout); + remove = (TextView) itemView.findViewById(R.id.treatments_remove); + remove.setOnClickListener(this); + remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + } + + @Override + public void onClick(View v) { + final Treatment treatment = (Treatment) v.getTag(); + final Context finalContext = context; + switch (v.getId()) { + case R.id.treatments_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(treatment.created_at)); + builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + final String _id = treatment._id; + if (_id != null && !_id.equals("")) { + MainApp.getConfigBuilder().removeCareportalEntryFromNS(_id); + } + MainApp.getDbHelper().delete(treatment); + treatmentsPlugin.initializeData(); + updateGUI(); + } + }); + builder.setNegativeButton(MainApp.sResources.getString(R.string.cancel), null); + builder.show(); + break; + } } } } @@ -143,6 +178,8 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener refreshFromNS = (Button) view.findViewById(R.id.treatments_reshreshfromnightscout); refreshFromNS.setOnClickListener(this); + context = getContext(); + updateGUI(); return view; } @@ -153,8 +190,8 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener case R.id.treatments_reshreshfromnightscout: SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getContext()); boolean nsUploadOnly = SP.getBoolean("ns_upload_only", false); - if(nsUploadOnly){ - ToastUtils.showToastInUiThread(getContext(),this.getContext().getString(R.string.ns_upload_only_enabled)); + if (nsUploadOnly) { + ToastUtils.showToastInUiThread(getContext(), this.getContext().getString(R.string.ns_upload_only_enabled)); } else { AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext()); builder.setTitle(this.getContext().getString(R.string.confirmation)); diff --git a/app/src/main/res/layout/treatments_item.xml b/app/src/main/res/layout/treatments_item.xml index 662069a5e3..f82b335d37 100644 --- a/app/src/main/res/layout/treatments_item.xml +++ b/app/src/main/res/layout/treatments_item.xml @@ -134,6 +134,13 @@ android:layout_marginRight="30dp" android:textStyle="bold" /> +