Merge pull request #4 from MilosKozak/master

dev
This commit is contained in:
LadyViktoria 2016-06-09 20:19:08 +02:00
commit 62f6ba73ef
15 changed files with 289 additions and 154 deletions

View file

@ -31,4 +31,5 @@ dependencies {
compile 'com.j256.ormlite:ormlite-android:4.46'
compile 'com.github.tony19:logback-android-classic:1.1.1-4'
compile 'org.slf4j:slf4j-api:1.7.12'
compile 'com.jjoe64:graphview:4.0.1'
}

View file

@ -17,10 +17,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment;
import info.nightscout.androidaps.tabs.*;
import info.nightscout.androidaps.plugins.Objectives.ObjectivesFragment;
public class MainActivity extends AppCompatActivity
implements ObjectivesFragment.OnFragmentInteractionListener,
TreatmentsFragment.OnFragmentInteractionListener,
TempBasalsFragment.OnFragmentInteractionListener {
public class MainActivity extends AppCompatActivity {
private static Logger log = LoggerFactory.getLogger(MainActivity.class);
private Toolbar toolbar;
@ -70,11 +67,6 @@ public class MainActivity extends AppCompatActivity
}
@Override
public void onFragmentInteraction(String param) {
}
private void registerBus() {
try {
MainApp.bus().unregister(this);

View file

@ -2,6 +2,8 @@ package info.nightscout.androidaps.db;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.DataPointInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -14,7 +16,7 @@ import info.nightscout.client.data.NSCal;
import info.nightscout.client.data.NSSgv;
@DatabaseTable(tableName = "BgReadings")
public class BgReading {
public class BgReading implements DataPointInterface {
private static Logger log = LoggerFactory.getLogger(BgReading.class);
public static final DecimalFormat mmolFormat = new DecimalFormat("0.0");
public static final DecimalFormat mgdlFormat = new DecimalFormat("0");
@ -45,6 +47,8 @@ public class BgReading {
@DatabaseField
public int battery_level;
public static String units = Constants.MGDL;
public BgReading() {}
public BgReading(NSSgv sgv) {
@ -77,4 +81,14 @@ public class BgReading {
", battery_level=" + battery_level +
'}';
}
@Override
public double getX() {
return timestamp;
}
@Override
public double getY() {
return valueToUnits(units);
}
}

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.db;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -11,6 +12,7 @@ import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
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.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
@ -140,4 +142,22 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return null;
}
public List<BgReading> getDataFromTime(long mills) {
try {
Dao<BgReading, Long> daoBgreadings = getDaoBgReadings();
List<BgReading> bgReadings;
QueryBuilder<BgReading, Long> queryBuilder = daoBgreadings.queryBuilder();
queryBuilder.orderBy("timeIndex", false);
Where where = queryBuilder.where();
where.ge("timeIndex", (long) Math.ceil(mills / 60000d));
PreparedQuery<BgReading> preparedQuery = queryBuilder.prepare();
bgReadings = daoBgreadings.query(preparedQuery);
return bgReadings;
} catch (SQLException e) {
e.printStackTrace();
}
return new ArrayList<BgReading>();
}
}

View file

@ -16,12 +16,21 @@ import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.PluginBase;
public class ObjectivesFragment extends Fragment {
public class ObjectivesFragment extends Fragment implements PluginBase {
RecyclerView recyclerView;
LinearLayoutManager llm;
private OnFragmentInteractionListener mListener;
@Override
public int getType() {
return PluginBase.GENERAL;
}
@Override
public boolean isFragmentVisible() {
return true;
}
class Objective {
String objective;
@ -151,43 +160,4 @@ public class ObjectivesFragment extends Fragment {
return view;
}
/*
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
*/
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(String param);
}
}

View file

@ -1,34 +1,56 @@
package info.nightscout.androidaps.plugins.Overview;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v4.app.ShareCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.jjoe64.graphview.series.PointsGraphSeries;
import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.plugins.PluginBase;
import info.nightscout.client.data.NSProfile;
public class OverviewFragment extends Fragment {
public class OverviewFragment extends Fragment implements PluginBase {
private static Logger log = LoggerFactory.getLogger(OverviewFragment.class);
TextView bg;
GraphView bgGraph;
@Override
public int getType() {
return PluginBase.GENERAL;
}
@Override
public boolean isFragmentVisible() {
return true;
}
public static OverviewFragment newInstance() {
OverviewFragment fragment = new OverviewFragment();
@ -46,6 +68,9 @@ public class OverviewFragment extends Fragment {
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.overview_fragment, container, false);
bg = (TextView) view.findViewById(R.id.overview_bg);
bgGraph = (GraphView) view.findViewById(R.id.overview_bggraph);
updateData();
return view;
}
@ -61,8 +86,81 @@ public class OverviewFragment extends Fragment {
private void updateData() {
BgReading bgReading = MainApp.getDbHelper().lastBg();
NSProfile profile = MainApp.getNSProfile();
if (profile != null && bgReading != null && bg != null)
if (profile != null && bgReading != null && bg != null) {
bg.setText(bgReading.valueToUnitsToString(profile.getUnits()));
BgReading.units = profile.getUnits();
} else
return;
// Skip if not initialized yet
if (bgGraph == null)
return;
// allign to hours
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(new Date().getTime());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.add(Calendar.HOUR, 1);
int hoursToFetch = 6;
long toTime = calendar.getTimeInMillis();
long fromTime = toTime - hoursToFetch * 60 * 60 * 1000l;
Double lowLine = 80d; // TODO: make this customisable
Double highLine = 180d;
Double maxY = 400d; // TODO: add some scale support
String units = profile.getUnits();
if (units.equals(Constants.MMOL)) {
lowLine = 4d;
highLine = 10d;
maxY = 20d;
}
List<BgReading> bgReadingsArray = MainApp.getDbHelper().getDataFromTime(fromTime);
BgReading[] bgReadings = new BgReading[bgReadingsArray.size()];
bgReadings = bgReadingsArray.toArray(bgReadings);
if (bgReadings.length == 0)
return;
PointsGraphSeries<BgReading> series = new PointsGraphSeries<BgReading>(bgReadings);
bgGraph.addSeries(series);
series.setShape(PointsGraphSeries.Shape.POINT);
series.setSize(5);
series.setColor(Color.GREEN);
// targets
LineGraphSeries<DataPoint> seriesLow = new LineGraphSeries<DataPoint>(new DataPoint[]{
new DataPoint(fromTime, lowLine),
new DataPoint(toTime, lowLine)
});
seriesLow.setColor(Color.RED);
bgGraph.addSeries(seriesLow);
LineGraphSeries<DataPoint> seriesHigh = new LineGraphSeries<DataPoint>(new DataPoint[]{
new DataPoint(fromTime, highLine),
new DataPoint(toTime, highLine)
});
seriesHigh.setColor(Color.RED);
bgGraph.addSeries(seriesHigh);
// set manual x bounds to have nice steps
bgGraph.getViewport().setMaxX(toTime);
bgGraph.getViewport().setMinX(fromTime);
bgGraph.getViewport().setXAxisBoundsManual(true);
bgGraph.getGridLabelRenderer().setLabelFormatter(new TimeAsXAxisLabelFormatter(getActivity(), "HH"));
bgGraph.getGridLabelRenderer().setNumHorizontalLabels(7); // only 7 because of the space
String test = new SimpleDateFormat("HH").format(calendar.getTimeInMillis());
// set manual y bounds to have nice steps
bgGraph.getViewport().setMaxY(maxY);
bgGraph.getViewport().setMinY(0);
bgGraph.getViewport().setYAxisBoundsManual(true);
bgGraph.getGridLabelRenderer().setNumVerticalLabels(11);
}
@Subscribe

View file

@ -0,0 +1,34 @@
package info.nightscout.androidaps.plugins.Overview;
import android.content.Context;
import com.jjoe64.graphview.DefaultLabelFormatter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
* Created by mike on 09.06.2016.
*/
public class TimeAsXAxisLabelFormatter extends DefaultLabelFormatter {
protected final String mFormat;
public TimeAsXAxisLabelFormatter(Context context, String format) {
mFormat = format;
}
@Override
public String formatLabel(double value, boolean isValueX) {
if (isValueX) {
// format as date
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis((long) value);
DateFormat dateFormat = new SimpleDateFormat(mFormat);
return dateFormat.format(calendar.getTimeInMillis());
} else {
return super.formatLabel(value, isValueX);
}
}
}

View file

@ -0,0 +1,14 @@
package info.nightscout.androidaps.plugins;
/**
* Created by mike on 09.06.2016.
*/
public interface PluginBase {
int GENERAL = 1;
int PROFILE = 2;
int APS = 3;
int PUMP = 4;
public int getType();
public boolean isFragmentVisible();
}

View file

@ -18,12 +18,14 @@ import java.text.DecimalFormat;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventNewBasalProfile;
import info.nightscout.androidaps.plugins.PluginBase;
import info.nightscout.client.data.NSProfile;
public class ProfileViewerFragment extends Fragment {
public class ProfileViewerFragment extends Fragment implements PluginBase {
private static Logger log = LoggerFactory.getLogger(ProfileViewerFragment.class);
private static TextView noProfile;
private static TextView units;
private static TextView dia;
private static TextView activeProfile;
private static TextView ic;
@ -33,7 +35,15 @@ public class ProfileViewerFragment extends Fragment {
private static DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00");
public ProfileViewerFragment() {
@Override
public int getType() {
return PluginBase.PROFILE;
}
@Override
public boolean isFragmentVisible() {
return true;
}
public static ProfileViewerFragment newInstance(String param1, String param2) {
@ -53,6 +63,7 @@ public class ProfileViewerFragment extends Fragment {
View layout = inflater.inflate(R.layout.profileviewer_fragment, container, false);
noProfile = (TextView) layout.findViewById(R.id.profileview_noprofile);
units = (TextView) layout.findViewById(R.id.profileview_units);
dia = (TextView) layout.findViewById(R.id.profileview_dia);
activeProfile = (TextView) layout.findViewById(R.id.profileview_activeprofile);
ic = (TextView) layout.findViewById(R.id.profileview_ic);
@ -77,6 +88,7 @@ public class ProfileViewerFragment extends Fragment {
} else {
noProfile.setVisibility(View.GONE);
}
units.setText(profile.getUnits());
dia.setText(formatNumber2decimalplaces.format(profile.getDia()) + " h");
activeProfile.setText(profile.getActiveProfile());
ic.setText(profile.getIcList());

View file

@ -35,9 +35,10 @@ import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.db.TempBasal;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.androidaps.plugins.PluginBase;
public class TempBasalsFragment extends Fragment {
public class TempBasalsFragment extends Fragment implements PluginBase {
private static Logger log = LoggerFactory.getLogger(TempBasalsFragment.class);
RecyclerView recyclerView;
@ -53,10 +54,19 @@ public class TempBasalsFragment extends Fragment {
private static DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00");
private static DecimalFormat formatNumber3decimalplaces = new DecimalFormat("0.000");
private OnFragmentInteractionListener mListener;
private List<TempBasal> tempBasals;
@Override
public int getType() {
return PluginBase.GENERAL;
}
@Override
public boolean isFragmentVisible() {
return true;
}
private void initializeData() {
try {
Dao<TempBasal, Long> dao = MainApp.getDbHelper().getDaoTempBasals();
@ -221,31 +231,6 @@ public class TempBasalsFragment extends Fragment {
return view;
}
/*
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
*/
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
private void registerBus() {
try {
@ -259,7 +244,7 @@ public class TempBasalsFragment extends Fragment {
@Subscribe
public void onStatusEvent(final EventTempBasalChange ev) {
Activity activity = getActivity();
if (activity != null)
if (activity != null && recyclerView != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
@ -274,7 +259,7 @@ public class TempBasalsFragment extends Fragment {
@Subscribe
public void onStatusEvent(final EventNewBG ev) {
Activity activity = getActivity();
if (activity != null)
if (activity != null && recyclerView != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
@ -293,19 +278,4 @@ public class TempBasalsFragment extends Fragment {
if (isVisibleToUser)
updateTotalIOBIfNeeded();
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(String param);
}
}

View file

@ -39,10 +39,11 @@ import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.plugins.PluginBase;
import info.nightscout.androidaps.plugins.Treatments.Dialogs.NewTreatmentDialogFragment;
import info.nightscout.androidaps.Services.Intents;
public class TreatmentsFragment extends Fragment implements View.OnClickListener, NewTreatmentDialogFragment.Communicator {
public class TreatmentsFragment extends Fragment implements View.OnClickListener, NewTreatmentDialogFragment.Communicator, PluginBase {
private static Logger log = LoggerFactory.getLogger(TreatmentsFragment.class);
RecyclerView recyclerView;
@ -59,10 +60,19 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
private static DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00");
private static DecimalFormat formatNumber3decimalplaces = new DecimalFormat("0.000");
private OnFragmentInteractionListener mListener;
private List<Treatment> treatments;
@Override
public int getType() {
return PluginBase.GENERAL;
}
@Override
public boolean isFragmentVisible() {
return true;
}
private void initializeData() {
try {
Dao<Treatment, Long> dao = MainApp.getDbHelper().getDaoTreatments();
@ -222,23 +232,6 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
private void registerBus() {
try {
MainApp.bus().unregister(this);
@ -251,7 +244,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
@Subscribe
public void onStatusEvent(final EventTreatmentChange ev) {
Activity activity = getActivity();
if (activity != null)
if (activity != null && recyclerView != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
@ -265,7 +258,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
@Subscribe
public void onStatusEvent(final EventNewBG ev) {
Activity activity = getActivity();
if (activity != null)
if (activity != null && recyclerView != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
@ -285,21 +278,6 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
updateTotalIOBIfNeeded();
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(String param);
}
@Override
public void treatmentDeliverRequest(Double insulin, Double carbs) {
// TODO: implement treatment delivery

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.tabs;
import android.os.Bundle;
import android.support.annotation.PluralsRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
@ -8,6 +9,8 @@ import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.plugins.PluginBase;
/**
* Created by mike on 30.05.2016.
*/
@ -39,12 +42,15 @@ public class TabPageAdapter extends FragmentPagerAdapter {
}
public int registerNewFragment(String name, Fragment fragment) {
fragmentList.add(fragment);
Bundle args = new Bundle();
args.putString("name", name);
fragment.setArguments(args);
registeredTabs++;
notifyDataSetChanged();
return registeredTabs-1;
if (((PluginBase) fragment).isFragmentVisible()){
fragmentList.add(fragment);
Bundle args = new Bundle();
args.putString("name", name);
fragment.setArguments(args);
registeredTabs++;
notifyDataSetChanged();
return registeredTabs - 1;
}
return registeredTabs;
}
}

View file

@ -5,10 +5,22 @@
tools:context="info.nightscout.androidaps.plugins.Overview.OverviewFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/overview_bg"
android:layout_gravity="left|top"
android:textSize="100dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/overview_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:textSize="100dp" />
<com.jjoe64.graphview.GraphView
android:id="@+id/overview_bggraph"
android:layout_width="match_parent"
android:layout_height="200dip" />
</LinearLayout>
</FrameLayout>

View file

@ -36,6 +36,19 @@
android:layout_height="wrap_content"
android:layout_marginLeft="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/profileview_units_label"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginLeft="10dp" />
<TextView
android:id="@+id/profileview_units"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"

View file

@ -55,5 +55,6 @@
<string name="treatments_wizard_unit_label">U</string>
<string name="treatments_wizard_iob_label">IOB</string>
<string name="treatments_wizard_total_label">TOTAL</string>
<string name="profileview_units_label">Units:</string>
</resources>