This file contains hidden or 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
@OptIn(ExperimentalPagingApi::class) | |
class GithubRemoteMediator( | |
… | |
) : RemoteMediator<Int, Repo>() { | |
… | |
} |
This file contains hidden or 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
fun getSearchResultStream(query: String): Flow<PagingData<Repo>> { | |
… | |
val pagingSourceFactory = { database.reposDao().reposByName(dbQuery) } | |
@OptIn(ExperimentalPagingApi::class) | |
return Pager( | |
config = PagingConfig( | |
pageSize = NETWORK_PAGE_SIZE, | |
enablePlaceholders = false |
This file contains hidden or 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
@Dao | |
interface RepoDao { | |
@Query( | |
"SELECT * FROM repos WHERE " + | |
"name LIKE :queryString" | |
) | |
fun reposByName(queryString: String): PagingSource<Int, Repo> | |
} |
This file contains hidden or 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
private fun ActivitySearchRepositoriesBinding.bindList( | |
repoAdapter: ReposAdapter, | |
uiState: StateFlow<UiState>, | |
pagingData: Flow<PagingData<Repo>>, | |
… | |
) { | |
… | |
val notLoading = repoAdapter.loadStateFlow | |
// Only emit when REFRESH LoadState for the paging source changes. | |
.distinctUntilChangedBy { it.source.refresh } |
This file contains hidden or 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
private fun ActivitySearchRepositoriesBinding.bindList( | |
… | |
pagingData: Flow<PagingData<Repo>>, | |
) { | |
… | |
lifecycleScope.launch { | |
pagingData.collectLatest(repoAdapter::submitData) | |
} | |
} |
This file contains hidden or 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
// Before | |
// class ReposAdapter : ListAdapter<Repo, RepoViewHolder>(REPO_COMPARATOR) { | |
// … | |
// } | |
// After | |
class ReposAdapter : PagingDataAdapter<Repo, RepoViewHolder>(REPO_COMPARATOR) { | |
… | |
} |
This file contains hidden or 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
class SearchRepositoriesViewModel( | |
… | |
) : ViewModel() { | |
val state: StateFlow<UiState> | |
val pagingDataFlow: Flow<PagingData<Repo>> | |
init { | |
… |
This file contains hidden or 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
class SearchRepositoriesViewModel( | |
private val repository: GithubRepository, | |
… | |
) : ViewModel() { | |
… | |
private fun searchRepo(queryString: String): Flow<PagingData<Repo>> = | |
repository.getSearchResultStream(queryString) | |
} |
This file contains hidden or 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
data class UiState( | |
val query: String, | |
val searchResult: RepoSearchResult | |
) | |
sealed class RepoSearchResult { | |
data class Success(val data: List<Repo>) : RepoSearchResult() | |
data class Error(val error: Exception) : RepoSearchResult() | |
} |