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
Step 1 - Add a footer that displays progress and error states, using MergeAdapter #46
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0ea1c81
Add a footer that displays progress and error states, using MergeAdapter
florina-muntenescu a031074
Addressing review comments:
florina-muntenescu 78ec14e
Using ViewBinding in ReposLoadStateViewHolder
florina-muntenescu 1ebcf36
Improve the layout of load state item and the naming in RepoLoadState…
florina-muntenescu File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
app/src/main/java/com/example/android/codelabs/paging/ui/LoadState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2020 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.android.codelabs.paging.ui | ||
|
||
/** | ||
* LoadState of a list load | ||
*/ | ||
sealed class LoadState { | ||
/** | ||
* Loading is in progress. | ||
*/ | ||
object Loading : LoadState() | ||
|
||
/** | ||
* Loading is complete. | ||
*/ | ||
object Done : LoadState() | ||
|
||
/** | ||
* Loading hit an error. | ||
* | ||
* @param error [Throwable] that caused the load operation to generate this error state. | ||
* | ||
*/ | ||
data class Error(val error: Throwable) : LoadState() { | ||
override fun toString() = "Error: $error" | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
app/src/main/java/com/example/android/codelabs/paging/ui/ReposLoadStateAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2020 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.android.codelabs.paging.ui | ||
|
||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
class ReposLoadStateAdapter(private val retry: () -> Unit) : RecyclerView.Adapter<ReposLoadStateViewHolder>() { | ||
|
||
/** | ||
* LoadState to present in the adapter. | ||
* | ||
* Changing this property will immediately notify the Adapter to change the item it's | ||
* presenting. | ||
*/ | ||
var loadState: LoadState = LoadState.Done | ||
set(loadState) { | ||
if (field != loadState) { | ||
val displayOldItem = displayLoadStateAsItem(field) | ||
val displayNewItem = displayLoadStateAsItem(loadState) | ||
|
||
if (displayOldItem && !displayNewItem) { | ||
notifyItemRemoved(0) | ||
} else if (displayNewItem && !displayOldItem) { | ||
notifyItemInserted(0) | ||
} else if (displayOldItem && displayNewItem) { | ||
notifyItemChanged(0) | ||
} | ||
field = loadState | ||
} | ||
} | ||
|
||
override fun onBindViewHolder(holder: ReposLoadStateViewHolder, position: Int) { | ||
holder.bind(loadState) | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReposLoadStateViewHolder { | ||
return ReposLoadStateViewHolder.create(parent, retry) | ||
} | ||
|
||
override fun getItemViewType(position: Int): Int = 0 | ||
|
||
override fun getItemCount(): Int = if (displayLoadStateAsItem(loadState)) 1 else 0 | ||
|
||
/** | ||
* Returns true if the LoadState should be displayed as a list item when active. | ||
* | ||
* [LoadState.Loading] and [LoadState.Error] present as list items, | ||
* [LoadState.Done] is not. | ||
*/ | ||
private fun displayLoadStateAsItem(loadState: LoadState): Boolean { | ||
return loadState is LoadState.Loading || loadState is LoadState.Error | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
app/src/main/java/com/example/android/codelabs/paging/ui/ReposLoadStateViewHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (C) 2020 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.android.codelabs.paging.ui | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.Button | ||
import android.widget.ProgressBar | ||
import android.widget.TextView | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.example.android.codelabs.paging.R | ||
import com.example.android.codelabs.paging.databinding.ReposLoadStateHeaderViewItemBinding | ||
|
||
class ReposLoadStateViewHolder( | ||
private val binding: ReposLoadStateHeaderViewItemBinding, | ||
retry: () -> Unit | ||
) : RecyclerView.ViewHolder(binding.root) { | ||
|
||
init { | ||
binding.retryButton.also { | ||
it.setOnClickListener { retry.invoke() } | ||
} | ||
} | ||
|
||
fun bind(loadState: LoadState) { | ||
if (loadState is LoadState.Error) { | ||
binding.errorMsg.text = loadState.error.localizedMessage | ||
} | ||
binding.progressBar.visibility = toVisibility(loadState == LoadState.Loading) | ||
binding.retryButton.visibility = toVisibility(loadState != LoadState.Loading) | ||
binding.errorMsg.visibility = toVisibility(loadState != LoadState.Loading) | ||
} | ||
|
||
private fun toVisibility(constraint: Boolean): Int = if (constraint) { | ||
View.VISIBLE | ||
} else { | ||
View.GONE | ||
} | ||
|
||
companion object { | ||
fun create(parent: ViewGroup, retry: () -> Unit): ReposLoadStateViewHolder { | ||
val view = LayoutInflater.from(parent.context) | ||
.inflate(R.layout.repos_load_state_header_view_item, parent, false) | ||
val binding = ReposLoadStateHeaderViewItemBinding.bind(view) | ||
return ReposLoadStateViewHolder(binding, retry) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
app/src/main/res/layout/repos_load_state_header_view_item.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="utf-8"?><!-- | ||
~ Copyright (C) 2018 The Android Open Source Project | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
--> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:orientation="vertical" | ||
android:padding="8dp"> | ||
<TextView | ||
android:id="@+id/error_msg" | ||
android:textColor="?android:textColorPrimary" | ||
android:textSize="@dimen/error_text_size" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:textAlignment="center" | ||
tools:text="Timeout"/> | ||
<ProgressBar | ||
android:id="@+id/progress_bar" | ||
style="?android:attr/progressBarStyle" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center"/> | ||
<Button | ||
android:id="@+id/retry_button" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:text="@string/retry"/> | ||
</LinearLayout> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map with a side effect feels weird, but looks like there's no better operator :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree! It felt strange for me writing it as well :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@florina-muntenescu @ChrisCraik I think this would be achievable by adding a new
Observer
andobserveForever
onrepoResult
. The observer would then be removed and nullified inonCleared
. Just tried it on this branch and it seems to be behaving the same.