Skip to content

Commit

Permalink
Fix issue where chips would sometimes disappear when scrolling
Browse files Browse the repository at this point in the history
Issue #119
  • Loading branch information
thellmund committed Oct 30, 2019
1 parent 6de831c commit 62123ef
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ internal fun Calendar.toEpochDays(): Int = (atStartOfDay.timeInMillis / DAY_IN_M
internal val Calendar.lengthOfMonth: Int
get() = getActualMaximum(Calendar.DAY_OF_MONTH)

internal fun newDate(year: Int, month: Int, dayOfMonth: Int): Calendar {
return today().apply {
set(Calendar.DAY_OF_MONTH, dayOfMonth)
set(Calendar.MONTH, month)
set(Calendar.YEAR, year)
}
}

internal fun Calendar.withTimeAtStartOfPeriod(hour: Int): Calendar {
return copy().apply {
set(Calendar.HOUR_OF_DAY, hour)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal fun <T> ConcurrentHashMap<Long, MutableList<EventChip<T>>>.addOrReplace
eventChip: EventChip<T>
) {
val results = getOrElse(key) { mutableListOf() }
val indexOfExisting = results.indexOfFirst { it.event == eventChip.event }
val indexOfExisting = results.indexOfFirst { it.event.id == eventChip.event.id }
if (indexOfExisting != -1) {
// If an event with the same ID already exists, replace it. The new event will likely be
// more up-to-date.
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/com/alamkanak/weekview/EventsLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ internal class PagedEventsLoader<T>(
) = fetchRange.periods.filter { it !in pagedCache }

private fun fetchPeriods(periods: List<Period>) {
periods.forEach { period ->
onLoadMoreListener.onLoadMore(period.startDate, period.endDate)
}
periods.forEach { onLoadMoreListener.onLoadMore(it.startDate, it.endDate) }
}
}

Expand Down
7 changes: 2 additions & 5 deletions core/src/main/java/com/alamkanak/weekview/Period.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ internal data class Period(
return Period(month, year)
}

val startDate: Calendar
get() = today().withYear(year).withMonth(month).withDayOfMonth(1)

val endDate: Calendar
get() = today().withYear(year).withMonth(month).withDayOfMonth(today().lengthOfMonth)
val startDate: Calendar = newDate(year, month, dayOfMonth = 1)
val endDate: Calendar = startDate.withDayOfMonth(startDate.lengthOfMonth)

override fun compareTo(other: Period): Int {
return when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,13 @@ class FakeEventsDatabase(private val context: Context) : EventsDatabase {
events.add(event)

startTime = Calendar.getInstance()
startTime.set(Calendar.DATE, 17)
startTime.set(Calendar.HOUR_OF_DAY, 11)
startTime.set(Calendar.MINUTE, 0)
startTime.set(Calendar.MONTH, newMonth)
startTime.set(Calendar.YEAR, newYear)
startTime.add(Calendar.DATE, 1)
endTime = startTime.clone() as Calendar
endTime.add(Calendar.HOUR_OF_DAY, 4)
endTime.set(Calendar.MONTH, newMonth)

event = createEvent(idOffset + 6, startTime, endTime, color3, isAllDay = false, isCanceled = false)
events.add(event)
Expand Down Expand Up @@ -134,6 +133,8 @@ class FakeEventsDatabase(private val context: Context) : EventsDatabase {
events.add(event)

startTime = Calendar.getInstance()
startTime.set(Calendar.YEAR, newYear)
startTime.set(Calendar.MONTH, newMonth)
startTime.set(Calendar.DAY_OF_MONTH, startTime.getActualMaximum(Calendar.DAY_OF_MONTH))
startTime.set(Calendar.HOUR_OF_DAY, 15)
startTime.set(Calendar.MINUTE, 0)
Expand All @@ -160,9 +161,9 @@ class FakeEventsDatabase(private val context: Context) : EventsDatabase {
event = createEvent(idOffset + 11, startTime, endTime, color2, isAllDay = true, isCanceled = false)
events.add(event)

// All-day event until 00:00 next days
// All-day event until 00:00 next day
startTime = Calendar.getInstance()
startTime.add(Calendar.DAY_OF_MONTH, 2)
startTime.set(Calendar.DAY_OF_MONTH, 14)
startTime.set(Calendar.HOUR_OF_DAY, 0)
startTime.set(Calendar.MINUTE, 0)
startTime.set(Calendar.SECOND, 0)
Expand All @@ -188,7 +189,7 @@ class FakeEventsDatabase(private val context: Context) : EventsDatabase {
) = Event(id, buildEventTitle(startTime), startTime, endTime, "Location $id", color, isAllDay, isCanceled)

private fun buildEventTitle(time: Calendar): String {
val sdf = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM)
val sdf = SimpleDateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT)
val formattedDate = sdf.format(time.time)
val hour = time.get(Calendar.HOUR_OF_DAY)
val minute = time.get(Calendar.MINUTE)
Expand Down

0 comments on commit 62123ef

Please sign in to comment.