Skip to content

Commit 9fb0339

Browse files
author
Evgenii Kanivets
committed
yev-kanivets#153. Add 'NON susbstitution currency' setting.
1 parent 1be7fed commit 9fb0339

File tree

8 files changed

+109
-90
lines changed

8 files changed

+109
-90
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 = currencyController.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: 14 additions & 8 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,17 @@ public String readDefaultCurrency() {
5451
return currency;
5552
}
5653

57-
@NonNull
58-
private List<String> fetchCurrencies() {
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+
64+
@NonNull private List<String> fetchCurrencies() {
5965
Set<Currency> toret = new HashSet<>();
6066
Locale[] locs = Locale.getAvailableLocales();
6167

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

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public class PreferenceController {
3333

3434
private static final int RATE_PERIOD = 5;
3535

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

3938
public PreferenceController(@NonNull Context context) {
4039
this.context = context;
@@ -113,22 +112,27 @@ public long readDefaultAccountId() {
113112
return Long.parseLong(preferences.getString(defaultAccountPref, "-1"));
114113
}
115114

116-
@Nullable
117-
public String readDefaultCurrency() {
115+
@Nullable public String readDefaultCurrency() {
118116
String defaultCurrencyPref = context.getString(R.string.pref_default_currency);
119117
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
120118

121119
return preferences.getString(defaultCurrencyPref, null);
122120
}
123121

124-
@NonNull
125-
public String readDisplayPrecision() {
122+
@NonNull public String readDisplayPrecision() {
126123
String displayPrecisionPref = context.getString(R.string.pref_display_precision);
127124
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
128125

129126
return preferences.getString(displayPrecisionPref, FormatController.PRECISION_MATH);
130127
}
131128

129+
@Nullable public String readNonSubstitutionCurrency() {
130+
String nonSubstitutionCurrencyPref = context.getString(R.string.pref_non_substitution_currency);
131+
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
132+
133+
return preferences.getString(nonSubstitutionCurrencyPref, null);
134+
}
135+
132136
public long readFirstTs() {
133137
return getDefaultPrefs().getLong(KEY_FIRST_TS, -1);
134138
}
@@ -137,28 +141,23 @@ public long readLastTs() {
137141
return getDefaultPrefs().getLong(KEY_LAST_TS, -1);
138142
}
139143

140-
@Nullable
141-
public String readPeriodType() {
144+
@Nullable public String readPeriodType() {
142145
return getDefaultPrefs().getString(KEY_PERIOD_TYPE, null);
143146
}
144147

145-
@Nullable
146-
public String readDropboxAccessToken() {
148+
@Nullable public String readDropboxAccessToken() {
147149
return getDefaultPrefs().getString(KEY_DROPBOX_ACCESS_TOKEN, null);
148150
}
149151

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

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

160-
Set<String> set = getDefaultPrefs().getStringSet(KEY_RECORD_TITLE_CATEGORY_PAIRS,
161-
new HashSet<String>());
160+
Set<String> set = getDefaultPrefs().getStringSet(KEY_RECORD_TITLE_CATEGORY_PAIRS, new HashSet<String>());
162161
for (String entry : set) {
163162
String[] words = entry.split(";");
164163
if (words.length == 2) {
@@ -169,13 +168,11 @@ public Map<String, String> readRecordTitleCategoryPairs() {
169168
return map;
170169
}
171170

172-
@NonNull
173-
private SharedPreferences getDefaultPrefs() {
171+
@NonNull private SharedPreferences getDefaultPrefs() {
174172
return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
175173
}
176174

177-
@NonNull
178-
private SharedPreferences.Editor getEditor() {
175+
@NonNull private SharedPreferences.Editor getEditor() {
179176
return getDefaultPrefs().edit();
180177
}
181178
}

app/src/main/res/values-ru/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<string name="settings">Настройки</string>
5757
<string name="default_account">Счет по умолчанию</string>
5858
<string name="default_currency">Валюта по умолчанию</string>
59+
<string name="non_substitution_currency">Замена валюты NON</string>
5960

6061
<string name="title_charts">Итоги</string>
6162
<string name="incomes">Доходы</string>

app/src/main/res/values-uk/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<string name="settings">Налаштування</string>
5757
<string name="default_account">Рахунок за замовчуванням</string>
5858
<string name="default_currency">Валюта за замовчуванням</string>
59+
<string name="non_substitution_currency">Замiна валюти NON</string>
5960

6061
<string name="title_charts">Підсумки</string>
6162
<string name="incomes">Доходи</string>

app/src/main/res/values-zh/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<string name="settings">设置</string>
5353
<string name="default_account">默认账户</string>
5454
<string name="default_currency">默认货币</string>
55+
<string name="non_substitution_currency">NON 替代貨幣</string>
5556

5657
<string name="title_charts">表格</string>
5758
<string name="incomes">收入</string>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
<string name="pref_default_account" translatable="false">pref_default_account</string>
5959
<string name="pref_default_currency" translatable="false">pref_default_currency</string>
6060
<string name="period_from_to" translatable="false">%1$s - %2$s</string>
61+
<string name="pref_non_substitution_currency" translatable="false">pref_non_substitution_currency</string>
6162
<string name="default_currency">Default currency</string>
63+
<string name="non_substitution_currency">NON substitution currency</string>
6264

6365
<string name="title_charts">Results</string>
6466
<string name="incomes">Incomes</string>
Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
3-
<ListPreference
4-
android:key="@string/pref_default_account"
5-
android:summary="%s"
6-
android:title="@string/default_account" />
3+
<ListPreference
4+
android:key="@string/pref_default_account"
5+
android:summary="%s"
6+
android:title="@string/default_account"/>
77

8-
<ListPreference
9-
android:key="@string/pref_default_currency"
10-
android:summary="%s"
11-
android:title="@string/default_currency" />
8+
<ListPreference
9+
android:key="@string/pref_default_currency"
10+
android:summary="%s"
11+
android:title="@string/default_currency"/>
1212

13-
<ListPreference
14-
android:key="@string/pref_display_precision"
15-
android:summary="%s"
16-
android:title="@string/display_precision" />
13+
<ListPreference
14+
android:key="@string/pref_non_substitution_currency"
15+
android:summary="%s"
16+
android:title="@string/non_substitution_currency"/>
17+
18+
<ListPreference
19+
android:key="@string/pref_display_precision"
20+
android:summary="%s"
21+
android:title="@string/display_precision"/>
22+
23+
<Preference
24+
android:key="@string/pref_about"
25+
android:title="About">
26+
<intent
27+
android:action="android.intent.action.VIEW"
28+
android:targetClass="com.blogspot.e_kanivets.moneytracker.activity.AboutActivity"
29+
android:targetPackage="com.blogspot.e_kanivets.moneytracker"/>
30+
</Preference>
1731

18-
<Preference
19-
android:key="@string/pref_about"
20-
android:title="About">
21-
<intent
22-
android:action="android.intent.action.VIEW"
23-
android:targetClass="com.blogspot.e_kanivets.moneytracker.activity.AboutActivity"
24-
android:targetPackage="com.blogspot.e_kanivets.moneytracker" />
25-
</Preference>
2632
</PreferenceScreen>

0 commit comments

Comments
 (0)