11package com .eveningoutpost .dexdrip .stats ;
22
3- import static android .app .PendingIntent .getActivity ;
4-
5-
63import android .content .Context ;
74import android .content .SharedPreferences ;
85import android .os .Bundle ;
96import android .preference .PreferenceManager ;
107import androidx .annotation .NonNull ;
118import androidx .annotation .Nullable ;
9+ import androidx .databinding .DataBindingUtil ;
1210import androidx .fragment .app .Fragment ;
1311
12+ import com .eveningoutpost .dexdrip .databinding .StatsGeneralBinding ;
13+ import com .eveningoutpost .dexdrip .models .UserError ;
1414import com .eveningoutpost .dexdrip .models .UserError .Log ;
1515import android .view .LayoutInflater ;
1616import android .view .View ;
1919
2020import com .eveningoutpost .dexdrip .importedlibraries .dexcom .Dex_Constants ;
2121import com .eveningoutpost .dexdrip .R ;
22+ import com .eveningoutpost .dexdrip .utilitymodels .Pref ;
2223
2324import java .text .DecimalFormat ;
2425import java .util .ArrayList ;
2829 * Created by adrian on 30/06/15.
2930 */
3031public class FirstPageFragment extends Fragment {
32+ private final static String TAG = FirstPageFragment .class .getSimpleName ();
3133
3234 private View myView ;
3335
@@ -36,8 +38,9 @@ public class FirstPageFragment extends Fragment {
3638 public View onCreateView (LayoutInflater inflater , @ Nullable ViewGroup container , @ Nullable Bundle savedInstanceState ) {
3739 Log .d ("DrawStats" , "FirstPageFragment onCreateView" );
3840
39- myView = inflater .inflate (
40- R .layout .stats_general , container , false );
41+ StatsGeneralBinding binding = DataBindingUtil .inflate (inflater , R .layout .stats_general , container , false );
42+ myView = binding .getRoot ();
43+ binding .setStatsview (new ViewStats ());
4144
4245 myView .setTag (0 );
4346
@@ -52,6 +55,56 @@ public View getView() {
5255 return myView ;
5356 }
5457
58+ public class ViewStats { // Linking to stats_general layout
59+ public boolean viewAbsolutes () { // Show absolute numbers
60+ return Pref .getBoolean ("show_statistics_absolutes" , false );
61+ }
62+
63+ public boolean viewMedianBG () { // Show BG median
64+ return Pref .getBoolean ("show_statistics_median" , false );
65+ }
66+
67+ public boolean viewA1C () { // Show estimated A1C
68+ return Pref .getBoolean ("show_statistics_a1cestimate" , false );
69+ }
70+
71+ public boolean viewSD () { // Show standard deviation
72+ return Pref .getBoolean ("show_statistics_sd" , false );
73+ }
74+
75+ public boolean viewRelSD () { // Show relative standard deviation
76+ return Pref .getBoolean ("show_statistics_relsd" , false );
77+ }
78+
79+ public boolean viewGviLine () { // Show the GVI line, including GVI and PGS
80+ return Pref .getBoolean ("show_statistics_gvi" , false ) || Pref .getBoolean ("show_statistics_pgs" , false );
81+ }
82+ }
83+
84+ public static void defineDefaults () { // This is where the defaults are defined.
85+ defineDefault ("show_statistics_absolutes" , true ); // Show absolute values
86+ defineDefault ("show_statistics_median" , true ); // Show median by default
87+ defineDefault ("show_statistics_a1cestimate" , true ); // Show estimated A1C
88+ defineDefault ("show_statistics_sd" , true ); // Show standard deviation
89+ defineDefault ("show_statistics_relsd" , true ); // Show relative standard deviation
90+ defineDefault ("show_statistics_pgs" , true ); // Show PGS
91+ defineDefault ("show_statistics_gvi" , true ); // Show GVI
92+ }
93+
94+ public static void defineDefault (String pref , Boolean def ) {
95+ if (!Pref .isPreferenceSet (pref )) { // If the value (of pref) has never been changed
96+ try {
97+ if (!def ) { // If the default is false
98+ // There is no need to take any action if the default is false
99+ } else if (def ) { // If the default is true
100+ Pref .setBoolean (pref , true ); // Enable the setting
101+ }
102+ } catch (Exception e ) {
103+ UserError .Log .wtf (TAG , "incorrect arguments" );
104+ }
105+ }
106+ }
107+
55108 private class CalculationThread extends Thread {
56109
57110 private final View localView ;
@@ -75,7 +128,7 @@ public void run() {
75128 return ;
76129 }
77130
78- //Ranges
131+ // Ranges
79132 long aboveRange = DBSearchUtil .noReadingsAboveRange (context );
80133 long belowRange = DBSearchUtil .noReadingsBelowRange (context );
81134 long inRange = DBSearchUtil .noReadingsInRange (context );
@@ -96,7 +149,7 @@ public void run() {
96149 double stats_high = Double .parseDouble (settings .getString ("highValue" , "170" ));
97150 double stats_low = Double .parseDouble (settings .getString ("lowValue" , "70" ));
98151 TextView rangeView = (TextView ) localView .findViewById (R .id .textView_stats_range_set );
99- //update stats_high/low
152+ // update stats_high/low
100153 if (!mgdl ) {
101154 updateText (localView , rangeView , (Math .round (stats_low * 10 ) / 10d ) + " - " + (Math .round (stats_high * 10 ) / 10d ) + " mmol/l" );
102155 } else {
@@ -127,14 +180,14 @@ public void run() {
127180 }
128181
129182 TextView meanView = (TextView ) localView .findViewById (R .id .textView_mean );
130- //update mean
183+ // update mean
131184 if (mgdl ) {
132185 updateText (localView , meanView , (Math .round (mean * 10 ) / 10d ) + " mg/dl" );
133186 } else {
134187 updateText (localView , meanView , (Math .round (mean * Dex_Constants .MG_DL_TO_MMOL_L * 100 ) / 100d ) + " mmol/l" );
135188
136189 }
137- //update A1c
190+ // update A1c
138191 TextView a1cView = (TextView ) localView .findViewById (R .id .textView_a1c );
139192 int a1c_ifcc = (int ) Math .round (((mean + 46.7 ) / 28.7 - 2.15 ) * 10.929 );
140193 double a1c_dcct = Math .round (10 * (mean + 46.7 ) / 28.7 ) / 10d ;
@@ -156,7 +209,7 @@ public void run() {
156209 updateText (localView , coefficientOfVariation , Math .round (1000d *stdev /mean )/10d + "%" );
157210
158211
159- //calculate BGI / PGS
212+ // calculate GVI / PGS
160213 // https://github.com/nightscout/cgm-remote-monitor/blob/master/lib/report_plugins/glucosedistribution.js#L150
161214 List <BgReadingStats > bgListByTime = DBSearchUtil .getFilteredReadingsWithFallback (false );
162215
@@ -193,7 +246,13 @@ public void run() {
193246 Log .d ("DrawStats" , "NormalReadingspct=" + normalReadingspct + " glucoseMean=" + glucoseMean + " tirMultiplier=" + tirMultiplier + " PGS=" + PGS );
194247 TextView gviView = (TextView ) localView .findViewById (R .id .textView_gvi );
195248 DecimalFormat df = new DecimalFormat ("#.00" );
196- updateText (localView , gviView , df .format (gvi ) + " PGS: " + df .format (PGS ));
249+ if (Pref .getBoolean ("show_statistics_pgs" , false ) && Pref .getBoolean ("show_statistics_gvi" , true )) { // Show both GVI and PGS
250+ updateText (localView , gviView , "GVI: " + df .format (gvi ) + " PGS: " + df .format (PGS ));
251+ } else if (Pref .getBoolean ("show_statistics_gvi" , true )) { // Show only GVI
252+ updateText (localView , gviView , "GVI: " + df .format (gvi ));
253+ } else if (Pref .getBoolean ("show_statistics_pgs" , false )) { // Show only PGS
254+ updateText (localView , gviView , "PGS: " + df .format (PGS ));
255+ }
197256
198257 }
199258 }
0 commit comments