Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/weekend days #576

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.android.tools.build:gradle:3.3.2'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't update gradle in a feature PR, unless it's required for the functionality you are building.

This avoids merge conflicts if gradle gets upgraded in master before your PR lands


// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Oct 08 10:18:59 CEST 2018
#Mon Apr 01 17:09:18 EET 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.fragment.app.DialogFragment;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
Expand All @@ -53,9 +47,17 @@
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.DialogFragment;

/**
* Dialog allowing users to select a date.
*/
Expand Down Expand Up @@ -871,7 +873,19 @@ public void setDisabledDays(Calendar[] disabledDays) {
mDefaultLimiter.setDisabledDays(disabledDays);
if (mDayPickerView != null) mDayPickerView.onChange();
}
/**
* Sets a list of days id that are not selectable in the picker
* like Calendar.SATURDAY = 7
* Setting this value will take precedence over using setMinDate() and setMaxDate(), but stacks with setSelectableDays()
*
* @param weekendDays an Array of Calendar Objects containing the disabled dates
*/
public void setWeekendDays(@NonNull List<Integer> weekendDays) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the naming here. There's nothing about a weekend day which implies that it's disabled. Some people might have the exact opposite requirement.

Maybe setDisabledWeekDays?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use this in the signature? https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html (it has utility methods to convert to a Calendar int later on).

That might be a more intuitive signature than int.


mDefaultLimiter.setWeekendDays(weekendDays);
if (mDayPickerView != null) mDayPickerView.onChange();

}
/**
* @return an Array of Calendar objects containing the list of days that are not selectable. null if no restriction is set
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@

import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wdullaer.materialdatetimepicker.Utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.TimeZone;
import java.util.TreeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

class DefaultDateRangeLimiter implements DateRangeLimiter {
private static final int DEFAULT_START_YEAR = 1900;
private static final int DEFAULT_END_YEAR = 2100;
Expand All @@ -39,8 +42,10 @@ class DefaultDateRangeLimiter implements DateRangeLimiter {
private Calendar mMaxDate;
private TreeSet<Calendar> selectableDays = new TreeSet<>();
private HashSet<Calendar> disabledDays = new HashSet<>();
private List<Integer> weekendDays = new ArrayList<>();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a set makes more sense than a list?


DefaultDateRangeLimiter() {}
DefaultDateRangeLimiter() {
}

@SuppressWarnings({"unchecked", "WeakerAccess"})
public DefaultDateRangeLimiter(Parcel in) {
Expand All @@ -50,6 +55,7 @@ public DefaultDateRangeLimiter(Parcel in) {
mMaxDate = (Calendar) in.readSerializable();
selectableDays = (TreeSet<Calendar>) in.readSerializable();
disabledDays = (HashSet<Calendar>) in.readSerializable();
in.readList(weekendDays, null);
}

@Override
Expand All @@ -60,6 +66,7 @@ public void writeToParcel(Parcel out, int flags) {
out.writeSerializable(mMaxDate);
out.writeSerializable(selectableDays);
out.writeSerializable(disabledDays);
out.writeList(weekendDays);
}

@Override
Expand Down Expand Up @@ -91,6 +98,11 @@ void setDisabledDays(@NonNull Calendar[] days) {
}
}

void setWeekendDays(@NonNull List<Integer> weekendDays) {

this.weekendDays = weekendDays;
}

void setMinDate(@NonNull Calendar calendar) {
mMinDate = Utils.trimToMidnight((Calendar) calendar.clone());
}
Expand All @@ -112,22 +124,31 @@ void setYearRange(int startYear, int endYear) {
mMaxYear = endYear;
}

@Nullable Calendar getMinDate() {
@Nullable
Calendar getMinDate() {
return mMinDate;
}

@Nullable Calendar getMaxDate() {
@Nullable
Calendar getMaxDate() {
return mMaxDate;
}

@Nullable Calendar[] getSelectableDays() {
return selectableDays.isEmpty() ? null : selectableDays.toArray(new Calendar[0]);
@Nullable
Calendar[] getSelectableDays() {
return selectableDays.isEmpty() ? null : selectableDays.toArray(new Calendar[0]);
}

@Nullable Calendar[] getDisabledDays() {
@Nullable
Calendar[] getDisabledDays() {
return disabledDays.isEmpty() ? null : disabledDays.toArray(new Calendar[0]);
}

@Nullable
List<Integer> getWeekendDays() {
return weekendDays;
}

@Override
public int getMinYear() {
if (!selectableDays.isEmpty()) return selectableDays.first().get(Calendar.YEAR);
Expand All @@ -143,7 +164,8 @@ public int getMaxYear() {
}

@Override
public @NonNull Calendar getStartDate() {
public @NonNull
Calendar getStartDate() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to make sure the start date is not on a disabled weekday

if (!selectableDays.isEmpty()) return (Calendar) selectableDays.first().clone();
if (mMinDate != null) return (Calendar) mMinDate.clone();
TimeZone timeZone = mController == null ? TimeZone.getDefault() : mController.getTimeZone();
Expand All @@ -155,7 +177,8 @@ public int getMaxYear() {
}

@Override
public @NonNull Calendar getEndDate() {
public @NonNull
Calendar getEndDate() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to make sure the end date is not on a disabled weekday

if (!selectableDays.isEmpty()) return (Calendar) selectableDays.last().clone();
if (mMaxDate != null) return (Calendar) mMaxDate.clone();
TimeZone timeZone = mController == null ? TimeZone.getDefault() : mController.getTimeZone();
Expand Down Expand Up @@ -183,7 +206,13 @@ public boolean isOutOfRange(int year, int month, int day) {

private boolean isOutOfRange(@NonNull Calendar calendar) {
Utils.trimToMidnight(calendar);
return isDisabled(calendar) || !isSelectable(calendar);
return isDisabled(calendar) || !isSelectable(calendar) || isWeekend(calendar);
}


private boolean isWeekend(@NonNull Calendar calendar) {
int dayId = calendar.get(Calendar.DAY_OF_WEEK);
return weekendDays.contains(dayId);
}

private boolean isDisabled(@NonNull Calendar c) {
Expand All @@ -203,7 +232,8 @@ private boolean isAfterMax(@NonNull Calendar calendar) {
}

@Override
public @NonNull Calendar setToNearestDate(@NonNull Calendar calendar) {
public @NonNull
Calendar setToNearestDate(@NonNull Calendar calendar) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setToNearest will need to take the disabled weekdays into account (you don't want to return a date that is disabled)

if (!selectableDays.isEmpty()) {
Calendar newCalendar = null;
Calendar higher = selectableDays.ceiling(calendar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -14,7 +12,12 @@

import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;

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

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

/**
* A simple {@link Fragment} subclass.
Expand All @@ -32,6 +35,7 @@ public class DatePickerFragment extends Fragment implements DatePickerDialog.OnD
private CheckBox switchOrientation;
private CheckBox limitSelectableDays;
private CheckBox highlightDays;
private CheckBox weekendDays;
private DatePickerDialog dpd;

public DatePickerFragment() {
Expand All @@ -57,7 +61,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
switchOrientation = view.findViewById(R.id.switch_orientation);
limitSelectableDays = view.findViewById(R.id.limit_dates);
highlightDays = view.findViewById(R.id.highlight_dates);

weekendDays = view.findViewById(R.id.weekend_dates);
view.findViewById(R.id.original_button).setOnClickListener(v -> {
Calendar now = Calendar.getInstance();
new android.app.DatePickerDialog(
Expand Down Expand Up @@ -128,6 +132,13 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
dpd.setScrollOrientation(DatePickerDialog.ScrollOrientation.VERTICAL);
}
}

if (weekendDays.isChecked()) {
List<Integer> integers = new ArrayList<>();
integers.add(Calendar.SUNDAY);
integers.add(Calendar.MONDAY);
dpd.setWeekendDays(integers);
}
dpd.setOnCancelListener(dialog -> Log.d("DatePickerDialog", "Dialog was cancelled"));
dpd.show(requireFragmentManager(), "Datepickerdialog");
});
Expand All @@ -139,12 +150,12 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
public void onResume() {
super.onResume();
DatePickerDialog dpd = (DatePickerDialog) requireFragmentManager().findFragmentByTag("Datepickerdialog");
if(dpd != null) dpd.setOnDateSetListener(this);
if (dpd != null) dpd.setOnDateSetListener(this);
}

@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = "You picked the following date: "+dayOfMonth+"/"+(++monthOfYear)+"/"+year;
String date = "You picked the following date: " + dayOfMonth + "/" + (++monthOfYear) + "/" + year;
dateTextView.setText(date);
}
}
45 changes: 28 additions & 17 deletions sample/src/main/res/layout/datepicker_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove this?

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<!-- DatePicker Options -->
<TextView
android:id="@+id/date_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/selected_date"/>
android:text="@string/selected_date" />

<Button
android:id="@+id/date_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_date"/>
android:text="@string/pick_date" />

<Button
android:id="@+id/original_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/original_button"/>
android:text="@string/original_button" />

<CheckBox
android:id="@+id/show_version_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_version_2"/>
android:text="@string/show_version_2" />

<CheckBox
android:id="@+id/mode_dark_date"
Expand All @@ -48,8 +50,8 @@
android:id="@+id/vibrate_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/vibrate"
android:checked="true"/>
android:checked="true"
android:text="@string/vibrate" />

<CheckBox
android:id="@+id/switch_orientation"
Expand All @@ -61,36 +63,45 @@
android:id="@+id/dismiss_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dismiss"
android:checked="false"/>
android:checked="false"
android:text="@string/dismiss" />

<CheckBox
android:id="@+id/show_year_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_year_first"
android:checked="false"/>
android:checked="false"
android:text="@string/show_year_first" />

<CheckBox
android:id="@+id/title_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title"
android:checked="false"/>
android:checked="false"
android:text="@string/title" />

<CheckBox
android:id="@+id/limit_dates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/limit_dates"
android:checked="false"/>
android:checked="false"
android:text="@string/limit_dates" />

<CheckBox
android:id="@+id/highlight_dates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/highlight_dates"
android:checked="false"/>
android:checked="false"
android:text="@string/highlight_dates" />


<CheckBox

android:id="@+id/weekend_dates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/weekend_dates" />

</LinearLayout>
</androidx.core.widget.NestedScrollView>
Loading