Skip to content

Commit

Permalink
Rename KeyProvider methods
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-ramotar committed Dec 8, 2023
1 parent 4d56511 commit b362b10
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class StoreMultiCache<Id : Any, Key : StoreKey<Id>, Single : StoreData.Single<Id
val single = value as Single
accessor.putSingle(key.castSingle(), single)

val collectionKey = keyProvider.from(key.castSingle(), single)
val collectionKey = keyProvider.fromSingle(key.castSingle(), single)
val existingCollection = accessor.getCollection(collectionKey)
if (existingCollection != null) {
val updatedItems = existingCollection.items.toMutableList().map {
Expand All @@ -131,7 +131,7 @@ class StoreMultiCache<Id : Any, Key : StoreKey<Id>, Single : StoreData.Single<Id
collection.items.forEach {
val single = it as? Single
if (single != null) {
accessor.putSingle(keyProvider.from(key.castCollection(), single), single)
accessor.putSingle(keyProvider.fromCollection(key.castCollection(), single), single)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.mobilenativefoundation.store.core5

interface KeyProvider<Id : Any, Single : StoreData.Single<Id>> {
fun from(key: StoreKey.Collection<Id>, value: Single): StoreKey.Single<Id>
fun from(key: StoreKey.Single<Id>, value: Single): StoreKey.Collection<Id>
fun fromCollection(key: StoreKey.Collection<Id>, value: Single): StoreKey.Single<Id>
fun fromSingle(key: StoreKey.Single<Id>, value: Single): StoreKey.Collection<Id>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ package org.mobilenativefoundation.store.paging5.util

import kotlinx.coroutines.flow.flow
import org.mobilenativefoundation.store.cache5.Cache
import org.mobilenativefoundation.store.core5.KeyProvider
import org.mobilenativefoundation.store.cache5.StoreMultiCache
import org.mobilenativefoundation.store.core5.KeyProvider
import org.mobilenativefoundation.store.core5.StoreKey
import org.mobilenativefoundation.store.store5.*
import org.mobilenativefoundation.store.store5.Converter
import org.mobilenativefoundation.store.store5.ExperimentalStoreApi
import org.mobilenativefoundation.store.store5.Fetcher
import org.mobilenativefoundation.store.store5.MutableStore
import org.mobilenativefoundation.store.store5.SourceOfTruth
import org.mobilenativefoundation.store.store5.StoreBuilder
import org.mobilenativefoundation.store.store5.Updater
import org.mobilenativefoundation.store.store5.UpdaterResult
import kotlin.math.floor

class PostStoreFactory(private val api: PostApi, private val db: PostDatabase) {
Expand Down Expand Up @@ -102,11 +109,14 @@ class PostStoreFactory(private val api: PostApi, private val db: PostDatabase) {

private fun createPagingCacheKeyProvider(): KeyProvider<String, PostData.Post> =
object : KeyProvider<String, PostData.Post> {
override fun from(key: StoreKey.Collection<String>, value: PostData.Post): StoreKey.Single<String> {
override fun fromCollection(
key: StoreKey.Collection<String>,
value: PostData.Post
): StoreKey.Single<String> {
return PostKey.Single(value.postId)
}

override fun from(key: StoreKey.Single<String>, value: PostData.Post): StoreKey.Collection<String> {
override fun fromSingle(key: StoreKey.Single<String>, value: PostData.Post): StoreKey.Collection<String> {
val id = value.postId.toInt()
val cursor = (floor(id.toDouble() / 10) * 10) + 1
return PostKey.Cursor(cursor.toInt().toString(), 10)
Expand Down

0 comments on commit b362b10

Please sign in to comment.