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

Commit 8015367

Browse files
author
Evgenii Kanivets
committed
#153. Substitute NON currency in AccountController.
1 parent 1898059 commit 8015367

File tree

6 files changed

+56
-33
lines changed

6 files changed

+56
-33
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private void setupDefaultAccountPref() {
9797
nonSubstitutionCurrencyPref.setEntries(currencyList.toArray(new String[0]));
9898
nonSubstitutionCurrencyPref.setEntryValues(currencyList.toArray(new String[0]));
9999

100-
String nonSubstitutionCurrency = currencyController.readNonSubstitutionCurrency();
100+
String nonSubstitutionCurrency = preferenceController.readNonSubstitutionCurrency();
101101
nonSubstitutionCurrencyPref.setDefaultValue(nonSubstitutionCurrency);
102102
nonSubstitutionCurrencyPref.setSummary(nonSubstitutionCurrency);
103103
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/CurrencyController.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ public CurrencyController(AccountController accountController, PreferenceControl
5151
return currency;
5252
}
5353

54-
@NonNull public String readNonSubstitutionCurrency() {
55-
// First of all read from Prefs
56-
String currency = preferenceController.readNonSubstitutionCurrency();
57-
58-
// If don't have default currency, use NON
59-
if (currency == null) currency = DbHelper.DEFAULT_ACCOUNT_CURRENCY;
60-
61-
return currency;
62-
}
63-
6454
@NonNull private List<String> fetchCurrencies() {
6555
Set<Currency> toret = new HashSet<>();
6656
Locale[] locs = Locale.getAvailableLocales();

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/PreferenceController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import com.blogspot.e_kanivets.moneytracker.R;
1010

11+
import com.blogspot.e_kanivets.moneytracker.repo.DbHelper;
1112
import java.util.HashSet;
1213
import java.util.Map;
1314
import java.util.Set;
@@ -130,7 +131,7 @@ public long readDefaultAccountId() {
130131
String nonSubstitutionCurrencyPref = context.getString(R.string.pref_non_substitution_currency);
131132
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
132133

133-
return preferences.getString(nonSubstitutionCurrencyPref, null);
134+
return preferences.getString(nonSubstitutionCurrencyPref, DbHelper.DEFAULT_ACCOUNT_CURRENCY);
134135
}
135136

136137
public long readFirstTs() {

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/data/AccountController.java

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.blogspot.e_kanivets.moneytracker.entity.data.Account;
99
import com.blogspot.e_kanivets.moneytracker.entity.data.Record;
1010
import com.blogspot.e_kanivets.moneytracker.entity.data.Transfer;
11+
import com.blogspot.e_kanivets.moneytracker.repo.DbHelper;
1112
import com.blogspot.e_kanivets.moneytracker.repo.base.IRepo;
1213

1314
import java.util.ArrayList;
@@ -20,8 +21,7 @@
2021
* @author Evgenii Kanivets
2122
*/
2223
public class AccountController extends BaseController<Account> {
23-
@SuppressWarnings("unused")
24-
private static final String TAG = "AccountController";
24+
@SuppressWarnings("unused") private static final String TAG = "AccountController";
2525

2626
private final PreferenceController preferenceController;
2727

@@ -30,8 +30,22 @@ public AccountController(IRepo<Account> accountRepo, PreferenceController prefer
3030
this.preferenceController = preferenceController;
3131
}
3232

33-
@NonNull
34-
public List<Account> readActiveAccounts() {
33+
@Nullable @Override public Account read(long id) {
34+
return substituteCurrency(super.read(id));
35+
}
36+
37+
@NonNull @Override public List<Account> readAll() {
38+
List<Account> accountList = super.readAll();
39+
40+
List<Account> result = new ArrayList<>();
41+
for (Account account : accountList) {
42+
result.add(substituteCurrency(account));
43+
}
44+
45+
return result;
46+
}
47+
48+
@NonNull public List<Account> readActiveAccounts() {
3549
List<Account> result = new ArrayList<>();
3650

3751
for (Account account : readAll()) {
@@ -43,8 +57,7 @@ public List<Account> readActiveAccounts() {
4357
return result;
4458
}
4559

46-
@NonNull
47-
public List<Account> readArchivedAccounts() {
60+
@NonNull public List<Account> readArchivedAccounts() {
4861
List<Account> result = new ArrayList<>();
4962

5063
for (Account account : readAll()) {
@@ -128,15 +141,18 @@ public boolean transferDone(@Nullable Transfer transfer) {
128141
return true;
129142
}
130143

131-
@Nullable
132-
public Account readDefaultAccount() {
144+
@Nullable public Account readDefaultAccount() {
133145
long defaultAccountId = preferenceController.readDefaultAccountId();
134146

135-
if (defaultAccountId == -1) return getFirstAccount();
136-
else {
147+
if (defaultAccountId == -1) {
148+
return getFirstAccount();
149+
} else {
137150
Account account = read(defaultAccountId);
138-
if (account == null) return getFirstAccount();
139-
else return account;
151+
if (account == null) {
152+
return getFirstAccount();
153+
} else {
154+
return account;
155+
}
140156
}
141157
}
142158

@@ -162,7 +178,23 @@ public boolean restore(@Nullable Account account) {
162178

163179
private Account getFirstAccount() {
164180
List<Account> accountList = readAll();
165-
if (accountList.size() == 0) return null;
166-
else return accountList.get(0);
181+
if (accountList.size() == 0) {
182+
return null;
183+
} else {
184+
return accountList.get(0);
185+
}
186+
}
187+
188+
private Account substituteCurrency(Account account) {
189+
if (account == null) {
190+
return null;
191+
} else {
192+
String currency = account.getCurrency();
193+
if (DbHelper.DEFAULT_ACCOUNT_CURRENCY.equals(currency)) {
194+
currency = preferenceController.readNonSubstitutionCurrency();
195+
}
196+
return new Account(account.getId(), account.getTitle(), account.getCurSum(), currency,
197+
account.getDecimals(), account.getGoal(), account.isArchived(), account.getColor());
198+
}
167199
}
168200
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/data/RecordController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import android.support.annotation.NonNull;
44
import android.support.annotation.Nullable;
55

6-
import com.blogspot.e_kanivets.moneytracker.controller.CurrencyController;
6+
import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController;
77
import com.blogspot.e_kanivets.moneytracker.repo.DbHelper;
88
import com.blogspot.e_kanivets.moneytracker.controller.base.BaseController;
99
import com.blogspot.e_kanivets.moneytracker.entity.data.Account;
@@ -27,14 +27,14 @@ public class RecordController extends BaseController<Record> {
2727

2828
private final CategoryController categoryController;
2929
private final AccountController accountController;
30-
private final CurrencyController currencyController;
30+
private final PreferenceController preferenceController;
3131

3232
public RecordController(IRepo<Record> recordRepo, CategoryController categoryController,
33-
AccountController accountController, CurrencyController currencyController) {
33+
AccountController accountController, PreferenceController preferenceController) {
3434
super(recordRepo);
3535
this.categoryController = categoryController;
3636
this.accountController = accountController;
37-
this.currencyController = currencyController;
37+
this.preferenceController = preferenceController;
3838
}
3939

4040
@Override @SuppressWarnings("SimplifiableIfStatement") public Record create(@Nullable Record record) {
@@ -110,7 +110,7 @@ record = validateRecord(record);
110110

111111
String currency = record.getCurrency();
112112
if (DbHelper.DEFAULT_ACCOUNT_CURRENCY.equals(currency)) {
113-
currency = currencyController.readNonSubstitutionCurrency();
113+
currency = preferenceController.readNonSubstitutionCurrency();
114114
}
115115

116116
completedRecordList.add(

app/src/main/java/com/blogspot/e_kanivets/moneytracker/di/module/ControllerModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public ExchangeRateController providesExchangeRateController(IRepo<ExchangeRate>
5757

5858
@Provides @NonNull @Singleton
5959
public RecordController providesRecordController(IRepo<Record> recordRepo, CategoryController categoryController,
60-
AccountController accountController, CurrencyController currencyController) {
61-
return new RecordController(recordRepo, categoryController, accountController, currencyController);
60+
AccountController accountController, PreferenceController preferenceController) {
61+
return new RecordController(recordRepo, categoryController, accountController, preferenceController);
6262
}
6363

6464
@Provides @NonNull @Singleton public TransferController providesTransferController(IRepo<Transfer> transferRepo,

0 commit comments

Comments
 (0)