Skip to content

Commit

Permalink
Revert back to using builder pattern
Browse files Browse the repository at this point in the history
This is more extensible for the date library modules.
  • Loading branch information
thellmund committed Jan 21, 2022
1 parent f5235d9 commit 7dcbd05
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 162 deletions.
78 changes: 62 additions & 16 deletions core/src/main/java/com/alamkanak/weekview/WeekViewItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.util.Calendar
* The item is rendered based on the information provided in its [style] and [configuration]
* properties.
*/
data class WeekViewItem(
data class WeekViewItem internal constructor(
val id: Long = 0L,
val title: CharSequence,
val subtitle: CharSequence? = null,
Expand Down Expand Up @@ -100,6 +100,67 @@ data class WeekViewItem(
}
}

companion object {
fun of(data: Any): Builder = Builder(data)
}

/**
* Builder to construct a [WeekViewItem]. A [WeekViewItem] needs an [id], a [title], and
* a [duration]. The latter can be a [WeekViewItem.Duration.AllDay] or a
* [WeekViewItem.Duration.Bounded].
*/
class Builder internal constructor(private val data: Any) {

private var id: Long? = null
private var title: CharSequence? = null
private var subtitle: CharSequence? = null
private var duration: Duration? = null
private var style: Style = Style()
private var configuration = Configuration()

fun setId(id: Long): Builder {
this.id = id
return this
}

fun setTitle(title: CharSequence): Builder {
this.title = title
return this
}

fun setSubtitle(subtitle: CharSequence): Builder {
this.subtitle = subtitle
return this
}

fun setAllDayDuration(date: Calendar): Builder {
this.duration = Duration.AllDay(date)
return this
}

fun setBoundedDuration(startTime: Calendar, endTime: Calendar): Builder {
this.duration = Duration.Bounded(startTime, endTime)
return this
}

fun setStyle(style: Style): Builder {
this.style = style
return this
}

fun setConfiguration(configuration: Configuration): Builder {
this.configuration = configuration
return this
}

fun build(): WeekViewItem {
val id = requireNotNull(id) { "id == null" }
val title = requireNotNull(title) { "title == null" }
val duration = requireNotNull(duration) { "duration == null" }
return WeekViewItem(id, title, subtitle, duration, style, configuration, data)
}
}

internal val isAllDay: Boolean = duration is Duration.AllDay

internal val isNotAllDay: Boolean = !isAllDay
Expand Down Expand Up @@ -141,21 +202,6 @@ data class WeekViewItem(
internal fun collidesWith(other: WeekViewItem): Boolean = duration.overlapsWith(other.duration)
}

/**
* Creates an [WeekViewItem.Duration.AllDay] with the receiving [Calendar] as the date.
*/
fun Calendar.toAllDayDuration(): WeekViewItem.Duration.AllDay {
return WeekViewItem.Duration.AllDay(date = this.atStartOfDay)
}

/**
* Creates an [WeekViewItem.Duration.Bounded] with the receiving [Calendar] as the start time and
* the provided parameter as the end time.
*/
fun Calendar.toBoundedDurationUntil(endTime: Calendar): WeekViewItem.Duration.Bounded {
return WeekViewItem.Duration.Bounded(startTime = this, endTime = endTime)
}

private fun WeekViewItem.Duration.overlapsWith(other: WeekViewItem.Duration): Boolean {
if (this is WeekViewItem.Duration.AllDay || other is WeekViewItem.Duration.AllDay) {
return false
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.alamkanak.weekview.jodatime

import com.alamkanak.weekview.WeekViewItem
import org.joda.time.LocalDate
import org.joda.time.LocalDateTime

fun WeekViewItem.Builder.setAllDayDuration(date: LocalDate): WeekViewItem.Builder {
return setAllDayDuration(date.toCalendar())
}

fun WeekViewItem.Builder.setBoundedDuration(
startTime: LocalDateTime,
endTime: LocalDateTime,
): WeekViewItem.Builder {
return setBoundedDuration(startTime.toCalendar(), endTime.toCalendar())
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.alamkanak.weekview.jsr310

import com.alamkanak.weekview.WeekViewItem
import java.time.LocalDate
import java.time.LocalDateTime

fun WeekViewItem.Builder.setAllDayDuration(date: LocalDate): WeekViewItem.Builder {
return setAllDayDuration(date.toCalendar())
}

fun WeekViewItem.Builder.setBoundedDuration(
startTime: LocalDateTime,
endTime: LocalDateTime,
): WeekViewItem.Builder {
return setBoundedDuration(startTime.toCalendar(), endTime.toCalendar())
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EventsRepository(private val context: Context) {

fun fetch(
yearMonths: List<YearMonth>,
onSuccess: (List<CalendarItem>) -> Unit
onSuccess: (List<CalendarItem>) -> Unit,
) {
val handlerThread = HandlerThread("events-fetching")
handlerThread.start()
Expand All @@ -34,7 +34,7 @@ class EventsRepository(private val context: Context) {

val calendarEntities = yearMonths.flatMap { yearMonth ->
apiEntities.mapIndexedNotNull { index, apiResult ->
apiResult.toCalendarEntity(yearMonth, index)
apiResult.toCalendarItem(yearMonth, index)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.time.LocalTime
import java.time.YearMonth

interface ApiResult {
fun toCalendarEntity(yearMonth: YearMonth, index: Int): CalendarItem?
fun toCalendarItem(yearMonth: YearMonth, index: Int): CalendarItem?
}

data class ApiEvent(
Expand All @@ -18,16 +18,16 @@ data class ApiEvent(
@SerializedName("duration") val duration: Int,
@SerializedName("color") val color: String,
@SerializedName("is_canceled") val isCanceled: Boolean,
@SerializedName("is_all_day") val isAllDay: Boolean
@SerializedName("is_all_day") val isAllDay: Boolean,
) : ApiResult {

override fun toCalendarEntity(yearMonth: YearMonth, index: Int): CalendarItem? {
override fun toCalendarItem(yearMonth: YearMonth, index: Int): CalendarItem? {
return try {
val startTime = LocalTime.parse(startTime)
val startDateTime = yearMonth.atDay(dayOfMonth).atTime(startTime)
val endDateTime = startDateTime.plusMinutes(duration.toLong())
CalendarItem.Event(
id = "100${yearMonth.year}00${yearMonth.monthValue}00$index".toLong(),
id = generateId(yearMonth, index),
title = title,
location = location,
startTime = startDateTime,
Expand All @@ -45,16 +45,16 @@ data class ApiEvent(
data class ApiBlockedTime(
@SerializedName("day_of_month") val dayOfMonth: Int,
@SerializedName("start_time") val startTime: String,
@SerializedName("duration") val duration: Int
@SerializedName("duration") val duration: Int,
) : ApiResult {

override fun toCalendarEntity(yearMonth: YearMonth, index: Int): CalendarItem? {
override fun toCalendarItem(yearMonth: YearMonth, index: Int): CalendarItem? {
return try {
val startTime = LocalTime.parse(startTime)
val startDateTime = yearMonth.atDay(dayOfMonth).atTime(startTime)
val endDateTime = startDateTime.plusMinutes(duration.toLong())
CalendarItem.BlockedTimeSlot(
id = "200${yearMonth.year}00${yearMonth.monthValue}00$index".toLong(),
id = generateId(yearMonth, index),
startTime = startDateTime,
endTime = endDateTime
)
Expand All @@ -63,3 +63,10 @@ data class ApiBlockedTime(
}
}
}

private fun generateId(yearMonth: YearMonth, index: Int): Long {
val eventNumber = index.toString().padStart(length = 4, padChar = '0')
val year = yearMonth.year * 1_000_000
val month = yearMonth.monthValue * 1_000
return "$year$month$eventNumber".toLong()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import android.text.style.TypefaceSpan
import androidx.core.content.ContextCompat
import com.alamkanak.weekview.WeekViewEntity
import com.alamkanak.weekview.WeekViewItem
import com.alamkanak.weekview.jsr310.setAllDayDuration
import com.alamkanak.weekview.jsr310.setBoundedDuration
import com.alamkanak.weekview.jsr310.setEndTime
import com.alamkanak.weekview.jsr310.setStartTime
import com.alamkanak.weekview.jsr310.toAllDayDuration
import com.alamkanak.weekview.jsr310.toBoundedDurationUntil
import com.alamkanak.weekview.sample.R
import java.time.LocalDateTime

Expand Down Expand Up @@ -63,40 +63,46 @@ fun CalendarItem.Event.toWeekViewItem(context: Context): WeekViewItem {
}
}

val timing = if (isAllDay) {
startTime.toAllDayDuration()
} else {
startTime.toBoundedDurationUntil(endTime)
}

return WeekViewItem(
id = id,
title = title,
subtitle = subtitle,
duration = timing,
style = WeekViewItem.Style(
textColor = textColor,
backgroundColor = backgroundColor,
borderWidth = borderWidth,
borderColor = color,
),
configuration = WeekViewItem.Configuration.foreground(),
data = this,
val style = WeekViewItem.Style(
textColor = textColor,
backgroundColor = backgroundColor,
borderWidth = borderWidth,
borderColor = color,
)

val config = WeekViewItem.Configuration.foreground()

return WeekViewItem.of(this)
.setId(id)
.setTitle(title)
.setSubtitle(subtitle)
.apply {
if (isAllDay) {
setAllDayDuration(startTime.toLocalDate())
} else {
setBoundedDuration(startTime, endTime)
}
}
.setStyle(style)
.setConfiguration(config)
.build()
}

fun CalendarItem.BlockedTimeSlot.toWeekViewItem(context: Context): WeekViewItem {
return WeekViewItem(
id = id,
title = "Unavailable",
duration = startTime.toBoundedDurationUntil(endTime),
style = WeekViewItem.Style(
backgroundColor = ContextCompat.getColor(context, R.color.gray_alpha10),
cornerRadius = context.resources.getDimensionPixelSize(R.dimen.no_corner_radius),
),
configuration = WeekViewItem.Configuration.background(),
data = this,
val style = WeekViewItem.Style(
backgroundColor = ContextCompat.getColor(context, R.color.gray_alpha10),
cornerRadius = context.resources.getDimensionPixelSize(R.dimen.no_corner_radius),
)

val config = WeekViewItem.Configuration.background()

return WeekViewItem.of(this)
.setId(id)
.setTitle("Unavailable")
.setBoundedDuration(startTime, endTime)
.setStyle(style)
.setConfiguration(config)
.build()
}

fun CalendarItem.toWeekViewEntity(): WeekViewEntity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class BasicActivity : AppCompatActivity() {
}
}

private class BasicActivityWeekViewAdapter(
class BasicActivityWeekViewAdapter(
private val dragHandler: (Long, LocalDateTime, LocalDateTime) -> Unit,
private val loadMoreHandler: (List<YearMonth>) -> Unit
) : WeekViewPagingAdapterJsr310<CalendarItem>() {
Expand Down
Loading

0 comments on commit 7dcbd05

Please sign in to comment.