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
28 changes: 14 additions & 14 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ android {
targetSdkVersion rootProject.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
Expand All @@ -38,19 +38,19 @@ android {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "com.android.support:appcompat-v7:$supportLibVersion"
implementation "com.android.support.constraint:constraint-layout:$constraintLayoutVersion"
implementation "com.android.support:design:$supportLibVersion"
implementation fileTree(dir: 'libs')
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "androidx.appcompat:appcompat:$supportLibVersion"
implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
implementation "com.google.android.material:material:$supportLibVersion"

// architecture components
implementation "android.arch.lifecycle:extensions:$archComponentsVersion"
implementation "android.arch.lifecycle:runtime:$archComponentsVersion"
implementation "android.arch.persistence.room:runtime:$roomVersion"
implementation "android.arch.paging:runtime:$pagingVersion"
kapt "android.arch.lifecycle:compiler:$archComponentsVersion"
kapt "android.arch.persistence.room:compiler:$roomVersion"
implementation "androidx.lifecycle:lifecycle-extensions:$archComponentsVersion"
implementation "androidx.lifecycle:lifecycle-runtime:$archComponentsVersion"
implementation "androidx.room:room-runtime:$roomVersion"
implementation "androidx.paging:paging-runtime:$pagingVersion"
kapt "androidx.lifecycle:lifecycle-compiler:$archComponentsVersion"
kapt "androidx.room:room-compiler:$roomVersion"

// retrofit
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
Expand All @@ -60,6 +60,6 @@ dependencies {

// testing
testImplementation "junit:junit:$junitVersion"
androidTestImplementation "com.android.support.test:runner:$runnerVersion"
androidTestImplementation "com.android.support.test.espresso:espresso-core:$espressoVersion"
androidTestImplementation "androidx.test:runner:$runnerVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.example.android.codelabs.paging

import android.arch.lifecycle.ViewModelProvider
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
Expand Down Expand Up @@ -55,5 +55,4 @@ object Injection {
fun provideViewModelFactory(context: Context): ViewModelProvider.Factory {
return ViewModelFactory(provideGithubRepository(context))
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ private const val IN_QUALIFIER = "in:name,description"
* @param onError function that defines how to handle request failure
*/
fun searchRepos(
service: GithubService,
query: String,
page: Int,
itemsPerPage: Int,
onSuccess: (repos: List<Repo>) -> Unit,
onError: (error: String) -> Unit) {
service: GithubService,
query: String,
page: Int,
itemsPerPage: Int,
onSuccess: (repos: List<Repo>) -> Unit,
onError: (error: String) -> Unit
) {
Log.d(TAG, "query: $query, page: $page, itemsPerPage: $itemsPerPage")

val apiQuery = query + IN_QUALIFIER
Expand All @@ -62,8 +63,8 @@ fun searchRepos(
}

override fun onResponse(
call: Call<RepoSearchResponse>?,
response: Response<RepoSearchResponse>
call: Call<RepoSearchResponse>?,
response: Response<RepoSearchResponse>
) {
Log.d(TAG, "got a response $response")
if (response.isSuccessful) {
Expand All @@ -85,10 +86,11 @@ interface GithubService {
* Get repos ordered by stars.
*/
@GET("search/repositories?sort=stars")
fun searchRepos(@Query("q") query: String,
@Query("page") page: Int,
@Query("per_page") itemsPerPage: Int): Call<RepoSearchResponse>

fun searchRepos(
@Query("q") query: String,
@Query("page") page: Int,
@Query("per_page") itemsPerPage: Int
): Call<RepoSearchResponse>

companion object {
private const val BASE_URL = "https://api.github.com/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

package com.example.android.codelabs.paging.api


import com.example.android.codelabs.paging.model.Repo
import com.google.gson.annotations.SerializedName

/**
* Data class to hold repo responses from searchRepo API calls.
*/
data class RepoSearchResponse(
@SerializedName("total_count") val total: Int = 0,
@SerializedName("items") val items: List<Repo> = emptyList(),
val nextPage: Int? = null
@SerializedName("total_count") val total: Int = 0,
@SerializedName("items") val items: List<Repo> = emptyList(),
val nextPage: Int? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.example.android.codelabs.paging.data

import android.arch.lifecycle.MutableLiveData
import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.example.android.codelabs.paging.api.GithubService
import com.example.android.codelabs.paging.api.searchRepos
import com.example.android.codelabs.paging.db.GithubLocalCache
Expand All @@ -27,8 +27,8 @@ 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
private val service: GithubService,
private val cache: GithubLocalCache
) {

// keep the last requested page. When the request is successful, increment the page number.
Expand Down Expand Up @@ -76,4 +76,4 @@ class GithubRepository(
companion object {
private const val NETWORK_PAGE_SIZE = 50
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

package com.example.android.codelabs.paging.db

import android.arch.lifecycle.LiveData
import android.arch.paging.DataSource
import android.util.Log
import androidx.lifecycle.LiveData
import com.example.android.codelabs.paging.model.Repo
import java.util.concurrent.Executor

Expand All @@ -27,14 +26,14 @@ import java.util.concurrent.Executor
* correct executor.
*/
class GithubLocalCache(
private val repoDao: RepoDao,
private val ioExecutor: Executor
private val repoDao: RepoDao,
private val ioExecutor: Executor
) {

/**
* Insert a list of repos in the database, on a background thread.
*/
fun insert(repos: List<Repo>, insertFinished: ()-> Unit) {
fun insert(repos: List<Repo>, insertFinished: () -> Unit) {
ioExecutor.execute {
Log.d("GithubLocalCache", "inserting ${repos.size} repos")
repoDao.insert(repos)
Expand All @@ -53,4 +52,4 @@ class GithubLocalCache(
val query = "%${name.replace(' ', '%')}%"
return repoDao.reposByName(query)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.example.android.codelabs.paging.db

import android.arch.lifecycle.LiveData
import android.arch.persistence.room.Dao
import android.arch.persistence.room.Insert
import android.arch.persistence.room.OnConflictStrategy
import android.arch.persistence.room.Query
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.example.android.codelabs.paging.model.Repo

/**
Expand All @@ -38,5 +38,4 @@ interface RepoDao {
@Query("SELECT * FROM repos WHERE (name LIKE :queryString) OR (description LIKE " +
":queryString) ORDER BY stars DESC, name ASC")
fun reposByName(queryString: String): LiveData<List<Repo>>

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.example.android.codelabs.paging.db

import android.arch.persistence.room.Database
import android.arch.persistence.room.Room
import android.arch.persistence.room.RoomDatabase
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import android.content.Context
import com.example.android.codelabs.paging.model.Repo

Expand Down Expand Up @@ -50,4 +50,4 @@ abstract class RepoDatabase : RoomDatabase() {
RepoDatabase::class.java, "Github.db")
.build()
}
}
}
20 changes: 10 additions & 10 deletions app/src/main/java/com/example/android/codelabs/paging/model/Repo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.example.android.codelabs.paging.model

import android.arch.persistence.room.Entity
import android.arch.persistence.room.PrimaryKey
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.google.gson.annotations.SerializedName

/**
Expand All @@ -28,12 +28,12 @@ import com.google.gson.annotations.SerializedName
*/
@Entity(tableName = "repos")
data class Repo(
@PrimaryKey @field:SerializedName("id") val id: Long,
@field:SerializedName("name") val name: String,
@field:SerializedName("full_name") val fullName: String,
@field:SerializedName("description") val description: String?,
@field:SerializedName("html_url") val url: String,
@field:SerializedName("stargazers_count") val stars: Int,
@field:SerializedName("forks_count") val forks: Int,
@field:SerializedName("language") val language: String?
@PrimaryKey @field:SerializedName("id") val id: Long,
@field:SerializedName("name") val name: String,
@field:SerializedName("full_name") val fullName: String,
@field:SerializedName("description") val description: String?,
@field:SerializedName("html_url") val url: String,
@field:SerializedName("stargazers_count") val stars: Int,
@field:SerializedName("forks_count") val forks: Int,
@field:SerializedName("language") val language: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

package com.example.android.codelabs.paging.model

import android.arch.lifecycle.LiveData
import androidx.lifecycle.LiveData

/**
* RepoSearchResult from a search, which contains LiveData<List<Repo>> holding query data,
* and a LiveData<String> of network error state.
*/
data class RepoSearchResult(
val data: LiveData<List<Repo>>,
val networkErrors: LiveData<String>
)
val data: LiveData<List<Repo>>,
val networkErrors: LiveData<String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.example.android.codelabs.paging.ui

import android.content.Intent
import android.net.Uri
import android.support.v7.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -92,4 +92,4 @@ class RepoViewHolder(view: View) : RecyclerView.ViewHolder(view) {
return RepoViewHolder(view)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@

package com.example.android.codelabs.paging.ui

import android.support.v7.recyclerview.extensions.ListAdapter
import android.support.v7.util.DiffUtil
import android.support.v7.widget.RecyclerView
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.example.android.codelabs.paging.model.Repo

/**
* Adapter for the list of repositories.
*/
class ReposAdapter : ListAdapter<Repo, RecyclerView.ViewHolder>(REPO_COMPARATOR) {
class ReposAdapter : ListAdapter<Repo, androidx.recyclerview.widget.RecyclerView.ViewHolder>(REPO_COMPARATOR) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
return RepoViewHolder.create(parent)
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
val repoItem = getItem(position)
if (repoItem != null) {
(holder as RepoViewHolder).bind(repoItem)
Expand All @@ -47,4 +46,4 @@ class ReposAdapter : ListAdapter<Repo, RecyclerView.ViewHolder>(REPO_COMPARATOR)
oldItem == newItem
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,21 @@

package com.example.android.codelabs.paging.ui

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.Toast
import com.example.android.codelabs.paging.R
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.DividerItemDecoration
import com.example.android.codelabs.paging.Injection
import com.example.android.codelabs.paging.R
import com.example.android.codelabs.paging.model.Repo
import kotlinx.android.synthetic.main.activity_search_repositories.*


class SearchRepositoriesActivity : AppCompatActivity() {

private lateinit var viewModel: SearchRepositoriesViewModel
Expand Down Expand Up @@ -71,7 +68,7 @@ class SearchRepositoriesActivity : AppCompatActivity() {
adapter.submitList(it)
})
viewModel.networkErrors.observe(this, Observer<String> {
Toast.makeText(this, "\uD83D\uDE28 Wooops ${it}", Toast.LENGTH_LONG).show()
Toast.makeText(this, "\uD83D\uDE28 Wooops $it", Toast.LENGTH_LONG).show()
})
}

Expand Down Expand Up @@ -117,9 +114,9 @@ class SearchRepositoriesActivity : AppCompatActivity() {
}

private fun setupScrollListener() {
val layoutManager = list.layoutManager as LinearLayoutManager
list.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
val layoutManager = list.layoutManager as androidx.recyclerview.widget.LinearLayoutManager
list.addOnScrollListener(object : androidx.recyclerview.widget.RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: androidx.recyclerview.widget.RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val totalItemCount = layoutManager.itemCount
val visibleItemCount = layoutManager.childCount
Expand Down
Loading