diff --git a/core/src/main/java/com/alamkanak/weekview/WeekViewConfig.kt b/core/src/main/java/com/alamkanak/weekview/WeekViewConfig.kt index ce284e274..ba2948973 100644 --- a/core/src/main/java/com/alamkanak/weekview/WeekViewConfig.kt +++ b/core/src/main/java/com/alamkanak/weekview/WeekViewConfig.kt @@ -1,10 +1,10 @@ package com.alamkanak.weekview import android.content.Context +import android.content.res.TypedArray import android.graphics.Color import android.graphics.Typeface import android.util.AttributeSet -import java.util.Calendar internal class WeekViewConfig( context: Context, @@ -12,7 +12,7 @@ internal class WeekViewConfig( ) { // Calendar configuration - var firstDayOfWeek: Int = 0 + var firstDayOfWeek: Int? = null var numberOfVisibleDays: Int = 0 var restoreNumberOfVisibleDays: Boolean = true var showFirstDayOfWeekFirst: Boolean = false @@ -116,115 +116,115 @@ internal class WeekViewConfig( init { val a = context.theme.obtainStyledAttributes(attrs, R.styleable.WeekView, 0, 0) - - try { + a.use { // Calendar configuration - firstDayOfWeek = a.getInteger(R.styleable.WeekView_firstDayOfWeek, Calendar.MONDAY) - numberOfVisibleDays = a.getInteger(R.styleable.WeekView_numberOfVisibleDays, 3) - restoreNumberOfVisibleDays = a.getBoolean(R.styleable.WeekView_restoreNumberOfVisibleDays, true) - showFirstDayOfWeekFirst = a.getBoolean(R.styleable.WeekView_showFirstDayOfWeekFirst, false) - showCurrentTimeFirst = a.getBoolean(R.styleable.WeekView_showCurrentTimeFirst, false) + if (a.hasValue(R.styleable.WeekView_firstDayOfWeek)) { + firstDayOfWeek = getInteger(R.styleable.WeekView_firstDayOfWeek, 0) + } + + numberOfVisibleDays = getInteger(R.styleable.WeekView_numberOfVisibleDays, 3) + restoreNumberOfVisibleDays = getBoolean(R.styleable.WeekView_restoreNumberOfVisibleDays, true) + showFirstDayOfWeekFirst = getBoolean(R.styleable.WeekView_showFirstDayOfWeekFirst, false) + showCurrentTimeFirst = getBoolean(R.styleable.WeekView_showCurrentTimeFirst, false) // Header bottom line - showHeaderRowBottomLine = a.getBoolean(R.styleable.WeekView_showHeaderRowBottomLine, false) - headerRowBottomLineColor = a.getColor(R.styleable.WeekView_headerRowBottomLineColor, Defaults.GRID_COLOR) - headerRowBottomLineWidth = a.getDimensionPixelSize(R.styleable.WeekView_headerRowBottomLineWidth, 1) + showHeaderRowBottomLine = getBoolean(R.styleable.WeekView_showHeaderRowBottomLine, false) + headerRowBottomLineColor = getColor(R.styleable.WeekView_headerRowBottomLineColor, Defaults.GRID_COLOR) + headerRowBottomLineWidth = getDimensionPixelSize(R.styleable.WeekView_headerRowBottomLineWidth, 1) // Time column - timeColumnTextColor = a.getColor(R.styleable.WeekView_timeColumnTextColor, Color.BLACK) - timeColumnBackgroundColor = a.getColor(R.styleable.WeekView_timeColumnBackgroundColor, Color.WHITE) - timeColumnPadding = a.getDimensionPixelSize(R.styleable.WeekView_timeColumnPadding, 10) - timeColumnTextSize = a.getDimensionPixelSize(R.styleable.WeekView_timeColumnTextSize, Defaults.textSize(context)) - showMidnightHour = a.getBoolean(R.styleable.WeekView_showMidnightHour, false) - showTimeColumnHourSeparator = a.getBoolean(R.styleable.WeekView_showTimeColumnHourSeparator, false) - timeColumnHoursInterval = a.getInteger(R.styleable.WeekView_timeColumnHoursInterval, 1) + timeColumnTextColor = getColor(R.styleable.WeekView_timeColumnTextColor, Color.BLACK) + timeColumnBackgroundColor = getColor(R.styleable.WeekView_timeColumnBackgroundColor, Color.WHITE) + timeColumnPadding = getDimensionPixelSize(R.styleable.WeekView_timeColumnPadding, 10) + timeColumnTextSize = getDimensionPixelSize(R.styleable.WeekView_timeColumnTextSize, Defaults.textSize(context)) + showMidnightHour = getBoolean(R.styleable.WeekView_showMidnightHour, false) + showTimeColumnHourSeparator = getBoolean(R.styleable.WeekView_showTimeColumnHourSeparator, false) + timeColumnHoursInterval = getInteger(R.styleable.WeekView_timeColumnHoursInterval, 1) // Time column separator - showTimeColumnSeparator = a.getBoolean(R.styleable.WeekView_showTimeColumnSeparator, false) - timeColumnSeparatorColor = a.getColor(R.styleable.WeekView_timeColumnSeparatorColor, Defaults.GRID_COLOR) - timeColumnSeparatorStrokeWidth = a.getDimensionPixelSize(R.styleable.WeekView_timeColumnSeparatorStrokeWidth, 1) + showTimeColumnSeparator = getBoolean(R.styleable.WeekView_showTimeColumnSeparator, false) + timeColumnSeparatorColor = getColor(R.styleable.WeekView_timeColumnSeparatorColor, Defaults.GRID_COLOR) + timeColumnSeparatorStrokeWidth = getDimensionPixelSize(R.styleable.WeekView_timeColumnSeparatorStrokeWidth, 1) // Time range - minHour = a.getInt(R.styleable.WeekView_minHour, 0) - maxHour = a.getInt(R.styleable.WeekView_maxHour, 24) + minHour = getInt(R.styleable.WeekView_minHour, 0) + maxHour = getInt(R.styleable.WeekView_maxHour, 24) // Header row - headerRowTextColor = a.getColor(R.styleable.WeekView_headerRowTextColor, Color.BLACK) - headerRowBackgroundColor = a.getColor(R.styleable.WeekView_headerRowBackgroundColor, Color.WHITE) - headerRowTextSize = a.getDimensionPixelSize(R.styleable.WeekView_headerRowTextSize, Defaults.textSize(context)) - headerRowPadding = a.getDimensionPixelSize(R.styleable.WeekView_headerRowPadding, 10) - todayHeaderTextColor = a.getColor(R.styleable.WeekView_todayHeaderTextColor, Defaults.HIGHLIGHT_COLOR) - singleLineHeader = a.getBoolean(R.styleable.WeekView_singleLineHeader, true) + headerRowTextColor = getColor(R.styleable.WeekView_headerRowTextColor, Color.BLACK) + headerRowBackgroundColor = getColor(R.styleable.WeekView_headerRowBackgroundColor, Color.WHITE) + headerRowTextSize = getDimensionPixelSize(R.styleable.WeekView_headerRowTextSize, Defaults.textSize(context)) + headerRowPadding = getDimensionPixelSize(R.styleable.WeekView_headerRowPadding, 10) + todayHeaderTextColor = getColor(R.styleable.WeekView_todayHeaderTextColor, Defaults.HIGHLIGHT_COLOR) + singleLineHeader = getBoolean(R.styleable.WeekView_singleLineHeader, true) // Event chips - eventCornerRadius = a.getDimensionPixelSize(R.styleable.WeekView_eventCornerRadius, 0) - eventTextSize = a.getDimensionPixelSize(R.styleable.WeekView_eventTextSize, Defaults.textSize(context)) - adaptiveEventTextSize = a.getBoolean(R.styleable.WeekView_adaptiveEventTextSize, false) - eventTextColor = a.getColor(R.styleable.WeekView_eventTextColor, Color.BLACK) - defaultEventColor = a.getColor(R.styleable.WeekView_defaultEventColor, Defaults.EVENT_COLOR) - allDayEventTextSize = a.getDimensionPixelSize(R.styleable.WeekView_allDayEventTextSize, eventTextSize) + eventCornerRadius = getDimensionPixelSize(R.styleable.WeekView_eventCornerRadius, 0) + eventTextSize = getDimensionPixelSize(R.styleable.WeekView_eventTextSize, Defaults.textSize(context)) + adaptiveEventTextSize = getBoolean(R.styleable.WeekView_adaptiveEventTextSize, false) + eventTextColor = getColor(R.styleable.WeekView_eventTextColor, Color.BLACK) + defaultEventColor = getColor(R.styleable.WeekView_defaultEventColor, Defaults.EVENT_COLOR) + allDayEventTextSize = getDimensionPixelSize(R.styleable.WeekView_allDayEventTextSize, eventTextSize) // Event padding - eventPaddingHorizontal = a.getDimensionPixelSize(R.styleable.WeekView_eventPaddingHorizontal, 8) - eventPaddingVertical = a.getDimensionPixelSize(R.styleable.WeekView_eventPaddingVertical, 8) + eventPaddingHorizontal = getDimensionPixelSize(R.styleable.WeekView_eventPaddingHorizontal, 8) + eventPaddingVertical = getDimensionPixelSize(R.styleable.WeekView_eventPaddingVertical, 8) // Event margins - columnGap = a.getDimensionPixelSize(R.styleable.WeekView_columnGap, 10) - overlappingEventGap = a.getDimensionPixelSize(R.styleable.WeekView_overlappingEventGap, 0) - eventMarginVertical = a.getDimensionPixelSize(R.styleable.WeekView_eventMarginVertical, 2) - eventMarginHorizontal = a.getDimensionPixelSize(R.styleable.WeekView_singleDayHorizontalMargin, 0) + columnGap = getDimensionPixelSize(R.styleable.WeekView_columnGap, 10) + overlappingEventGap = getDimensionPixelSize(R.styleable.WeekView_overlappingEventGap, 0) + eventMarginVertical = getDimensionPixelSize(R.styleable.WeekView_eventMarginVertical, 2) + eventMarginHorizontal = getDimensionPixelSize(R.styleable.WeekView_singleDayHorizontalMargin, 0) // Colors - dayBackgroundColor = a.getColor(R.styleable.WeekView_dayBackgroundColor, Defaults.BACKGROUND_COLOR) - todayBackgroundColor = a.getColor(R.styleable.WeekView_todayBackgroundColor, Defaults.BACKGROUND_COLOR) - showDistinctPastFutureColor = a.getBoolean(R.styleable.WeekView_showDistinctPastFutureColor, false) - showDistinctWeekendColor = a.getBoolean(R.styleable.WeekView_showDistinctWeekendColor, false) - pastBackgroundColor = a.getColor(R.styleable.WeekView_pastBackgroundColor, Defaults.PAST_BACKGROUND_COLOR) - futureBackgroundColor = a.getColor(R.styleable.WeekView_futureBackgroundColor, Defaults.FUTURE_BACKGROUND_COLOR) - pastWeekendBackgroundColor = a.getColor(R.styleable.WeekView_pastWeekendBackgroundColor, pastBackgroundColor) - futureWeekendBackgroundColor = a.getColor(R.styleable.WeekView_futureWeekendBackgroundColor, futureBackgroundColor) + dayBackgroundColor = getColor(R.styleable.WeekView_dayBackgroundColor, Defaults.BACKGROUND_COLOR) + todayBackgroundColor = getColor(R.styleable.WeekView_todayBackgroundColor, Defaults.BACKGROUND_COLOR) + showDistinctPastFutureColor = getBoolean(R.styleable.WeekView_showDistinctPastFutureColor, false) + showDistinctWeekendColor = getBoolean(R.styleable.WeekView_showDistinctWeekendColor, false) + pastBackgroundColor = getColor(R.styleable.WeekView_pastBackgroundColor, Defaults.PAST_BACKGROUND_COLOR) + futureBackgroundColor = getColor(R.styleable.WeekView_futureBackgroundColor, Defaults.FUTURE_BACKGROUND_COLOR) + pastWeekendBackgroundColor = getColor(R.styleable.WeekView_pastWeekendBackgroundColor, pastBackgroundColor) + futureWeekendBackgroundColor = getColor(R.styleable.WeekView_futureWeekendBackgroundColor, futureBackgroundColor) // Hour height - hourHeight = a.getDimensionPixelSize(R.styleable.WeekView_hourHeight, 50).toFloat() - minHourHeight = a.getDimensionPixelSize(R.styleable.WeekView_minHourHeight, 0) - maxHourHeight = a.getDimensionPixelSize(R.styleable.WeekView_maxHourHeight, 400) + hourHeight = getDimension(R.styleable.WeekView_hourHeight, 50f) + minHourHeight = getDimensionPixelSize(R.styleable.WeekView_minHourHeight, 0) + maxHourHeight = getDimensionPixelSize(R.styleable.WeekView_maxHourHeight, 400) effectiveMinHourHeight = minHourHeight - showCompleteDay = a.getBoolean(R.styleable.WeekView_showCompleteDay, false) + showCompleteDay = getBoolean(R.styleable.WeekView_showCompleteDay, false) // Now line - showNowLine = a.getBoolean(R.styleable.WeekView_showNowLine, false) - nowLineColor = a.getColor(R.styleable.WeekView_nowLineColor, Defaults.NOW_COLOR) - nowLineStrokeWidth = a.getDimensionPixelSize(R.styleable.WeekView_nowLineStrokeWidth, 5) + showNowLine = getBoolean(R.styleable.WeekView_showNowLine, false) + nowLineColor = getColor(R.styleable.WeekView_nowLineColor, Defaults.NOW_COLOR) + nowLineStrokeWidth = getDimensionPixelSize(R.styleable.WeekView_nowLineStrokeWidth, 5) // Now line dot - showNowLineDot = a.getBoolean(R.styleable.WeekView_showNowLineDot, false) - nowLineDotColor = a.getColor(R.styleable.WeekView_nowLineDotColor, Defaults.NOW_COLOR) - nowLineDotRadius = a.getDimensionPixelSize(R.styleable.WeekView_nowLineDotRadius, 16) + showNowLineDot = getBoolean(R.styleable.WeekView_showNowLineDot, false) + nowLineDotColor = getColor(R.styleable.WeekView_nowLineDotColor, Defaults.NOW_COLOR) + nowLineDotRadius = getDimensionPixelSize(R.styleable.WeekView_nowLineDotRadius, 16) // Hour separators - showHourSeparator = a.getBoolean(R.styleable.WeekView_showHourSeparator, true) - hourSeparatorColor = a.getColor(R.styleable.WeekView_hourSeparatorColor, Defaults.SEPARATOR_COLOR) - hourSeparatorStrokeWidth = a.getDimensionPixelSize(R.styleable.WeekView_hourSeparatorStrokeWidth, 2) + showHourSeparator = getBoolean(R.styleable.WeekView_showHourSeparator, true) + hourSeparatorColor = getColor(R.styleable.WeekView_hourSeparatorColor, Defaults.SEPARATOR_COLOR) + hourSeparatorStrokeWidth = getDimensionPixelSize(R.styleable.WeekView_hourSeparatorStrokeWidth, 2) // Day separators - showDaySeparator = a.getBoolean(R.styleable.WeekView_showDaySeparator, true) - daySeparatorColor = a.getColor(R.styleable.WeekView_daySeparatorColor, Defaults.SEPARATOR_COLOR) - daySeparatorStrokeWidth = a.getDimensionPixelSize(R.styleable.WeekView_daySeparatorStrokeWidth, 2) + showDaySeparator = getBoolean(R.styleable.WeekView_showDaySeparator, true) + daySeparatorColor = getColor(R.styleable.WeekView_daySeparatorColor, Defaults.SEPARATOR_COLOR) + daySeparatorStrokeWidth = getDimensionPixelSize(R.styleable.WeekView_daySeparatorStrokeWidth, 2) // Scrolling - xScrollingSpeed = a.getFloat(R.styleable.WeekView_xScrollingSpeed, 1f) - horizontalFlingEnabled = a.getBoolean(R.styleable.WeekView_horizontalFlingEnabled, true) - horizontalScrollingEnabled = a.getBoolean(R.styleable.WeekView_horizontalScrollingEnabled, true) - verticalFlingEnabled = a.getBoolean(R.styleable.WeekView_verticalFlingEnabled, true) - scrollDuration = a.getInt(R.styleable.WeekView_scrollDuration, 250) + xScrollingSpeed = getFloat(R.styleable.WeekView_xScrollingSpeed, 1f) + horizontalFlingEnabled = getBoolean(R.styleable.WeekView_horizontalFlingEnabled, true) + horizontalScrollingEnabled = getBoolean(R.styleable.WeekView_horizontalScrollingEnabled, true) + verticalFlingEnabled = getBoolean(R.styleable.WeekView_verticalFlingEnabled, true) + scrollDuration = getInt(R.styleable.WeekView_scrollDuration, 250) // Font - val fontFamily: String? = a.getString(R.styleable.WeekView_fontFamily) - val typeface = a.getInteger(R.styleable.WeekView_typeface, 0) - val textStyle = a.getInteger(R.styleable.WeekView_textStyle, 0) + val fontFamily = getString(R.styleable.WeekView_fontFamily) + val typeface = getInteger(R.styleable.WeekView_typeface, 0) + val textStyle = getInteger(R.styleable.WeekView_textStyle, 0) setTypefaceFromAttrs(fontFamily, typeface, textStyle) - } finally { - a.recycle() } } @@ -255,3 +255,11 @@ internal class WeekViewConfig( private const val MONOSPACE = 3 } } + +private fun TypedArray.use(block: TypedArray.() -> Unit) { + try { + block() + } finally { + recycle() + } +} diff --git a/core/src/main/java/com/alamkanak/weekview/WeekViewConfigWrapper.kt b/core/src/main/java/com/alamkanak/weekview/WeekViewConfigWrapper.kt index 987824ac8..0077b14b4 100644 --- a/core/src/main/java/com/alamkanak/weekview/WeekViewConfigWrapper.kt +++ b/core/src/main/java/com/alamkanak/weekview/WeekViewConfigWrapper.kt @@ -10,6 +10,8 @@ import java.util.Calendar import kotlin.math.max import kotlin.math.min +private const val DAYS_PER_WEEK = 7 + internal class WeekViewConfigWrapper( private val view: WeekView<*>, private val config: WeekViewConfig @@ -310,7 +312,7 @@ internal class WeekViewConfigWrapper( } var firstDayOfWeek: Int - get() = config.firstDayOfWeek + get() = config.firstDayOfWeek ?: now().firstDayOfWeek set(value) { config.firstDayOfWeek = value } @@ -589,7 +591,16 @@ internal class WeekViewConfigWrapper( private fun computeDifferenceWithFirstDayOfWeek( date: Calendar - ): Int = date.dayOfWeek - firstDayOfWeek + ): Int { + val firstDayOfWeek = firstDayOfWeek + return if (firstDayOfWeek == Calendar.MONDAY && date.dayOfWeek == Calendar.SUNDAY) { + // Special case, because Calendar.MONDAY has constant value 2 and Calendar.SUNDAY has + // constant value 1. The correct result to return is 6 days, not -1 days. + 6 + } else { + date.dayOfWeek - firstDayOfWeek + } + } private fun refreshAfterZooming() { if (showCompleteDay) { diff --git a/sample/src/main/res/layout/activity_static.xml b/sample/src/main/res/layout/activity_static.xml index da79daf3c..b6116daff 100644 --- a/sample/src/main/res/layout/activity_static.xml +++ b/sample/src/main/res/layout/activity_static.xml @@ -1,47 +1,47 @@ + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical"> + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="horizontal"> + android:id="@+id/previousWeekButton" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginTop="8dp" + android:layout_marginBottom="8dp" + android:layout_weight="1" + app:srcCompat="@drawable/ic_chevron_left_black_24dp" /> + android:id="@+id/dateRangeTextView" + android:layout_width="0dp" + android:layout_height="match_parent" + android:layout_marginStart="8dp" + android:layout_marginTop="8dp" + android:layout_marginEnd="8dp" + android:layout_marginBottom="8dp" + android:layout_weight="5" + android:gravity="center" + tools:text="01/01/2018" /> + android:id="@+id/nextWeekButton" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginTop="8dp" + android:layout_marginBottom="8dp" + android:layout_weight="1" + app:srcCompat="@drawable/ic_chevron_right_black_24dp" /> @@ -52,6 +52,7 @@ app:columnGap="1dp" app:eventCornerRadius="4dp" app:eventTextColor="@android:color/white" + app:firstDayOfWeek="monday" app:headerRowBottomLineColor="@color/header_bottom_line" app:headerRowBottomLineWidth="1dp" app:headerRowPadding="12dp" @@ -61,7 +62,6 @@ app:nowLineDotColor="@color/accent" app:nowLineDotRadius="5dp" app:nowLineStrokeWidth="1dp" - app:firstDayOfWeek="monday" app:numberOfVisibleDays="7" app:overlappingEventGap="1dp" app:showFirstDayOfWeekFirst="true"