Skip to content

Commit

Permalink
#7 Use "b" letters in the pattern for "Number of days from today". If…
Browse files Browse the repository at this point in the history
… number of letters is 5 (e.g. bbbbb ) then Yesterday, Today and Tomorrow are used for these close days.
  • Loading branch information
yvolk committed Feb 13, 2020
1 parent e7115df commit be39309
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -63,4 +76,4 @@ public void setValue(DateFormatValue value) {
private void showValue() {
setSummary(getSummary());
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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, "");

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 : "");
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)";
}
Expand All @@ -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) {
Expand All @@ -87,12 +104,26 @@ 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) {
return e.getLocalizedMessage();
}
}

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) : "");
}

}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
<string name="date_format_day_in_month">Day in month</string>
<string name="date_format_month_day">Month-Day</string>
<string name="date_format_week_in_year">Week in year</string>
<string name="pattern_example">Pattern example</string>
<string name="custom_pattern">Custom pattern</string>
<string name="sample_date">Sample date (in yyyy-MM-dd format)</string>
<string name="result_formatted_date">Result, formatted date</string>
Expand Down

0 comments on commit be39309

Please sign in to comment.