package biz growapp gifteka ui contacts my delegates import android co

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package biz.growapp.gifteka.ui.contacts.my.delegates
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import biz.growapp.base.adapters.AbstractAdapterDelegate
import biz.growapp.base.adapters.pagination.PaginationAdapter
import biz.growapp.base.helpers.onClick
import biz.growapp.base.utils.SwipeLayoutManager
import biz.growapp.gifteka.R
import biz.growapp.gifteka.helpers.bind
import biz.growapp.gifteka.models.users.User
import biz.growapp.gifteka.utils.FullNameSpannable
import com.facebook.drawee.view.SimpleDraweeView
import ru.rambler.libs.swipe_layout.SwipeLayout
internal class ContactsDelegate(
context: Context,
private val adapter: PaginationAdapter<Any>,
private val callback: Callback
) : AbstractAdapterDelegate<User, Any, ContactsDelegate.Holder>() {
interface Callback {
fun onUserClick(position: Int)
fun onUserDelete(position: Int)
}
private val inflater: LayoutInflater = LayoutInflater.from(context)
private val swipeLayoutManager: SwipeLayoutManager = SwipeLayoutManager(
context.resources.getDimensionPixelSize(R.dimen.friends_action_delete)
)
override fun isForViewType(item: Any, items: MutableList<Any>, position: Int): Boolean {
return item is User
}
override fun onCreateViewHolder(parent: ViewGroup): Holder =
Holder(inflater.inflate(R.layout.item_my_contact, parent, false)).apply {
userContainer.onClick {
callback.onUserClick(adapterPosition)
}
ivDelete.onClick {
swipeLayoutManager.remove(adapter.items[adapterPosition])
callback.onUserDelete(adapterPosition)
}
}
override fun onBindViewHolder(holder: Holder, item: User) = with(holder) {
tvUserName.text = FullNameSpannable.construct(item)
sdvAvatar.setImageURI(item.photo?.middle)
swipeLayoutManager.bindSwipeLayout(swipeLayout, item)
item.relation?.friendsGroupType.bind(tvGroup)
}
class Holder(view: View) : RecyclerView.ViewHolder(view) {
val tvUserName: TextView = itemView.findViewById(R.id.tvUserName)
val sdvAvatar: SimpleDraweeView = itemView.findViewById(R.id.sdvAvatar)
val userContainer: ViewGroup = itemView.findViewById(R.id.userContainer)
val tvGroup: TextView = itemView.findViewById(R.id.tvGroup)
val ivDelete: ImageView = itemView.findViewById(R.id.ivDelete)
val swipeLayout: SwipeLayout = itemView.findViewById(R.id.swipeLayout)
}
}