Skip to content

Commit

Permalink
Custom width and height + Layout improvements (henninghall#82)
Browse files Browse the repository at this point in the history
* Custom height and width. Layout fixes. Organising example projects

* After merge fixes

* cleanup

* cleanup example

* cleanup
  • Loading branch information
henninghall authored Jul 12, 2019
1 parent 99191b0 commit e215538
Show file tree
Hide file tree
Showing 605 changed files with 8,935 additions and 6,567 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ demo
docs
example
example-cocoapods
examples
githubREADME.md
.gitignore
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ android {
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.facebook.react:react-native:+'
compile 'com.henninghall.android:NumberPickerView:1.0.1'
compile 'com.henninghall.android:NumberPickerView:1.1.0'
compile 'org.apache.commons:commons-lang3:3.6'
compile group: 'net.time4j', name: 'time4j-android', version: '4.2-2018i'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ public void setUtc(PickerView view, @Nullable boolean utc) throws Exception {
@ReactPropGroup(names = {"height", "width"}, customType = "Style")
public void setStyle(PickerView view, int index, Integer style) {
if(index == 0) view.style.setHeight(style);
if(index == 1) {
int width = (int) Utils.dpToPixels(style, DatePickerManager.context);
view.style.setWidth(width);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.henninghall.date_picker;

import java.util.HashMap;
import cn.carbswang.android.numberpickerview.library.NumberPickerView;

public class EmptyWheelUpdater {

private final PickerView pickerView;
private final HashMap<Integer, NumberPickerView> views;

private int[] ids = {
R.id.empty1,
R.id.empty2,
R.id.empty3
};

EmptyWheelUpdater(PickerView pickerView) {
this.pickerView = pickerView;
this.views = getViews();
}

private HashMap<Integer, NumberPickerView> getViews() {
HashMap<Integer, NumberPickerView> views = new HashMap<>();
for (int id: ids) {
NumberPickerView view = (NumberPickerView) pickerView.findViewById(id);
views.put(id, view);
}
return views;
}

void update(Mode mode) {
hideAll();
int numberOfVisibleWheels = pickerView.getVisibleWheels().size();
int emptyViewsToAdd = numberOfVisibleWheels - 1;
int numberOfPickerWheelsBeforeMode = getNumberOfPickerWheelsBeforeMode(mode);

for (int i = 0; i < emptyViewsToAdd; i++) {
int index = numberOfPickerWheelsBeforeMode + 1 + i * 2;
pickerView.wheelsWrapper.addView(views.get(ids[i]), index);
}
}

private int getNumberOfPickerWheelsBeforeMode(Mode mode) {
if(mode == Mode.date) return 1;
if(mode == Mode.datetime) return 4;
if(mode == Mode.time) return 5;
return 0;
}

private void hideAll() {
for (NumberPickerView view: views.values()) {
pickerView.wheelsWrapper.removeView(view);
}
}

}
1,542 changes: 1,542 additions & 0 deletions android/src/main/java/com/henninghall/date_picker/NumberPickerView.java

Large diffs are not rendered by default.

36 changes: 27 additions & 9 deletions android/src/main/java/com/henninghall/date_picker/PickerView.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package com.henninghall.date_picker;

import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.henninghall.date_picker.wheelFunctions.AnimateToDate;
import com.henninghall.date_picker.wheelFunctions.Refresh;
import com.henninghall.date_picker.wheelFunctions.SetDate;
import com.henninghall.date_picker.wheelFunctions.UpdateVisibility;
Expand All @@ -24,13 +18,11 @@
import com.henninghall.date_picker.wheels.Wheel;
import com.henninghall.date_picker.wheels.YearWheel;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
Expand All @@ -40,6 +32,7 @@

public class PickerView extends RelativeLayout {

public LinearLayout wheelsWrapper;
public SimpleDateFormat dateFormat;
private HourWheel hourWheel;
private DayWheel dayWheel;
Expand All @@ -53,6 +46,7 @@ public class PickerView extends RelativeLayout {
public MonthWheel monthWheel;
public YearWheel yearWheel;
private WheelOrderUpdater wheelOrderUpdater;
private EmptyWheelUpdater emptyWheelUpdater;
public boolean requireDisplayValueUpdate = true;
public TimeZone timeZone = TimeZone.getDefault();
private DateBoundary minDate;
Expand All @@ -61,11 +55,13 @@ public class PickerView extends RelativeLayout {

public PickerView() {
super(DatePickerManager.context);

View rootView = inflate(getContext(), R.layout.datepicker_view, this);
this.style = new Style(this);
this.wheelOrderUpdater = new WheelOrderUpdater(this);
this.emptyWheelUpdater = new EmptyWheelUpdater(this);

LinearLayout wheelsWrapper = (LinearLayout) rootView.findViewById(R.id.wheelsWrapper);
wheelsWrapper = (LinearLayout) rootView.findViewById(R.id.wheelsWrapper);
wheelsWrapper.setWillNotDraw(false);

locale = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? Locale.forLanguageTag("en") : Locale.getDefault();
Expand Down Expand Up @@ -106,6 +102,7 @@ public void onValueChangeInScrolling(NumberPickerView picker, int oldVal, int ne
});
}


public void setMinimumDate(String date) {
minDate = new DateBoundary(this, date);
requireDisplayValueUpdate = true;
Expand All @@ -126,6 +123,7 @@ public void setLocale(Locale locale) {
this.locale = locale;
setDateFormat();
wheelOrderUpdater.update(locale, mode);
emptyWheelUpdater.update(mode);
requireDisplayValueUpdate = true;
}

Expand Down Expand Up @@ -177,6 +175,7 @@ public void setMode(Mode mode) {
setDateFormat();
applyOnAllWheels(new UpdateVisibility());
wheelOrderUpdater.update(locale, mode);
emptyWheelUpdater.update(mode);
}

public Collection<Wheel> getVisibleWheels() {
Expand Down Expand Up @@ -228,13 +227,32 @@ public Calendar getMaximumDate(){
if (maxDate == null) return null;
return maxDate.get();
}

public void setDateFormat(){
dateFormat = new SimpleDateFormat(getDateFormatTemplate(), Locale.US);
}

public void update2DigitYearStart(Calendar selectedDate){
Calendar cal = (Calendar) selectedDate.clone();
cal.add(Calendar.YEAR, -50); // subtract 50 years to hit the middle of the century
dateFormat.set2DigitYearStart(cal.getTime());
}

public void setShownCountOnEmptyWheels(int shownCount) {
int[] ids = {
R.id.emptyStart,
R.id.empty1,
R.id.empty2,
R.id.empty3,
R.id.emptyEnd
};

for (int id : ids) {
NumberPickerView view = (NumberPickerView) findViewById(id);
if(view != null) view.setShownCount(shownCount);
}



}
}
8 changes: 1 addition & 7 deletions android/src/main/java/com/henninghall/date_picker/Style.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,11 @@ public void setTextColor(String color) {
this.pickerView.applyOnAllWheels(new TextColor(color));
}

public void setWidth(int width) {
View view = pickerView.findViewById(R.id.container);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = width;
view.setLayoutParams(layoutParams);
}

public void setHeight(int height) {
int showCount = height / DP_PER_SHOW_SHOW_COUNT;
int oddShowCount = showCount % 2 == 0 ? showCount + 1 : showCount;
pickerView.applyOnAllWheels(new SetShowCount(oddShowCount));
pickerView.setShownCountOnEmptyWheels(oddShowCount);
}

private boolean validColor(String color){
Expand Down
23 changes: 10 additions & 13 deletions android/src/main/java/com/henninghall/date_picker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.res.Resources;
import android.text.format.DateUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

Expand All @@ -13,6 +14,8 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

Expand All @@ -38,19 +41,13 @@ public static String dateToIso(Calendar date) {
return getIsoUTCFormat().format(date.getTime());
}

public static int getWheelHeight(View pickerView) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 160, pickerView.getResources().getDisplayMetrics());
}

public static String localeToYmdPattern(Locale locale) {
DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
String pattern = ((SimpleDateFormat)formatter).toLocalizedPattern();
pattern = pattern.replaceAll("\\[", "");
pattern = pattern.replaceAll("]", "");
pattern = pattern.replaceAll(" ", "");
pattern = pattern.replaceAll("[.]", "/");
pattern = pattern.replaceAll("-", "/");
return pattern;
public static boolean monthNameBeforeMonthDate(Locale locale){
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, 2);
String string = df.format(calendar.getTime());
return !string.startsWith("1") && !string.startsWith("01");
}

public static boolean isToday(Calendar cal){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,65 +1,38 @@
package com.henninghall.date_picker;

import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.henninghall.date_picker.wheels.Wheel;
import com.henninghall.date_picker.wheels.YearWheel;

import java.util.ArrayList;
import java.util.Locale;

public class WheelOrderUpdater
{
private final PickerView pickerView;
private String ymdPattern = "";

WheelOrderUpdater(final PickerView v) {
this.pickerView = v;
}

public void update(final Locale locale, final Mode mode) {
if (mode != Mode.date) return;
String lastYmdPattern = ymdPattern;
ymdPattern = Utils.localeToYmdPattern(locale);
if(lastYmdPattern.equals(ymdPattern)) return;

final ArrayList<Wheel> wheelOrder = this.ymdPatternToWheelOrder(ymdPattern);
wheelOrder.get(0).picker.setLayoutParams(getDefaultLayoutParams());
this.placeWheelRightOf(wheelOrder.get(0), wheelOrder.get(1));
this.placeWheelRightOf(wheelOrder.get(1), wheelOrder.get(2));
}

private void placeWheelRightOf(final Wheel leftWheel, final Wheel rightWheel) {
final RelativeLayout.LayoutParams params = getDefaultLayoutParams();
params.addRule(1, leftWheel.id);
if (Build.VERSION.SDK_INT >= 17) params.addRule(17, leftWheel.id);
rightWheel.picker.setLayoutParams(params);
final ArrayList<Wheel> wheelOrder = this.localeToWheelOrder(locale);
pickerView.wheelsWrapper.removeView(wheelOrder.get(0).picker);
pickerView.wheelsWrapper.removeView(wheelOrder.get(1).picker);
pickerView.wheelsWrapper.addView(wheelOrder.get(0).picker, 1); // 0 and 2 are emptyWheels
pickerView.wheelsWrapper.addView(wheelOrder.get(1).picker, 2); // 0 and 2 are emptyWheels
}

private RelativeLayout.LayoutParams getDefaultLayoutParams(){
return new RelativeLayout.LayoutParams(-2, Utils.getWheelHeight(this.pickerView));
}

private ArrayList<Wheel> ymdPatternToWheelOrder(final String ymdPattern) {
final String[] parts = ymdPattern.split("/");
private ArrayList<Wheel> localeToWheelOrder(final Locale locale) {
final ArrayList<Wheel> wheelList = new ArrayList<Wheel>();
for (final String s : parts) {
switch (s.charAt(0)) {
case 'y': {
wheelList.add(this.pickerView.yearWheel);
break;
}
case 'M': {
wheelList.add(this.pickerView.monthWheel);
break;
}
case 'd': {
wheelList.add(this.pickerView.dateWheel);
break;
}
}
if (Utils.monthNameBeforeMonthDate(locale)) {
wheelList.add(this.pickerView.dateWheel);
wheelList.add(this.pickerView.monthWheel);
}
else {
wheelList.add(this.pickerView.monthWheel);
wheelList.add(this.pickerView.dateWheel);
}
return wheelList;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.henninghall.date_picker;

public enum WheelPosition {
LEFT, RIGHT, MIDDLE
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.henninghall.date_picker.wheels;

import android.graphics.Paint;

import com.henninghall.date_picker.Mode;
import com.henninghall.date_picker.PickerView;
import com.henninghall.date_picker.Settings;
import com.henninghall.date_picker.Utils;
import com.henninghall.date_picker.WheelPosition;

import java.util.Calendar;
import cn.carbswang.android.numberpickerview.library.NumberPickerView;


public class AmPmWheel extends Wheel {
Expand Down Expand Up @@ -43,4 +45,9 @@ public String getFormatTemplate() {
return Settings.usesAmPm() ? " a " : "";
}

@Override
public Paint.Align getTextAlign() {
return Paint.Align.LEFT;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.henninghall.date_picker.wheels;

import android.graphics.Paint;

import java.util.*;
import com.henninghall.date_picker.*;

Expand Down Expand Up @@ -36,4 +38,9 @@ public String getFormatTemplate() {
return "d";
}

@Override
public Paint.Align getTextAlign() {
return Paint.Align.RIGHT;
}

}
Loading

0 comments on commit e215538

Please sign in to comment.