2525import android .widget .EditText ;
2626import android .widget .TextView ;
2727import android .widget .TimePicker ;
28+ import android .widget .Toast ;
2829
2930import com .blogspot .e_kanivets .moneytracker .R ;
3031import com .blogspot .e_kanivets .moneytracker .activity .base .BaseBackActivity ;
3435import com .blogspot .e_kanivets .moneytracker .controller .data .CategoryController ;
3536import com .blogspot .e_kanivets .moneytracker .controller .data .RecordController ;
3637import com .blogspot .e_kanivets .moneytracker .entity .data .Account ;
37- import com .blogspot .e_kanivets .moneytracker .entity .data .Category ;
3838import com .blogspot .e_kanivets .moneytracker .entity .data .Record ;
3939import com .blogspot .e_kanivets .moneytracker .util .CategoryAutoCompleter ;
40+ import com .blogspot .e_kanivets .moneytracker .util .validator .IValidator ;
41+ import com .blogspot .e_kanivets .moneytracker .util .validator .RecordValidator ;
4042
4143import java .util .ArrayList ;
4244import java .util .Calendar ;
@@ -61,6 +63,7 @@ public class AddRecordActivity extends BaseBackActivity {
6163 public static final String KEY_MODE = "key_mode" ;
6264 public static final String KEY_TYPE = "key_type" ;
6365
66+ @ Nullable
6467 private Record record ;
6568 private Mode mode ;
6669 private int type ;
@@ -79,6 +82,10 @@ public class AddRecordActivity extends BaseBackActivity {
7982 @ Inject
8083 FormatController formatController ;
8184
85+ private IValidator <Record > recordValidator ;
86+
87+ @ Bind (R .id .content )
88+ View contentView ;
8289 @ Bind (R .id .tv_date )
8390 TextView tvDate ;
8491 @ Bind (R .id .tv_time )
@@ -107,10 +114,11 @@ record = getIntent().getParcelableExtra(KEY_RECORD);
107114 type = getIntent ().getIntExtra (KEY_TYPE , -1 );
108115 accountList = accountController .readAll ();
109116
110- if (mode == Mode .MODE_EDIT ) timestamp = record .getTime ();
117+ if (mode == Mode .MODE_EDIT && record != null ) timestamp = record .getTime ();
111118 else timestamp = new Date ().getTime ();
112119
113- return mode != null && type != -1 && (!mode .equals (Mode .MODE_EDIT ) || record != null );
120+ return mode != null && (type == Record .TYPE_INCOME || type == Record .TYPE_EXPENSE )
121+ && (!mode .equals (Mode .MODE_EDIT ) || record != null );
114122 }
115123
116124 @ SuppressWarnings ("deprecation" )
@@ -119,6 +127,10 @@ record = getIntent().getParcelableExtra(KEY_RECORD);
119127 protected void initViews () {
120128 super .initViews ();
121129
130+ long recordId = record == null ? -1 : record .getId ();
131+ recordValidator = new RecordValidator (AddRecordActivity .this , contentView , mode ,
132+ accountList , timestamp , type , recordId );
133+
122134 // Add texts to dialog if it's edit dialog
123135 if (mode == Mode .MODE_EDIT ) {
124136 etTitle .setText (record .getTitle ());
@@ -132,7 +144,8 @@ protected void initViews() {
132144 if (getSupportActionBar () != null ) {
133145 switch (type ) {
134146 case Record .TYPE_EXPENSE :
135- if (mode == Mode .MODE_ADD ) getSupportActionBar ().setTitle (R .string .title_add_expense );
147+ if (mode == Mode .MODE_ADD )
148+ getSupportActionBar ().setTitle (R .string .title_add_expense );
136149 else getSupportActionBar ().setTitle (R .string .title_edit_expense );
137150 getSupportActionBar ().setBackgroundDrawable (
138151 new ColorDrawable (getResources ().getColor (R .color .red_light )));
@@ -145,7 +158,8 @@ protected void initViews() {
145158 break ;
146159
147160 case Record .TYPE_INCOME :
148- if (mode == Mode .MODE_ADD ) getSupportActionBar ().setTitle (R .string .title_add_income );
161+ if (mode == Mode .MODE_ADD )
162+ getSupportActionBar ().setTitle (R .string .title_add_income );
149163 else getSupportActionBar ().setTitle (R .string .title_edit_income );
150164 getSupportActionBar ().setBackgroundDrawable (
151165 new ColorDrawable (getResources ().getColor (R .color .green_light )));
@@ -217,9 +231,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
217231 return true ;
218232
219233 case R .id .action_delete :
220- recordController .delete (record );
221- setResult (RESULT_OK );
222- finish ();
234+ if (recordController .delete (record )) {
235+ setResult (RESULT_OK );
236+ finish ();
237+ }
223238 return true ;
224239
225240 default :
@@ -244,6 +259,8 @@ public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth
244259 if (calendar .getTimeInMillis () < new Date ().getTime ()) {
245260 timestamp = calendar .getTimeInMillis ();
246261 updateDateAndTime ();
262+ } else {
263+ showToast (R .string .record_in_future );
247264 }
248265 }
249266 }, calendar .get (Calendar .YEAR ), calendar .get (Calendar .MONTH ),
@@ -267,20 +284,15 @@ public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
267284 if (calendar .getTimeInMillis () < new Date ().getTime ()) {
268285 timestamp = calendar .getTimeInMillis ();
269286 updateDateAndTime ();
287+ } else {
288+ showToast (R .string .record_in_future );
270289 }
271290 }
272291 }, calendar .get (Calendar .HOUR_OF_DAY ), calendar .get (Calendar .MINUTE ),
273292 DateFormat .is24HourFormat (AddRecordActivity .this ));
274293 dialog .show ();
275294 }
276295
277- private void tryRecord () {
278- if (prepareRecord ()) {
279- setResult (RESULT_OK );
280- finish ();
281- } else showToast (R .string .wrong_number_text );
282- }
283-
284296 private void presentSpinnerAccount () {
285297 List <String > accounts = new ArrayList <>();
286298 for (Account account : accountList ) {
@@ -290,7 +302,7 @@ private void presentSpinnerAccount() {
290302 int selectedAccountIndex = -1 ;
291303
292304 if (mode == Mode .MODE_EDIT ) {
293- if (record .getAccount () != null ) {
305+ if (record != null && record .getAccount () != null ) {
294306 for (int i = 0 ; i < accountList .size (); i ++) {
295307 Account account = accountList .get (i );
296308 if (account .getId () == record .getAccount ().getId ()) selectedAccountIndex = i ;
@@ -310,72 +322,36 @@ private void presentSpinnerAccount() {
310322 selectedAccountIndex = 0 ;
311323 spinnerAccount .setEnabled (false );
312324
313- accounts = new ArrayList <>();
314- accounts .add (getString (R .string .account_was_removed ));
325+ if (accounts .size () == 0 ) {
326+ accounts .add (getString (R .string .none ));
327+ } else {
328+ accounts .add (getString (R .string .account_was_removed ));
329+ }
315330 }
316331
317332 spinnerAccount .setAdapter (new ArrayAdapter <>(AddRecordActivity .this ,
318333 R .layout .view_spinner_item , accounts ));
319334 spinnerAccount .setSelection (selectedAccountIndex );
320335 }
321336
322- private boolean prepareRecord () {
323- String title = etTitle .getText ().toString ().trim ();
324- String category = etCategory .getText ().toString ().trim ();
325-
326- if (title .isEmpty ()) return false ;
327- if (category .isEmpty ()) return false ;
328-
329- //Check if price is valid
330- double price = -1 ;
331- try {
332- price = Double .parseDouble (etPrice .getText ().toString ());
333- } catch (NumberFormatException e ) {
334- e .printStackTrace ();
337+ private void tryRecord () {
338+ if (addRecord ()) {
339+ setResult (RESULT_OK );
340+ finish ();
335341 }
336-
337- long now = new Date ().getTime ();
338- if (timestamp > now ) return false ;
339-
340- if (price < 0.0 || price > 1000000000.0 ) return false ;
341-
342- if (spinnerAccount .getSelectedItemPosition () < 0 ) return false ;
343-
344- Account account = null ;
345- if (spinnerAccount .isEnabled ())
346- account = accountList .get (spinnerAccount .getSelectedItemPosition ());
347-
348- return doRecord (timestamp , title , category , price , account );
349342 }
350343
351- private boolean doRecord (long timestamp , String title , String category , double price ,
352- @ Nullable Account account ) {
353- if (account == null ) return false ;
354-
355- if (mode == Mode .MODE_ADD ) {
356- switch (type ) {
357- case Record .TYPE_EXPENSE :
358- recordController .create (new Record (timestamp , Record .TYPE_EXPENSE , title ,
359- new Category (category ), price , account , account .getCurrency ()));
360- return true ;
361-
362- case Record .TYPE_INCOME :
363- recordController .create (new Record (timestamp , Record .TYPE_INCOME , title ,
364- new Category (category ), price , account , account .getCurrency ()));
365- return true ;
366-
367- default :
368- return false ;
369- }
370- } else if (mode == Mode .MODE_EDIT ) {
371- Record updatedRecord = new Record (record .getId (), timestamp , record .getType (),
372- title , new Category (category ), price , account , account .getCurrency ());
373- recordController .update (updatedRecord );
374-
375- return true ;
344+ @ SuppressWarnings ("SimplifiableIfStatement" )
345+ private boolean addRecord () {
346+ Record newRecord = recordValidator .validate ();
347+ if (newRecord == null ) return false ;
348+ else {
349+ if (mode == Mode .MODE_ADD ) {
350+ return recordController .create (newRecord ) != null ;
351+ } else if (mode == Mode .MODE_EDIT ) {
352+ return recordController .update (newRecord ) != null ;
353+ } else return false ;
376354 }
377-
378- return false ;
379355 }
380356
381357 private void updateDateAndTime () {
0 commit comments