Skip to content
Merged
Show file tree
Hide file tree
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 @@ -21,11 +21,8 @@ import com.example.android.codelabs.paging.api.GithubService
import com.example.android.codelabs.paging.api.IN_QUALIFIER
import com.example.android.codelabs.paging.model.Repo
import com.example.android.codelabs.paging.model.RepoSearchResult
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.MutableSharedFlow
import retrofit2.HttpException
import java.io.IOException

Expand All @@ -35,15 +32,14 @@ private const val GITHUB_STARTING_PAGE_INDEX = 1
/**
* Repository class that works with local and remote data sources.
*/
@ExperimentalCoroutinesApi
class GithubRepository(private val service: GithubService) {

// keep the list of all results received
private val inMemoryCache = mutableListOf<Repo>()

// keep channel of results. The channel allows us to broadcast updates so
// shared flow of results, which allows us to broadcast updates so
// the subscriber will have the latest data
private val searchResults = ConflatedBroadcastChannel<RepoSearchResult>()
private val searchResults = MutableSharedFlow<RepoSearchResult>(replay = 1)

// keep the last requested page. When the request is successful, increment the page number.
private var lastRequestedPage = GITHUB_STARTING_PAGE_INDEX
Expand All @@ -61,7 +57,7 @@ class GithubRepository(private val service: GithubService) {
inMemoryCache.clear()
requestAndSaveData(query)

return searchResults.asFlow()
return searchResults
}

suspend fun requestMore(query: String) {
Expand All @@ -88,12 +84,12 @@ class GithubRepository(private val service: GithubService) {
val repos = response.items ?: emptyList()
inMemoryCache.addAll(repos)
val reposByName = reposByName(query)
searchResults.offer(RepoSearchResult.Success(reposByName))
searchResults.emit(RepoSearchResult.Success(reposByName))
successful = true
} catch (exception: IOException) {
searchResults.offer(RepoSearchResult.Error(exception))
searchResults.emit(RepoSearchResult.Error(exception))
} catch (exception: HttpException) {
searchResults.offer(RepoSearchResult.Error(exception))
searchResults.emit(RepoSearchResult.Error(exception))
}
isRequestInProgress = false
return successful
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ import android.view.inputmethod.EditorInfo
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.observe
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.OnScrollListener
import com.example.android.codelabs.paging.Injection
import com.example.android.codelabs.paging.databinding.ActivitySearchRepositoriesBinding
import com.example.android.codelabs.paging.model.RepoSearchResult
import kotlinx.coroutines.ExperimentalCoroutinesApi

@ExperimentalCoroutinesApi
class SearchRepositoriesActivity : AppCompatActivity() {

private lateinit var binding: ActivitySearchRepositoriesBinding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ import androidx.lifecycle.*
import com.example.android.codelabs.paging.data.GithubRepository
import com.example.android.codelabs.paging.model.RepoSearchResult
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.launch

/**
* ViewModel for the [SearchRepositoriesActivity] screen.
* The ViewModel works with the [GithubRepository] to get the data.
*/
@ExperimentalCoroutinesApi
class SearchRepositoriesViewModel(private val repository: GithubRepository) : ViewModel() {

companion object {
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.4.10'
ext.kotlin_version = '1.4.21'
repositories {
google()
jcenter()
Expand Down Expand Up @@ -50,8 +50,8 @@ ext {
materialVersion = '1.2.1'
lifecycleVersion = '2.2.0'
roomVersion = '2.3.0-alpha03'
pagingVersion = '3.0.0-alpha09'
pagingVersion = '3.0.0-alpha11'
retrofitVersion = '2.7.2'
okhttpLoggingInterceptorVersion = '4.0.0'
coroutines = '1.3.7'
coroutines = '1.4.1'
}