diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/ExpiringLruCache.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/ExpiringLruCache.kt index 922711717..a72c1061d 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/ExpiringLruCache.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/cache/ExpiringLruCache.kt @@ -13,6 +13,7 @@ class ExpiringLruCache( private val cache = SieveCache>(maxSize) + @Synchronized operator fun get(key: CacheKey): T? { val value = cache[key] ?: return null if (value.isExpired) { @@ -22,21 +23,28 @@ class ExpiringLruCache( } operator fun set(key: CacheKey, value: T) { - cache.put(key, ExpiringValue(value, lifetime, timeUnit)) + val value = ExpiringValue(value, lifetime, timeUnit) + synchronized(this) { + cache.put(key, value) + } } + @Synchronized fun clear() { cache.evictAll() } + @Synchronized fun trimToSize(size: Int) { cache.trimToSize(size) } + @Synchronized fun remove(key: CacheKey) { cache.remove(key) } + @Synchronized fun removeAll(source: MangaSource) { cache.removeIf { key, _ -> key.source == source } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/favourites/ui/container/FavouritesContainerAdapter.kt b/app/src/main/kotlin/org/koitharu/kotatsu/favourites/ui/container/FavouritesContainerAdapter.kt index 8c2e0e4cd..1687e5ab1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/favourites/ui/container/FavouritesContainerAdapter.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/favourites/ui/container/FavouritesContainerAdapter.kt @@ -4,6 +4,7 @@ import androidx.fragment.app.Fragment import androidx.recyclerview.widget.AdapterListUpdateCallback import androidx.recyclerview.widget.AsyncDifferConfig import androidx.recyclerview.widget.AsyncListDiffer +import androidx.recyclerview.widget.RecyclerView import androidx.viewpager2.adapter.FragmentStateAdapter import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.asExecutor @@ -26,7 +27,7 @@ class FavouritesContainerAdapter(fragment: Fragment) : FragmentStateAdapter(frag override fun getItemCount(): Int = differ.currentList.size override fun getItemId(position: Int): Long { - return differ.currentList[position].id + return differ.currentList.getOrNull(position)?.id ?: RecyclerView.NO_ID } override fun containsItem(itemId: Long): Boolean {