Skip to content
Closed
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
19 changes: 4 additions & 15 deletions app/src/main/java/com/example/android/codelabs/paging/Injection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import android.content.Context
import androidx.lifecycle.ViewModelProvider
import com.example.android.codelabs.paging.api.GithubService
import com.example.android.codelabs.paging.data.GithubRepository
import com.example.android.codelabs.paging.db.GithubLocalCache
import com.example.android.codelabs.paging.db.RepoDatabase
import com.example.android.codelabs.paging.ui.ViewModelFactory
import java.util.concurrent.Executors

/**
* Class that handles object creation.
Expand All @@ -32,27 +29,19 @@ import java.util.concurrent.Executors
*/
object Injection {

/**
* Creates an instance of [GithubLocalCache] based on the database DAO.
*/
private fun provideCache(context: Context): GithubLocalCache {
val database = RepoDatabase.getInstance(context)
return GithubLocalCache(database.reposDao(), Executors.newSingleThreadExecutor())
}

/**
* Creates an instance of [GithubRepository] based on the [GithubService] and a
* [GithubLocalCache]
*/
private fun provideGithubRepository(context: Context): GithubRepository {
return GithubRepository(GithubService.create(), provideCache(context))
private fun provideGithubRepository(): GithubRepository {
return GithubRepository(GithubService.create())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess GithubRepository would have the same lifetime as GithubService so it doesn't really matter, but it's interesting that you have a separate provide for GithubRepository, but not for the Service

}

/**
* Provides the [ViewModelProvider.Factory] that is then used to get a reference to
* [ViewModel] objects.
*/
fun provideViewModelFactory(context: Context): ViewModelProvider.Factory {
return ViewModelFactory(provideGithubRepository(context))
fun provideViewModelFactory(): ViewModelProvider.Factory {
return ViewModelFactory(provideGithubRepository())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@
package com.example.android.codelabs.paging.data

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.example.android.codelabs.paging.api.GithubService
import com.example.android.codelabs.paging.api.searchRepos
import com.example.android.codelabs.paging.db.GithubLocalCache
import com.example.android.codelabs.paging.model.Repo
import com.example.android.codelabs.paging.model.RepoSearchResult

/**
* Repository class that works with local and remote data sources.
*/
class GithubRepository(
private val service: GithubService,
private val cache: GithubLocalCache
) {
class GithubRepository(private val service: GithubService) {

// keep the list of responses
private val inMemoryCache = MutableLiveData<List<Repo>>()

// keep the last requested page. When the request is successful, increment the page number.
private var lastRequestedPage = 1
Expand All @@ -48,8 +50,8 @@ class GithubRepository(
lastRequestedPage = 1
requestAndSaveData(query)

// Get data from the local cache
val data = cache.reposByName(query)
// Get data from the in memory cache
val data = reposByName(query)

return RepoSearchResult(data, networkErrors)
}
Expand All @@ -63,16 +65,33 @@ class GithubRepository(

isRequestInProgress = true
searchRepos(service, query, lastRequestedPage, NETWORK_PAGE_SIZE, { repos ->
cache.insert(repos) {
lastRequestedPage++
isRequestInProgress = false
}
// add the new result list to the existing list
val allResults = mutableListOf<Repo>()
inMemoryCache.value?.let { allResults.addAll(it) }
allResults.addAll(repos)

inMemoryCache.postValue(allResults)
lastRequestedPage++
isRequestInProgress = false
}, { error ->
networkErrors.postValue(error)
isRequestInProgress = false
})
}

private fun reposByName(query: String): LiveData<List<Repo>> {
return Transformations.switchMap(inMemoryCache) { repos ->
// from the in memory cache select only the repos whose name or description matches
// the query. Then order the results.
val filteredList = repos.filter {
it.name.contains(query, true) ||
(it.description != null && it.description.contains(query, true))
}.sortedWith(compareByDescending<Repo> { it.stars }.thenBy { it.name })

MutableLiveData(filteredList)
}
}

companion object {
private const val NETWORK_PAGE_SIZE = 50
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ data class Repo(
@field:SerializedName("stargazers_count") val stars: Int,
@field:SerializedName("forks_count") val forks: Int,
@field:SerializedName("language") val language: String?
)
){

override fun toString(): String {
return "Repo(name='$name', stars=$stars)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import android.view.inputmethod.EditorInfo
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DividerItemDecoration
import com.example.android.codelabs.paging.Injection
import com.example.android.codelabs.paging.R
Expand All @@ -41,7 +41,7 @@ class SearchRepositoriesActivity : AppCompatActivity() {
setContentView(R.layout.activity_search_repositories)

// get the view model
viewModel = ViewModelProviders.of(this, Injection.provideViewModelFactory(this))
viewModel = ViewModelProvider(this, Injection.provideViewModelFactory())
.get(SearchRepositoriesViewModel::class.java)

// add dividers between RecyclerView's row items
Expand Down