-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LayoutContainer for SuperHeroViewHolder
Add binding adapter to load images Add data binding to recycler view adapter Fix tests
- Loading branch information
1 parent
9cabf42
commit b4a01d7
Showing
9 changed files
with
112 additions
and
85 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
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/karumi/jetpack/superheroes/common/BindingAdapters.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,12 @@ | ||
package com.karumi.jetpack.superheroes.common | ||
|
||
import android.widget.ImageView | ||
import androidx.databinding.BindingAdapter | ||
import com.karumi.jetpack.superheroes.ui.utils.picasso | ||
|
||
@BindingAdapter("imageUrl") | ||
fun setImageUrl(view: ImageView, url: String?) { | ||
if (url != null) { | ||
view.context.picasso.load(url).fit().centerCrop().fit().into(view) | ||
} | ||
} |
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
30 changes: 4 additions & 26 deletions
30
app/src/main/java/com/karumi/jetpack/superheroes/ui/view/adapter/SuperHeroViewHolder.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 |
---|---|---|
@@ -1,43 +1,21 @@ | ||
package com.karumi.jetpack.superheroes.ui.view.adapter | ||
|
||
import android.view.View | ||
import android.widget.ImageView | ||
import android.widget.TextView | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.karumi.jetpack.superheroes.R | ||
import com.karumi.jetpack.superheroes.databinding.SuperHeroRowBinding | ||
import com.karumi.jetpack.superheroes.domain.model.SuperHero | ||
import com.karumi.jetpack.superheroes.ui.presenter.SuperHeroesPresenter | ||
import com.karumi.jetpack.superheroes.ui.utils.setImageBackground | ||
|
||
class SuperHeroViewHolder( | ||
itemView: View, | ||
private val binding: SuperHeroRowBinding, | ||
private val presenter: SuperHeroesPresenter | ||
) : RecyclerView.ViewHolder(itemView) { | ||
|
||
private val photoImageView: ImageView = itemView.findViewById(R.id.iv_super_hero_photo) | ||
private val nameTextView: TextView = itemView.findViewById(R.id.tv_super_hero_name) | ||
private val avengersBadgeView: View = itemView.findViewById(R.id.iv_avengers_badge) | ||
) : RecyclerView.ViewHolder(binding.root) { | ||
|
||
fun render(superHero: SuperHero) { | ||
hookListeners(superHero) | ||
renderSuperHeroPhoto(superHero.photo) | ||
renderSuperHeroName(superHero.name) | ||
renderAvengersBadge(superHero.isAvenger) | ||
binding.superHero = superHero | ||
} | ||
|
||
private fun hookListeners(superHero: SuperHero) { | ||
itemView.setOnClickListener { presenter.onSuperHeroClicked(superHero) } | ||
} | ||
|
||
private fun renderSuperHeroPhoto(photo: String?) { | ||
photoImageView.setImageBackground(photo) | ||
} | ||
|
||
private fun renderSuperHeroName(name: String) { | ||
nameTextView.text = name | ||
} | ||
|
||
private fun renderAvengersBadge(isAvenger: Boolean) { | ||
avengersBadgeView.visibility = if (isAvenger) View.VISIBLE else View.GONE | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,41 +1,56 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
<layout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="@dimen/super_hero_row_height"> | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<ImageView | ||
android:id="@+id/iv_super_hero_photo" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
tools:ignore="ContentDescription" /> | ||
<data> | ||
|
||
<import type="android.view.View" /> | ||
|
||
<View | ||
<variable | ||
name="superHero" | ||
type="com.karumi.jetpack.superheroes.domain.model.SuperHero" /> | ||
</data> | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="@dimen/super_hero_row_gradient_height" | ||
android:background="@drawable/super_hero_gradient" | ||
app:layout_constraintBottom_toBottomOf="parent" /> | ||
|
||
<TextView | ||
android:id="@+id/tv_super_hero_name" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="@dimen/default_margin" | ||
android:textColor="@android:color/white" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
tools:text="Iron Man" /> | ||
|
||
<ImageView | ||
android:id="@+id/iv_avengers_badge" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="@dimen/default_margin" | ||
android:src="@mipmap/ic_avengers" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
android:contentDescription="@string/is_avenger_badge_content_description" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
android:layout_height="@dimen/super_hero_row_height"> | ||
|
||
<ImageView | ||
android:id="@+id/iv_super_hero_photo" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
app:imageUrl="@{ superHero.photo }" | ||
app:layout_constraintTop_toTopOf="parent" | ||
tools:ignore="ContentDescription" /> | ||
|
||
<View | ||
android:layout_width="match_parent" | ||
android:layout_height="@dimen/super_hero_row_gradient_height" | ||
android:background="@drawable/super_hero_gradient" | ||
app:layout_constraintBottom_toBottomOf="parent" /> | ||
|
||
<TextView | ||
android:id="@+id/tv_super_hero_name" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="@dimen/default_margin" | ||
android:text="@{ superHero.name }" | ||
android:textColor="@android:color/white" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
tools:text="Iron Man" /> | ||
|
||
<ImageView | ||
android:id="@+id/iv_avengers_badge" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="@dimen/default_margin" | ||
android:contentDescription="@string/is_avenger_badge_content_description" | ||
android:src="@mipmap/ic_avengers" | ||
android:visibility="@{ superHero.avenger ? View.VISIBLE : View.GONE, default=gone }" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
</layout> |