From 33b8218463475743d93f813e4fa74701cb48fa7a Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Mon, 15 Aug 2016 15:52:47 -0500 Subject: [PATCH] Fix locale number formatting issues --- .../com/todoroo/astrid/adapter/FilterAdapter.java | 7 +++++-- .../todoroo/astrid/core/CustomFilterActivity.java | 4 +++- .../todoroo/astrid/core/CustomFilterAdapter.java | 8 ++++++-- .../tasks/activities/FilterSelectionActivity.java | 4 +++- .../java/org/tasks/dialogs/SeekBarDialog.java | 15 ++++++++++++++- src/main/java/org/tasks/locale/Locale.java | 10 ++++++++++ .../org/tasks/ui/NavigationDrawerFragment.java | 4 +++- .../org/tasks/widget/WidgetConfigActivity.java | 12 ++++++------ src/main/res/layout/dialog_font_size_seekbar.xml | 10 ++++------ src/main/res/layout/dialog_opacity_seekbar.xml | 10 ++++------ src/main/res/layout/filter_adapter_subheader.xml | 13 +++++++++---- 11 files changed, 67 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/todoroo/astrid/adapter/FilterAdapter.java b/src/main/java/com/todoroo/astrid/adapter/FilterAdapter.java index 0cee569e4..ad859ce6a 100644 --- a/src/main/java/com/todoroo/astrid/adapter/FilterAdapter.java +++ b/src/main/java/com/todoroo/astrid/adapter/FilterAdapter.java @@ -31,6 +31,7 @@ import org.tasks.filters.FilterProvider; import org.tasks.filters.NavigationDrawerAction; import org.tasks.filters.NavigationDrawerSeparator; import org.tasks.filters.NavigationDrawerSubheader; +import org.tasks.locale.Locale; import org.tasks.preferences.BasicPreferences; import org.tasks.preferences.HelpAndFeedbackActivity; import org.tasks.themes.Theme; @@ -51,6 +52,7 @@ public class FilterAdapter extends ArrayAdapter { private final FilterCounter filterCounter; private final Activity activity; private boolean navigationDrawer; + private final Locale locale; private final FilterListUpdateReceiver filterListUpdateReceiver = new FilterListUpdateReceiver(); /** layout inflater */ @@ -58,12 +60,13 @@ public class FilterAdapter extends ArrayAdapter { private final ThemeCache themeCache; public FilterAdapter(FilterProvider filterProvider, FilterCounter filterCounter, Activity activity, - boolean navigationDrawer, Theme theme, ThemeCache themeCache) { + boolean navigationDrawer, Theme theme, ThemeCache themeCache, Locale locale) { super(activity, 0); this.filterProvider = filterProvider; this.filterCounter = filterCounter; this.activity = activity; this.navigationDrawer = navigationDrawer; + this.locale = locale; this.inflater = theme.getLayoutInflater(activity); this.themeCache = themeCache; } @@ -294,7 +297,7 @@ public class FilterAdapter extends ArrayAdapter { int countInt = 0; if(filterCounter.containsKey(filter)) { countInt = filterCounter.get(filter); - viewHolder.size.setText(Integer.toString(countInt)); + viewHolder.size.setText(locale.formatNumber(countInt)); } viewHolder.size.setVisibility(countInt > 0 ? View.VISIBLE : View.INVISIBLE); diff --git a/src/main/java/com/todoroo/astrid/core/CustomFilterActivity.java b/src/main/java/com/todoroo/astrid/core/CustomFilterActivity.java index bcba7709b..e0fd422e6 100644 --- a/src/main/java/com/todoroo/astrid/core/CustomFilterActivity.java +++ b/src/main/java/com/todoroo/astrid/core/CustomFilterActivity.java @@ -40,6 +40,7 @@ import org.tasks.dialogs.DialogBuilder; import org.tasks.filters.FilterCriteriaProvider; import org.tasks.injection.ActivityComponent; import org.tasks.injection.ThemedInjectingAppCompatActivity; +import org.tasks.locale.Locale; import org.tasks.ui.MenuColorizer; import java.util.ArrayList; @@ -134,6 +135,7 @@ public class CustomFilterActivity extends ThemedInjectingAppCompatActivity imple @Inject StoreObjectDao storeObjectDao; @Inject DialogBuilder dialogBuilder; @Inject FilterCriteriaProvider filterCriteriaProvider; + @Inject Locale locale; @BindView(R.id.tag_name) EditText filterName; @BindView(R.id.toolbar) Toolbar toolbar; @@ -157,7 +159,7 @@ public class CustomFilterActivity extends ThemedInjectingAppCompatActivity imple List startingCriteria = new ArrayList<>(); startingCriteria.add(getStartingUniverse()); - adapter = new CustomFilterAdapter(this, dialogBuilder, startingCriteria); + adapter = new CustomFilterAdapter(this, dialogBuilder, startingCriteria, locale); listView.setAdapter(adapter); updateList(); diff --git a/src/main/java/com/todoroo/astrid/core/CustomFilterAdapter.java b/src/main/java/com/todoroo/astrid/core/CustomFilterAdapter.java index 68687816c..b6efe9056 100644 --- a/src/main/java/com/todoroo/astrid/core/CustomFilterAdapter.java +++ b/src/main/java/com/todoroo/astrid/core/CustomFilterAdapter.java @@ -24,6 +24,7 @@ import com.todoroo.astrid.core.CustomFilterActivity.CriterionInstance; import org.tasks.R; import org.tasks.dialogs.AlertDialogBuilder; import org.tasks.dialogs.DialogBuilder; +import org.tasks.locale.Locale; import java.util.List; @@ -38,11 +39,14 @@ public class CustomFilterAdapter extends ArrayAdapter { private final CustomFilterActivity activity; private DialogBuilder dialogBuilder; private final LayoutInflater inflater; + private final Locale locale; - public CustomFilterAdapter(CustomFilterActivity activity, DialogBuilder dialogBuilder, List objects) { + public CustomFilterAdapter(CustomFilterActivity activity, DialogBuilder dialogBuilder, + List objects, Locale locale) { super(activity, R.id.name, objects); this.activity = activity; this.dialogBuilder = dialogBuilder; + this.locale = locale; inflater = activity.getLayoutInflater(); } @@ -189,7 +193,7 @@ public class CustomFilterAdapter extends ArrayAdapter { } viewHolder.name.setText(title); - viewHolder.filterCount.setText(Integer.toString(item.end)); + viewHolder.filterCount.setText(locale.formatNumber(item.end)); } diff --git a/src/main/java/org/tasks/activities/FilterSelectionActivity.java b/src/main/java/org/tasks/activities/FilterSelectionActivity.java index 1ede673a9..e88cfc456 100644 --- a/src/main/java/org/tasks/activities/FilterSelectionActivity.java +++ b/src/main/java/org/tasks/activities/FilterSelectionActivity.java @@ -12,6 +12,7 @@ import org.tasks.filters.FilterCounter; import org.tasks.filters.FilterProvider; import org.tasks.injection.ActivityComponent; import org.tasks.injection.InjectingAppCompatActivity; +import org.tasks.locale.Locale; import org.tasks.themes.Theme; import org.tasks.themes.ThemeCache; @@ -30,6 +31,7 @@ public class FilterSelectionActivity extends InjectingAppCompatActivity { @Inject DialogBuilder dialogBuilder; @Inject Theme theme; @Inject ThemeCache themeCache; + @Inject Locale locale; @Override protected void onCreate(Bundle savedInstanceState) { @@ -38,7 +40,7 @@ public class FilterSelectionActivity extends InjectingAppCompatActivity { final boolean returnFilter = getIntent().getBooleanExtra(EXTRA_RETURN_FILTER, false); final FilterAdapter filterAdapter = new FilterAdapter(filterProvider, filterCounter, this, - false, theme, themeCache); + false, theme, themeCache, locale); filterAdapter.populateList(); dialogBuilder.newDialog() diff --git a/src/main/java/org/tasks/dialogs/SeekBarDialog.java b/src/main/java/org/tasks/dialogs/SeekBarDialog.java index 41b8f8601..ac9dc3068 100644 --- a/src/main/java/org/tasks/dialogs/SeekBarDialog.java +++ b/src/main/java/org/tasks/dialogs/SeekBarDialog.java @@ -6,12 +6,14 @@ import android.os.Bundle; import android.support.annotation.NonNull; import android.view.LayoutInflater; import android.view.View; +import android.widget.TextView; import com.rey.material.widget.Slider; import org.tasks.R; import org.tasks.injection.InjectingNativeDialogFragment; import org.tasks.injection.NativeDialogFragmentComponent; +import org.tasks.locale.Locale; import org.tasks.themes.Theme; import javax.inject.Inject; @@ -23,12 +25,16 @@ public class SeekBarDialog extends InjectingNativeDialogFragment { private static final String EXTRA_LAYOUT = "extra_layout"; private static final String EXTRA_INITIAL_VALUE = "extra_initial_value"; + private static final String EXTRA_MIN = "extra_min"; + private static final String EXTRA_MAX = "extra_max"; private static final String EXTRA_REQUEST_CODE = "extra_request_code"; - public static SeekBarDialog newSeekBarDialog(int layout, int initial, int requestCode) { + public static SeekBarDialog newSeekBarDialog(int layout, int min, int max, int initial, int requestCode) { SeekBarDialog dialog = new SeekBarDialog(); Bundle args = new Bundle(); args.putInt(EXTRA_LAYOUT, layout); + args.putInt(EXTRA_MIN, min); + args.putInt(EXTRA_MAX, max); dialog.setArguments(args); dialog.initial = initial; dialog.requestCode = requestCode; @@ -40,9 +46,12 @@ public class SeekBarDialog extends InjectingNativeDialogFragment { } @BindView(R.id.slider) Slider slider; + @BindView(R.id.min) TextView min; + @BindView(R.id.max) TextView max; @Inject DialogBuilder dialogBuilder; @Inject Theme theme; + @Inject Locale locale; private int initial; private int requestCode; @@ -63,6 +72,10 @@ public class SeekBarDialog extends InjectingNativeDialogFragment { ButterKnife.bind(this, view); slider.setValue(initial, true); + slider.setValueDescriptionProvider(value -> locale.formatNumber(value)); + slider.setValueRange(arguments.getInt(EXTRA_MIN), arguments.getInt(EXTRA_MAX), false); + min.setText(locale.formatNumber(slider.getMinValue())); + max.setText(locale.formatNumber(slider.getMaxValue())); return dialogBuilder.newDialog() .setView(view) .setPositiveButton(android.R.string.ok, (dialogInterface, i) -> callback.valueSelected(slider.getValue(), requestCode)) diff --git a/src/main/java/org/tasks/locale/Locale.java b/src/main/java/org/tasks/locale/Locale.java index 330aa56ce..e5a5e36f5 100644 --- a/src/main/java/org/tasks/locale/Locale.java +++ b/src/main/java/org/tasks/locale/Locale.java @@ -15,6 +15,8 @@ import com.google.common.base.Strings; import org.tasks.R; +import java.text.NumberFormat; + import static com.todoroo.andlib.utility.AndroidUtilities.atLeastJellybeanMR1; public class Locale { @@ -138,6 +140,14 @@ public class Locale { return locale.getDisplayName(locale); } + public String formatNumber(int number) { + return NumberFormat.getNumberInstance(appLocale).format(number); + } + + public String formatPercentage(int percentage) { + return NumberFormat.getPercentInstance(appLocale).format(percentage / 100.0); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/org/tasks/ui/NavigationDrawerFragment.java b/src/main/java/org/tasks/ui/NavigationDrawerFragment.java index 0a14247a1..4a5dfb831 100644 --- a/src/main/java/org/tasks/ui/NavigationDrawerFragment.java +++ b/src/main/java/org/tasks/ui/NavigationDrawerFragment.java @@ -27,6 +27,7 @@ import org.tasks.filters.FilterProvider; import org.tasks.filters.NavigationDrawerAction; import org.tasks.injection.FragmentComponent; import org.tasks.injection.InjectingFragment; +import org.tasks.locale.Locale; import org.tasks.preferences.AppearancePreferences; import org.tasks.themes.Theme; import org.tasks.themes.ThemeCache; @@ -64,6 +65,7 @@ public class NavigationDrawerFragment extends InjectingFragment { @Inject FilterProvider filterProvider; @Inject Theme theme; @Inject ThemeCache themeCache; + @Inject Locale locale; @Override public void onCreate(Bundle savedInstanceState) { @@ -133,7 +135,7 @@ public class NavigationDrawerFragment extends InjectingFragment { private void setUpList() { adapter = new FilterAdapter(filterProvider, filterCounter, getActivity(), - true, theme, themeCache); + true, theme, themeCache, locale); mDrawerListView.setAdapter(adapter); registerForContextMenu(mDrawerListView); } diff --git a/src/main/java/org/tasks/widget/WidgetConfigActivity.java b/src/main/java/org/tasks/widget/WidgetConfigActivity.java index 153f1e8ab..d18b43204 100644 --- a/src/main/java/org/tasks/widget/WidgetConfigActivity.java +++ b/src/main/java/org/tasks/widget/WidgetConfigActivity.java @@ -18,14 +18,13 @@ import org.tasks.dialogs.DialogBuilder; import org.tasks.dialogs.SeekBarDialog; import org.tasks.injection.ActivityComponent; import org.tasks.injection.InjectingPreferenceActivity; +import org.tasks.locale.Locale; import org.tasks.preferences.DefaultFilterProvider; import org.tasks.preferences.Preferences; import org.tasks.themes.ThemeCache; import org.tasks.themes.ThemeColor; import org.tasks.themes.WidgetTheme; -import java.text.NumberFormat; - import javax.inject.Inject; import static org.tasks.dialogs.SeekBarDialog.newSeekBarDialog; @@ -47,6 +46,7 @@ public class WidgetConfigActivity extends InjectingPreferenceActivity implements @Inject Preferences preferences; @Inject DefaultFilterProvider defaultFilterProvider; @Inject ThemeCache themeCache; + @Inject Locale locale; private WidgetPreferences widgetPreferences; private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; @@ -99,13 +99,13 @@ public class WidgetConfigActivity extends InjectingPreferenceActivity implements }); getPref(R.string.p_widget_opacity).setOnPreferenceClickListener(preference -> { - newSeekBarDialog(R.layout.dialog_opacity_seekbar, widgetPreferences.getOpacity(), REQUEST_OPACITY) + newSeekBarDialog(R.layout.dialog_opacity_seekbar, 0, 100, widgetPreferences.getOpacity(), REQUEST_OPACITY) .show(getFragmentManager(), FRAG_TAG_OPACITY_SEEKBAR); return false; }); getPref(R.string.p_widget_font_size).setOnPreferenceClickListener(preference -> { - newSeekBarDialog(R.layout.dialog_font_size_seekbar, widgetPreferences.getFontSize(), REQUEST_FONT_SIZE) + newSeekBarDialog(R.layout.dialog_font_size_seekbar, 10, 22, widgetPreferences.getFontSize(), REQUEST_FONT_SIZE) .show(getFragmentManager(), FRAG_TAG_FONT_SIZE_SEEKBAR); return false; }); @@ -127,12 +127,12 @@ public class WidgetConfigActivity extends InjectingPreferenceActivity implements private void updateOpacity() { int opacity = widgetPreferences.getOpacity(); - getPref(R.string.p_widget_opacity).setSummary(NumberFormat.getPercentInstance().format(opacity / 100.0)); + getPref(R.string.p_widget_opacity).setSummary(locale.formatPercentage(opacity)); } private void updateFontSize() { int fontSize = widgetPreferences.getFontSize(); - getPref(R.string.p_widget_font_size).setSummary(NumberFormat.getIntegerInstance().format(fontSize)); + getPref(R.string.p_widget_font_size).setSummary(locale.formatNumber(fontSize)); } private void updateFilter() { diff --git a/src/main/res/layout/dialog_font_size_seekbar.xml b/src/main/res/layout/dialog_font_size_seekbar.xml index 73ffebab7..e477455e9 100644 --- a/src/main/res/layout/dialog_font_size_seekbar.xml +++ b/src/main/res/layout/dialog_font_size_seekbar.xml @@ -15,12 +15,11 @@ android:paddingRight="@dimen/keyline_first" android:paddingStart="@dimen/keyline_first" app:sl_discreteMode="true" - app:sl_maxValue="22" - app:sl_minValue="10" app:sl_stepValue="2" app:sl_travelAnimDuration="100" /> + android:paddingTop="5dp" /> + android:paddingTop="5dp" /> diff --git a/src/main/res/layout/dialog_opacity_seekbar.xml b/src/main/res/layout/dialog_opacity_seekbar.xml index 0aa62e7c3..8ecfcc0ec 100644 --- a/src/main/res/layout/dialog_opacity_seekbar.xml +++ b/src/main/res/layout/dialog_opacity_seekbar.xml @@ -15,12 +15,11 @@ android:paddingRight="@dimen/keyline_first" android:paddingStart="@dimen/keyline_first" app:sl_discreteMode="true" - app:sl_maxValue="100" - app:sl_minValue="0" app:sl_stepValue="5" app:sl_travelAnimDuration="100" /> + android:paddingTop="5dp" /> + android:paddingTop="5dp" /> diff --git a/src/main/res/layout/filter_adapter_subheader.xml b/src/main/res/layout/filter_adapter_subheader.xml index cac10ce56..7ece55722 100644 --- a/src/main/res/layout/filter_adapter_subheader.xml +++ b/src/main/res/layout/filter_adapter_subheader.xml @@ -1,12 +1,14 @@ - - - \ No newline at end of file + \ No newline at end of file