Last active
June 1, 2020 06:34
-
-
Save yongjhih/9b9b76c7ea180d7e8ba931e3ffca42b2 to your computer and use it in GitHub Desktop.
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
class UserView(context: Context) : ContourLayout(context) { | |
init { | |
contourHeightOf { avatar.bottom() + 5.dip } | |
} | |
val name: TextView = | |
TextView(context).apply { | |
text = "" | |
applyLayout( | |
x = leftTo { avatar.right() + 5.dip } | |
.widthOf { parent.right() - 5.dip }, | |
y = topTo { avatar.top() + 5.dip } | |
) | |
} | |
val avatar = | |
CircularImageView(context).apply { | |
scaleType = ImageView.ScaleType.CENTER_CROP | |
applyLayout( | |
x = leftTo { parent.left() + 5.dip }.widthOf { | |
context.resources.getDimensionPixelSize(R.dimen.avatar_size).toXInt() | |
}, | |
y = topTo { parent.top() + 5.dip }.heightOf { | |
context.resources.getDimensionPixelSize(R.dimen.avatar_size).toYInt() | |
} | |
) | |
} | |
} | |
/** | |
* CardView { | |
* UserView { | |
* ImageView {} | |
* TextView {} | |
* } | |
* } | |
*/ | |
class UserCardView(context: Context) : ContourLayout(context) { | |
init { | |
contourHeightOf { cardView.bottom() + 10.dip } | |
} | |
private val cardView: CardView = CardView(context) | |
val userView: UserView = UserView(context).also { | |
cardView.apply { | |
addView(it) | |
radius = context.resources.getDimension(R.dimen.avatar_radius_dimen) | |
applyLayout( | |
x = leftTo { parent.left() + 5.dip} | |
.rightTo { parent.right() - 5.dip }, | |
y = topTo { parent.top() + 5.dip } | |
) | |
} | |
} | |
} | |
abstract class BindViewHolder<T>(itemView: View) : ViewHolder(itemView) { | |
abstract fun bind(user: T?) | |
} | |
abstract class TypedViewHolder<T, V : View>(context: Context, val view: V) | |
: BindViewHolder<T>( | |
LinearLayout(context).apply { | |
layoutParams = RecyclerView.LayoutParams( | |
RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT) | |
addView(view) | |
} | |
) | |
class UserViewHolder(context: Context, view: UserCardView) : TypedViewHolder<User, UserCardView>(context, view) { | |
override fun bind(user: User?) { | |
user?.avatarUrl?.let { | |
view.userView.avatar.url(it) | |
} | |
view.userView.name.text = user?.name ?: user?.login ?: "" | |
} | |
} | |
/// TODO: Move to util/PicassoKtx | |
fun ImageView.url(url: String) { | |
Picasso.get() | |
.load(url) | |
.into(this) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment