-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from harisheoran/ep_loading_bug
Ep loading bug
- Loading branch information
Showing
23 changed files
with
422 additions
and
148 deletions.
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
Binary file not shown.
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,16 @@ | ||
package com.example.rickmorty | ||
|
||
import android.widget.ImageView | ||
import androidx.databinding.BindingAdapter | ||
import com.squareup.picasso.Picasso | ||
|
||
|
||
//In Kotlin, the let function is a scoping function that allows you to perform operations on a non-null object within a safe context. | ||
// It is particularly useful for executing a block of code only if the object is not null. | ||
@BindingAdapter("setImage") | ||
fun bindImage(imgView: ImageView, imgUrl: String?) { | ||
// it will only execute if imageUrl is non null | ||
imgUrl?.let { | ||
Picasso.get().load(imgUrl).into(imgView) | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
app/src/main/java/com/example/rickmorty/characters/CharacterListAdapter.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,46 @@ | ||
package com.example.rickmorty.characters | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.paging.PagingDataAdapter | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.example.rickmorty.databinding.ModelCharacterListItemBinding | ||
import com.example.rickmorty.network.response.GetCharacterByIdResponse | ||
|
||
class CharacterListAdapter(private val onCharacterClick: (Int) -> Unit) : | ||
PagingDataAdapter<GetCharacterByIdResponse, RecyclerView.ViewHolder>(DiffCallback) { | ||
|
||
class CharacterViewHolder(var binding: ModelCharacterListItemBinding) : RecyclerView.ViewHolder(binding.root) { | ||
fun bind(onCharacterClick: (Int) -> Unit, character: GetCharacterByIdResponse) { | ||
binding.character = character | ||
binding.executePendingBindings() | ||
binding.root.setOnClickListener { | ||
onCharacterClick(character.id) | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | ||
val view = CharacterViewHolder(ModelCharacterListItemBinding.inflate(LayoutInflater.from(parent.context))) | ||
return view | ||
} | ||
|
||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { | ||
val myCharacter = getItem(position) | ||
(holder as? CharacterViewHolder)?.bind(onCharacterClick, myCharacter!!) | ||
} | ||
|
||
companion object DiffCallback : DiffUtil.ItemCallback<GetCharacterByIdResponse>() { | ||
override fun areItemsTheSame(oldItem: GetCharacterByIdResponse, newItem: GetCharacterByIdResponse): Boolean { | ||
return oldItem.name == newItem.name | ||
} | ||
|
||
override fun areContentsTheSame(oldItem: GetCharacterByIdResponse, newItem: GetCharacterByIdResponse): Boolean { | ||
return oldItem.name == newItem.name | ||
} | ||
} | ||
} | ||
|
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
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
48 changes: 48 additions & 0 deletions
48
app/src/main/java/com/example/rickmorty/characters/PagingLoadStateAdapter.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,48 @@ | ||
package com.example.rickmorty.characters | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.core.view.isVisible | ||
import androidx.core.widget.ContentLoadingProgressBar | ||
import androidx.paging.LoadState | ||
import androidx.paging.LoadStateAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.example.rickmorty.R | ||
import com.google.android.material.button.MaterialButton | ||
import com.google.android.material.textview.MaterialTextView | ||
|
||
class PagingLoadStateAdapter(val retryCallback: () -> Unit) : | ||
LoadStateAdapter<PagingLoadStateAdapter.LoadStateViewHolder>() { | ||
|
||
class LoadStateViewHolder(itemView: View, retry: () -> Unit) : RecyclerView.ViewHolder(itemView) { | ||
|
||
init { | ||
itemView.findViewById<MaterialButton>(R.id.retry_button).setOnClickListener { | ||
retry.invoke() | ||
} | ||
} | ||
|
||
val progressBar = itemView.findViewById<ContentLoadingProgressBar>(R.id.progress_bar) | ||
val errorText = itemView.findViewById<MaterialTextView>(R.id.error_msg) | ||
fun bind(loadState: LoadState) { | ||
if (loadState is LoadState.Error) { | ||
errorText.text = "Try Again later..." | ||
} | ||
progressBar.isVisible = loadState is LoadState.Loading | ||
itemView.findViewById<MaterialButton>(R.id.retry_button).isVisible = loadState !is LoadState.Error | ||
errorText.isVisible = loadState !is LoadState.Loading | ||
} | ||
} | ||
|
||
override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) { | ||
holder.bind(loadState) | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): LoadStateViewHolder { | ||
return LoadStateViewHolder( | ||
LayoutInflater.from(parent.context).inflate(R.layout.model_network_state, parent, false), | ||
retry = retryCallback | ||
) | ||
} | ||
} |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:height="24dp" android:tint="#000000" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,17h-2v-6h2v6zM13,9h-2L11,7h2v2z"/> | ||
</vector> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.