@@ -2,14 +2,15 @@ package com.alamkanak.weekview
22
33import java.util.Calendar
44import java.util.concurrent.ConcurrentHashMap
5+ import java.util.concurrent.CopyOnWriteArrayList
56
67internal class EventChipsCache {
78
89 val allEventChips: List <EventChip >
910 get() = normalEventChipsByDate.values.flatten() + allDayEventChipsByDate.values.flatten()
1011
11- private val normalEventChipsByDate = ConcurrentHashMap <Long , MutableList <EventChip >>()
12- private val allDayEventChipsByDate = ConcurrentHashMap <Long , MutableList <EventChip >>()
12+ private val normalEventChipsByDate = ConcurrentHashMap <Long , CopyOnWriteArrayList <EventChip >>()
13+ private val allDayEventChipsByDate = ConcurrentHashMap <Long , CopyOnWriteArrayList <EventChip >>()
1314
1415 fun allEventChipsInDateRange (
1516 dateRange : List <Calendar >
@@ -62,11 +63,11 @@ internal class EventChipsCache {
6263 normalEventChipsByDate.clear()
6364 }
6465
65- private fun ConcurrentHashMap <Long , MutableList <EventChip >>.addOrReplace (
66+ private fun ConcurrentHashMap <Long , CopyOnWriteArrayList <EventChip >>.addOrReplace (
6667 key : Long ,
6768 eventChip : EventChip
6869 ) {
69- val results = getOrElse(key) { mutableListOf () }
70+ val results = getOrElse(key) { CopyOnWriteArrayList () }
7071 val indexOfExisting = results.indexOfFirst { it.event.id == eventChip.event.id }
7172 if (indexOfExisting != - 1 ) {
7273 // If an event with the same ID already exists, replace it. The new event will likely be
@@ -76,7 +77,6 @@ internal class EventChipsCache {
7677 } else {
7778 results.add(eventChip)
7879 }
79-
8080 this [key] = results
8181 }
8282}
0 commit comments