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 Replace "Show number of days to event" with initial implementation…
… of "Entry date format" preference.
- Loading branch information
Showing
14 changed files
with
323 additions
and
30 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
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
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
56 changes: 56 additions & 0 deletions
56
app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatDialog.java
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 |
---|---|---|
@@ -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(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatPreference.java
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 |
---|---|---|
@@ -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()); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
app/src/main/java/org/andstatus/todoagenda/prefs/dateformat/DateFormatType.java
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.