Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DayView localization issue #576

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -6,6 +6,7 @@
import android.view.ViewGroup;

import com.prolificinteractive.materialcalendarview.MaterialCalendarView.ShowOtherDates;
import com.prolificinteractive.materialcalendarview.format.DateFormatDayFormatter;
import com.prolificinteractive.materialcalendarview.format.DayFormatter;
import com.prolificinteractive.materialcalendarview.format.TitleFormatter;
import com.prolificinteractive.materialcalendarview.format.WeekDayFormatter;
Expand Down Expand Up @@ -36,7 +37,7 @@ abstract class CalendarPagerAdapter<V extends CalendarPagerView> extends PagerAd
private DateRangeIndex rangeIndex;
private List<CalendarDay> selectedDates = new ArrayList<>();
private WeekDayFormatter weekDayFormatter = WeekDayFormatter.DEFAULT;
private DayFormatter dayFormatter = DayFormatter.DEFAULT;
private DayFormatter dayFormatter = DateFormatDayFormatter.getInstance();
private List<DayViewDecorator> decorators = new ArrayList<>();
private List<DecoratorResult> decoratorResults = null;
private boolean selectionEnabled = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.widget.CheckedTextView;

import com.prolificinteractive.materialcalendarview.MaterialCalendarView.ShowOtherDates;
import com.prolificinteractive.materialcalendarview.format.DateFormatDayFormatter;
import com.prolificinteractive.materialcalendarview.format.DayFormatter;

import java.util.List;
Expand All @@ -42,7 +43,7 @@ class DayView extends CheckedTextView {
private Drawable customBackground = null;
private Drawable selectionDrawable;
private Drawable mCircleDrawable;
private DayFormatter formatter = DayFormatter.DEFAULT;
private DayFormatter formatter = DateFormatDayFormatter.getInstance();

private boolean isInRange = true;
private boolean isInMonth = true;
Expand Down Expand Up @@ -77,7 +78,7 @@ public void setDay(CalendarDay date) {
* @param formatter new label formatter
*/
public void setDayFormatter(DayFormatter formatter) {
this.formatter = formatter == null ? DayFormatter.DEFAULT : formatter;
this.formatter = formatter == null ? DateFormatDayFormatter.getInstance(): formatter;
CharSequence currentLabel = getText();
Object[] spans = null;
if (currentLabel instanceof Spanned) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.widget.TextView;

import com.prolificinteractive.materialcalendarview.format.ArrayWeekDayFormatter;
import com.prolificinteractive.materialcalendarview.format.DateFormatDayFormatter;
import com.prolificinteractive.materialcalendarview.format.DateFormatTitleFormatter;
import com.prolificinteractive.materialcalendarview.format.DayFormatter;
import com.prolificinteractive.materialcalendarview.format.MonthArrayTitleFormatter;
Expand Down Expand Up @@ -939,7 +940,7 @@ public void setWeekDayFormatter(WeekDayFormatter formatter) {
* @param formatter the new formatter, null for default
*/
public void setDayFormatter(DayFormatter formatter) {
adapter.setDayFormatter(formatter == null ? DayFormatter.DEFAULT : formatter);
adapter.setDayFormatter(formatter == null ? DateFormatDayFormatter.getInstance() : formatter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,60 @@
*/
public class DateFormatDayFormatter implements DayFormatter {

private final DateFormat dateFormat;
private DateFormat dateFormat;
private Locale lastKnownLocal;

//create singleton instance from DateFormatDayFormatter to avoid creating redundant instance for each DayView.
private static DateFormatDayFormatter dateFormatDayFormatter;


public static DateFormatDayFormatter getInstance() {
if (dateFormatDayFormatter == null)
dateFormatDayFormatter = new DateFormatDayFormatter();
else {
//if user change local and resume the activity the calender now will draw DayView text with the new Local.
Locale currentLocal = Locale.getDefault();
if (!dateFormatDayFormatter.getLastKnownLocal().equals(currentLocal)) {
dateFormatDayFormatter.setLastKnownLocal(currentLocal);
dateFormatDayFormatter.setDateFormat(new SimpleDateFormat("d", currentLocal));
}
}
return dateFormatDayFormatter;
}

/**
* Format using a default format
*/
public DateFormatDayFormatter() {
private DateFormatDayFormatter() {
this.dateFormat = new SimpleDateFormat("d", Locale.getDefault());
this.lastKnownLocal = Locale.getDefault();
}

/**
* Format using a specific {@linkplain DateFormat}
*
* @param format the format to use
*/
public DateFormatDayFormatter(@NonNull DateFormat format) {
private DateFormatDayFormatter(@NonNull DateFormat format) {
this.dateFormat = format;
}

public Locale getLastKnownLocal() {
return lastKnownLocal;
}

public void setLastKnownLocal(Locale lastKnownLocal) {
this.lastKnownLocal = lastKnownLocal;
}

public DateFormat getDateFormat() {
return dateFormat;
}

public void setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ public interface DayFormatter {
/**
* Default implementation used by {@linkplain com.prolificinteractive.materialcalendarview.MaterialCalendarView}
*/
public static final DayFormatter DEFAULT = new DateFormatDayFormatter();
}