77import android .os .Build ;
88import android .os .Environment ;
99import android .support .annotation .NonNull ;
10- import android .support .design .widget .TextInputLayout ;
1110import android .support .v4 .app .DialogFragment ;
1211import android .support .v7 .app .AppCompatActivity ;
1312import android .os .Bundle ;
4645
4746import static com .medavox .util .io .DateTime .TimeFormat ;
4847import static com .medavox .util .io .DateTime .DateFormat ;
48+ import static com .medavox .util .validate .Validator .check ;
4949
50- //todo: import numberpicker module from https://github.com/SimonVT/android-numberpicker,
51- //todo: then customise it to my needs
50+ //consider importing numberpicker module from https://github.com/SimonVT/android-numberpicker,
51+ //then customising it to my needs
5252
5353public class MainActivity extends AppCompatActivity {
5454
@@ -61,12 +61,12 @@ public class MainActivity extends AppCompatActivity {
6161 private static final int smsSendRequestCode = 42 ;
6262 private static final String TAG = "DiabeticDiary" ;
6363
64- private final String [] names = new String [] {"BG" , "CP" , "QA" , "BI" , "KT" };
64+ private final String [] names = new String [] {"BG" , "CP" , "QA" , "BI" , "KT" , "NOTES" };
6565 private final int [] inputIDs = new int [] {R .id .BGinput , R .id .CPinput , R .id .QAinput , R .id .BIinput ,
66- R .id .KTinput };
66+ R .id .KTinput , R . id . notesInput };
6767 private final EditText [] inputs = new EditText [inputIDs .length ];
6868 private final int [] checkboxIDs = new int [] {R .id .BGcheckBox , R .id .CPcheckBox , R .id .QAcheckBox ,
69- R .id .BIcheckBox , R .id .KTcheckBox };
69+ R .id .BIcheckBox , R .id .KTcheckBox , R . id . notesCheckbox };
7070 private final CheckBox [] checkBoxes = new CheckBox [checkboxIDs .length ];
7171
7272 private String waitingMessage = null ;
@@ -84,6 +84,16 @@ protected void onCreate(Bundle savedInstanceState) {
8484 setContentView (R .layout .activity_main );
8585 ButterKnife .bind (this );
8686
87+ //quick sanity check
88+ try {
89+ check (names .length == inputIDs .length && names .length == checkboxIDs .length ,
90+ "the number of checkboxes, input fields and names don't match!" );
91+ }
92+ catch (Exception e ) {
93+ Log .e (TAG , e .getLocalizedMessage ());
94+ System .exit (1 );
95+ }
96+
8797 for (int i = 0 ; i < checkBoxes .length ; i ++) {
8898 checkBoxes [i ] = (CheckBox )findViewById (checkboxIDs [i ]);
8999 }
@@ -95,11 +105,8 @@ protected void onCreate(Bundle savedInstanceState) {
95105 //tick the box when there's text in the input field, and untick it when there's not
96106 final CheckBox cb = checkBoxes [i ];
97107 inputs [i ].addTextChangedListener (new TextWatcher () {
98- @ Override
99- public void beforeTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) { }
100-
101- @ Override
102- public void onTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) { }
108+ @ Override public void beforeTextChanged (CharSequence c , int i , int j , int k ){}
109+ @ Override public void onTextChanged (CharSequence c , int i , int j , int k ){}
103110
104111 @ Override
105112 public void afterTextChanged (Editable editable ) {
@@ -131,14 +138,6 @@ else if(editable.length() > 0 && !cb.isChecked()) {
131138 storageDir = Environment .getExternalStorageDirectory ();
132139 }
133140
134- public int recalculateSmsLength () {
135- int length = 0 ;
136- for (int i = 0 ; i < inputs .length ; i ++) {
137- length += (checkBoxes [i ].isChecked () ? names [i ].length () + inputs [i ].length () +3 : 0 );
138- }
139- return length ;
140- }
141-
142141 @ Override
143142 protected void onResume () {
144143 super .onResume ();
@@ -170,7 +169,6 @@ public boolean onCreateOptionsMenu(Menu menu) {
170169 public boolean onOptionsItemSelected (MenuItem item ) {
171170 switch (item .getItemId ()) {
172171 case R .id .edit_numbers_menu_item :
173- //todo: show phone number entry dialog
174172 DialogFragment newFragment = new EditNumbersDialogFragment ();
175173 newFragment .show (getSupportFragmentManager (), "EditNumbersDialog" );
176174 return true ;
@@ -200,11 +198,17 @@ public void clickRecordButton() {
200198 if (checkBoxes [i ].isChecked ()) {
201199 anyTicked = true ;
202200 smsFormatOut += names [i ]+":" +inputs [i ].getText ()+", " ;
203- csvFormatOut += inputs [i ].getText ();
201+ boolean isNotes = names [i ].equals ("NOTES" );
202+ csvFormatOut += (isNotes ? "\" " : "" ) + inputs [i ].getText () + (isNotes ?"\" " :"" );
204203 }
205204 }
205+ if (smsFormatOut .endsWith (", " )) {
206+ smsFormatOut = smsFormatOut .substring (0 , smsFormatOut .length ()-2 );
207+ }
206208 //csvFormatLine = csvFormatLine.substring(1);
207209 Log .i (TAG , smsFormatOut );
210+ Log .i (TAG , "notes length:" +inputs [5 ].getText ().length ());
211+ Log .i (TAG , "sms length:" +smsFormatOut .length ());
208212
209213 if (!anyTicked ) {
210214 Toast .makeText (MainActivity .this , "No inputs ticked!" , Toast .LENGTH_SHORT ).show ();
@@ -389,5 +393,4 @@ public static String stringsOf(Object[] array) {
389393 public static boolean isValidPhoneNumber (String s ) {
390394 return s .length () == 11 && s .startsWith ("07" );
391395 }
392-
393396}
0 commit comments