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
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ dependencies {
implementation "com.google.android.material:material:$materialVersion"

// architecture components
implementation "androidx.core:core-ktx:$coreVersion"
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.example.android.codelabs.paging.ui
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.paging.LoadState
import androidx.recyclerview.widget.RecyclerView
import com.example.android.codelabs.paging.R
Expand All @@ -37,9 +38,9 @@ class ReposLoadStateViewHolder(
if (loadState is LoadState.Error) {
binding.errorMsg.text = loadState.error.localizedMessage
}
binding.progressBar.visibility = toVisibility(loadState is LoadState.Loading)
binding.retryButton.visibility = toVisibility(loadState !is LoadState.Loading)
binding.errorMsg.visibility = toVisibility(loadState !is LoadState.Loading)
binding.progressBar.isVisible = loadState is LoadState.Loading
binding.retryButton.isVisible = loadState !is LoadState.Loading
binding.errorMsg.isVisible = loadState !is LoadState.Loading
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package com.example.android.codelabs.paging.ui

import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.paging.ExperimentalPagingApi
Expand Down Expand Up @@ -60,7 +60,7 @@ class SearchRepositoriesActivity : AppCompatActivity() {
val view = binding.root
setContentView(view)

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

Expand All @@ -85,41 +85,28 @@ class SearchRepositoriesActivity : AppCompatActivity() {
header = ReposLoadStateAdapter { adapter.retry() },
footer = ReposLoadStateAdapter { adapter.retry() }
)

adapter.addLoadStateListener { loadState ->
if (loadState.refresh !is LoadState.NotLoading) {
// We're refreshing: either loading or we had an error
// So we can hide the list
binding.list.visibility = View.GONE
binding.progressBar.visibility = toVisibility(loadState.refresh is LoadState.Loading)
binding.retryButton.visibility = toVisibility(loadState.refresh is LoadState.Error)
} else {
// We're not refreshing - we're either prepending or appending
// So we should show the list
binding.list.visibility = View.VISIBLE
binding.progressBar.visibility = View.GONE
binding.retryButton.visibility = View.GONE
// If we have an error, show a toast
val errorState = when {
loadState.append is LoadState.Error -> {
loadState.append as LoadState.Error
}
loadState.prepend is LoadState.Error -> {
loadState.prepend as LoadState.Error
}
else -> {
null
}
}
errorState?.let {
Toast.makeText(
this,
"\uD83D\uDE28 Wooops ${it.error}",
Toast.LENGTH_LONG
).show()
}
// Only show the list if refresh succeeds.
binding.list.isVisible = loadState.refresh is LoadState.NotLoading
// Show loading spinner during initial load or refresh.
binding.progressBar.isVisible = loadState.refresh is LoadState.Loading
// Show the retry state if initial load or refresh fails.
binding.retryButton.isVisible = loadState.refresh is LoadState.Error

// Toast on any error, regardless of whether it came from RemoteMediator or PagingSource
val errorState = loadState.source.append as? LoadState.Error
?: loadState.source.prepend as? LoadState.Error
?: loadState.append as? LoadState.Error
?: loadState.prepend as? LoadState.Error
errorState?.let {
Toast.makeText(
this,
"\uD83D\uDE28 Wooops ${it.error}",
Toast.LENGTH_LONG
).show()
}
}

}

private fun initSearch(query: String) {
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ext {
minSdkVersion = 15
targetSdkVersion = 28
supportLibVersion = '1.1.0'
coreVersion = '1.3.0'
recyclerViewVersion = '1.2.0-alpha03'
constraintLayoutVersion = '1.1.3'
materialVersion = '1.1.0'
Expand Down