forked from plusonelabs/calendar-widget
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#7 Use "b" letters in the pattern for "Number of days from today". If…
… number of letters is 5 (e.g. bbbbb ) then Yesterday, Today and Tomorrow are used for these close days.
- Loading branch information
Showing
6 changed files
with
123 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -26,9 +42,6 @@ | |
import java.util.Date; | ||
import java.util.Locale; | ||
|
||
/** | ||
* @author [email protected] | ||
*/ | ||
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(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -10,9 +26,6 @@ | |
|
||
import org.andstatus.todoagenda.prefs.ApplicationPreferences; | ||
|
||
/** | ||
* @author [email protected] | ||
*/ | ||
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -11,19 +27,18 @@ | |
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** See https://github.com/andstatus/todoagenda/issues/7 | ||
* @author [email protected] | ||
* */ | ||
/** 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, ""); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -8,9 +24,6 @@ | |
|
||
import static org.andstatus.todoagenda.prefs.dateformat.DateFormatType.CUSTOM; | ||
|
||
/** | ||
* @author [email protected] | ||
*/ | ||
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 : ""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -13,10 +29,8 @@ | |
import java.util.Formatter; | ||
import java.util.Locale; | ||
|
||
/** | ||
* @author [email protected] | ||
*/ | ||
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,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) : ""); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters