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 1 commit
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
Prev Previous commit
Next Next commit
#80[1h 30m]. Add preference to select precision mode.
  • Loading branch information
Evgenii Kanivets committed Jun 3, 2016
commit 053990649270a5012f3d35b4f29e64ee0546e903
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.blogspot.e_kanivets.moneytracker.MtApp;
import com.blogspot.e_kanivets.moneytracker.R;
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity;
import com.blogspot.e_kanivets.moneytracker.controller.FormatController;
import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController;
import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController;
import com.blogspot.e_kanivets.moneytracker.controller.CurrencyController;
import com.blogspot.e_kanivets.moneytracker.entity.data.Account;
Expand Down Expand Up @@ -41,6 +43,8 @@ public static class SettingsFragment extends PreferenceFragment {
AccountController accountController;
@Inject
CurrencyController currencyController;
@Inject
PreferenceController preferenceController;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -53,43 +57,62 @@ public void onCreate(Bundle savedInstanceState) {

setupDefaultAccountPref();
setupDefaultCurrencyPref();
setupDisplayPrecision();
}

private void setupDefaultAccountPref() {
ListPreference defaultAccountPref = (ListPreference) findPreference(getString(R.string.pref_default_account));
defaultAccountPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
getActivity().setResult(RESULT_OK);
return true;
}
});
defaultAccountPref.setOnPreferenceChangeListener(preferenceChangeListener);

List<Account> accountList = accountController.readAll();

if (accountList.size() > 0)
defaultAccountPref.setDefaultValue(Long.toString(accountList.get(0).getId()));
defaultAccountPref.setEntries(getEntries(accountList));
defaultAccountPref.setEntryValues(getEntryValues(accountList));

Account defaultAccount = accountController.readDefaultAccount();
if (defaultAccount == null) {
defaultAccountPref.setDefaultValue("");
defaultAccountPref.setSummary("");
} else {
defaultAccountPref.setDefaultValue(defaultAccount.getTitle());
defaultAccountPref.setSummary(defaultAccount.getTitle());
}
}

@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
private void setupDefaultCurrencyPref() {
ListPreference defaultCurrencyPref = (ListPreference) findPreference(getString(R.string.pref_default_currency));
defaultCurrencyPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
getActivity().setResult(RESULT_OK);
return true;
}
});
defaultCurrencyPref.setOnPreferenceChangeListener(preferenceChangeListener);

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

String defaultCurrency = currencyController.readDefaultCurrency();
defaultCurrencyPref.setDefaultValue(defaultCurrency);
defaultCurrencyPref.setSummary(defaultCurrency);
}

defaultCurrencyPref.setEntries(currencyList.toArray(new String[0]));
defaultCurrencyPref.setEntryValues(currencyList.toArray(new String[0]));
@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
private void setupDisplayPrecision() {
ListPreference displayPrecisionPref = (ListPreference) findPreference(getString(R.string.pref_display_precision));
displayPrecisionPref.setOnPreferenceChangeListener(preferenceChangeListener);

List<String> precisionListValues = new ArrayList<>();
precisionListValues.add(FormatController.PRECISION_MATH);
precisionListValues.add(FormatController.PRECISION_INT);
precisionListValues.add(FormatController.PRECISION_NONE);
displayPrecisionPref.setEntryValues(precisionListValues.toArray(new String[0]));

List<String> precisionList = new ArrayList<>();
precisionList.add(getString(R.string.precision_math));
precisionList.add(getString(R.string.precision_int));
precisionList.add(getString(R.string.precision_none));
displayPrecisionPref.setEntries(precisionList.toArray(new String[0]));

if (FormatController.PRECISION_MATH.equals(preferenceController.readDisplayPrecision())) {
displayPrecisionPref.setDefaultValue(getString(R.string.precision_math));
displayPrecisionPref.setSummary(getString(R.string.precision_math));
}
}

@SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
Expand All @@ -113,5 +136,17 @@ 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;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,30 @@
* @author Evgenii Kanivets
*/
public class FormatController {
public static final String PRECISION_MATH = "precision_math";
public static final String PRECISION_INT = "precision_int";
public static final String PRECISION_NONE = "precision_none";

private PreferenceController preferenceController;

public FormatController(PreferenceController preferenceController) {
this.preferenceController = preferenceController;
}

public String formatAmount(double amount) {
return String.format(Locale.getDefault(), "%.2f", amount);
switch (preferenceController.readDisplayPrecision()) {
case PRECISION_MATH:
return String.format(Locale.getDefault(), "%d", Math.round(amount));

case PRECISION_INT:
return String.format(Locale.getDefault(), "%d", (int) amount);

case PRECISION_NONE:
return String.format(Locale.getDefault(), "%.2f", amount);

default:
return String.format(Locale.getDefault(), "%d", Math.round(amount));
}
}

public String formatSignedAmount(double amount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public String readDefaultCurrency() {
return preferences.getString(defaultCurrencyPref, null);
}

@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);
}

public long readFirstTs() {
return getDefaultPrefs().getLong(KEY_FIRST_TS, -1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public PeriodController providesPeriodController(PreferenceController preference
@Provides
@NonNull
@Singleton
public FormatController providesFormatController() {
return new FormatController();
public FormatController providesFormatController(PreferenceController preferenceController) {
return new FormatController(preferenceController);
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@
<string name="incomes">Доходы</string>
<string name="expenses">Расходы</string>

<string name="display_precision">Точность отображения</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-uk/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@
<string name="incomes">Доходи</string>
<string name="expenses">Витрати</string>

<string name="display_precision">Точність відображення</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,10 @@
<string name="incomes">Incomes</string>
<string name="expenses">Expenses</string>

<string name="pref_display_precision" translatable="false">pref_display_precision</string>
<string name="display_precision">Display precision</string>
<string name="precision_math">9.99$ = 10$</string>
<string name="precision_int">9.99$ = 9$</string>
<string name="precision_none">9.99$ = 9.99$</string>
<!-- Not translated strings -->
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
android:key="@string/pref_default_currency"
android:summary="%s"
android:title="@string/default_currency" />

<ListPreference
android:key="@string/pref_display_precision"
android:summary="%s"
android:title="@string/display_precision" />
</PreferenceScreen>