Skip to content

Commit e941fd9

Browse files
in app database for feed using persistance-room-storage
history activity and its fragments
1 parent dd154d0 commit e941fd9

File tree

624 files changed

+654
-25
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

624 files changed

+654
-25
lines changed

Diff for: .gitignore

100755100644
File mode changed.

Diff for: LICENSE

100755100644
File mode changed.

Diff for: README.md

100755100644
File mode changed.

Diff for: app/.gitignore

100755100644
File mode changed.

Diff for: app/build.gradle

100755100644
+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ dependencies {
3535
implementation fileTree(dir: 'libs', include: ['*.jar'])
3636
wearApp project(path: ':wear')
3737

38+
def room_version = "1.1.1"
39+
40+
implementation "android.arch.persistence.room:runtime:$room_version"
41+
annotationProcessor "android.arch.persistence.room:compiler:$room_version"
42+
3843
// nRF Toolbox is using Play Service 10.2.0 in order to make the app working in China:
3944
// https://developer.android.com/training/wearables/apps/creating-app-china.html
4045
//noinspection GradleDependency

Diff for: app/libs/achartengine-1.2.0.jar

100755100644
File mode changed.

Diff for: app/proguard-rules.pro

100755100644
File mode changed.

Diff for: app/src/androidTest/java/in/programmeraki/ApplicationTest.java

100755100644
File mode changed.

Diff for: app/src/main/AndroidManifest.xml

100755100644
+1
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@
260260
</intent-filter>
261261
</activity>
262262
<activity android:name=".AlertsActivity" />
263+
<activity android:name=".fragment.HistoryActivity" />
263264

264265
<service
265266
android:name="in.programmeraki.hbt.nrfkit.cgms.CGMService"

Diff for: app/src/main/assets/fonts/trebuc.ttf

100755100644
File mode changed.

Diff for: app/src/main/assets/fonts/trebucbd.ttf

100755100644
File mode changed.

Diff for: app/src/main/ic_launcher-web.png

100755100644
File mode changed.

Diff for: app/src/main/java/in/programmeraki/hbt/AlertsActivity.java

100755100644
+2-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
import android.os.Bundle;
44
import android.support.annotation.Nullable;
55
import android.support.design.widget.TabLayout;
6-
import android.support.v4.content.ContextCompat;
76
import android.support.v4.view.ViewPager;
87
import android.support.v7.app.AppCompatActivity;
98
import android.view.LayoutInflater;
109
import android.widget.TextView;
1110

12-
import in.programmeraki.hbt.adapter.ViewPagerAdapter;
11+
import in.programmeraki.hbt.adapter.AlertsViewPagerAdapter;
1312
import in.programmeraki.hbt.fragment.CommonAlertsFragment;
1413

1514
public class AlertsActivity extends AppCompatActivity {
@@ -39,7 +38,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3938
}
4039

4140
private void setupViewPager(ViewPager viewPager) {
42-
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
41+
AlertsViewPagerAdapter adapter = new AlertsViewPagerAdapter(getSupportFragmentManager());
4342
adapter.addFragment(new CommonAlertsFragment(), "Pulse");
4443
adapter.addFragment(new CommonAlertsFragment(), "Temp");
4544
viewPager.setAdapter(adapter);

Diff for: app/src/main/java/in/programmeraki/hbt/Common.java

100755100644
+23-2
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package in.programmeraki.hbt;
22

33
import android.app.Application;
4+
import android.arch.persistence.room.Room;
5+
import android.content.Context;
46

57
import java.util.ArrayList;
68

79
import in.programmeraki.hbt.model.TrackerAlert;
10+
import in.programmeraki.hbt.roomdb.AppDatabase;
811

912
public class Common extends Application {
1013

1114
public static Common instance = new Common();
1215
public ArrayList<TrackerAlert> trackerAlerts = new ArrayList<>();
16+
AppDatabase db;
1317

1418
public ArrayList<TrackerAlert> getPulseTrackerAlerts() {
1519
ArrayList<TrackerAlert> temp = new ArrayList<>();
1620
for (int i = 0; i < trackerAlerts.size(); i++) {
17-
if(trackerAlerts.get(i).getType() == TrackerAlert.pulseType){
21+
if (trackerAlerts.get(i).getType() == TrackerAlert.pulseType) {
1822
temp.add(trackerAlerts.get(i));
1923
}
2024
}
@@ -24,10 +28,27 @@ public ArrayList<TrackerAlert> getPulseTrackerAlerts() {
2428
public ArrayList<TrackerAlert> getTempTrackerAlerts() {
2529
ArrayList<TrackerAlert> temp = new ArrayList<>();
2630
for (int i = 0; i < trackerAlerts.size(); i++) {
27-
if(trackerAlerts.get(i).getType() == TrackerAlert.tempType){
31+
if (trackerAlerts.get(i).getType() == TrackerAlert.tempType) {
2832
temp.add(trackerAlerts.get(i));
2933
}
3034
}
3135
return temp;
3236
}
37+
38+
39+
public AppDatabase getAppDatabase() {
40+
return db;
41+
}
42+
43+
public void cleanUp(){
44+
db = null;
45+
}
46+
47+
public void setUpAppDatabase(Context context) {
48+
if(db != null){
49+
return;
50+
}
51+
db = Room.databaseBuilder(context, AppDatabase.class,
52+
"BabyMonitor").allowMainThreadQueries().build();
53+
}
3354
}

Diff for: app/src/main/java/in/programmeraki/hbt/DebugActivity.java

100755100644
+9-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class DebugActivity extends BleProfileActivity implements HRSManagerCallb
4141
private final String TAG = "Main";
4242
BLEFeedAdapter bleFeedAdapter;
4343
private Handler mHandler = new Handler();
44-
private TextView title_tv;
44+
private TextView title_tv, clear_db_tv;
4545
private Button sync_btn, action_connect;
4646
private ImageView back_iv;
4747
private ViewGroup content_vg, topbar_ll;
@@ -59,6 +59,8 @@ public class DebugActivity extends BleProfileActivity implements HRSManagerCallb
5959
protected void onCreateView(Bundle savedInstanceState) {
6060
setContentView(R.layout.activity_debug);
6161

62+
Common.instance.setUpAppDatabase(getApplicationContext());
63+
6264
title_tv = findViewById(R.id.title_tv);
6365
action_connect = findViewById(R.id.action_connect);
6466
sync_btn = findViewById(R.id.sync_btn);
@@ -69,6 +71,7 @@ protected void onCreateView(Bundle savedInstanceState) {
6971
recyclerView = findViewById(R.id.recyclerView);
7072
mHRSValue = findViewById(R.id.text_hrs_value);
7173
mHRSPosition = findViewById(R.id.text_hrs_position);
74+
clear_db_tv = findViewById(R.id.clear_db_tv);
7275

7376
progressBar.setVisibility(View.GONE);
7477
bleFeedAdapter = new BLEFeedAdapter();
@@ -83,6 +86,11 @@ protected void onCreateView(Bundle savedInstanceState) {
8386
finish();
8487
});
8588

89+
clear_db_tv.setOnClickListener(view -> {
90+
Common.instance.getAppDatabase().feedDataDao().deleteAll();
91+
Log.d(TAG, "onCreateView: totalRows:" + String.valueOf(Common.instance.getAppDatabase().feedDataDao().numberOfRows()));
92+
});
93+
8694
rvHandler = new Handler();
8795
}
8896

Diff for: app/src/main/java/in/programmeraki/hbt/FeaturesActivity.java

100755100644
File mode changed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
package in.programmeraki.hbt;
2+
3+
import android.content.Context;
4+
import android.graphics.Color;
5+
import android.os.Bundle;
6+
import android.support.annotation.NonNull;
7+
import android.support.annotation.Nullable;
8+
import android.support.v4.app.Fragment;
9+
import android.view.LayoutInflater;
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
13+
import com.github.mikephil.charting.charts.LineChart;
14+
import com.github.mikephil.charting.components.Legend;
15+
import com.github.mikephil.charting.components.XAxis;
16+
import com.github.mikephil.charting.components.YAxis;
17+
import com.github.mikephil.charting.data.Entry;
18+
import com.github.mikephil.charting.data.LineData;
19+
import com.github.mikephil.charting.data.LineDataSet;
20+
import com.github.mikephil.charting.highlight.Highlight;
21+
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
22+
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
23+
24+
import java.util.ArrayList;
25+
26+
import in.programmeraki.hbt.roomdb.FeedData;
27+
28+
public class HistoryFragment extends Fragment implements OnChartValueSelectedListener{
29+
30+
public boolean isLastWeekType = false;
31+
final String pulseDataSetLabel = "Pulse (bpm)";
32+
final String tempDataSetLabel = "Temp (˚C)";
33+
final int pulseAndTempBoth = 1;
34+
final int pulseOnly = 2;
35+
final int tempOnly = 3;
36+
37+
private Context activity;
38+
ArrayList<Entry> pulseValues = new ArrayList<>();
39+
ArrayList<Entry> tempValues = new ArrayList<>();
40+
LineDataSet pulseDataSet;
41+
LineDataSet tempDataSet;
42+
ViewGroup graph_fl;
43+
private LineChart mChart;
44+
45+
@Nullable
46+
@Override
47+
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
48+
super.onCreateView(inflater, container, savedInstanceState);
49+
return inflater.inflate(R.layout.fragment_history, container, false);
50+
}
51+
52+
@Override
53+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
54+
super.onViewCreated(view, savedInstanceState);
55+
graph_fl = view.findViewById(R.id.graph_fl);
56+
mChart = view.findViewById(R.id.mChart);
57+
setUpChart();
58+
59+
makeGraphFromDatabase();
60+
}
61+
62+
private void makeGraphFromDatabase(){
63+
ArrayList<FeedData> allFeedData = (ArrayList<FeedData>) Common.instance.getAppDatabase().feedDataDao().getAll();
64+
for (int i = 0; i < allFeedData.size(); i++) {
65+
FeedData feedData = allFeedData.get(i);
66+
appendLineChartData(feedData.getPulse(), feedData.getTemp());
67+
}
68+
}
69+
70+
/*
71+
* Chart Methods
72+
*/
73+
private void setUpChart(){
74+
mChart.setOnChartValueSelectedListener(this);
75+
mChart.setDrawGridBackground(false);
76+
77+
// no description text
78+
mChart.getDescription().setEnabled(false);
79+
80+
// enable touch gestures
81+
mChart.setTouchEnabled(true);
82+
83+
// enable scaling and dragging
84+
mChart.setDragEnabled(true);
85+
mChart.setScaleEnabled(true);
86+
// mChart.setScaleXEnabled(true);
87+
// mChart.setScaleYEnabled(true);
88+
89+
// if disabled, scaling can be done on x- and y-axis separately
90+
mChart.setPinchZoom(true);
91+
92+
// set an alternative background color
93+
mChart.setBackgroundColor(getResources().getColor(R.color.colorWhite));
94+
95+
XAxis xAxis = mChart.getXAxis();
96+
xAxis.enableGridDashedLine(10f, 10f, 0f);
97+
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
98+
xAxis.setDrawGridLines(false);
99+
// IAxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(mChart);
100+
// xAxis.setValueFormatter(xAxisFormatter);
101+
102+
YAxis leftAxis = mChart.getAxisLeft();
103+
leftAxis.setAxisMaximum(150f);
104+
leftAxis.setAxisMinimum(0f);
105+
leftAxis.enableGridDashedLine(10f, 10f, 0f);
106+
leftAxis.setDrawZeroLine(true);
107+
108+
mChart.getAxisRight().setEnabled(false);
109+
110+
// get the legend (only possible after setting data)
111+
Legend l = mChart.getLegend();
112+
113+
// modify the legend ...
114+
l.setForm(Legend.LegendForm.LINE);
115+
}
116+
117+
private void appendLineChartData(int pulse, int temp){
118+
// Entry pulseEntry = new Entry(Calendar.getInstance().getTime().getTime(), pulse);
119+
// Entry tempEntry = new Entry(Calendar.getInstance().getTime().getTime(), temp);
120+
121+
Entry pulseEntry = new Entry(pulseValues.size(), pulse);
122+
Entry tempEntry = new Entry(tempValues.size(), temp);
123+
124+
pulseValues.add(pulseEntry);
125+
tempValues.add(tempEntry);
126+
127+
if( mChart.getData() != null && mChart.getData().getDataSetCount() > 1){
128+
mChart.getData().getDataSets().get(0).addEntry(pulseEntry);
129+
mChart.getData().getDataSets().get(1).addEntry(tempEntry);
130+
131+
mChart.getData().notifyDataChanged();
132+
mChart.notifyDataSetChanged();
133+
mChart.invalidate();
134+
return;
135+
}
136+
137+
// create a dataset and give it a type
138+
pulseDataSet = new LineDataSet(pulseValues, pulseDataSetLabel);
139+
tempDataSet = new LineDataSet(tempValues, tempDataSetLabel);
140+
141+
// set the line to be drawn like this "- - - - - -"
142+
pulseDataSet.setColor(getResources().getColor(R.color.redPrimaryDark));
143+
tempDataSet.setColor(getResources().getColor(R.color.orangePrimaryDark));
144+
145+
ArrayList<ILineDataSet> chartDataSets = new ArrayList<>();
146+
chartDataSets.add(pulseDataSet); // add the datasets
147+
chartDataSets.add(tempDataSet); // add the datasets
148+
149+
// create a data object with the datasets
150+
LineData data = new LineData(chartDataSets);
151+
152+
pulseDataSet.setDrawIcons(false);
153+
tempDataSet.setDrawIcons(false);
154+
155+
pulseDataSet.setDrawCircles(false);
156+
tempDataSet.setDrawCircles(false);
157+
158+
// pulseSet.setCircleColor(getResources().getColor(R.color.redPrimaryDark));
159+
// tempSet.setCircleColor(getResources().getColor(R.color.orangePrimaryDark));
160+
161+
pulseDataSet.setLineWidth(1f);
162+
tempDataSet.setLineWidth(1f);
163+
164+
// pulseDataSet.setCircleRadius(3f);
165+
// tempDataSet.setCircleRadius(3f);
166+
167+
// pulseDataSet.setDrawCircleHole(false);
168+
// tempDataSet.setDrawCircleHole(false);
169+
170+
// pulseDataSet.setValueTextSize(9f);
171+
// tempDataSet.setValueTextSize(9f);
172+
173+
pulseDataSet.setDrawFilled(true);
174+
tempDataSet.setDrawFilled(true);
175+
176+
pulseDataSet.setFormLineWidth(1f);
177+
tempDataSet.setFormLineWidth(1f);
178+
179+
pulseDataSet.setFormSize(15.f);
180+
tempDataSet.setFormSize(15.f);
181+
182+
pulseDataSet.setFillColor(Color.BLACK);
183+
tempDataSet.setFillColor(Color.BLACK);
184+
185+
// set data
186+
mChart.setData(data);
187+
}
188+
189+
private void toggleChartVisibility(int mode){
190+
if (mode == pulseAndTempBoth) {
191+
if (mChart.getData().getDataSetByLabel(pulseDataSetLabel, true) == null ){
192+
mChart.getData().addDataSet(pulseDataSet);
193+
}
194+
if (mChart.getData().getDataSetByLabel(tempDataSetLabel, true) == null ){
195+
mChart.getData().addDataSet(tempDataSet);
196+
}
197+
} else if (mode == pulseOnly) {
198+
if (mChart.getData().getDataSetByLabel(tempDataSetLabel, true) != null) {
199+
mChart.getData().removeDataSet(tempDataSet);
200+
}
201+
if (mChart.getData().getDataSetByLabel(pulseDataSetLabel, true) == null ){
202+
mChart.getData().addDataSet(pulseDataSet);
203+
}
204+
} else if (mode == tempOnly){
205+
if (mChart.getData().getDataSetByLabel(pulseDataSetLabel, true) != null) {
206+
mChart.getData().removeDataSet(pulseDataSet);
207+
}
208+
if (mChart.getData().getDataSetByLabel(tempDataSetLabel, true) == null ){
209+
mChart.getData().addDataSet(tempDataSet);
210+
}
211+
}
212+
mChart.getData().notifyDataChanged();
213+
mChart.notifyDataSetChanged();
214+
mChart.invalidate();
215+
}
216+
217+
@Override
218+
public void onValueSelected(Entry e, Highlight h) {
219+
}
220+
221+
@Override
222+
public void onNothingSelected() {
223+
}
224+
}

0 commit comments

Comments
 (0)