Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 1ca4cb9

Browse files
author
Evgenii Kanivets
committed
#137[30m]. Fix 'Can't edit date and time of record'.
1 parent a8d1db5 commit 1ca4cb9

File tree

9 files changed

+81
-104
lines changed

9 files changed

+81
-104
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/AddAccountActivity.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.view.MenuItem;
66
import android.view.View;
77
import android.widget.ArrayAdapter;
8+
import android.widget.EditText;
89

910
import com.blogspot.e_kanivets.moneytracker.R;
1011
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity;
@@ -34,6 +35,10 @@ public class AddAccountActivity extends BaseBackActivity {
3435

3536
@Bind(R.id.content)
3637
View contentView;
38+
@Bind(R.id.et_title)
39+
EditText etTitle;
40+
@Bind(R.id.et_init_sum)
41+
EditText etInitSum;
3742
@Bind(R.id.spinner)
3843
AppCompatSpinner spinner;
3944

@@ -88,8 +93,13 @@ private void tryAddAccount() {
8893

8994
@SuppressWarnings("SimplifiableIfStatement")
9095
private boolean addAccount() {
91-
Account account = accountValidator.validate();
92-
if (account == null) return false;
93-
else return accountController.create(account) != null;
96+
if (accountValidator.validate()) {
97+
String title = etTitle.getText().toString().trim();
98+
double initSum = Double.parseDouble(etInitSum.getText().toString().trim());
99+
String currency = (String) spinner.getSelectedItem();
100+
return accountController.create(new Account(title, initSum, currency)) != null;
101+
} else {
102+
return false;
103+
}
94104
}
95105
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/TransferActivity.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.view.MenuItem;
66
import android.view.View;
77
import android.widget.ArrayAdapter;
8+
import android.widget.EditText;
89

910
import com.blogspot.e_kanivets.moneytracker.R;
1011
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity;
@@ -42,6 +43,10 @@ public class TransferActivity extends BaseBackActivity {
4243
AppCompatSpinner spinnerFrom;
4344
@Bind(R.id.spinner_to)
4445
AppCompatSpinner spinnerTo;
46+
@Bind(R.id.et_from_amount)
47+
EditText etFromAmount;
48+
@Bind(R.id.et_to_amount)
49+
EditText etToAmount;
4550

4651
@Override
4752
protected int getContentViewId() {
@@ -65,7 +70,7 @@ protected void initViews() {
6570
accounts.add(account.getTitle());
6671
}
6772

68-
transferValidator = new TransferValidator(TransferActivity.this, contentView, accountList);
73+
transferValidator = new TransferValidator(TransferActivity.this, contentView);
6974

7075
if (accounts.size() == 0) {
7176
accounts.add(getString(R.string.none));
@@ -109,8 +114,16 @@ private void tryTransfer() {
109114

110115
@SuppressWarnings("SimplifiableIfStatement")
111116
private boolean doTransfer() {
112-
Transfer transfer = transferValidator.validate();
113-
if (transfer == null) return false;
114-
else return transferController.create(transfer) != null;
117+
if (transferValidator.validate()) {
118+
Account fromAccount = accountList.get(spinnerFrom.getSelectedItemPosition());
119+
Account toAccount = accountList.get(spinnerTo.getSelectedItemPosition());
120+
double fromAmount = Double.parseDouble(etFromAmount.getText().toString());
121+
double toAmount = Double.parseDouble(etToAmount.getText().toString());
122+
123+
return transferController.create(new Transfer(System.currentTimeMillis(),
124+
fromAccount.getId(), toAccount.getId(), fromAmount, toAmount)) != null;
125+
} else {
126+
return false;
127+
}
115128
}
116129
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/exchange_rate/AddExchangeRateActivity.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,16 @@ private void tryAddExchangeRate() {
136136

137137
@SuppressWarnings("SimplifiableIfStatement")
138138
private boolean addExchangeRate() {
139-
ExchangeRatePair pair = exchangeRatePairValidator.validate();
140-
if (pair == null) return false;
141-
else return exchangeRateController.createExchangeRatePair(pair) != null;
139+
if (exchangeRatePairValidator.validate()) {
140+
String fromCurrency = (String) spinnerFromCurrency.getSelectedItem();
141+
String toCurrency = (String) spinnerToCurrency.getSelectedItem();
142+
double amountBuy = Double.parseDouble(etBuy.getText().toString().trim());
143+
double amountSell = Double.parseDouble(etSell.getText().toString().trim());
144+
145+
return exchangeRateController.createExchangeRatePair(
146+
new ExchangeRatePair(fromCurrency, toCurrency, amountBuy, amountSell)) != null;
147+
} else {
148+
return false;
149+
}
142150
}
143151
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/record/AddRecordActivity.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.blogspot.e_kanivets.moneytracker.controller.data.CategoryController;
3030
import com.blogspot.e_kanivets.moneytracker.controller.data.RecordController;
3131
import com.blogspot.e_kanivets.moneytracker.entity.data.Account;
32+
import com.blogspot.e_kanivets.moneytracker.entity.data.Category;
3233
import com.blogspot.e_kanivets.moneytracker.entity.data.Record;
3334
import com.blogspot.e_kanivets.moneytracker.ui.AddRecordUiDecorator;
3435
import com.blogspot.e_kanivets.moneytracker.util.AnswersProxy;
@@ -124,9 +125,7 @@ record = getIntent().getParcelableExtra(KEY_RECORD);
124125
protected void initViews() {
125126
super.initViews();
126127

127-
long recordId = record == null ? -1 : record.getId();
128-
recordValidator = new RecordValidator(AddRecordActivity.this, contentView, mode,
129-
accountList, timestamp, type, recordId);
128+
recordValidator = new RecordValidator(AddRecordActivity.this, contentView);
130129

131130
// Add texts to dialog if it's edit dialog
132131
if (mode == Mode.MODE_EDIT) {
@@ -306,14 +305,32 @@ private void tryRecord() {
306305

307306
@SuppressWarnings("SimplifiableIfStatement")
308307
private boolean addRecord() {
309-
Record newRecord = recordValidator.validate();
310-
if (newRecord == null) return false;
311-
else {
308+
if (recordValidator.validate()) {
309+
long now = new Date().getTime();
310+
if (timestamp > now) {
311+
showToast(R.string.record_in_future);
312+
return false;
313+
}
314+
315+
String title = etTitle.getText().toString().trim();
316+
String category = etCategory.getText().toString().trim();
317+
double price = Double.parseDouble(etPrice.getText().toString());
318+
Account account = accountList.get(spinnerAccount.getSelectedItemPosition());
319+
320+
if (title.isEmpty()) {
321+
title = category;
322+
}
323+
312324
if (mode == Mode.MODE_ADD) {
313-
return recordController.create(newRecord) != null;
325+
return recordController.create(new Record(timestamp, type, title,
326+
new Category(category), price, account, account.getCurrency())) != null;
314327
} else if (mode == Mode.MODE_EDIT) {
315-
return recordController.update(newRecord) != null;
328+
long recordId = record == null ? -1 : record.getId();
329+
return recordController.update(new Record(recordId, timestamp, type, title,
330+
new Category(category), price, account, account.getCurrency())) != null;
316331
} else return false;
332+
} else {
333+
return false;
317334
}
318335
}
319336

app/src/main/java/com/blogspot/e_kanivets/moneytracker/util/validator/AccountValidator.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.content.Context;
44
import android.support.annotation.NonNull;
5-
import android.support.annotation.Nullable;
65
import android.support.design.widget.TextInputLayout;
76
import android.support.v7.widget.AppCompatSpinner;
87
import android.view.View;
@@ -43,12 +42,10 @@ public AccountValidator(@NonNull Context context, @NonNull View view) {
4342
initTextWatchers();
4443
}
4544

46-
@Nullable
4745
@Override
48-
public Account validate() {
46+
public boolean validate() {
4947
String title = etTitle.getText().toString().trim();
5048
double initSum = Double.MAX_VALUE;
51-
String currency = (String) spinner.getSelectedItem();
5249

5350
try {
5451
initSum = Double.parseDouble(etInitSum.getText().toString().trim());
@@ -74,7 +71,7 @@ public Account validate() {
7471
valid = false;
7572
}
7673

77-
return valid ? new Account(title, initSum, currency) : null;
74+
return valid;
7875
}
7976

8077
private void initTextWatchers() {

app/src/main/java/com/blogspot/e_kanivets/moneytracker/util/validator/ExchangeRatePairValidator.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.content.Context;
44
import android.support.annotation.NonNull;
5-
import android.support.annotation.Nullable;
65
import android.support.design.widget.TextInputLayout;
76
import android.support.v7.widget.AppCompatSpinner;
87
import android.view.View;
@@ -47,9 +46,8 @@ public ExchangeRatePairValidator(@NonNull Context context, @NonNull View view) {
4746
initTextWatchers();
4847
}
4948

50-
@Nullable
5149
@Override
52-
public ExchangeRatePair validate() {
50+
public boolean validate() {
5351
boolean valid = true;
5452

5553
String fromCurrency = null;
@@ -107,7 +105,7 @@ public ExchangeRatePair validate() {
107105
valid = false;
108106
}
109107

110-
return valid ? new ExchangeRatePair(fromCurrency, toCurrency, amountBuy, amountSell) : null;
108+
return valid;
111109
}
112110

113111
private void initTextWatchers() {
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.blogspot.e_kanivets.moneytracker.util.validator;
22

3-
import android.support.annotation.Nullable;
4-
53
/**
64
* Interface of validators for data models. It may works with UI elements directly.
75
* Created on 06.12.2016.
@@ -13,8 +11,7 @@ public interface IValidator<T> {
1311
long MAX_ABS_VALUE = Integer.MAX_VALUE * 1024L;
1412

1513
/**
16-
* @return instance of class T if validation passed or null otherwise
14+
* @return true if validation passed or false otherwise
1715
*/
18-
@Nullable
19-
T validate();
16+
boolean validate();
2017
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/util/validator/RecordValidator.java

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@
22

33
import android.content.Context;
44
import android.support.annotation.NonNull;
5-
import android.support.annotation.Nullable;
65
import android.support.design.widget.TextInputLayout;
76
import android.support.v7.widget.AppCompatSpinner;
87
import android.view.View;
98
import android.widget.EditText;
109
import android.widget.Toast;
1110

1211
import com.blogspot.e_kanivets.moneytracker.R;
13-
import com.blogspot.e_kanivets.moneytracker.activity.record.AddRecordActivity;
14-
import com.blogspot.e_kanivets.moneytracker.entity.data.Account;
15-
import com.blogspot.e_kanivets.moneytracker.entity.data.Category;
1612
import com.blogspot.e_kanivets.moneytracker.entity.data.Record;
1713

18-
import java.util.Date;
19-
import java.util.List;
20-
2114
import butterknife.Bind;
2215
import butterknife.ButterKnife;
2316

@@ -33,12 +26,6 @@ public class RecordValidator implements IValidator<Record> {
3326
@NonNull
3427
private final Context context;
3528

36-
private AddRecordActivity.Mode mode;
37-
private List<Account> accountList;
38-
private long timestamp;
39-
private long recordId;
40-
private int recordType;
41-
4229
@Bind(R.id.til_title)
4330
TextInputLayout tilTitle;
4431
@Bind(R.id.et_title)
@@ -54,32 +41,19 @@ public class RecordValidator implements IValidator<Record> {
5441
@Bind(R.id.spinner_account)
5542
AppCompatSpinner spinnerAccount;
5643

57-
public RecordValidator(@NonNull Context context, @NonNull View view,
58-
@NonNull AddRecordActivity.Mode mode, @NonNull List<Account> accountList,
59-
long timestamp, int recordType, long recordId) {
44+
public RecordValidator(@NonNull Context context, @NonNull View view) {
6045
this.context = context;
61-
this.mode = mode;
62-
this.accountList = accountList;
63-
this.timestamp = timestamp;
64-
this.recordType = recordType;
65-
this.recordId = recordId;
6646

6747
ButterKnife.bind(this, view);
6848
initTextWatchers();
6949
}
7050

71-
@Nullable
7251
@Override
73-
public Record validate() {
52+
public boolean validate() {
7453
boolean valid = true;
7554

76-
String title = etTitle.getText().toString().trim();
7755
String category = etCategory.getText().toString().trim();
7856

79-
if (title.isEmpty()) {
80-
title = category;
81-
}
82-
8357
if (category.isEmpty()) {
8458
tilCategory.setError(context.getString(R.string.field_cant_be_empty));
8559
valid = false;
@@ -104,32 +78,12 @@ public Record validate() {
10478
valid = false;
10579
}
10680

107-
long now = new Date().getTime();
108-
if (timestamp > now) {
109-
Toast.makeText(context, R.string.record_in_future, Toast.LENGTH_SHORT).show();
110-
valid = false;
111-
}
112-
113-
Account account = null;
114-
if (spinnerAccount.isEnabled()) {
115-
account = accountList.get(spinnerAccount.getSelectedItemPosition());
116-
} else {
81+
if (!spinnerAccount.isEnabled()) {
11782
Toast.makeText(context, R.string.one_account_needed, Toast.LENGTH_SHORT).show();
11883
valid = false;
11984
}
12085

121-
Record record = null;
122-
if (account != null) {
123-
if (mode == AddRecordActivity.Mode.MODE_ADD) {
124-
record = new Record(timestamp, recordType, title, new Category(category),
125-
price, account, account.getCurrency());
126-
} else if (mode == AddRecordActivity.Mode.MODE_EDIT) {
127-
record = new Record(recordId, timestamp, recordType, title, new Category(category),
128-
price, account, account.getCurrency());
129-
}
130-
}
131-
132-
return valid ? record : null;
86+
return valid;
13387
}
13488

13589
private void initTextWatchers() {

0 commit comments

Comments
 (0)