aminography / PrimeDatePicker

PrimeDatePicker is a tool that provides picking a single day, multiple days, and a range of days.
Apache License 2.0
467 stars 50 forks source link

FirstDayOfWeek always Sunday #14

Closed squallsama closed 4 years ago

squallsama commented 4 years ago

Looks like it's not supporting right now changing firstDayOfWeek from Sunday to Monday. Even if Locale of user have firstDayOfWeek Monday

squallsama commented 4 years ago

com/aminography/primedatepicker/monthview/PrimeMonthView.kt

    weekStartDay = when (calendarType) {
        CalendarType.CIVIL -> Calendar.SUNDAY
        CalendarType.PERSIAN -> Calendar.SATURDAY
        CalendarType.HIJRI -> Calendar.SATURDAY
        CalendarType.JAPANESE -> Calendar.SUNDAY
    }

Where is MONDAY ?

aminography commented 4 years ago

Thanks for reporting the issue. I'll fix it soon.

alloqlag commented 4 years ago

Oh Yes a Monday start day would be great great great :) thx

nzoba commented 4 years ago

Hello. Still can't change weekStartDay to Monday. Any news ? Thanks

aminography commented 4 years ago

Hi; I've been developed it and it is available in the next release, soon. See this: https://github.com/aminography/PrimeDatePicker/tree/feature_theme#configurations-based-on-input-calendar

nzoba commented 4 years ago

Awesome, thanks ! Any date in mind for the next release ?

aminography commented 4 years ago

You're welcome. At most in the next 2 or 3 days.

aminography commented 4 years ago

Version 3.0.1 has been released. So you'll be able to change the first day of week. You have 2 alternatives to do this:

  1. Set the firstDayOfWeek to the initial date calendar:
    
    val calendar = CivilCalendar().also {
    it.firstDayOfWeek = Calendar.MONDAY  // sets first day of week to Monday
    }

val datePicker = PrimeDatePicker.bottomSheetWith(calendar) .pickSingleDay(singleDayPickCallback) ... .build()

2. Set it directly using builder pattern:
```kotlin
val datePicker = PrimeDatePicker.bottomSheetWith(calendar)
                    .pickSingleDay(singleDayPickCallback)
                    .firstDayOfWeek(Calendar.MONDAY)
                     ...
                    .build()
nzoba commented 4 years ago

Thanks for the release, first day's change works perfectly ! But I found a slight problem. My app crashes when I use .bottomSheetWith while it works perfectly with .dialogWith. Here is the error I get :

java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.widget.AppCompatImageView.setColorFilter(int)' on a null object reference
        at com.aminography.primedatepicker.picker.PrimeDatePickerImpl.onInitViews$library_release(PrimeDatePickerImpl.kt:112)
        at com.aminography.primedatepicker.picker.dialog.PrimeDatePickerBottomSheet.onInitViews(PrimeDatePickerBottomSheet.kt:34)
        at com.aminography.primedatepicker.picker.base.BaseBottomSheetDialogFragment.setupDialog(BaseBottomSheetDialogFragment.kt:31)
        at androidx.fragment.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:420)
        at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1484)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
        at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7811)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)

It seems to come from these lines in PrimeDatePickerImpl :

 with(rootView) {
            themeFactory.let {
                cardBackgroundImageView.setColorFilter(it.dialogBackgroundColor)
                circularRevealFrameLayout.setBackgroundColor(it.gotoViewBackgroundColor)
            }
aminography commented 4 years ago

@Nzolo0: Please clean your project then upgrade the library to version 3.0.2. I have tested it, there should not be a problem with this.

nzoba commented 4 years ago

I did that but I still have an error at the same line in PrimeDatePickerImpl :

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageTintList(android.content.res.ColorStateList)' on a null object reference
        at androidx.core.widget.ImageViewCompat.setImageTintList(ImageViewCompat.java:51)
        at com.aminography.primedatepicker.picker.PrimeDatePickerImpl.onInitViews$library_release(PrimeDatePickerImpl.kt:112)

Well it's possible that the problem comes from my project. It's not a big deal, I wanted to use the dialog version anyway and this one works perfectly for me. Thanks for your work !

aminography commented 4 years ago

It's so weird!:expressionless: I'm digging it more. Thanks for your reporting issues. You're welcome!

alloqlag commented 4 years ago

Hi @aminography . Thx for you work and fixes.

I'm not sure but i did not find anything to change de firstDayOfWeek in standalone mode (i don't use it as PrimeDatePicker). A solution please ?

aminography commented 4 years ago

Hi @alloqlag; You're welcome buddy. You can simply call calendarView.firstDayOfWeek = Calendar.MONDAY. Another way is to set the desired firstDayOfWeek to the calendar instance passing to the goto(calendar: PrimeCalendar) function:

val calendar = CivilCalendar()
calendar.firstDayOfWeek = Calendar.MONDAY
calendarView.goto(calendar)
EtiennePriou commented 4 years ago

Hi @aminography I try to change the first day of week with your method

val calendar = CivilCalendar()
calendar.firstDayOfWeek = Calendar.MONDAY
calendarView.goto(calendar)

here part of my code :

        val dayCalendar = CivilCalendar(Locale.FRANCE).also {
            it.setTime(Date())
            it.firstDayOfWeek = Calendar.MONDAY     //<---  Here Monday
        }

        b.calendarView.calendarType = CalendarType.CIVIL
        b.calendarView.goto(calendar = dayCalendar, animate = true)
        b.calendarView.flingOrientation = PrimeCalendarView.FlingOrientation.HORIZONTAL
        b.calendarView.pickType = PickType.SINGLE
        b.calendarView.onDayPickedListener = object : OnDayPickedListener {
            override fun onDayPicked(pickType: PickType, singleDay: PrimeCalendar?, startDay: PrimeCalendar?, endDay: PrimeCalendar?, multipleDays: List<PrimeCalendar>?) {
                if (singleDay != null) {
                    when (b.dateSwitcher.selectedPosition) {
                        DateSwitcher.START_DATE.ordinal -> {
                            viewModel.startDate.value = singleDay.getTime()
                            viewModel.endDate.value = null
                        }
                        DateSwitcher.END_DATE.ordinal -> {
                            viewModel.endDate.value = singleDay.getTime()
                            viewModel.startDate.value = null
                        }
                    }
                }
            }
        }
        b.calendarView.minDateCalendar = CivilCalendar(Locale.FRANCE).apply { setTime(Date()) }
        b.calendarView.maxDateCalendar = CivilCalendar(Locale.FRANCE).apply {
            setTime(params.searchPublicationDate?.let { it } ?: run { Date() })
            add(field = Calendar.YEAR, amount = 1)
        }

        if (viewModel.startDate.value != null || viewModel.endDate.value != null) {
            b.dateSwitcher.setSelection(if (viewModel.startDate.value != null) DateSwitcher.START_DATE.ordinal else DateSwitcher.END_DATE.ordinal)
            b.calendarView.pickedSingleDayCalendar = CivilCalendar(Locale.FRANCE).also {
                viewModel.startDate.value?.let { date ->
                    it.setTime(date)
                    it.firstDayOfWeek = Calendar.MONDAY   //<---  tried here aswell
                    b.calendarView.goto(calendar = it, animate = true)
                }
                viewModel.endDate.value?.let { date ->
                    it.setTime(date)
                    b.calendarView.goto(calendar = it, animate = true)
                }
            }
        }

my xml layout :

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/gu_2"
                android:text="@string/wished_date"
                android:textAppearance="@style/ChocoStarfishTheme.Text.H1"
                android:textSize="20sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageButton
                android:id="@+id/close"
                android:layout_width="@dimen/gu_5"
                android:layout_height="@dimen/gu_5"
                android:layout_gravity="right|center_vertical"
                android:layout_marginRight="@dimen/gu_2"
                android:background="@drawable/circle_guyabano"
                android:padding="@dimen/gu_1"
                app:srcCompat="@drawable/ic_action_close"
                android:tint="@color/purple_taupe"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <View
            style="@style/HorizontalDivider"
            android:layout_marginTop="@dimen/gu_1"
            android:layout_marginBottom="@dimen/gu_1" />

        <ziapps.ilokyou.view.ChocoSwitcherMedium
            android:id="@+id/date_switcher"
            android:layout_width="300dp"
            android:layout_height="@dimen/gu_6"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/gu_1"
            app:csm_background="@drawable/background_chocoswitcher_medium_guyabano"
            app:csm_option1title="Pour le"
            app:csm_option2title="Au plus tard le"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.aminography.primedatepicker.calendarview.PrimeCalendarView
            android:id="@+id/calendarView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:calendarType="civil"
            app:dayLabelTextColor="@color/text_color_primary"
            app:dayLabelVerticalPadding="@dimen/gu_1_5"
            app:disabledDayLabelTextColor="@color/av_very_light_text_color"
            app:flingOrientation="horizontal"
            app:monthLabelTextColor="@color/text_color_primary"
            app:pickedDayCircleColor="@color/green"
            android:layout_marginBottom="@dimen/gu_2"
            app:todayLabelTextColor="@color/pink"
            app:weekLabelTextColor="@color/black_text" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:layout_marginBottom="@dimen/gu_1_5"
            android:orientation="vertical">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/validateButton"
                style="@style/MaterialButtonGreen"
                android:layout_width="@dimen/gu_19"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/validate" />

            <ziapps.ilokyou.view.ChocoButton
                android:id="@+id/deleteDateButton"
                style="@style/Button.Borderless.Stone"
                android:layout_width="@dimen/gu_19"
                android:layout_height="@dimen/gu_6"
                android:layout_gravity="center"
                android:text="@string/delete_needed_date"
                android:textColor="@color/pink_dark" />

        </LinearLayout>
    </LinearLayout>
</layout>

any idea ?

Thanks a Lot

aminography commented 4 years ago

@EtiennePriou: Hi; The problem comes from calling

b.calendarView.goto(calendar = dayCalendar, animate = true)
b.calendarView.flingOrientation = PrimeCalendarView.FlingOrientation.HORIZONTAL

sequentially. In meanwhile, As you are setting app:flingOrientation="horizontal" in xml layout, there is no need to set it programmatically. By the way, I'm solving the problem.