Skip to content

Commit

Permalink
Keep date state when changing locale.
Browse files Browse the repository at this point in the history
  • Loading branch information
henninghall committed May 19, 2018
1 parent 28184b9 commit 355518e
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import com.henninghall.date_picker.Utils;
import com.henninghall.date_picker.WheelChangeListener;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import cn.carbswang.android.numberpickerview.library.NumberPickerView;
Expand All @@ -34,7 +30,6 @@ void init() {

picker.setMinValue(0);
picker.setMaxValue(1);
picker.setValue(0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public DayWheel(NumberPickerView p, WheelChangeListener listener, Locale locale)

@Override
void init() {

int min = 0;
int max = 10000; // bug

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -max/2);

Expand All @@ -40,8 +40,6 @@ void init() {

picker.setMinValue(min);
picker.setMaxValue(max);
picker.setValue(5000);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public abstract class Wheel {
this.locale = locale;
this.picker = picker;

refresh();
refresh(false);

picker.setOnValueChangedListener(new NumberPickerView.OnValueChangeListener() {
@Override
Expand Down Expand Up @@ -62,18 +62,21 @@ public void setValue(Date date) {

public void setLocale(Locale locale) {
this.locale = locale;
refresh();
refresh(true);
}

private void refresh() {
private void refresh(boolean keepOldValue) {
this.displayFormat = new SimpleDateFormat(getFormatTemplate(), locale);
this.format = new SimpleDateFormat(getFormatTemplate(), LocaleUtils.toLocale("en_US"));
this.values = new ArrayList<>();
this.displayValues= new ArrayList<>();
int oldValue = picker.getValue();

if (visible()) {
add();
init();
if(keepOldValue) picker.setValue(oldValue);

}
else remove();

Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"moment": "^2.22.1",
"react": "16.3.1",
"react-native": "0.55.2",
"react-native-date-picker-x": "^1.0.5",
"react-native-date-picker-x": "^1.0.6",
"react-native-device-info": "^0.21.5",
"react-native-search-filter": "^0.1.3"
},
Expand Down
6 changes: 3 additions & 3 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4297,9 +4297,9 @@ react-is@^16.3.1:
version "16.3.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.3.1.tgz#ee66e6d8283224a83b3030e110056798488359ba"

react-native-date-picker-x@1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/react-native-date-picker-x/-/react-native-date-picker-x-1.0.4.tgz#290a48cc21d555bd704c42a8b52cc68876891609"
react-native-date-picker-x@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/react-native-date-picker-x/-/react-native-date-picker-x-1.0.6.tgz#4f2c8d0aed0ded1ab49786986a7ebff4e3df4ecf"

react-native-device-info@^0.21.5:
version "0.21.5"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-date-picker-x",
"version": "1.0.5",
"version": "1.0.6",
"description": "A Cross Platform React Native Picker",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 355518e

Please sign in to comment.