Skip to content

Commit

Permalink
Example with custom key
Browse files Browse the repository at this point in the history
Signed-off-by: mramotar_dbx <mramotar@dropbox.com>
  • Loading branch information
matt-ramotar committed Feb 19, 2024
1 parent 8ee88bb commit e787b4c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,44 @@ class RealPagerTest {
assertEquals("11", state3.items[10].postId)
}
}

@Test
fun multipleCustomKeysWithReadsAndWrites() = testScope.runTest {
val api = FakePostApi()
val db = FakePostDatabase(userId)
val factory = PostStoreFactory(api = api, db = db)
val mutableStore = factory.create()

pager = Pager.create(this, mutableStore, joiner, keyFactory)

val key1 = PostKey.Custom("1", 10)
val key2 = PostKey.Custom("11", 10)

val stateFlow = pager.state
stateFlow.test {
pager.load(key1)
val initialState = awaitItem()
assertEquals(0, initialState.items.size)

val state1 = awaitItem()
assertEquals(10, state1.items.size)
assertEquals("1", state1.items[0].postId)

pager.load(key2)

val state2 = awaitItem()
assertEquals(20, state2.items.size)
assertEquals("1", state2.items[0].postId)
assertEquals("11", state2.items[10].postId)

mutableStore.write(StoreWriteRequest.of(PostKey.Single("2"), PostData.Post("2", "2-modified")))
advanceUntilIdle()

val state3 = awaitItem()
assertEquals(20, state3.items.size)
assertEquals("1", state3.items[0].postId)
assertEquals("2-modified", state3.items[1].title)
assertEquals("11", state3.items[10].postId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ sealed class PostKey : StoreKey<String> {
override val insertionStrategy: InsertionStrategy = InsertionStrategy.APPEND
) : StoreKey.Collection.Cursor<String>, PostKey()

data class Custom(
val page: String,
val size: Int,
val sort: StoreKey.Sort? = null,
override val insertionStrategy: InsertionStrategy = InsertionStrategy.APPEND
): StoreKey.Collection<String>, PostKey()

data class Single(
override val id: String
) : StoreKey.Single<String>, PostKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ class PostStoreFactory(private val api: PostApi, private val db: PostDatabase) {
}
}
}

is PostKey.Custom -> {
when (val result = api.get(key.page, key.size)) {
is FeedGetRequestResult.Data -> {
result.data
}

is FeedGetRequestResult.Error.Exception -> {
throw Throwable(result.error)
}

is FeedGetRequestResult.Error.Message -> {
throw Throwable(result.error)
}
}
}
}
}

Expand All @@ -63,6 +79,11 @@ class PostStoreFactory(private val api: PostApi, private val db: PostDatabase) {
val feed = db.findFeedByUserId(key.cursor, key.size)
emit(feed)
}

is PostKey.Custom -> {
val feed = db.findFeedByUserId(key.page, key.size)
emit(feed)
}
}
}
},
Expand All @@ -75,6 +96,10 @@ class PostStoreFactory(private val api: PostApi, private val db: PostDatabase) {
key is PostKey.Cursor && data is PostData.Feed -> {
db.add(data)
}

key is PostKey.Custom && data is PostData.Feed -> {
db.add(data)
}
}
}
)
Expand Down

0 comments on commit e787b4c

Please sign in to comment.