Skip to content

Commit

Permalink
Added setting to change defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
maltaisn committed Jan 4, 2018
1 parent 7a77a29 commit c373bf8
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 101 deletions.
13 changes: 13 additions & 0 deletions app/src/main/java/com/maltaisn/recurpickerdemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.maltaisn.recurpicker.RecurrencePickerDialog;
import com.maltaisn.recurpicker.RecurrencePickerView;

import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -327,6 +328,17 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
showDoneBtnCheck.setChecked(RecurrencePickerView.DEFAULT_SHOW_DONE_IN_LIST);
showCancelBtnCheck.setChecked(RecurrencePickerView.DEFAULT_SHOW_CANCEL_BTN);

final Recurrence[] defaults = new Recurrence[]{
new Recurrence(0, Recurrence.DAILY).setFrequency(2),
new Recurrence(0, Recurrence.WEEKLY).setWeeklySetting(0b10000010),
new Recurrence(0, Recurrence.WEEKLY).setWeeklySetting(0b01111100),
};
final CharSequence[] titles = new CharSequence[]{
"Every 2 days",
"On weekends",
"On weekdays",
};

// Set up dialog recurrence picker
final RecurrencePickerDialog pickerDialog = RecurrencePickerDialog.newInstance();
pickerDialog.setDateFormat(dateFormatShort, dateFormatLong);
Expand All @@ -346,6 +358,7 @@ public void onClick(View view) {
.setShowHeaderInOptionList(showHeaderCheck.isChecked())
.setShowDoneButtonInOptionList(showDoneBtnCheck.isChecked())
.setShowCancelButton(showCancelBtnCheck.isChecked())
.setOptionListDefaults(defaults, titles)
.setRecurrence(recurrence, startDate.getTimeInMillis());

// Not necessary, but if a cancel button is shown, often dialog isn't cancelable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Recurrence(@NonNull Recurrence r) {

/**
* Create a recurrence from a byte array
* @param array byte array obtained with {@link Recurrence#}
* @param array byte array containing recurrence
* @param index recurrence object position in byte array
*/
public Recurrence(byte[] array, int index) {
Expand Down Expand Up @@ -209,8 +209,9 @@ public Recurrence setFrequency(int freq) {

/**
* If repeating weekly, sets the days of the week on which to repeat
* @param days bit field of Calendar.SUNDAY to Calendar.SATURDAY constants
* ex: {@code int days = Calendar.SUNDAY | Calendar.FRIDAY}
* @param days bit field of 1 << [Calendar.SUNDAY to Calendar.SATURDAY] constants
* ex: {@code int days = (1 << Calendar.SATURDAY) | (1 << Calendar.SUNDAY)}
* You can also use binary literals like this 0b10000010 (saturday and sunday)
* If setting 0 or 1 (no days), the recurrence will become Does not repeat
* If setting all days and frequency is 1, the recurrence becomes daily
* So make sure to set frequency before weekly settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class RecurrencePickerDialog extends DialogFragment implements Recurrence
private boolean showCancelBtn = RecurrencePickerView.DEFAULT_SHOW_CANCEL_BTN;
private int enabledPeriods = RecurrencePickerView.DEFAULT_ENABLED_PERIODS;
private int enabledEndTypes = RecurrencePickerView.DEFAULT_ENABLED_END_TYPES;
private Recurrence[] optionListDefaults = null;
private CharSequence[] optionListDefaultsTitle = null;

private static final int CREATOR_TRANSITION_DURATION = 200;

Expand Down Expand Up @@ -100,9 +102,8 @@ public void run() {
handler.postDelayed(runnable, CREATOR_TRANSITION_DURATION);
}
});
if (savedInstanceState == null) {
newPicker.setRecurrence(recurrence, startDate)
.setDateFormat(endDateFormat, optionListDateFormat)
if (savedInstanceState == null || picker == null) {
newPicker.setDateFormat(endDateFormat, optionListDateFormat)
.setMaxEventCount(maxEndCount)
.setMaxFrequency(maxFrequency)
.setMaxEndDate(maxEndDate)
Expand All @@ -113,7 +114,9 @@ public void run() {
.setShowHeaderInOptionList(showHeaderInList)
.setShowCancelButton(showCancelBtn)
.setEnabledPeriods(enabledPeriods)
.setEnabledEndTypes(enabledEndTypes);
.setEnabledEndTypes(enabledEndTypes)
.setOptionListDefaults(optionListDefaults, optionListDefaultsTitle)
.setRecurrence(recurrence, startDate);
newPicker.updateMode();
} else {
newPicker.onRestoreInstanceState(picker.onSaveInstanceState());
Expand Down Expand Up @@ -215,6 +218,13 @@ public RecurrencePickerSettings setEnabledEndTypes(int types) {
return this;
}

@Override
public RecurrencePickerSettings setOptionListDefaults(@Nullable Recurrence[] defaults, @Nullable CharSequence[] titles) {
optionListDefaults = defaults;
optionListDefaultsTitle = titles;
return this;
}

@Override
public void onCancel(DialogInterface dialog) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.maltaisn.recurpicker;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

Expand Down Expand Up @@ -79,7 +80,6 @@ public interface RecurrencePickerSettings {
/**
* Select which modes are enabled: default options list and recurrence creator
* By default, both modes are enabled, and at least one of them should be
* Must be set before setting the recurrence
* @param optionListEnabled whether to enable the default options list
* @param creatorEnabled whether to enabled the recurrence creator
*/
Expand All @@ -89,7 +89,6 @@ public interface RecurrencePickerSettings {
* Set whether to show the Done button in the first screen, the list of default recurrence options,
* or to hide it. If shown, listener won't be called when option is clicked, only when button is.
* By default, done button is not shown in option list.
* Must be set before setting the recurrence
* @param show Whether to show it or not
*/
RecurrencePickerSettings setShowDoneButtonInOptionList(boolean show);
Expand Down Expand Up @@ -132,4 +131,17 @@ public interface RecurrencePickerSettings {
*/
RecurrencePickerSettings setEnabledEndTypes(int types);

/**
* Change defaults in option list. Note that "Does not repeat" will always be shown.
* By default, a daily, weekly, monthly and yearly recurrences with frequency of 1 are used
* MUST be set before setting the recurrence if using the view by itself
* @param defaults Array of recurrence to use as defaults, leave null to use default defaults (exactly)
* or leave null to only change the titles
* You can set any start date for these recurrences because it gets changed
* @param titles Array of titles to use for each recurrence, leave null to use
* {@link Recurrence#format(Context, DateFormat)} instead. You can also leave
* specific items in array null to only format those.
*/
RecurrencePickerSettings setOptionListDefaults(@Nullable Recurrence[] defaults, @Nullable CharSequence[] titles);

}
Loading

0 comments on commit c373bf8

Please sign in to comment.