diff --git a/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatDialog.java b/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatDialog.java index fbe10a53..53f623c2 100644 --- a/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatDialog.java +++ b/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatDialog.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2019 yvolk (Yuri Volkov), http://yurivolkov.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.andstatus.todoagenda.prefs.dateformat; import android.content.Context; @@ -26,9 +42,6 @@ import java.util.Date; import java.util.Locale; -/** - * @author yvolk@yurivolkov.com - */ public class DateFormatDialog extends PreferenceDialogFragmentCompat implements AdapterView.OnItemSelectedListener, View.OnKeyListener, TextWatcher { private final DateFormatPreference preference; private Spinner typeSpinner; @@ -126,13 +139,17 @@ private DateFormatValue getValue() { } private void calcResult() { + DateFormatValue dateFormatValue = getValue(); SimpleDateFormat sampleFormat = getSampleDateFormat(); CharSequence result; try { + if (customPatternText.isEnabled() != (dateFormatValue.type == DateFormatType.CUSTOM)) { + customPatternText.setEnabled(dateFormatValue.type == DateFormatType.CUSTOM); + } Date sampleDate = sampleFormat.parse(sampleDateText.getText().toString()); result = sampleDate == null ? "null" - : new DateFormatter(this.getContext(), getValue(), getSettings().clock().now()) + : new DateFormatter(this.getContext(), dateFormatValue, getSettings().clock().now()) .formatMillis(sampleDate.getTime()); } catch (ParseException e) { result = e.getLocalizedMessage(); diff --git a/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatPreference.java b/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatPreference.java index d42d8ac3..0643164b 100644 --- a/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatPreference.java +++ b/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatPreference.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2019 yvolk (Yuri Volkov), http://yurivolkov.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.andstatus.todoagenda.prefs.dateformat; import android.content.Context; @@ -10,9 +26,6 @@ import org.andstatus.todoagenda.prefs.ApplicationPreferences; -/** - * @author yvolk@yurivolkov.com - */ public class DateFormatPreference extends DialogPreference { DateFormatValue defaultValue = DateFormatType.unknownValue(); DateFormatValue value = DateFormatType.unknownValue(); @@ -63,4 +76,4 @@ public void setValue(DateFormatValue value) { private void showValue() { setSummary(getSummary()); } -} +} \ No newline at end of file diff --git a/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatType.java b/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatType.java index 38e9a4ca..bc278db0 100644 --- a/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatType.java +++ b/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatType.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2019 yvolk (Yuri Volkov), http://yurivolkov.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.andstatus.todoagenda.prefs.dateformat; import android.content.Context; @@ -11,19 +27,18 @@ import java.util.ArrayList; import java.util.List; -/** See https://github.com/andstatus/todoagenda/issues/7 - * @author yvolk@yurivolkov.com - * */ +/** See https://github.com/andstatus/todoagenda/issues/7 */ public enum DateFormatType { HIDDEN("hidden", R.string.hidden, ""), DEVICE_DEFAULT("deviceDefault", R.string.device_default, ""), DEFAULT_WEEKDAY("defaultWeekday", R.string.date_format_default_weekday, ""), DEFAULT_DAYS("defaultDays", R.string.date_format_default_days, ""), ABBREVIATED("abbrev", R.string.appearance_abbreviate_dates_title, ""), - NUMBER_OF_DAYS("days", R.string.date_format_number_of_days_to_event, ""), + NUMBER_OF_DAYS("days", R.string.date_format_number_of_days_to_event, "bbbbb"), DAY_IN_MONTH("dayInMonth", R.string.date_format_day_in_month, "dd"), MONTH_DAY("monthDay", R.string.date_format_month_day, "MM-dd"), - WEEK_YEAR("weekInYear", R.string.date_format_week_in_year, "ww"), + WEEK_IN_YEAR("weekInYear", R.string.date_format_week_in_year, "ww"), + PATTERN_EXAMPLE1("example1", R.string.pattern_example, "b EEE, d MMM yyyy 'W'ww"), CUSTOM("custom-01", R.string.custom_pattern, ""), UNKNOWN("unknown", R.string.not_found, ""); diff --git a/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatValue.java b/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatValue.java index 939aa306..a7eee3d6 100644 --- a/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatValue.java +++ b/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatValue.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2019 yvolk (Yuri Volkov), http://yurivolkov.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.andstatus.todoagenda.prefs.dateformat; import android.content.Context; @@ -8,9 +24,6 @@ import static org.andstatus.todoagenda.prefs.dateformat.DateFormatType.CUSTOM; -/** - * @author yvolk@yurivolkov.com - */ public class DateFormatValue { public final DateFormatType type; public final String value; @@ -52,6 +65,6 @@ public String getPattern() { } public CharSequence getSummary(Context context) { - return context.getText(type.titleResourceId) + (type == CUSTOM ? ": \"" + value + "\"" : ""); + return context.getText(type.titleResourceId) + (type == CUSTOM ? ": " + value : ""); } } diff --git a/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatter.java b/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatter.java index a648b5d7..f88d2106 100644 --- a/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatter.java +++ b/app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatter.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2019 yvolk (Yuri Volkov), http://yurivolkov.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.andstatus.todoagenda.prefs.dateformat; import android.content.Context; @@ -13,10 +29,8 @@ import java.util.Formatter; import java.util.Locale; -/** - * @author yvolk@yurivolkov.com - */ public class DateFormatter { + private static final char NUMBER_OF_DAYS_LETTER = 'b'; private final Context context; private final DateFormatValue dateFormatValue; private final DateTime now; @@ -45,10 +59,10 @@ public CharSequence formatMillis(long millis) { return formatDateTime(millis, DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY); case DEFAULT_DAYS: - return getDaysFromTodayString(context, getDaysFromToday(millis)) + ", " + + return getDaysFromTodayString(context, 5, getDaysFromToday(millis)) + ", " + formatDateTime(millis, DateUtils.FORMAT_SHOW_DATE); case NUMBER_OF_DAYS: - return getDaysFromTodayString(context, getDaysFromToday(millis)); + return getDaysFromTodayString(context, 5, getDaysFromToday(millis)); default: return "(not implemented)"; } @@ -67,17 +81,20 @@ private String formatDateTime(long millis, int flags) { .toString(); } - public static CharSequence getDaysFromTodayString(Context context, int daysFromToday) { - switch (daysFromToday) { - case -1: - return context.getText(R.string.yesterday); - case 0: - return context.getText(R.string.today); - case 1: - return context.getText(R.string.tomorrow); - default: - return Math.abs(daysFromToday) > 9999 ? "..." : Integer.toString(daysFromToday); + public static CharSequence getDaysFromTodayString(Context context, int formatLength, int daysFromToday) { + if (formatLength > 4) { + switch (daysFromToday) { + case -1: + return context.getText(R.string.yesterday); + case 0: + return context.getText(R.string.today); + case 1: + return context.getText(R.string.tomorrow); + default: + break; + } } + return Math.abs(daysFromToday) > 9999 ? "..." : Integer.toString(daysFromToday); } public int getDaysFromToday(long millis) { @@ -87,7 +104,8 @@ public int getDaysFromToday(long millis) { private String formatDateCustom(long millis, String pattern) { try { - SimpleDateFormat format = new SimpleDateFormat(pattern, locale); + String pattern2 = preProcessNumberOfDaysToEvent(millis, pattern); + SimpleDateFormat format = new SimpleDateFormat(pattern2, locale); Date date = new Date(millis); return format.format(date); } catch (Exception e) { @@ -95,4 +113,17 @@ private String formatDateCustom(long millis, String pattern) { } } + private String preProcessNumberOfDaysToEvent(long millis, String pattern) { + int ind1 = pattern.indexOf(NUMBER_OF_DAYS_LETTER); + if (ind1 < 0) return pattern; + int ind2 = ind1; + while (ind2 < pattern.length() && pattern.charAt(ind2) == NUMBER_OF_DAYS_LETTER) { + ind2++; + } + CharSequence result = getDaysFromTodayString(context, ind2 - ind1, getDaysFromToday(millis)); + return (ind1 > 0 ? pattern.substring(0, ind1) : "") + + "'" + result + "'" + + (ind2 < pattern.length() ? pattern.substring(ind2) : ""); + } + } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b4f3f4d7..948eea61 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -107,6 +107,7 @@ Day in month Month-Day Week in year + Pattern example Custom pattern Sample date (in yyyy-MM-dd format) Result, formatted date