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

Commit c1f2570

Browse files
author
Evgenii
committed
Merge pull request #84 from evgenii-kanivets/decimals
[6h]. Add Decimals feature.
2 parents e15edc9 + 841ab5f commit c1f2570

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+531
-187
lines changed

app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/AccountRepoTest.java

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

66
import com.blogspot.e_kanivets.moneytracker.entity.data.Account;
77
import com.blogspot.e_kanivets.moneytracker.repo.DbHelper;
8-
import com.blogspot.e_kanivets.moneytracker.repo.data.AccountRepo;
98

109
import junit.framework.TestCase;
1110

@@ -41,12 +40,13 @@ public void testGetTable() throws Exception {
4140
}
4241

4342
public void testContentValues() throws Exception {
44-
Account account = new Account(-1, "title1", 100, "NON");
43+
Account account = new Account(-1, "title1", 100, "NON", 30);
4544

4645
ContentValues expected = new ContentValues();
4746
expected.put(DbHelper.TITLE_COLUMN, "title1");
4847
expected.put(DbHelper.CUR_SUM_COLUMN, 100);
4948
expected.put(DbHelper.CURRENCY_COLUMN, "NON");
49+
expected.put(DbHelper.DECIMALS_COLUMN, 30);
5050

5151
ContentValues actual = repo.contentValues(account);
5252

@@ -71,7 +71,7 @@ public void testGetListFromCursor() throws Exception {
7171
Mockito.when(mockCursor.getString(4)).thenReturn("NON");
7272

7373
List<Account> expected = new ArrayList<>();
74-
expected.add(new Account(1, "title", 100, "NON"));
74+
expected.add(new Account(1, "title", 100, "NON", 0));
7575

7676
assertEquals(expected, repo.getListFromCursor(mockCursor));
7777

app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/RecordRepoTest.java

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

66
import com.blogspot.e_kanivets.moneytracker.entity.data.Record;
77
import com.blogspot.e_kanivets.moneytracker.repo.DbHelper;
8-
import com.blogspot.e_kanivets.moneytracker.repo.data.RecordRepo;
98

109
import junit.framework.TestCase;
1110

@@ -41,16 +40,17 @@ public void testGetTable() throws Exception {
4140
}
4241

4342
public void testContentValues() throws Exception {
44-
Record record = new Record(1, 1, Record.TYPE_EXPENSE, "title", 1, 100, 1, "NON");
43+
Record record = new Record(1, 1, Record.TYPE_EXPENSE, "title", 1, 100, 1, "NON", 50);
4544

4645
ContentValues expected = new ContentValues();
4746
expected.put(DbHelper.TIME_COLUMN, 1L);
4847
expected.put(DbHelper.TYPE_COLUMN, Record.TYPE_EXPENSE);
4948
expected.put(DbHelper.TITLE_COLUMN, "title");
5049
expected.put(DbHelper.CATEGORY_ID_COLUMN, 1L);
51-
expected.put(DbHelper.PRICE_COLUMN, 100.0);
50+
expected.put(DbHelper.PRICE_COLUMN, 100);
5251
expected.put(DbHelper.ACCOUNT_ID_COLUMN, 1L);
5352
expected.put(DbHelper.CURRENCY_COLUMN, "NON");
53+
expected.put(DbHelper.DECIMALS_COLUMN, 50);
5454

5555
ContentValues actual = repo.contentValues(record);
5656

@@ -72,6 +72,7 @@ public void testGetListFromCursor() throws Exception {
7272
Mockito.when(mockCursor.getColumnIndex(DbHelper.PRICE_COLUMN)).thenReturn(6);
7373
Mockito.when(mockCursor.getColumnIndex(DbHelper.ACCOUNT_ID_COLUMN)).thenReturn(7);
7474
Mockito.when(mockCursor.getColumnIndex(DbHelper.CURRENCY_COLUMN)).thenReturn(8);
75+
Mockito.when(mockCursor.getColumnIndex(DbHelper.DECIMALS_COLUMN)).thenReturn(9);
7576

7677
Mockito.when(mockCursor.getLong(1)).thenReturn(1L);
7778
Mockito.when(mockCursor.getLong(2)).thenReturn(1L);
@@ -81,9 +82,10 @@ public void testGetListFromCursor() throws Exception {
8182
Mockito.when(mockCursor.getInt(6)).thenReturn(100);
8283
Mockito.when(mockCursor.getLong(7)).thenReturn(1L);
8384
Mockito.when(mockCursor.getString(8)).thenReturn("NON");
85+
Mockito.when(mockCursor.getInt(9)).thenReturn(50);
8486

8587
List<Record> expected = new ArrayList<>();
86-
expected.add(new Record(1, 1, Record.TYPE_EXPENSE, "title", 1, 100, 1, "NON"));
88+
expected.add(new Record(1, 1, Record.TYPE_EXPENSE, "title", 1, 100, 1, "NON", 50));
8789

8890
assertEquals(expected, repo.getListFromCursor(mockCursor));
8991

app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/TransferRepoTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import com.blogspot.e_kanivets.moneytracker.entity.data.Transfer;
77
import com.blogspot.e_kanivets.moneytracker.repo.DbHelper;
8-
import com.blogspot.e_kanivets.moneytracker.repo.data.TransferRepo;
98

109
import junit.framework.TestCase;
1110

@@ -41,14 +40,16 @@ public void testGetTable() throws Exception {
4140
}
4241

4342
public void testContentValues() throws Exception {
44-
Transfer transfer = new Transfer(1, 1, 1, 2, 100, 200);
43+
Transfer transfer = new Transfer(1, 1, 1, 2, 100, 200, 45, 50);
4544

4645
ContentValues expected = new ContentValues();
4746
expected.put(DbHelper.TIME_COLUMN, 1L);
4847
expected.put(DbHelper.FROM_ACCOUNT_ID_COLUMN, 1L);
4948
expected.put(DbHelper.TO_ACCOUNT_ID_COLUMN, 2L);
5049
expected.put(DbHelper.FROM_AMOUNT_COLUMN, 100);
5150
expected.put(DbHelper.TO_AMOUNT_COLUMN, 200);
51+
expected.put(DbHelper.DECIMALS_FROM_COLUMN, 45);
52+
expected.put(DbHelper.DECIMALS_TO_COLUMN, 50);
5253

5354
ContentValues actual = repo.contentValues(transfer);
5455

@@ -68,16 +69,20 @@ public void testGetListFromCursor() throws Exception {
6869
Mockito.when(mockCursor.getColumnIndex(DbHelper.TO_ACCOUNT_ID_COLUMN)).thenReturn(4);
6970
Mockito.when(mockCursor.getColumnIndex(DbHelper.FROM_AMOUNT_COLUMN)).thenReturn(5);
7071
Mockito.when(mockCursor.getColumnIndex(DbHelper.TO_AMOUNT_COLUMN)).thenReturn(6);
72+
Mockito.when(mockCursor.getColumnIndex(DbHelper.DECIMALS_FROM_COLUMN)).thenReturn(7);
73+
Mockito.when(mockCursor.getColumnIndex(DbHelper.DECIMALS_TO_COLUMN)).thenReturn(8);
7174

7275
Mockito.when(mockCursor.getLong(1)).thenReturn(1L);
7376
Mockito.when(mockCursor.getLong(2)).thenReturn(1L);
7477
Mockito.when(mockCursor.getLong(3)).thenReturn(1L);
7578
Mockito.when(mockCursor.getLong(4)).thenReturn(2L);
7679
Mockito.when(mockCursor.getInt(5)).thenReturn(100);
7780
Mockito.when(mockCursor.getInt(6)).thenReturn(200);
81+
Mockito.when(mockCursor.getInt(7)).thenReturn(45);
82+
Mockito.when(mockCursor.getInt(8)).thenReturn(50);
7883

7984
List<Transfer> expected = new ArrayList<>();
80-
expected.add(new Transfer(1, 1, 1, 2, 100, 200));
85+
expected.add(new Transfer(1, 1, 1, 2, 100, 200, 45, 50));
8186

8287
assertEquals(expected, repo.getListFromCursor(mockCursor));
8388

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/SettingsActivity.java

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.blogspot.e_kanivets.moneytracker.MtApp;
99
import com.blogspot.e_kanivets.moneytracker.R;
1010
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity;
11+
import com.blogspot.e_kanivets.moneytracker.controller.FormatController;
12+
import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController;
1113
import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController;
1214
import com.blogspot.e_kanivets.moneytracker.controller.CurrencyController;
1315
import com.blogspot.e_kanivets.moneytracker.entity.data.Account;
@@ -41,6 +43,8 @@ public static class SettingsFragment extends PreferenceFragment {
4143
AccountController accountController;
4244
@Inject
4345
CurrencyController currencyController;
46+
@Inject
47+
PreferenceController preferenceController;
4448

4549
@Override
4650
public void onCreate(Bundle savedInstanceState) {
@@ -53,43 +57,62 @@ public void onCreate(Bundle savedInstanceState) {
5357

5458
setupDefaultAccountPref();
5559
setupDefaultCurrencyPref();
60+
setupDisplayPrecision();
5661
}
5762

5863
private void setupDefaultAccountPref() {
5964
ListPreference defaultAccountPref = (ListPreference) findPreference(getString(R.string.pref_default_account));
60-
defaultAccountPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
61-
@Override
62-
public boolean onPreferenceChange(Preference preference, Object newValue) {
63-
getActivity().setResult(RESULT_OK);
64-
return true;
65-
}
66-
});
65+
defaultAccountPref.setOnPreferenceChangeListener(preferenceChangeListener);
6766

6867
List<Account> accountList = accountController.readAll();
69-
70-
if (accountList.size() > 0)
71-
defaultAccountPref.setDefaultValue(Long.toString(accountList.get(0).getId()));
7268
defaultAccountPref.setEntries(getEntries(accountList));
7369
defaultAccountPref.setEntryValues(getEntryValues(accountList));
70+
71+
Account defaultAccount = accountController.readDefaultAccount();
72+
if (defaultAccount == null) {
73+
defaultAccountPref.setDefaultValue("");
74+
defaultAccountPref.setSummary("");
75+
} else {
76+
defaultAccountPref.setDefaultValue(defaultAccount.getTitle());
77+
defaultAccountPref.setSummary(defaultAccount.getTitle());
78+
}
7479
}
7580

7681
@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
7782
private void setupDefaultCurrencyPref() {
7883
ListPreference defaultCurrencyPref = (ListPreference) findPreference(getString(R.string.pref_default_currency));
79-
defaultCurrencyPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
80-
@Override
81-
public boolean onPreferenceChange(Preference preference, Object newValue) {
82-
getActivity().setResult(RESULT_OK);
83-
return true;
84-
}
85-
});
84+
defaultCurrencyPref.setOnPreferenceChangeListener(preferenceChangeListener);
8685

8786
List<String> currencyList = currencyController.readAll();
87+
defaultCurrencyPref.setEntries(currencyList.toArray(new String[0]));
88+
defaultCurrencyPref.setEntryValues(currencyList.toArray(new String[0]));
89+
8890
String defaultCurrency = currencyController.readDefaultCurrency();
8991
defaultCurrencyPref.setDefaultValue(defaultCurrency);
92+
defaultCurrencyPref.setSummary(defaultCurrency);
93+
}
9094

91-
defaultCurrencyPref.setEntries(currencyList.toArray(new String[0]));
92-
defaultCurrencyPref.setEntryValues(currencyList.toArray(new String[0]));
95+
@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
96+
private void setupDisplayPrecision() {
97+
ListPreference displayPrecisionPref = (ListPreference) findPreference(getString(R.string.pref_display_precision));
98+
displayPrecisionPref.setOnPreferenceChangeListener(preferenceChangeListener);
99+
100+
List<String> precisionListValues = new ArrayList<>();
101+
precisionListValues.add(FormatController.PRECISION_MATH);
102+
precisionListValues.add(FormatController.PRECISION_INT);
103+
precisionListValues.add(FormatController.PRECISION_NONE);
104+
displayPrecisionPref.setEntryValues(precisionListValues.toArray(new String[0]));
105+
106+
List<String> precisionList = new ArrayList<>();
107+
precisionList.add(getString(R.string.precision_math));
108+
precisionList.add(getString(R.string.precision_int));
109+
precisionList.add(getString(R.string.precision_none));
110+
displayPrecisionPref.setEntries(precisionList.toArray(new String[0]));
111+
112+
if (FormatController.PRECISION_MATH.equals(preferenceController.readDisplayPrecision())) {
113+
displayPrecisionPref.setDefaultValue(getString(R.string.precision_math));
114+
displayPrecisionPref.setSummary(getString(R.string.precision_math));
115+
}
93116
}
94117

95118
@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
@@ -113,5 +136,17 @@ private String[] getEntryValues(List<Account> accountList) {
113136

114137
return result.toArray(new String[0]);
115138
}
139+
140+
private Preference.OnPreferenceChangeListener preferenceChangeListener
141+
= new Preference.OnPreferenceChangeListener() {
142+
@Override
143+
public boolean onPreferenceChange(Preference preference, Object newValue) {
144+
// Previously we could set summary to default value,
145+
// but now it's needed to display selected entry
146+
preference.setSummary("%s");
147+
getActivity().setResult(RESULT_OK);
148+
return true;
149+
}
150+
};
116151
}
117152
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
7878

7979
private void addAccount() {
8080
String title = etTitle.getText().toString().trim();
81-
int initSum = Integer.parseInt(etInitSum.getText().toString().trim());
81+
double initSum = Double.parseDouble(etInitSum.getText().toString().trim());
8282
String currency = (String) spinner.getSelectedItem();
8383

8484
Account account = new Account(title, initSum, currency);
85-
8685
accountController.create(account);
8786
}
8887
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ private void doTransfer() {
9494
Account fromAccount = accountList.get(spinnerFrom.getSelectedItemPosition());
9595
Account toAccount = accountList.get(spinnerTo.getSelectedItemPosition());
9696

97-
int fromAmount = -1;
97+
double fromAmount = -1;
9898
try {
99-
fromAmount = Integer.parseInt(etFromAmount.getText().toString());
99+
fromAmount = Double.parseDouble(etFromAmount.getText().toString());
100100
} catch (NumberFormatException e) {
101101
e.printStackTrace();
102102
}
103103

104-
int toAmount = -1;
104+
double toAmount = -1;
105105
try {
106-
toAmount = Integer.parseInt(etToAmount.getText().toString());
106+
toAmount = Double.parseDouble(etToAmount.getText().toString());
107107
} catch (NumberFormatException e) {
108108
e.printStackTrace();
109109
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.blogspot.e_kanivets.moneytracker.R;
2222
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity;
2323
import com.blogspot.e_kanivets.moneytracker.adapter.CategoryAutoCompleteAdapter;
24+
import com.blogspot.e_kanivets.moneytracker.controller.FormatController;
2425
import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController;
2526
import com.blogspot.e_kanivets.moneytracker.controller.data.CategoryController;
2627
import com.blogspot.e_kanivets.moneytracker.controller.data.RecordController;
@@ -62,6 +63,8 @@ public class AddRecordActivity extends BaseBackActivity {
6263
RecordController recordController;
6364
@Inject
6465
AccountController accountController;
66+
@Inject
67+
FormatController formatController;
6568

6669
@Bind(R.id.et_title)
6770
EditText etTitle;
@@ -96,11 +99,11 @@ record = getIntent().getParcelableExtra(KEY_RECORD);
9699
protected void initViews() {
97100
super.initViews();
98101

99-
//Add texts to dialog if it's edit dialog
102+
// Add texts to dialog if it's edit dialog
100103
if (mode == Mode.MODE_EDIT) {
101104
etTitle.setText(record.getTitle());
102105
if (record.getCategory() != null) etCategory.setText(record.getCategory().getName());
103-
etPrice.setText(Integer.toString((int) record.getPrice()));
106+
etPrice.setText(formatController.formatAmount(record.getFullPrice()));
104107
}
105108

106109
presentSpinnerAccount();
@@ -221,14 +224,14 @@ private boolean prepareRecord() {
221224

222225
//Check if price is valid
223226
//noinspection UnusedAssignment
224-
int price = -1;
227+
double price = -1;
225228
try {
226-
price = Integer.parseInt(etPrice.getText().toString());
229+
price = Double.parseDouble(etPrice.getText().toString());
227230
} catch (NumberFormatException e) {
228231
e.printStackTrace();
229232
}
230233

231-
if (price >= 0 && price <= 1000000000 && spinnerAccount.getSelectedItemPosition() >= 0) {
234+
if (price >= 0.0 && price <= 1000000000.0 && spinnerAccount.getSelectedItemPosition() >= 0) {
232235
Account account = null;
233236
if (spinnerAccount.isEnabled())
234237
account = accountList.get(spinnerAccount.getSelectedItemPosition());
@@ -237,7 +240,7 @@ private boolean prepareRecord() {
237240
} else return false;
238241
}
239242

240-
private boolean doRecord(String title, String category, int price, @Nullable Account account) {
243+
private boolean doRecord(String title, String category, double price, @Nullable Account account) {
241244
if (account == null) return false;
242245

243246
if (mode == Mode.MODE_ADD) {
@@ -267,4 +270,4 @@ private boolean doRecord(String title, String category, int price, @Nullable Acc
267270
}
268271

269272
public enum Mode {MODE_ADD, MODE_EDIT}
270-
}
273+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.blogspot.e_kanivets.moneytracker.activity.ReportActivity;
1616
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseDrawerActivity;
1717
import com.blogspot.e_kanivets.moneytracker.adapter.RecordAdapter;
18+
import com.blogspot.e_kanivets.moneytracker.controller.FormatController;
1819
import com.blogspot.e_kanivets.moneytracker.controller.PeriodController;
1920
import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController;
2021
import com.blogspot.e_kanivets.moneytracker.controller.data.ExchangeRateController;
@@ -59,6 +60,8 @@ public class MainActivity extends BaseDrawerActivity {
5960
PreferenceController preferenceController;
6061
@Inject
6162
PeriodController periodController;
63+
@Inject
64+
FormatController formatController;
6265

6366
private ShortSummaryPresenter summaryPresenter;
6467

@@ -228,7 +231,7 @@ private void fillDefaultAccount() {
228231
if (defaultAccount == null) return;
229232

230233
tvDefaultAccountTitle.setText(defaultAccount.getTitle());
231-
tvDefaultAccountSum.setText(Integer.toString(defaultAccount.getCurSum()));
234+
tvDefaultAccountSum.setText(formatController.formatAmount(defaultAccount.getFullSum()));
232235
tvCurrency.setText(defaultAccount.getCurrency());
233236
}
234237
}

0 commit comments

Comments
 (0)