33import android .annotation .SuppressLint ;
44import android .app .DatePickerDialog ;
55import android .app .TimePickerDialog ;
6- import android .graphics .drawable .ColorDrawable ;
7- import android .os .Build ;
8- import android .support .annotation .Nullable ;
9- import android .support .annotation .StyleRes ;
106import android .support .v7 .widget .AppCompatSpinner ;
117import android .text .InputFilter ;
128import android .text .Spanned ;
1511import android .view .Menu ;
1612import android .view .MenuItem ;
1713import android .view .View ;
18- import android .view .Window ;
19- import android .view .WindowManager ;
2014import android .view .inputmethod .EditorInfo ;
2115import android .widget .AdapterView ;
2216import android .widget .ArrayAdapter ;
3630import com .blogspot .e_kanivets .moneytracker .entity .data .Account ;
3731import com .blogspot .e_kanivets .moneytracker .entity .data .Category ;
3832import com .blogspot .e_kanivets .moneytracker .entity .data .Record ;
33+ import com .blogspot .e_kanivets .moneytracker .ui .AddRecordUiDecorator ;
3934import com .blogspot .e_kanivets .moneytracker .util .CategoryAutoCompleter ;
4035
4136import java .util .ArrayList ;
@@ -67,8 +62,6 @@ public class AddRecordActivity extends BaseBackActivity {
6762
6863 private List <Account > accountList ;
6964 private long timestamp ;
70- @ StyleRes
71- private int dialogTheme ;
7265
7366 @ Inject
7467 CategoryController categoryController ;
@@ -79,6 +72,8 @@ public class AddRecordActivity extends BaseBackActivity {
7972 @ Inject
8073 FormatController formatController ;
8174
75+ AddRecordUiDecorator uiDecorator ;
76+
8277 @ Bind (R .id .tv_date )
8378 TextView tvDate ;
8479 @ Bind (R .id .tv_time )
@@ -102,6 +97,8 @@ protected boolean initData() {
10297 super .initData ();
10398 getAppComponent ().inject (AddRecordActivity .this );
10499
100+ uiDecorator = new AddRecordUiDecorator (AddRecordActivity .this );
101+
105102 record = getIntent ().getParcelableExtra (KEY_RECORD );
106103 mode = (Mode ) getIntent ().getSerializableExtra (KEY_MODE );
107104 type = getIntent ().getIntExtra (KEY_TYPE , -1 );
@@ -126,42 +123,9 @@ protected void initViews() {
126123 etPrice .setText (formatController .formatPrecisionNone (record .getFullPrice ()));
127124 }
128125
126+ uiDecorator .decorateActionBar (getSupportActionBar (), mode , type );
129127 presentSpinnerAccount ();
130128
131- dialogTheme = 0 ;
132- if (getSupportActionBar () != null ) {
133- switch (type ) {
134- case Record .TYPE_EXPENSE :
135- if (mode == Mode .MODE_ADD ) getSupportActionBar ().setTitle (R .string .title_add_expense );
136- else getSupportActionBar ().setTitle (R .string .title_edit_expense );
137- getSupportActionBar ().setBackgroundDrawable (
138- new ColorDrawable (getResources ().getColor (R .color .red_light )));
139- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
140- Window window = getWindow ();
141- window .addFlags (WindowManager .LayoutParams .FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS );
142- window .setStatusBarColor (getResources ().getColor (R .color .red_dark ));
143- dialogTheme = R .style .RedDialogTheme ;
144- }
145- break ;
146-
147- case Record .TYPE_INCOME :
148- if (mode == Mode .MODE_ADD ) getSupportActionBar ().setTitle (R .string .title_add_income );
149- else getSupportActionBar ().setTitle (R .string .title_edit_income );
150- getSupportActionBar ().setBackgroundDrawable (
151- new ColorDrawable (getResources ().getColor (R .color .green_light )));
152- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
153- Window window = getWindow ();
154- window .addFlags (WindowManager .LayoutParams .FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS );
155- window .setStatusBarColor (getResources ().getColor (R .color .green_dark ));
156- dialogTheme = R .style .GreenDialogTheme ;
157- }
158- break ;
159-
160- default :
161- break ;
162- }
163- }
164-
165129 etCategory .setAdapter (new CategoryAutoCompleteAdapter (AddRecordActivity .this ,
166130 R .layout .view_category_item , new CategoryAutoCompleter (categoryController )));
167131 etCategory .setOnItemClickListener (new AdapterView .OnItemClickListener () {
@@ -199,9 +163,6 @@ public boolean onPrepareOptionsMenu(Menu menu) {
199163 menu .removeItem (R .id .action_delete );
200164 break ;
201165
202- case MODE_EDIT :
203- break ;
204-
205166 default :
206167 break ;
207168 }
@@ -231,22 +192,22 @@ public boolean onOptionsItemSelected(MenuItem item) {
231192 public void selectDate () {
232193 Calendar calendar = Calendar .getInstance ();
233194 calendar .setTimeInMillis (timestamp );
234- DatePickerDialog dialog = new DatePickerDialog (AddRecordActivity .this , dialogTheme ,
235- new DatePickerDialog .OnDateSetListener () {
236- @ Override
237- public void onDateSet (DatePicker view , int year , int monthOfYear , int dayOfMonth ) {
238- Calendar calendar = Calendar .getInstance ();
239- calendar .setTimeInMillis (timestamp );
240- calendar .set (Calendar .YEAR , year );
241- calendar .set (Calendar .MONTH , monthOfYear );
242- calendar .set (Calendar .DAY_OF_MONTH , dayOfMonth );
243-
244- if (calendar .getTimeInMillis () < new Date ().getTime ()) {
245- timestamp = calendar .getTimeInMillis ();
246- updateDateAndTime ();
247- }
248- }
249- }, calendar .get (Calendar .YEAR ), calendar .get (Calendar .MONTH ),
195+ DatePickerDialog dialog = new DatePickerDialog (AddRecordActivity .this ,
196+ uiDecorator . getTheme ( type ), new DatePickerDialog .OnDateSetListener () {
197+ @ Override
198+ public void onDateSet (DatePicker view , int year , int monthOfYear , int dayOfMonth ) {
199+ Calendar calendar = Calendar .getInstance ();
200+ calendar .setTimeInMillis (timestamp );
201+ calendar .set (Calendar .YEAR , year );
202+ calendar .set (Calendar .MONTH , monthOfYear );
203+ calendar .set (Calendar .DAY_OF_MONTH , dayOfMonth );
204+
205+ if (calendar .getTimeInMillis () < new Date ().getTime ()) {
206+ timestamp = calendar .getTimeInMillis ();
207+ updateDateAndTime ();
208+ }
209+ }
210+ }, calendar .get (Calendar .YEAR ), calendar .get (Calendar .MONTH ),
250211 calendar .get (Calendar .DAY_OF_MONTH ));
251212 dialog .show ();
252213 }
@@ -255,21 +216,21 @@ public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth
255216 public void selectTime () {
256217 Calendar calendar = Calendar .getInstance ();
257218 calendar .setTimeInMillis (timestamp );
258- TimePickerDialog dialog = new TimePickerDialog (AddRecordActivity .this , dialogTheme ,
259- new TimePickerDialog .OnTimeSetListener () {
260- @ Override
261- public void onTimeSet (TimePicker view , int hourOfDay , int minute ) {
262- Calendar calendar = Calendar .getInstance ();
263- calendar .setTimeInMillis (timestamp );
264- calendar .set (Calendar .HOUR_OF_DAY , hourOfDay );
265- calendar .set (Calendar .MINUTE , minute );
266-
267- if (calendar .getTimeInMillis () < new Date ().getTime ()) {
268- timestamp = calendar .getTimeInMillis ();
269- updateDateAndTime ();
270- }
271- }
272- }, calendar .get (Calendar .HOUR_OF_DAY ), calendar .get (Calendar .MINUTE ),
219+ TimePickerDialog dialog = new TimePickerDialog (AddRecordActivity .this ,
220+ uiDecorator . getTheme ( type ), new TimePickerDialog .OnTimeSetListener () {
221+ @ Override
222+ public void onTimeSet (TimePicker view , int hourOfDay , int minute ) {
223+ Calendar calendar = Calendar .getInstance ();
224+ calendar .setTimeInMillis (timestamp );
225+ calendar .set (Calendar .HOUR_OF_DAY , hourOfDay );
226+ calendar .set (Calendar .MINUTE , minute );
227+
228+ if (calendar .getTimeInMillis () < new Date ().getTime ()) {
229+ timestamp = calendar .getTimeInMillis ();
230+ updateDateAndTime ();
231+ }
232+ }
233+ }, calendar .get (Calendar .HOUR_OF_DAY ), calendar .get (Calendar .MINUTE ),
273234 DateFormat .is24HourFormat (AddRecordActivity .this ));
274235 dialog .show ();
275236 }
@@ -344,38 +305,25 @@ private boolean prepareRecord() {
344305 Account account = null ;
345306 if (spinnerAccount .isEnabled ())
346307 account = accountList .get (spinnerAccount .getSelectedItemPosition ());
347-
348- return doRecord (timestamp , title , category , price , account );
349- }
350-
351- private boolean doRecord (long timestamp , String title , String category , double price ,
352- @ Nullable Account account ) {
308+ //noinspection SimplifiableIfStatement
353309 if (account == null ) return false ;
354310
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 ;
311+ return makeRecord (timestamp , title , category , price , account );
312+ }
361313
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 ;
314+ /**
315+ * All data must be valid here.
316+ */
317+ private boolean makeRecord (long timestamp , String title , String category , double price , Account account ) {
318+ long recordId = (record == null ? -1 : record .getId ());
319+ Record record = new Record (recordId , timestamp , type , title , new Category (category ),
320+ price , account , account .getCurrency ());
366321
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 ;
376- }
322+ Record resultedRecord = null ;
323+ if (mode == Mode .MODE_ADD ) resultedRecord = recordController .create (record );
324+ else if (mode == Mode .MODE_EDIT ) resultedRecord = recordController .update (record );
377325
378- return false ;
326+ return resultedRecord != null ;
379327 }
380328
381329 private void updateDateAndTime () {
0 commit comments