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

Commit 69aab3f

Browse files
author
Evgenii Kanivets
authored
Merge pull request #159 from evgenii-kanivets/feature-153_non_substitution
153 NON substitution.
2 parents 1be7fed + 1dd6dd4 commit 69aab3f

File tree

12 files changed

+188
-165
lines changed

12 files changed

+188
-165
lines changed

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

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,25 @@
2222
import javax.inject.Inject;
2323

2424
public class SettingsActivity extends BaseBackActivity {
25-
@SuppressWarnings("unused")
26-
private static final String TAG = "SettingsActivity";
25+
@SuppressWarnings("unused") private static final String TAG = "SettingsActivity";
2726

28-
@Override
29-
protected int getContentViewId() {
27+
@Override protected int getContentViewId() {
3028
return R.layout.activity_settings;
3129
}
3230

33-
@Override
34-
protected void initViews() {
31+
@Override protected void initViews() {
3532
super.initViews();
3633

3734
// Display the fragment as the main content.
38-
getFragmentManager().beginTransaction()
39-
.replace(R.id.content, new SettingsFragment())
40-
.commit();
35+
getFragmentManager().beginTransaction().replace(R.id.content, new SettingsFragment()).commit();
4136
}
4237

4338
public static class SettingsFragment extends PreferenceFragment {
44-
@Inject
45-
AccountController accountController;
46-
@Inject
47-
CurrencyController currencyController;
48-
@Inject
49-
PreferenceController preferenceController;
50-
51-
@Override
52-
public void onCreate(Bundle savedInstanceState) {
39+
@Inject AccountController accountController;
40+
@Inject CurrencyController currencyController;
41+
@Inject PreferenceController preferenceController;
42+
43+
@Override public void onCreate(Bundle savedInstanceState) {
5344
super.onCreate(savedInstanceState);
5445

5546
MtApp.get().getAppComponent().inject(SettingsFragment.this);
@@ -59,18 +50,14 @@ public void onCreate(Bundle savedInstanceState) {
5950

6051
setupDefaultAccountPref();
6152
setupDefaultCurrencyPref();
53+
setupNonSubstitutionCurrencyPref();
6254
setupDisplayPrecision();
6355
setupAboutPref();
6456
}
6557

66-
private void setupAboutPref() {
67-
Preference preference = findPreference(getString(R.string.pref_about));
68-
preference.setSummary(getString(R.string.about_summary, BuildConfig.VERSION_NAME,
69-
Build.VERSION.RELEASE));
70-
}
71-
7258
private void setupDefaultAccountPref() {
73-
ListPreference defaultAccountPref = (ListPreference) findPreference(getString(R.string.pref_default_account));
59+
ListPreference defaultAccountPref =
60+
(ListPreference) findPreference(getString(R.string.pref_default_account));
7461
defaultAccountPref.setOnPreferenceChangeListener(preferenceChangeListener);
7562

7663
List<Account> accountList = accountController.readActiveAccounts();
@@ -87,9 +74,9 @@ private void setupDefaultAccountPref() {
8774
}
8875
}
8976

90-
@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
91-
private void setupDefaultCurrencyPref() {
92-
ListPreference defaultCurrencyPref = (ListPreference) findPreference(getString(R.string.pref_default_currency));
77+
@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument") private void setupDefaultCurrencyPref() {
78+
ListPreference defaultCurrencyPref =
79+
(ListPreference) findPreference(getString(R.string.pref_default_currency));
9380
defaultCurrencyPref.setOnPreferenceChangeListener(preferenceChangeListener);
9481

9582
List<String> currencyList = currencyController.readAll();
@@ -101,9 +88,23 @@ private void setupDefaultCurrencyPref() {
10188
defaultCurrencyPref.setSummary(defaultCurrency);
10289
}
10390

104-
@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
105-
private void setupDisplayPrecision() {
106-
ListPreference displayPrecisionPref = (ListPreference) findPreference(getString(R.string.pref_display_precision));
91+
@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument") private void setupNonSubstitutionCurrencyPref() {
92+
ListPreference nonSubstitutionCurrencyPref =
93+
(ListPreference) findPreference(getString(R.string.pref_non_substitution_currency));
94+
nonSubstitutionCurrencyPref.setOnPreferenceChangeListener(preferenceChangeListener);
95+
96+
List<String> currencyList = currencyController.readAll();
97+
nonSubstitutionCurrencyPref.setEntries(currencyList.toArray(new String[0]));
98+
nonSubstitutionCurrencyPref.setEntryValues(currencyList.toArray(new String[0]));
99+
100+
String nonSubstitutionCurrency = preferenceController.readNonSubstitutionCurrency();
101+
nonSubstitutionCurrencyPref.setDefaultValue(nonSubstitutionCurrency);
102+
nonSubstitutionCurrencyPref.setSummary(nonSubstitutionCurrency);
103+
}
104+
105+
@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument") private void setupDisplayPrecision() {
106+
ListPreference displayPrecisionPref =
107+
(ListPreference) findPreference(getString(R.string.pref_display_precision));
107108
displayPrecisionPref.setOnPreferenceChangeListener(preferenceChangeListener);
108109

109110
List<String> precisionListValues = new ArrayList<>();
@@ -124,6 +125,11 @@ private void setupDisplayPrecision() {
124125
}
125126
}
126127

128+
private void setupAboutPref() {
129+
Preference preference = findPreference(getString(R.string.pref_about));
130+
preference.setSummary(getString(R.string.about_summary, BuildConfig.VERSION_NAME, Build.VERSION.RELEASE));
131+
}
132+
127133
@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
128134
private String[] getEntries(List<Account> accountList) {
129135
List<String> result = new ArrayList<>();
@@ -146,16 +152,15 @@ private String[] getEntryValues(List<Account> accountList) {
146152
return result.toArray(new String[0]);
147153
}
148154

149-
private Preference.OnPreferenceChangeListener preferenceChangeListener
150-
= new Preference.OnPreferenceChangeListener() {
151-
@Override
152-
public boolean onPreferenceChange(Preference preference, Object newValue) {
153-
// Previously we could set summary to default value,
154-
// but now it's needed to display selected entry
155-
preference.setSummary("%s");
156-
getActivity().setResult(RESULT_OK);
157-
return true;
158-
}
159-
};
155+
private Preference.OnPreferenceChangeListener preferenceChangeListener =
156+
new Preference.OnPreferenceChangeListener() {
157+
@Override public boolean onPreferenceChange(Preference preference, Object newValue) {
158+
// Previously we could set summary to default value,
159+
// but now it's needed to display selected entry
160+
preference.setSummary("%s");
161+
getActivity().setResult(RESULT_OK);
162+
return true;
163+
}
164+
};
160165
}
161166
}

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,19 @@ public class CurrencyController {
2525
private AccountController accountController;
2626
private PreferenceController preferenceController;
2727

28-
@NonNull
29-
private List<String> currencyList;
28+
@NonNull private List<String> currencyList;
3029

3130
public CurrencyController(AccountController accountController, PreferenceController preferenceController) {
3231
this.accountController = accountController;
3332
this.preferenceController = preferenceController;
3433
currencyList = fetchCurrencies();
3534
}
3635

37-
@NonNull
38-
public List<String> readAll() {
36+
@NonNull public List<String> readAll() {
3937
return currencyList;
4038
}
4139

42-
@NonNull
43-
public String readDefaultCurrency() {
40+
@NonNull public String readDefaultCurrency() {
4441
// First of all read from Prefs
4542
String currency = preferenceController.readDefaultCurrency();
4643

@@ -54,8 +51,7 @@ public String readDefaultCurrency() {
5451
return currency;
5552
}
5653

57-
@NonNull
58-
private List<String> fetchCurrencies() {
54+
@NonNull private List<String> fetchCurrencies() {
5955
Set<Currency> toret = new HashSet<>();
6056
Locale[] locs = Locale.getAvailableLocales();
6157

@@ -67,13 +63,15 @@ private List<String> fetchCurrencies() {
6763
}
6864
}
6965

70-
List<String> currencyList = new ArrayList<>();
66+
List<String> currencySet = new ArrayList<>();
7167
for (Currency currency : toret) {
72-
currencyList.add(currency.getCurrencyCode());
68+
currencySet.add(currency.getCurrencyCode());
7369
}
7470

75-
currencyList.add(DbHelper.DEFAULT_ACCOUNT_CURRENCY);
76-
currencyList.add("BYN"); // New belorussian ruble
71+
currencySet.add(DbHelper.DEFAULT_ACCOUNT_CURRENCY);
72+
currencySet.add("BYN"); // New belorussian ruble
73+
74+
currencyList = new ArrayList<>(currencySet);
7775

7876
Collections.sort(currencyList);
7977

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

Lines changed: 18 additions & 20 deletions
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;
@@ -33,8 +34,7 @@ public class PreferenceController {
3334

3435
private static final int RATE_PERIOD = 5;
3536

36-
@NonNull
37-
private Context context;
37+
@NonNull private Context context;
3838

3939
public PreferenceController(@NonNull Context context) {
4040
this.context = context;
@@ -113,22 +113,27 @@ public long readDefaultAccountId() {
113113
return Long.parseLong(preferences.getString(defaultAccountPref, "-1"));
114114
}
115115

116-
@Nullable
117-
public String readDefaultCurrency() {
116+
@Nullable public String readDefaultCurrency() {
118117
String defaultCurrencyPref = context.getString(R.string.pref_default_currency);
119118
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
120119

121120
return preferences.getString(defaultCurrencyPref, null);
122121
}
123122

124-
@NonNull
125-
public String readDisplayPrecision() {
123+
@NonNull public String readDisplayPrecision() {
126124
String displayPrecisionPref = context.getString(R.string.pref_display_precision);
127125
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
128126

129127
return preferences.getString(displayPrecisionPref, FormatController.PRECISION_MATH);
130128
}
131129

130+
@Nullable public String readNonSubstitutionCurrency() {
131+
String nonSubstitutionCurrencyPref = context.getString(R.string.pref_non_substitution_currency);
132+
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
133+
134+
return preferences.getString(nonSubstitutionCurrencyPref, DbHelper.DEFAULT_ACCOUNT_CURRENCY);
135+
}
136+
132137
public long readFirstTs() {
133138
return getDefaultPrefs().getLong(KEY_FIRST_TS, -1);
134139
}
@@ -137,28 +142,23 @@ public long readLastTs() {
137142
return getDefaultPrefs().getLong(KEY_LAST_TS, -1);
138143
}
139144

140-
@Nullable
141-
public String readPeriodType() {
145+
@Nullable public String readPeriodType() {
142146
return getDefaultPrefs().getString(KEY_PERIOD_TYPE, null);
143147
}
144148

145-
@Nullable
146-
public String readDropboxAccessToken() {
149+
@Nullable public String readDropboxAccessToken() {
147150
return getDefaultPrefs().getString(KEY_DROPBOX_ACCESS_TOKEN, null);
148151
}
149152

150-
@NonNull
151-
public Set<String> readFilteredCategories() {
153+
@NonNull public Set<String> readFilteredCategories() {
152154
// http://stackoverflow.com/questions/14034803/misbehavior-when-trying-to-store-a-string-set-using-sharedpreferences/14034804#14034804
153155
return new HashSet<>(getDefaultPrefs().getStringSet(KEY_FILTERED_CATEGORIES, new HashSet<String>()));
154156
}
155157

156-
@NonNull
157-
public Map<String, String> readRecordTitleCategoryPairs() {
158+
@NonNull public Map<String, String> readRecordTitleCategoryPairs() {
158159
Map<String, String> map = new TreeMap<>();
159160

160-
Set<String> set = getDefaultPrefs().getStringSet(KEY_RECORD_TITLE_CATEGORY_PAIRS,
161-
new HashSet<String>());
161+
Set<String> set = getDefaultPrefs().getStringSet(KEY_RECORD_TITLE_CATEGORY_PAIRS, new HashSet<String>());
162162
for (String entry : set) {
163163
String[] words = entry.split(";");
164164
if (words.length == 2) {
@@ -169,13 +169,11 @@ public Map<String, String> readRecordTitleCategoryPairs() {
169169
return map;
170170
}
171171

172-
@NonNull
173-
private SharedPreferences getDefaultPrefs() {
172+
@NonNull private SharedPreferences getDefaultPrefs() {
174173
return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
175174
}
176175

177-
@NonNull
178-
private SharedPreferences.Editor getEditor() {
176+
@NonNull private SharedPreferences.Editor getEditor() {
179177
return getDefaultPrefs().edit();
180178
}
181179
}

0 commit comments

Comments
 (0)