Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
ViewExtension: Simplify Picasso load avatar
Browse files Browse the repository at this point in the history
Signed-off-by: Siubeng Fung <[email protected]>
  • Loading branch information
fython committed Feb 17, 2020
1 parent 90a526d commit a1066b7
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 57 deletions.
4 changes: 1 addition & 3 deletions app/src/main/java/moe/feng/danmaqua/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,7 @@ class MainActivity : BaseActivity(),
val cur = database.subscriptions().findSelected()
if (cur != null) {
usernameView.text = cur.username
Picasso.get().load(cur.avatar)
.placeholder(R.drawable.avatar_placeholder_empty)
.into(avatarView)
avatarView.avatarUrl = cur.avatar
} else {
usernameView.setText(R.string.no_streamer_selected_title)
Picasso.get().cancelRequest(avatarView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlinx.coroutines.launch
import moe.feng.danmaqua.R
import moe.feng.danmaqua.model.BlockedUserRule
import moe.feng.danmaqua.ui.view.CircleImageView
import moe.feng.danmaqua.util.ext.avatarUrl

abstract class ConfirmBlockUserDialogFragment : BaseDialogFragment() {

Expand Down Expand Up @@ -64,14 +65,7 @@ abstract class ConfirmBlockUserDialogFragment : BaseDialogFragment() {
val usernameView = view.findViewById<TextView>(R.id.usernameView)
val uidView = view.findViewById<TextView>(R.id.uidView)

if (face != null) {
Picasso.get()
.load(face)
.placeholder(R.drawable.avatar_placeholder_empty)
.into(avatarView)
} else {
avatarView.setImageResource(R.drawable.avatar_placeholder_empty)
}
avatarView.avatarUrl = face
usernameView.text = username
uidView.text = getString(R.string.uid_text_format, uid)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import moe.feng.danmaqua.R
import moe.feng.danmaqua.event.OnConfirmSubscribeStreamerListener
import moe.feng.danmaqua.model.Subscription
import moe.feng.danmaqua.ui.view.CircleImageView
import moe.feng.danmaqua.util.ext.avatarUrl
import moe.feng.danmaqua.util.ext.eventsHelper

open class ConfirmSubscribeStreamerDialogFragment : BaseDialogFragment() {
Expand Down Expand Up @@ -55,14 +56,7 @@ open class ConfirmSubscribeStreamerDialogFragment : BaseDialogFragment() {
val usernameView = view.findViewById<TextView>(R.id.usernameView)
val uidView = view.findViewById<TextView>(R.id.uidView)

if (subscription.avatar.isNotEmpty()) {
Picasso.get()
.load(subscription.avatar)
.placeholder(R.drawable.avatar_placeholder_empty)
.into(avatarView)
} else {
avatarView.setImageResource(R.drawable.avatar_placeholder_empty)
}
avatarView.avatarUrl = subscription.avatar
usernameView.text = subscription.username
uidView.text = getString(R.string.room_id_text_format, subscription.roomId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.contact_author_dialog_layout.*
import moe.feng.danmaqua.R
import moe.feng.danmaqua.util.IntentUtils
import moe.feng.danmaqua.util.ext.avatarUrl
import java.lang.Exception

class ContactAuthorDialogFragment : BaseBottomSheetDialogFragment() {
Expand All @@ -30,10 +31,7 @@ class ContactAuthorDialogFragment : BaseBottomSheetDialogFragment() {
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Picasso.get()
.load("https://avatars.githubusercontent.com/fython")
.placeholder(R.drawable.avatar_placeholder_empty)
.into(avatarView)
avatarView.avatarUrl = "https://avatars.githubusercontent.com/fython"

emailSummary.text = email

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import moe.feng.common.eventshelper.of
import moe.feng.danmaqua.R
import moe.feng.danmaqua.event.OnRecommendedStreamerItemClickListener
import moe.feng.danmaqua.model.Recommendation
import moe.feng.danmaqua.util.ext.avatarUrl
import moe.feng.danmaqua.util.ext.eventsHelper

class RecommendedStreamerItemViewDelegate :
Expand Down Expand Up @@ -38,11 +39,7 @@ class RecommendedStreamerItemViewDelegate :
with(holder) {
this.item = item

Picasso.get()
.load(item.face)
.placeholder(R.drawable.avatar_placeholder_empty)
.into(avatarView)

avatarView.avatarUrl = item.face
nameText.text = item.name
reasonText.text = item.reason
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.drakeet.multitype.ItemViewDelegate
import com.squareup.picasso.Picasso
import moe.feng.danmaqua.R
import moe.feng.danmaqua.model.Subscription
import moe.feng.danmaqua.util.ext.avatarUrl

class SubscriptionItemViewDelegate(var callback: Callback? = null)
: ItemViewDelegate<Subscription, SubscriptionItemViewDelegate.ViewHolder>() {
Expand Down Expand Up @@ -59,10 +60,7 @@ class SubscriptionItemViewDelegate(var callback: Callback? = null)
})
avatarRing.isVisible = item.selected

Picasso.get()
.load(item.avatar)
.placeholder(R.drawable.avatar_placeholder_empty)
.into(avatarView)
avatarView.avatarUrl = item.avatar
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import moe.feng.common.eventshelper.of
import moe.feng.danmaqua.R
import moe.feng.danmaqua.event.OnCatalogGroupItemClickListener
import moe.feng.danmaqua.model.VTuberCatalog
import moe.feng.danmaqua.util.ext.avatarUrl
import moe.feng.danmaqua.util.ext.eventsHelper

class VTuberCatalogItemViewDelegate :
Expand All @@ -37,14 +38,7 @@ class VTuberCatalogItemViewDelegate :
override fun onBindViewHolder(holder: ViewHolder, item: VTuberCatalog.Group) {
with(holder) {
data = item
if (item.icon.isNullOrEmpty()) {
avatarView.setImageResource(R.drawable.avatar_placeholder_empty)
} else {
Picasso.get()
.load(item.icon)
.placeholder(R.drawable.avatar_placeholder_empty)
.into(avatarView)
}
avatarView.avatarUrl = item.icon
nameText.text = item.title
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import moe.feng.common.eventshelper.of
import moe.feng.danmaqua.R
import moe.feng.danmaqua.event.OnCatalogSingleItemClickListener
import moe.feng.danmaqua.model.VTuberSingleItem
import moe.feng.danmaqua.util.ext.avatarUrl
import moe.feng.danmaqua.util.ext.eventsHelper

class VTuberSingleItemViewDelegate :
Expand All @@ -37,14 +38,7 @@ class VTuberSingleItemViewDelegate :
override fun onBindViewHolder(holder: ViewHolder, item: VTuberSingleItem) {
with(holder) {
data = item
if (item.face.isEmpty()) {
avatarView.setImageResource(R.drawable.avatar_placeholder_empty)
} else {
Picasso.get()
.load(item.face)
.placeholder(R.drawable.avatar_placeholder_empty)
.into(avatarView)
}
avatarView.avatarUrl = item.face
nameText.text = item.name
descText.text = item.description
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import moe.feng.danmaqua.model.BlockedUserRule
import moe.feng.danmaqua.model.SpaceInfo
import moe.feng.danmaqua.ui.BaseFragment
import moe.feng.danmaqua.ui.view.CircleImageView
import moe.feng.danmaqua.util.ext.avatarUrl

class ManageBlockedUsersFragment : BaseFragment() {

Expand Down Expand Up @@ -107,10 +108,7 @@ class ManageBlockedUsersFragment : BaseFragment() {
val avatarView = dialogView.findViewById<CircleImageView>(R.id.avatarView)
val usernameView = dialogView.findViewById<TextView>(R.id.usernameView)
val uidView = dialogView.findViewById<TextView>(R.id.uidView)
Picasso.get()
.load(info.data.face)
.placeholder(R.drawable.avatar_placeholder_empty)
.into(avatarView)
avatarView.avatarUrl = info.data.face
usernameView.text = info.data.name
uidView.text = getString(R.string.uid_text_format, info.data.uid)
AlertDialog.Builder(activity!!)
Expand Down Expand Up @@ -168,10 +166,7 @@ class ManageBlockedUsersFragment : BaseFragment() {
with(holder) {
usernameView.text = item.username
uidView.text = getString(R.string.uid_text_format, item.uid)
Picasso.get()
.load(item.face)
.placeholder(R.drawable.avatar_placeholder_empty)
.into(avatarView)
avatarView.avatarUrl = item.face
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fun Float.spToPx(context: Context): Float {
var ImageView.avatarUrl: String?
get() = throw UnsupportedOperationException("ImageView#avatarUrl can only set")
set(value) {
if (value == null) {
if (value.isNullOrEmpty()) {
setImageResource(R.drawable.avatar_placeholder_empty)
} else {
Picasso.get()
Expand Down

0 comments on commit a1066b7

Please sign in to comment.