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 b3cd0ace70..17c01bd9ff 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -421,6 +421,16 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { return new ArrayList(); } + public void delete(TempBasal tempBasal) { + try { + getDaoTempBasals().delete(tempBasal); + latestTreatmentChange = tempBasal.getTimeIndex(); + } catch (SQLException e) { + e.printStackTrace(); + } + scheduleTreatmentChange(); + } + public List getTemptargetsDataFromTime(long mills, boolean ascending) { try { Dao daoTempTargets = getDaoTempTargets(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java index fee0d278e8..1bd43719de 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java @@ -1,9 +1,13 @@ package info.nightscout.androidaps.plugins.TempBasals; import android.app.Activity; +import android.content.Context; +import android.content.DialogInterface; +import android.graphics.Paint; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; +import android.support.v7.app.AlertDialog; import android.support.v7.widget.CardView; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; @@ -13,6 +17,8 @@ import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; +import com.crashlytics.android.answers.Answers; +import com.crashlytics.android.answers.CustomEvent; import com.squareup.otto.Subscribe; import org.slf4j.Logger; @@ -44,7 +50,9 @@ public class TempBasalsFragment extends Fragment { TextView tempBasalTotalView; - public static class RecyclerViewAdapter extends RecyclerView.Adapter { + Context context; + + public class RecyclerViewAdapter extends RecyclerView.Adapter { List tempBasalList; @@ -55,8 +63,7 @@ public class TempBasalsFragment extends Fragment { @Override public TempBasalsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.tempbasals_item, viewGroup, false); - TempBasalsViewHolder tempBasalsViewHolder = new TempBasalsViewHolder(v); - return tempBasalsViewHolder; + return new TempBasalsViewHolder(v); } @Override @@ -89,6 +96,7 @@ public class TempBasalsFragment extends Fragment { holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.colorAffectingIOB)); else holder.dateLinearLayout.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.cardColorBackground)); + holder.remove.setTag(tempBasal); } @Override @@ -101,7 +109,7 @@ public class TempBasalsFragment extends Fragment { super.onAttachedToRecyclerView(recyclerView); } - public static class TempBasalsViewHolder extends RecyclerView.ViewHolder { + public class TempBasalsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { CardView cv; TextView date; TextView duration; @@ -113,6 +121,7 @@ public class TempBasalsFragment extends Fragment { TextView iob; TextView extendedFlag; LinearLayout dateLinearLayout; + TextView remove; TempBasalsViewHolder(View itemView) { super(itemView); @@ -127,6 +136,36 @@ public class TempBasalsFragment extends Fragment { iob = (TextView) itemView.findViewById(R.id.tempbasals_iob); extendedFlag = (TextView) itemView.findViewById(R.id.tempbasals_extendedflag); dateLinearLayout = (LinearLayout) itemView.findViewById(R.id.tempbasals_datelinearlayout); + remove = (TextView) itemView.findViewById(R.id.tempbasals_remove); + remove.setOnClickListener(this); + remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + } + + @Override + public void onClick(View v) { + final TempBasal tempBasal = (TempBasal) v.getTag(); + switch (v.getId()) { + case R.id.tempbasals_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(tempBasal.timeStart)); + builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + // TODO: handle this in NS too + //final String _id = tempBasal._id; + //if (_id != null && !_id.equals("")) { + // MainApp.getConfigBuilder().removeCareportalEntryFromNS(_id); + //} + MainApp.getDbHelper().delete(tempBasal); + tempBasalsPlugin.initializeData(); + updateGUI(); + Answers.getInstance().logCustom(new CustomEvent("RemoveTempBasal")); + } + }); + builder.setNegativeButton(MainApp.sResources.getString(R.string.cancel), null); + builder.show(); + break; + } } } } @@ -145,6 +184,9 @@ public class TempBasalsFragment extends Fragment { recyclerView.setAdapter(adapter); tempBasalTotalView = (TextView) view.findViewById(R.id.tempbasals_totaltempiob); + + context = getContext(); + updateGUI(); return view; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsPlugin.java index d398ddfad6..51c5a2c0a1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsPlugin.java @@ -112,7 +112,7 @@ public class TempBasalsPlugin implements PluginBase, TempBasalsInterface { return PluginBase.TEMPBASAL; } - private void initializeData() { + public void initializeData() { double dia = 3; if (MainApp.getConfigBuilder().getActiveProfile() != null && MainApp.getConfigBuilder().getActiveProfile().getProfile() != null) dia = MainApp.getConfigBuilder().getActiveProfile().getProfile().getDia(); 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 0c4dcb26f3..e4442dde0f 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 @@ -38,6 +38,7 @@ import info.nightscout.androidaps.data.Iob; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.InsulinInterface; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; @@ -73,16 +74,15 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener @Override public TreatmentsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.treatments_item, viewGroup, false); - TreatmentsViewHolder treatmentsViewHolder = new TreatmentsViewHolder(v); - return treatmentsViewHolder; + return new TreatmentsViewHolder(v); } @Override public void onBindViewHolder(TreatmentsViewHolder holder, int position) { - if (MainApp.getConfigBuilder() == null || MainApp.getConfigBuilder().getActiveProfile() == null) // app not initialized yet + if (MainApp.getConfigBuilder() == null || ConfigBuilderPlugin.getActiveProfile() == null) // app not initialized yet return; - NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); - InsulinInterface insulinInterface = MainApp.getConfigBuilder().getActiveInsulin(); + NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile(); + InsulinInterface insulinInterface = ConfigBuilderPlugin.getActiveInsulin(); if (profile == null || insulinInterface == null) return; holder.date.setText(DateUtil.dateAndTimeString(treatments.get(position).created_at)); @@ -138,7 +138,6 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener @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); @@ -153,7 +152,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener MainApp.getDbHelper().delete(treatment); treatmentsPlugin.initializeData(); updateGUI(); - Answers.getInstance().logCustom(new CustomEvent("RefreshTreatments")); + Answers.getInstance().logCustom(new CustomEvent("RemoveTreatment")); } }); builder.setNegativeButton(MainApp.sResources.getString(R.string.cancel), null); diff --git a/app/src/main/res/layout/tempbasals_item.xml b/app/src/main/res/layout/tempbasals_item.xml index 796865e343..a5766f9713 100644 --- a/app/src/main/res/layout/tempbasals_item.xml +++ b/app/src/main/res/layout/tempbasals_item.xml @@ -168,6 +168,15 @@ android:text="0.12 U" android:textStyle="bold" /> + + diff --git a/app/src/main/res/layout/treatments_item.xml b/app/src/main/res/layout/treatments_item.xml index f82b335d37..f1eec9c048 100644 --- a/app/src/main/res/layout/treatments_item.xml +++ b/app/src/main/res/layout/treatments_item.xml @@ -135,10 +135,11 @@ android:textStyle="bold" />