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
| override suspend fun load(loadType: LoadType, state: PagingState<Int, Repo>): MediatorResult { | |
| val page = when (loadType) { | |
| LoadType.REFRESH -> … | |
| LoadType.PREPEND -> … | |
| LoadType.APPEND -> … | |
| } | |
| val apiQuery = query + IN_QUALIFIER |
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
| override suspend fun initialize(): InitializeAction { | |
| return InitializeAction.LAUNCH_INITIAL_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
| @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 { | |
| … |