diff --git a/app/src/main/java/info/nightscout/androidaps/data/ProfileIntervals.java b/app/src/main/java/info/nightscout/androidaps/data/ProfileIntervals.java index e5ecea9f97..ef255ed901 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ProfileIntervals.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ProfileIntervals.java @@ -77,15 +77,16 @@ public class ProfileIntervals { final int mid = (lo + hi) >>> 1; final Interval midVal = rawData.valueAt(mid); - if (midVal.before(value)) { + if (midVal.match(value)) { + return mid; // value found + } else if (midVal.before(value)) { lo = mid + 1; } else if (midVal.after(value)) { hi = mid - 1; - } else if (midVal.match(value)) { - return mid; // value found } } // not found, try nearest older with duration 0 + lo = lo - 1; while (lo >= 0 && lo < rawData.size()) { if (rawData.valueAt(lo).isEndingEvent()) return lo; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 1a5f61de97..ea081d23fb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -998,6 +998,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain return getProfile(new Date().getTime()); } public Profile getProfile(long time) { + //log.debug("Profile for: " + new Date(time).toLocaleString() + " : " + getProfileName(time)); ProfileSwitch profileSwitch = getProfileSwitchFromHistory(time); if (profileSwitch != null) { if (profileSwitch.profileJson != null) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index ba6e489b57..55fe2d289a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -25,6 +25,7 @@ import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.view.ViewTreeObserver; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; @@ -268,6 +269,19 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, llm = new LinearLayoutManager(view.getContext()); notificationsView.setLayoutManager(llm); + final LinearLayout graphs = (LinearLayout)view.findViewById(R.id.overview_graphs_layout); + ViewTreeObserver observer = graphs.getViewTreeObserver(); + observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + log.debug("Height: " + graphs.getHeight()); + graphs.getViewTreeObserver().removeGlobalOnLayoutListener( + this); + int heightNeeded = Math.max(320, graphs.getHeight() - 200); + if (heightNeeded != bgGraph.getHeight()) + bgGraph.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, heightNeeded)); + } + }); bgGraph.getGridLabelRenderer().setGridColor(Color.rgb(0x75, 0x75, 0x75)); bgGraph.getGridLabelRenderer().reloadStyles(); @@ -1441,7 +1455,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, for (int tx = 0; tx < profileSwitches.size(); tx++) { ProfileSwitch t = profileSwitches.get(tx); - if (t.date < fromTime || t.date > now) continue; + if (t.date < fromTime || t.date > endTime) continue; filteredProfileSwitches.add(t); } ProfileSwitch[] profileSwitchArray = new ProfileSwitch[filteredProfileSwitches.size()]; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/VerticalTextsGraphSeries.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/VerticalTextsGraphSeries.java index c27b2f0088..75803c24b5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/VerticalTextsGraphSeries.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/VerticalTextsGraphSeries.java @@ -237,12 +237,12 @@ public class VerticalTextsGraphSeries ext Rect bounds = new Rect(); mPaint.getTextBounds(value.getLabel(), 0, value.getLabel().length(), bounds); mPaint.setStyle(Paint.Style.STROKE); - float px = endX; - float py = (float) (graphHeight * ratY + bounds.height() + 80); + float px = endX + bounds.height() / 2; + float py = (float) (graphHeight * ratY + bounds.width() + 10); canvas.save(); canvas.rotate(-90, px, py); canvas.drawText(value.getLabel(), px, py, mPaint); - canvas.drawRect(bounds.left + px - 3, bounds.top + py - 3, bounds.right + px + 3, bounds.bottom + py + 3, mPaint); + canvas.drawRect(px - 3, bounds.top + py - 3, bounds.right + px + 3, bounds.bottom + py + 3, mPaint); canvas.restore(); } } 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 9644ff1be6..1bf66ca5ec 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 @@ -16,6 +16,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.Treatments.fragments.TreatmentsBolusFragment; import info.nightscout.androidaps.plugins.Treatments.fragments.TreatmentsExtendedBolusesFragment; +import info.nightscout.androidaps.plugins.Treatments.fragments.TreatmentsProfileSwitchFragment; import info.nightscout.androidaps.plugins.Treatments.fragments.TreatmentsTempTargetFragment; import info.nightscout.androidaps.plugins.Treatments.fragments.TreatmentsTemporaryBasalsFragment; @@ -28,11 +29,11 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener return treatmentsPlugin; } - Context context; TextView treatmentsTab; TextView extendedBolusesTab; TextView tempBasalsTab; TextView tempTargetTab; + TextView profileSwitchTab; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -43,11 +44,12 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener extendedBolusesTab = (TextView) view.findViewById(R.id.treatments_extendedboluses); tempBasalsTab = (TextView) view.findViewById(R.id.treatments_tempbasals); tempTargetTab = (TextView) view.findViewById(R.id.treatments_temptargets); + profileSwitchTab = (TextView) view.findViewById(R.id.treatments_profileswitches); treatmentsTab.setOnClickListener(this); extendedBolusesTab.setOnClickListener(this); tempBasalsTab.setOnClickListener(this); tempTargetTab.setOnClickListener(this); - context = getContext(); + profileSwitchTab.setOnClickListener(this); setFragment(new TreatmentsBolusFragment()); setBackgroundColorOnSelected(treatmentsTab); @@ -75,6 +77,10 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener setFragment(new TreatmentsTempTargetFragment()); setBackgroundColorOnSelected(tempTargetTab); break; + case R.id.treatments_profileswitches: + setFragment(new TreatmentsProfileSwitchFragment()); + setBackgroundColorOnSelected(profileSwitchTab); + break; } } @@ -91,6 +97,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener extendedBolusesTab.setBackgroundColor(MainApp.sResources.getColor(R.color.defaultbackground)); tempBasalsTab.setBackgroundColor(MainApp.sResources.getColor(R.color.defaultbackground)); tempTargetTab.setBackgroundColor(MainApp.sResources.getColor(R.color.defaultbackground)); + profileSwitchTab.setBackgroundColor(MainApp.sResources.getColor(R.color.defaultbackground)); selected.setBackgroundColor(MainApp.sResources.getColor(R.color.tabBgColorSelected)); } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java index c670fbbe16..c2bbd123e3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java @@ -183,7 +183,7 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis case R.id.treatments_reshreshfromnightscout: AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext()); builder.setTitle(this.getContext().getString(R.string.confirmation)); - builder.setMessage(this.getContext().getString(R.string.refreshtreatmentsfromnightscout)); + 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().resetTreatments(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsProfileSwitchFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsProfileSwitchFragment.java new file mode 100644 index 0000000000..01955ba937 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsProfileSwitchFragment.java @@ -0,0 +1,203 @@ +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.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; +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 info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.Services.Intents; +import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.data.ProfileIntervals; +import info.nightscout.androidaps.db.ProfileSwitch; +import info.nightscout.androidaps.db.TempTarget; +import info.nightscout.androidaps.events.EventProfileSwitchChange; +import info.nightscout.androidaps.events.EventTempTargetChange; +import info.nightscout.utils.DateUtil; +import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.NSUpload; +import info.nightscout.utils.OKDialog; +import info.nightscout.utils.SP; + +/** + * Created by mike on 13/01/17. + */ + +public class TreatmentsProfileSwitchFragment extends Fragment implements View.OnClickListener { + + RecyclerView recyclerView; + LinearLayoutManager llm; + Button refreshFromNS; + + Context context; + + public class RecyclerViewAdapter extends RecyclerView.Adapter { + + ProfileIntervals profileSwitchList; + + RecyclerViewAdapter(ProfileIntervals profileSwitchList) { + this.profileSwitchList = profileSwitchList; + } + + @Override + public ProfileSwitchViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { + View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.treatments_profileswitch_item, viewGroup, false); + ProfileSwitchViewHolder ProfileSwitchViewHolder = new ProfileSwitchViewHolder(v); + return ProfileSwitchViewHolder; + } + + @Override + public void onBindViewHolder(ProfileSwitchViewHolder holder, int position) { + Profile profile = MainApp.getConfigBuilder().getProfile(); + if (profile == null) return; + ProfileSwitch profileSwitch = profileSwitchList.getReversed(position); + + holder.date.setText(DateUtil.dateAndTimeString(profileSwitch.date)); + if (!profileSwitch.isEndingEvent()) { + holder.duration.setText(DecimalFormatter.to0Decimal(profileSwitch.durationInMinutes) + " min"); + } else { + holder.duration.setText(""); + } + holder.name.setText(profileSwitch.profileName); + if (profileSwitch.isInProgress()) + holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive)); + else + holder.date.setTextColor(holder.duration.getCurrentTextColor()); + holder.remove.setTag(profileSwitch); + } + + @Override + public int getItemCount() { + return profileSwitchList.size(); + } + + @Override + public void onAttachedToRecyclerView(RecyclerView recyclerView) { + super.onAttachedToRecyclerView(recyclerView); + } + + public class ProfileSwitchViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { + CardView cv; + TextView date; + TextView duration; + TextView name; + TextView remove; + + ProfileSwitchViewHolder(View itemView) { + super(itemView); + cv = (CardView) itemView.findViewById(R.id.profileswitch_cardview); + date = (TextView) itemView.findViewById(R.id.profileswitch_date); + duration = (TextView) itemView.findViewById(R.id.profileswitch_duration); + name = (TextView) itemView.findViewById(R.id.profileswitch_name); + remove = (TextView) itemView.findViewById(R.id.profileswitch_remove); + remove.setOnClickListener(this); + remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + } + + @Override + public void onClick(View v) { + final ProfileSwitch profileSwitch = (ProfileSwitch) v.getTag(); + switch (v.getId()) { + case R.id.profileswitch_remove: + OKDialog.show(getActivity(), MainApp.sResources.getString(R.string.confirmation), MainApp.sResources.getString(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(profileSwitch.date), new Runnable() { + @Override + public void run() { + final String _id = profileSwitch._id; + if (_id != null && !_id.equals("")) { + NSUpload.removeCareportalEntryFromNS(_id); + } + MainApp.getDbHelper().delete(profileSwitch); + } + }); + break; + } + } + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.treatments_profileswitch_fragment, container, false); + + recyclerView = (RecyclerView) view.findViewById(R.id.profileswitch_recyclerview); + recyclerView.setHasFixedSize(true); + llm = new LinearLayoutManager(view.getContext()); + recyclerView.setLayoutManager(llm); + + RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainApp.getConfigBuilder().getProfileSwitchesFromHistory()); + recyclerView.setAdapter(adapter); + + refreshFromNS = (Button) view.findViewById(R.id.profileswitch_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.profileswitch_refreshfromnightscout: + OKDialog.show(getActivity(), MainApp.sResources.getString(R.string.confirmation), MainApp.sResources.getString(R.string.refresheventsfromnightscout) + "?", new Runnable() { + @Override + public void run() { + MainApp.getDbHelper().resetProfileSwitch(); + Intent restartNSClient = new Intent(Intents.ACTION_RESTART); + MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient); + } + }); + break; + } + } + + @Override + public void onPause() { + super.onPause(); + MainApp.bus().unregister(this); + } + + @Override + public void onResume() { + super.onResume(); + MainApp.bus().register(this); + } + + @Subscribe + public void onStatusEvent(final EventProfileSwitchChange ev) { + updateGUI(); + } + + void updateGUI() { + Activity activity = getActivity(); + if (activity != null) + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getConfigBuilder().getProfileSwitchesFromHistory()), false); + } + }); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java index ad977e758d..d8a84b0e1c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java @@ -65,7 +65,7 @@ public class TreatmentsTempTargetFragment extends Fragment implements View.OnCli if (profile == null) return; TempTarget tempTarget = tempTargetList.getReversed(position); if (!tempTarget.isEndingEvent()) { - holder.date.setText(DateUtil.dateAndTimeString(tempTarget.date) + " - " + DateUtil.timeString(tempTargetList.get(position).originalEnd())); + holder.date.setText(DateUtil.dateAndTimeString(tempTarget.date) + " - " + DateUtil.timeString(tempTarget.originalEnd())); holder.duration.setText(DecimalFormatter.to0Decimal(tempTarget.durationInMinutes) + " min"); holder.low.setText(tempTarget.lowValueToUnitsToString(profile.getUnits())); holder.high.setText(tempTarget.highValueToUnitsToString(profile.getUnits())); @@ -180,7 +180,7 @@ public class TreatmentsTempTargetFragment extends Fragment implements View.OnCli case R.id.temptargetrange_refreshfromnightscout: AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext()); builder.setTitle(this.getContext().getString(R.string.confirmation)); - builder.setMessage(this.getContext().getString(R.string.refreshtemptargetsfromnightscout)); + 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().resetTempTargets(); diff --git a/app/src/main/res/layout/overview_fragment.xml b/app/src/main/res/layout/overview_fragment.xml index 2f56efeda4..27e7cd1180 100644 --- a/app/src/main/res/layout/overview_fragment.xml +++ b/app/src/main/res/layout/overview_fragment.xml @@ -12,11 +12,12 @@ @@ -71,14 +72,14 @@ @@ -162,7 +163,7 @@ @@ -289,15 +290,17 @@ + android:layout_height="0dp" + android:layout_weight="1" /> - + + + + + +