Skip to content

Commit

Permalink
#7 Replace "Show number of days to event" with initial implementation…
Browse files Browse the repository at this point in the history
… of "Entry date format" preference.
  • Loading branch information
yvolk committed Feb 9, 2020
1 parent a0fc71d commit ce4313d
Show file tree
Hide file tree
Showing 14 changed files with 323 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.andstatus.todoagenda.EndedSomeTimeAgo;
import org.andstatus.todoagenda.TextShading;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatValue;
import org.andstatus.todoagenda.widget.EventEntryLayout;
import org.andstatus.todoagenda.widget.WidgetHeaderLayout;

Expand All @@ -17,9 +18,9 @@
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ABBREVIATE_DATES;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ABBREVIATE_DATES_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ACTIVE_SOURCES;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_TIME_FORMAT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_TIME_FORMAT_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_DAY_HEADER_ALIGNMENT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ENTRY_DATE_FORMAT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ENTRY_DATE_FORMAT_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_EVENTS_BACKGROUND_COLOR;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_EVENTS_BACKGROUND_COLOR_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_EVENTS_ENDED;
Expand Down Expand Up @@ -59,6 +60,8 @@
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_TASK_SCHEDULING;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_TASK_WITHOUT_DATES;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_TEXT_SIZE_SCALE;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_TIME_FORMAT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_TIME_FORMAT_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_TODAYS_EVENTS_BACKGROUND_COLOR;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_TODAYS_EVENTS_BACKGROUND_COLOR_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_WIDGET_HEADER_BACKGROUND_COLOR;
Expand Down Expand Up @@ -95,7 +98,7 @@ public static void fromInstanceSettings(Context context, Integer widgetId) {
setShowPastEventsUnderOneHeader(context, settings.getShowPastEventsUnderOneHeader());
setShowPastEventsWithDefaultColor(context, settings.getShowPastEventsWithDefaultColor());
setShowEventIcon(context, settings.getShowEventIcon());
setShowNumberOfDaysToEvent(context, settings.getShowNumberOfDaysToEvent());
setEntryDateFormat(context, settings.getEntryDateFormat());
setBoolean(context, PREF_SHOW_END_TIME, settings.getShowEndTime());
setBoolean(context, PREF_SHOW_LOCATION, settings.getShowLocation());
setString(context, PREF_TIME_FORMAT, settings.getTimeFormat());
Expand Down Expand Up @@ -243,18 +246,10 @@ public static boolean getShowEventIcon(Context context) {
return getBoolean(context, PREF_SHOW_EVENT_ICON, false);
}

public static boolean getShowNumberOfDaysToEvent(Context context) {
return getBoolean(context, PREF_SHOW_NUMBER_OF_DAYS_TO_EVENT, false);
}

public static void setShowEventIcon(Context context, boolean value) {
setBoolean(context, PREF_SHOW_EVENT_ICON, value);
}

public static void setShowNumberOfDaysToEvent(Context context, boolean value) {
setBoolean(context, PREF_SHOW_NUMBER_OF_DAYS_TO_EVENT, value);
}

public static boolean getShowPastEventsWithDefaultColor(Context context) {
return getBoolean(context, PREF_SHOW_PAST_EVENTS_WITH_DEFAULT_COLOR, false);
}
Expand All @@ -271,6 +266,18 @@ public static boolean getShowLocation(Context context) {
return getBoolean(context, PREF_SHOW_LOCATION, PREF_SHOW_LOCATION_DEFAULT);
}

public static DateFormatValue getEntryDateFormat(Context context) {
return getDateFormat(context, PREF_ENTRY_DATE_FORMAT, PREF_ENTRY_DATE_FORMAT_DEFAULT);
}

public static void setEntryDateFormat(Context context, DateFormatValue value) {
setString(context, PREF_ENTRY_DATE_FORMAT, value.save());
}

public static DateFormatValue getDateFormat(Context context, String key, DateFormatValue defaultValue) {
return DateFormatValue.load(getString(context, key, ""), defaultValue);
}

public static String getTimeFormat(Context context) {
return getString(context, PREF_TIME_FORMAT, PREF_TIME_FORMAT_DEFAULT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.andstatus.todoagenda.EndedSomeTimeAgo;
import org.andstatus.todoagenda.TextShading;
import org.andstatus.todoagenda.TextSizeScale;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatType;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatValue;
import org.andstatus.todoagenda.provider.EventProviderType;
import org.andstatus.todoagenda.provider.QueryResultsStorage;
import org.andstatus.todoagenda.util.InstanceId;
Expand Down Expand Up @@ -70,8 +72,10 @@ public class InstanceSettings {
private EventEntryLayout eventEntryLayout = EventEntryLayout.DEFAULT;
static final String PREF_SHOW_EVENT_ICON = "showEventIcon";
private boolean showEventIcon = true;
static final String PREF_SHOW_NUMBER_OF_DAYS_TO_EVENT = "showNumberOfDaysToEvent";
private boolean showNumberOfDaysToEvent = true;
static final String PREF_ENTRY_DATE_FORMAT = "entryDateFormat";
static final DateFormatValue PREF_ENTRY_DATE_FORMAT_DEFAULT = DateFormatType.NUMBER_OF_DAYS.defaultValue();
private DateFormatValue entryDateFormat = PREF_ENTRY_DATE_FORMAT_DEFAULT;
static final String PREF_SHOW_NUMBER_OF_DAYS_TO_EVENT = "showNumberOfDaysToEvent"; // till v 4.0
static final String PREF_MULTILINE_TITLE = "multiline_title";
static final boolean PREF_MULTILINE_TITLE_DEFAULT = false;
private boolean multilineTitle = PREF_MULTILINE_TITLE_DEFAULT;
Expand Down Expand Up @@ -236,8 +240,15 @@ private InstanceSettings setFromJson(JSONObject json) {
if (json.has(PREF_SHOW_EVENT_ICON)) {
showEventIcon = json.getBoolean(PREF_SHOW_EVENT_ICON);
}
if (json.has(PREF_SHOW_NUMBER_OF_DAYS_TO_EVENT)) {
showNumberOfDaysToEvent = json.getBoolean(PREF_SHOW_NUMBER_OF_DAYS_TO_EVENT);
if (json.has(PREF_ENTRY_DATE_FORMAT)) {
entryDateFormat = DateFormatValue.load(
json.getString(PREF_ENTRY_DATE_FORMAT), PREF_ENTRY_DATE_FORMAT_DEFAULT);
} else if (json.has(PREF_SHOW_NUMBER_OF_DAYS_TO_EVENT)) {
entryDateFormat = (json.getBoolean(PREF_SHOW_NUMBER_OF_DAYS_TO_EVENT) &&
eventEntryLayout == EventEntryLayout.ONE_LINE
? DateFormatType.NUMBER_OF_DAYS
: DateFormatType.HIDDEN)
.defaultValue();
}
if (json.has(PREF_SHOW_END_TIME)) {
showEndTime = json.getBoolean(PREF_SHOW_END_TIME);
Expand Down Expand Up @@ -337,7 +348,7 @@ static InstanceSettings fromApplicationPreferences(Context context, int widgetId
settings.showPastEventsUnderOneHeader = ApplicationPreferences.getShowPastEventsUnderOneHeader(context);
settings.showPastEventsWithDefaultColor = ApplicationPreferences.getShowPastEventsWithDefaultColor(context);
settings.showEventIcon = ApplicationPreferences.getShowEventIcon(context);
settings.showNumberOfDaysToEvent = ApplicationPreferences.getShowNumberOfDaysToEvent(context);
settings.entryDateFormat = ApplicationPreferences.getEntryDateFormat(context);
settings.showEndTime = ApplicationPreferences.getShowEndTime(context);
settings.showLocation = ApplicationPreferences.getShowLocation(context);
settings.timeFormat = ApplicationPreferences.getTimeFormat(context);
Expand Down Expand Up @@ -427,7 +438,7 @@ public JSONObject toJson() {
json.put(PREF_SHOW_PAST_EVENTS_UNDER_ONE_HEADER, showPastEventsUnderOneHeader);
json.put(PREF_SHOW_PAST_EVENTS_WITH_DEFAULT_COLOR, showPastEventsWithDefaultColor);
json.put(PREF_SHOW_EVENT_ICON, showEventIcon);
json.put(PREF_SHOW_NUMBER_OF_DAYS_TO_EVENT, showNumberOfDaysToEvent);
json.put(PREF_ENTRY_DATE_FORMAT, entryDateFormat.save());
json.put(PREF_SHOW_END_TIME, showEndTime);
json.put(PREF_SHOW_LOCATION, showLocation);
json.put(PREF_TIME_FORMAT, timeFormat);
Expand Down Expand Up @@ -549,8 +560,8 @@ public boolean getShowPastEventsWithDefaultColor() {
return showPastEventsWithDefaultColor;
}

public boolean getShowNumberOfDaysToEvent() {
return showNumberOfDaysToEvent;
public DateFormatValue getEntryDateFormat() {
return entryDateFormat;
}

public boolean getShowEventIcon() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.DialogFragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

import org.andstatus.todoagenda.R;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatDialog;
import org.andstatus.todoagenda.prefs.dateformat.DateFormatPreference;

import static org.andstatus.todoagenda.WidgetConfigurationActivity.FRAGMENT_TAG;

public class LayoutPreferencesFragment extends PreferenceFragmentCompat
implements SharedPreferences.OnSharedPreferenceChangeListener {
Expand Down Expand Up @@ -38,6 +43,21 @@ private void showWidgetHeaderLayout() {
}
}

@Override
public void onDisplayPreferenceDialog(Preference preference) {
DialogFragment dialogFragment = null;
if (preference instanceof DateFormatPreference) {
dialogFragment = new DateFormatDialog((DateFormatPreference) preference);
}

if (dialogFragment != null) {
dialogFragment.setTargetFragment(this, 0);
dialogFragment.show(getFragmentManager(), FRAGMENT_TAG);
} else {
super.onDisplayPreferenceDialog(preference);
}
}

@Override
public void onPause() {
super.onPause();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.andstatus.todoagenda.prefs.dateformat;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

import androidx.preference.PreferenceDialogFragmentCompat;

import org.andstatus.todoagenda.R;

public class DateFormatDialog extends PreferenceDialogFragmentCompat {
private final DateFormatPreference preference;
private LinearLayout dialogView;
private Spinner typeSpinner;

public DateFormatDialog(DateFormatPreference preference) {
this.preference = preference;

final Bundle b = new Bundle();
b.putString(ARG_KEY, preference.getKey());
setArguments(b);
}

@Override
protected View onCreateDialogView(Context context) {
dialogView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.dateformat_preference, null);

typeSpinner = dialogView.findViewById(R.id.date_format_type);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(
context, android.R.layout.simple_spinner_item, DateFormatType.getSpinnerEntryList(context));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
typeSpinner.setAdapter(adapter);
typeSpinner.setSelection(preference.getValue().type.getSpinnerPosition());

return dialogView;
}

@Override
public void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
DateFormatValue value = getValue();
if (preference.callChangeListener(value)) {
preference.setValue(value);
}
}
}

private DateFormatValue getValue() {
int position = typeSpinner.getSelectedItemPosition();
return position >= 0 ? DateFormatType.values()[position].defaultValue() : DateFormatType.UNKNOWN.defaultValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.andstatus.todoagenda.prefs.dateformat;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;

import androidx.annotation.Nullable;
import androidx.preference.DialogPreference;

import org.andstatus.todoagenda.prefs.ApplicationPreferences;

public class DateFormatPreference extends DialogPreference {
DateFormatValue defaultValue = DateFormatType.unknownValue();
DateFormatValue value = DateFormatType.unknownValue();

public DateFormatPreference(Context context) {
super(context);
}

public DateFormatPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
if (a.peekValue(index) != null && a.peekValue(index).type == TypedValue.TYPE_STRING) {
return DateFormatValue.load(a.getString(index), DateFormatType.unknownValue());
}
return DateFormatType.unknownValue();
}

@Override
public void setDefaultValue(Object defaultValue) {
super.setDefaultValue(defaultValue);
this.defaultValue = DateFormatValue.loadOrUnknown(defaultValue);
}

@Override
protected void onSetInitialValue(@Nullable Object defaultValue) {
value = ApplicationPreferences.getDateFormat(getContext(), getKey(), this.defaultValue);
showValue();
}

public DateFormatValue getValue() {
return value.type == DateFormatType.UNKNOWN ? defaultValue : value;
}

@Override
public CharSequence getSummary() {
return value.getSummary(getContext());
}

public void setValue(DateFormatValue value) {
this.value = value;
ApplicationPreferences.setEntryDateFormat(getContext(), value);
showValue();
}

private void showValue() {
setSummary(getSummary());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.andstatus.todoagenda.prefs.dateformat;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;

import org.andstatus.todoagenda.R;
import org.andstatus.todoagenda.util.LazyVal;

import java.util.ArrayList;
import java.util.List;

/** See https://github.com/andstatus/todoagenda/issues/7
* */
public enum DateFormatType {
HIDDEN("hidden", R.string.hidden),
DEFAULT_DEVICE("device", R.string.device_default),
DEFAULT_ABBREVIATED("abbrev", R.string.appearance_abbreviate_dates_title),
NUMBER_OF_DAYS("days", R.string.date_format_number_of_days_to_event),
CUSTOM("custom-01", R.string.custom),
UNKNOWN("unknown", R.string.not_found);

public final String code;
@StringRes
public final int titleResourceId;

public final static DateFormatType DEFAULT = DEFAULT_DEVICE;

private final LazyVal<DateFormatValue> defaultValue = LazyVal.of( () ->
new DateFormatValue(DateFormatType.this, ""));

DateFormatType(String code, int titleResourceId) {
this.code = code;
this.titleResourceId = titleResourceId;
}

@NonNull
public static DateFormatValue load(String storedValue, @NonNull DateFormatValue defaultValue) {
DateFormatType formatType = DateFormatType.load(storedValue, UNKNOWN);
switch (formatType) {
case UNKNOWN:
return defaultValue;
case CUSTOM:
return new DateFormatValue(formatType, storedValue.substring(CUSTOM.code.length() + 1));
default:
return formatType.defaultValue();
}
}

@NonNull
private static DateFormatType load(String storedValue, @NonNull DateFormatType defaultType) {
if (storedValue == null) return defaultType;

for (DateFormatType type: values()) {
if (storedValue.startsWith( type.code + ":")) return type;
}
return defaultType;
}

public static DateFormatValue unknownValue() {
return UNKNOWN.defaultValue();
}

public static List<CharSequence> getSpinnerEntryList(Context context) {
List<CharSequence> list = new ArrayList<>();
for (DateFormatType type: values()) {
if (type == UNKNOWN) break;
list.add(context.getText(type.titleResourceId));
}
return list;
}

public DateFormatValue defaultValue() {
return defaultValue.get();
}

public int getSpinnerPosition() {
for (int position = 0; position < values().length; position++) {
DateFormatType type = values()[position];
if (type == UNKNOWN) break;
if (type == this) return position;
}
return 0;
}
}
Loading

0 comments on commit ce4313d

Please sign in to comment.