display temptargets

This commit is contained in:
Milos Kozak 2017-01-13 23:43:17 +01:00
parent 339e62c120
commit 458bee4538
5 changed files with 263 additions and 2 deletions

View file

@ -216,6 +216,23 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return new ArrayList<BgReading>();
}
public List<TempTarget> getTemptargetsDataFromTime(long mills, boolean ascending) {
try {
Dao<TempTarget, Long> daoTempTargets = getDaoTempTargets();
List<TempTarget> tempTargets;
QueryBuilder<TempTarget, Long> queryBuilder = daoTempTargets.queryBuilder();
queryBuilder.orderBy("timeIndex", ascending);
Where where = queryBuilder.where();
where.ge("timeIndex", mills);
PreparedQuery<TempTarget> preparedQuery = queryBuilder.prepare();
tempTargets = daoTempTargets.query(preparedQuery);
return tempTargets;
} catch (SQLException e) {
e.printStackTrace();
}
return new ArrayList<TempTarget>();
}
/*
* Returns glucose_status for openAPS or null if no actual data available
*/

View file

@ -8,7 +8,11 @@ import org.slf4j.LoggerFactory;
import java.util.Date;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.plugins.TempBasals.TempBasalsPlugin;
import info.nightscout.androidaps.plugins.TempTargetRange.TempTargetRangePlugin;
import info.nightscout.utils.DecimalFormatter;
@DatabaseTable(tableName = DatabaseHelper.DATABASE_TEMPTARGETS)
public class TempTarget {
@ -29,10 +33,10 @@ public class TempTarget {
public Date timeStart;
@DatabaseField
public double low;
public double low; // in mgdl
@DatabaseField
public double high;
public double high; // in mgdl
@DatabaseField
public String reason;
@ -44,6 +48,20 @@ public class TempTarget {
return new Date(timeStart.getTime() + 60 * 1_000 * duration);
}
public String lowValueToUnitsToString(String units) {
if (units.equals(Constants.MGDL)) return DecimalFormatter.to0Decimal(low);
else return DecimalFormatter.to1Decimal(low * Constants.MGDL_TO_MMOLL);
}
public String highValueToUnitsToString(String units) {
if (units.equals(Constants.MGDL)) return DecimalFormatter.to0Decimal(high);
else return DecimalFormatter.to1Decimal(low * Constants.MGDL_TO_MMOLL);
}
public boolean isInProgress() {
return ((TempTargetRangePlugin) MainApp.getSpecificPlugin(TempTargetRangePlugin.class)).getTempTargetInProgress(new Date().getTime()) == this;
}
public String log() {
return "TempTarget{" +
"timeIndex=" + timeIndex +

View file

@ -3,16 +3,28 @@ package info.nightscout.androidaps.plugins.TempTargetRange;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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.LinearLayout;
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.db.TempTarget;
import info.nightscout.androidaps.interfaces.FragmentBase;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.TempTargetRange.events.EventNewTempTargetRange;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter;
/**
* Created by mike on 13/01/17.
@ -26,11 +38,86 @@ public class TempTargetRangeFragment extends Fragment implements FragmentBase {
return tempTargetRangePlugin;
}
RecyclerView recyclerView;
LinearLayoutManager llm;
public static class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.TempTargetsViewHolder> {
List<TempTarget> tempTargetList;
RecyclerViewAdapter(List<TempTarget> TempTargetList) {
this.tempTargetList = TempTargetList;
}
@Override
public TempTargetsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.temptargetrange_item, viewGroup, false);
TempTargetsViewHolder TempTargetsViewHolder = new TempTargetsViewHolder(v);
return TempTargetsViewHolder;
}
@Override
public void onBindViewHolder(TempTargetsViewHolder holder, int position) {
NSProfile profile = ConfigBuilderPlugin.getActiveProfile().getProfile();
if (profile == null) return;
TempTarget tempTarget = tempTargetList.get(position);
holder.date.setText(DateUtil.dateAndTimeString(tempTarget.timeStart) + " - " + DateUtil.timeString(tempTargetList.get(position).getPlannedTimeEnd()));
holder.duration.setText(DecimalFormatter.to0Decimal(tempTarget.duration) + " min");
holder.low.setText(tempTarget.lowValueToUnitsToString(profile.getUnits()));
holder.high.setText(tempTarget.highValueToUnitsToString(profile.getUnits()));
holder.reason.setText(tempTarget.reason);
if (tempTarget.isInProgress())
holder.dateLinearLayout.setBackgroundColor(MainApp.instance().getResources().getColor(R.color.colorInProgress));
else
holder.dateLinearLayout.setBackgroundColor(MainApp.instance().getResources().getColor(R.color.cardColorBackground));
}
@Override
public int getItemCount() {
return tempTargetList.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class TempTargetsViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView date;
TextView duration;
TextView low;
TextView high;
TextView reason;
LinearLayout dateLinearLayout;
TempTargetsViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.temptargetrange_cardview);
date = (TextView) itemView.findViewById(R.id.temptargetrange_date);
duration = (TextView) itemView.findViewById(R.id.temptargetrange_duration);
low = (TextView) itemView.findViewById(R.id.temptargetrange_low);
high = (TextView) itemView.findViewById(R.id.temptargetrange_high);
reason = (TextView) itemView.findViewById(R.id.temptargetrange_reason);
dateLinearLayout = (LinearLayout) itemView.findViewById(R.id.temptargetrange_datelinearlayout);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.temptargetrange_fragment, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.temptargetrange_recyclerview);
recyclerView.setHasFixedSize(true);
llm = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(llm);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(tempTargetRangePlugin.getList());
recyclerView.setAdapter(adapter);
updateGUI();
return view;
}

View file

@ -1,9 +1,20 @@
package info.nightscout.androidaps.plugins.TempTargetRange;
import android.support.annotation.Nullable;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.squareup.otto.Subscribe;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.TempTargetRange.events.EventNewTempTargetRange;
@ -16,7 +27,10 @@ public class TempTargetRangePlugin implements PluginBase {
static boolean fragmentEnabled = true;
static boolean fragmentVisible = true;
private static List<TempTarget> tempTargets;
TempTargetRangePlugin() {
initializeData();
MainApp.bus().register(this);
}
@ -71,5 +85,21 @@ public class TempTargetRangePlugin implements PluginBase {
}
private void initializeData() {
long fromMills = (long) (new Date().getTime() - 60 * 60 * 1000L * 24);
tempTargets = MainApp.getDbHelper().getTemptargetsDataFromTime(fromMills, false);
}
public List<TempTarget> getList() {
return tempTargets;
}
@Nullable
public TempTarget getTempTargetInProgress(long time) {
for (int i = tempTargets.size() - 1; i >= 0; i--) {
if (tempTargets.get(i).timeStart.getTime() > time) continue;
if (tempTargets.get(i).getPlannedTimeEnd().getTime() >= time) return tempTargets.get(i);
}
return null;
}
}

View file

@ -0,0 +1,109 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/temptargetrange_cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardBackgroundColor="@color/cardColorBackground"
card_view:cardCornerRadius="6dp"
card_view:cardUseCompatPadding="true"
card_view:contentPadding="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/temptargetrange_datelinearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:orientation="horizontal">
<com.joanzapata.iconify.widget.IconTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|right"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="{fa-clock-o}"
android:textColor="@color/cardItemLabel" />
<TextView
android:id="@+id/temptargetrange_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="1.1.2000 18:00"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/cardItemLabel" />
<TextView
android:id="@+id/temptargetrange_low"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="80"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/cardItemLabel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="-"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/cardItemLabel" />
<TextView
android:id="@+id/temptargetrange_high"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="100"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/cardItemLabel" />
<TextView
android:id="@+id/temptargetrange_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="30 min"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/cardItemLabel" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:text="@string/reason"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/temptargetrange_reason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingRight="10dp"
android:text="Activity"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>