-
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.
> This is an idea I have to load the feed lazily given the current infrastructure. > > When we request a feed, we have a list of ID's, so we can populate our LazyList with a bunch of Loading State items. > > Whenever I Loading State item comes into the Composition, it launches a `SideEffect` which will call up to the ViewModel to fetch that item. This seems like a decent approach without having to explicitly manage a paging mechanism in the ViewModel. 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. Created 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
7 changed files
with
207 additions
and
78 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
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.