Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,25 @@
import javax.inject.Inject;

public class SettingsActivity extends BaseBackActivity {
@SuppressWarnings("unused")
private static final String TAG = "SettingsActivity";
@SuppressWarnings("unused") private static final String TAG = "SettingsActivity";

@Override
protected int getContentViewId() {
@Override protected int getContentViewId() {
return R.layout.activity_settings;
}

@Override
protected void initViews() {
@Override protected void initViews() {
super.initViews();

// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.content, new SettingsFragment())
.commit();
getFragmentManager().beginTransaction().replace(R.id.content, new SettingsFragment()).commit();
}

public static class SettingsFragment extends PreferenceFragment {
@Inject
AccountController accountController;
@Inject
CurrencyController currencyController;
@Inject
PreferenceController preferenceController;

@Override
public void onCreate(Bundle savedInstanceState) {
@Inject AccountController accountController;
@Inject CurrencyController currencyController;
@Inject PreferenceController preferenceController;

@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

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

setupDefaultAccountPref();
setupDefaultCurrencyPref();
setupNonSubstitutionCurrencyPref();
setupDisplayPrecision();
setupAboutPref();
}

private void setupAboutPref() {
Preference preference = findPreference(getString(R.string.pref_about));
preference.setSummary(getString(R.string.about_summary, BuildConfig.VERSION_NAME,
Build.VERSION.RELEASE));
}

private void setupDefaultAccountPref() {
ListPreference defaultAccountPref = (ListPreference) findPreference(getString(R.string.pref_default_account));
ListPreference defaultAccountPref =
(ListPreference) findPreference(getString(R.string.pref_default_account));
defaultAccountPref.setOnPreferenceChangeListener(preferenceChangeListener);

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

@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
private void setupDefaultCurrencyPref() {
ListPreference defaultCurrencyPref = (ListPreference) findPreference(getString(R.string.pref_default_currency));
@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument") private void setupDefaultCurrencyPref() {
ListPreference defaultCurrencyPref =
(ListPreference) findPreference(getString(R.string.pref_default_currency));
defaultCurrencyPref.setOnPreferenceChangeListener(preferenceChangeListener);

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

@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
private void setupDisplayPrecision() {
ListPreference displayPrecisionPref = (ListPreference) findPreference(getString(R.string.pref_display_precision));
@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument") private void setupNonSubstitutionCurrencyPref() {
ListPreference nonSubstitutionCurrencyPref =
(ListPreference) findPreference(getString(R.string.pref_non_substitution_currency));
nonSubstitutionCurrencyPref.setOnPreferenceChangeListener(preferenceChangeListener);

List<String> currencyList = currencyController.readAll();
nonSubstitutionCurrencyPref.setEntries(currencyList.toArray(new String[0]));
nonSubstitutionCurrencyPref.setEntryValues(currencyList.toArray(new String[0]));

String nonSubstitutionCurrency = preferenceController.readNonSubstitutionCurrency();
nonSubstitutionCurrencyPref.setDefaultValue(nonSubstitutionCurrency);
nonSubstitutionCurrencyPref.setSummary(nonSubstitutionCurrency);
}

@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument") private void setupDisplayPrecision() {
ListPreference displayPrecisionPref =
(ListPreference) findPreference(getString(R.string.pref_display_precision));
displayPrecisionPref.setOnPreferenceChangeListener(preferenceChangeListener);

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

private void setupAboutPref() {
Preference preference = findPreference(getString(R.string.pref_about));
preference.setSummary(getString(R.string.about_summary, BuildConfig.VERSION_NAME, Build.VERSION.RELEASE));
}

@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
private String[] getEntries(List<Account> accountList) {
List<String> result = new ArrayList<>();
Expand All @@ -146,16 +152,15 @@ private String[] getEntryValues(List<Account> accountList) {
return result.toArray(new String[0]);
}

private Preference.OnPreferenceChangeListener preferenceChangeListener
= new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// Previously we could set summary to default value,
// but now it's needed to display selected entry
preference.setSummary("%s");
getActivity().setResult(RESULT_OK);
return true;
}
};
private Preference.OnPreferenceChangeListener preferenceChangeListener =
new Preference.OnPreferenceChangeListener() {
@Override public boolean onPreferenceChange(Preference preference, Object newValue) {
// Previously we could set summary to default value,
// but now it's needed to display selected entry
preference.setSummary("%s");
getActivity().setResult(RESULT_OK);
return true;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,19 @@ public class CurrencyController {
private AccountController accountController;
private PreferenceController preferenceController;

@NonNull
private List<String> currencyList;
@NonNull private List<String> currencyList;

public CurrencyController(AccountController accountController, PreferenceController preferenceController) {
this.accountController = accountController;
this.preferenceController = preferenceController;
currencyList = fetchCurrencies();
}

@NonNull
public List<String> readAll() {
@NonNull public List<String> readAll() {
return currencyList;
}

@NonNull
public String readDefaultCurrency() {
@NonNull public String readDefaultCurrency() {
// First of all read from Prefs
String currency = preferenceController.readDefaultCurrency();

Expand All @@ -54,8 +51,7 @@ public String readDefaultCurrency() {
return currency;
}

@NonNull
private List<String> fetchCurrencies() {
@NonNull private List<String> fetchCurrencies() {
Set<Currency> toret = new HashSet<>();
Locale[] locs = Locale.getAvailableLocales();

Expand All @@ -67,13 +63,15 @@ private List<String> fetchCurrencies() {
}
}

List<String> currencyList = new ArrayList<>();
List<String> currencySet = new ArrayList<>();
for (Currency currency : toret) {
currencyList.add(currency.getCurrencyCode());
currencySet.add(currency.getCurrencyCode());
}

currencyList.add(DbHelper.DEFAULT_ACCOUNT_CURRENCY);
currencyList.add("BYN"); // New belorussian ruble
currencySet.add(DbHelper.DEFAULT_ACCOUNT_CURRENCY);
currencySet.add("BYN"); // New belorussian ruble

currencyList = new ArrayList<>(currencySet);

Collections.sort(currencyList);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.blogspot.e_kanivets.moneytracker.R;

import com.blogspot.e_kanivets.moneytracker.repo.DbHelper;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand All @@ -33,8 +34,7 @@ public class PreferenceController {

private static final int RATE_PERIOD = 5;

@NonNull
private Context context;
@NonNull private Context context;

public PreferenceController(@NonNull Context context) {
this.context = context;
Expand Down Expand Up @@ -113,22 +113,27 @@ public long readDefaultAccountId() {
return Long.parseLong(preferences.getString(defaultAccountPref, "-1"));
}

@Nullable
public String readDefaultCurrency() {
@Nullable public String readDefaultCurrency() {
String defaultCurrencyPref = context.getString(R.string.pref_default_currency);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

return preferences.getString(defaultCurrencyPref, null);
}

@NonNull
public String readDisplayPrecision() {
@NonNull public String readDisplayPrecision() {
String displayPrecisionPref = context.getString(R.string.pref_display_precision);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

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

@Nullable public String readNonSubstitutionCurrency() {
String nonSubstitutionCurrencyPref = context.getString(R.string.pref_non_substitution_currency);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

return preferences.getString(nonSubstitutionCurrencyPref, DbHelper.DEFAULT_ACCOUNT_CURRENCY);
}

public long readFirstTs() {
return getDefaultPrefs().getLong(KEY_FIRST_TS, -1);
}
Expand All @@ -137,28 +142,23 @@ public long readLastTs() {
return getDefaultPrefs().getLong(KEY_LAST_TS, -1);
}

@Nullable
public String readPeriodType() {
@Nullable public String readPeriodType() {
return getDefaultPrefs().getString(KEY_PERIOD_TYPE, null);
}

@Nullable
public String readDropboxAccessToken() {
@Nullable public String readDropboxAccessToken() {
return getDefaultPrefs().getString(KEY_DROPBOX_ACCESS_TOKEN, null);
}

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

@NonNull
public Map<String, String> readRecordTitleCategoryPairs() {
@NonNull public Map<String, String> readRecordTitleCategoryPairs() {
Map<String, String> map = new TreeMap<>();

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

@NonNull
private SharedPreferences getDefaultPrefs() {
@NonNull private SharedPreferences getDefaultPrefs() {
return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
}

@NonNull
private SharedPreferences.Editor getEditor() {
@NonNull private SharedPreferences.Editor getEditor() {
return getDefaultPrefs().edit();
}
}
Loading