Skip to content

Bug: Endless fetching on finite lists #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master_old
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class GithubRepository(private val service: GithubService) {
// keep the last requested page. When the request is successful, increment the page number.
private var lastRequestedPage = GITHUB_STARTING_PAGE_INDEX

// keeps the total number of available items from our service, necessary to
// go through with requesting more or not
private var totalItemCount = Int.MAX_VALUE

// avoid triggering multiple requests in the same time
private var isRequestInProgress = false

Expand All @@ -54,6 +58,7 @@ class GithubRepository(private val service: GithubService) {
suspend fun getSearchResultStream(query: String): Flow<RepoSearchResult> {
Log.d("GithubRepository", "New query: $query")
lastRequestedPage = 1
totalItemCount = Int.MAX_VALUE
inMemoryCache.clear()
requestAndSaveData(query)

Expand All @@ -62,10 +67,8 @@ class GithubRepository(private val service: GithubService) {

suspend fun requestMore(query: String) {
if (isRequestInProgress) return
val successful = requestAndSaveData(query)
if (successful) {
lastRequestedPage++
}
if (totalItemCount < (lastRequestedPage - 1) * NETWORK_PAGE_SIZE) return
requestAndSaveData(query)
}

suspend fun retry(query: String) {
Expand All @@ -82,9 +85,11 @@ class GithubRepository(private val service: GithubService) {
val response = service.searchRepos(apiQuery, lastRequestedPage, NETWORK_PAGE_SIZE)
Log.d("GithubRepository", "response $response")
val repos = response.items ?: emptyList()
totalItemCount = response.total
inMemoryCache.addAll(repos)
val reposByName = reposByName(query)
searchResults.emit(RepoSearchResult.Success(reposByName))
lastRequestedPage++
successful = true
} catch (exception: IOException) {
searchResults.emit(RepoSearchResult.Error(exception))
Expand Down