-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrating to a Simulated Paging Strategy
After second thought, I think the better approach is paging. And since we don't have actual paging via the API, we can simulate it. Just create a simple repository layer which wraps the actual base client, and adds the ability to request pages. The requests are still sequential, but that can be updated later to be a series of async requests. Some rationale here is that although with first approach we can keep scrolling, a fling will cause a bunch of network requests which could end up being quite costly. The only thing that is a bit funky is the LazyList detecting how close we are to the end logic. I think this solution works but it probably needs more testing.
- Loading branch information
Showing
6 changed files
with
178 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
android/app/src/main/java/com/emergetools/hackernews/data/ItemRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.emergetools.hackernews.data | ||
|
||
import com.emergetools.hackernews.features.stories.FeedType | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
typealias ItemId = Long | ||
typealias Page = List<ItemId> | ||
|
||
class ItemRepository( | ||
private val baseClient: HackerNewsBaseDataSource, | ||
) { | ||
suspend fun getFeedIds(type: FeedType): Page { | ||
return withContext(Dispatchers.IO) { | ||
when (type) { | ||
FeedType.Top -> { | ||
baseClient.api.getTopStoryIds() | ||
} | ||
FeedType.New -> { | ||
baseClient.api.getNewStoryIds() | ||
} | ||
} | ||
} | ||
} | ||
|
||
suspend fun getItem(id: ItemId): Item { | ||
return withContext(Dispatchers.IO) { | ||
baseClient.api.getItem(id) | ||
} | ||
} | ||
|
||
suspend fun getPage(page: Page): List<Item> { | ||
return withContext(Dispatchers.IO) { | ||
val result = mutableListOf<Item>() | ||
page.forEach { itemId -> | ||
val item = baseClient.api.getItem(itemId) | ||
result.add(item) | ||
} | ||
result.toList() | ||
} | ||
} | ||
} | ||
|
||
fun MutableList<Page>.next() = removeFirst() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.