-
Notifications
You must be signed in to change notification settings - Fork 306
/
Copy pathSubscriptionDurationAdapter.kt
68 lines (59 loc) · 2.29 KB
/
SubscriptionDurationAdapter.kt
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
68
package com.automattic.simplenote.billing
import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.automattic.simplenote.R
import com.automattic.simplenote.viewmodels.IapViewModel
class SubscriptionDurationAdapter :
ListAdapter<IapViewModel.PlansListItem, SubscriptionDurationAdapter.PlanListItemViewHolder>(
SubscriptionOffersDiffCallback
) {
override fun onBindViewHolder(
holder: PlanListItemViewHolder,
position: Int
) {
val uiState = getItem(position)
holder.onBind(uiState)
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): PlanListItemViewHolder = PlanListItemViewHolder(parent)
class PlanListItemViewHolder(
internal val parent: ViewGroup,
) : RecyclerView.ViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.subscription_duration_list_row, parent, false)
) {
private val planName = itemView.findViewById<TextView>(R.id.plan_name)
private val planPrice = itemView.findViewById<TextView>(R.id.plan_price)
private val container = itemView.findViewById<View>(R.id.container)
fun onBind(uiState: IapViewModel.PlansListItem) {
planName.setText(uiState.period)
planPrice.text = uiState.price
container.setOnClickListener {
uiState.onTapListener.invoke(uiState.offerId, uiState.tracker)
}
}
}
object SubscriptionOffersDiffCallback :
DiffUtil.ItemCallback<IapViewModel.PlansListItem>() {
override fun areItemsTheSame(
oldItem: IapViewModel.PlansListItem,
newItem: IapViewModel.PlansListItem
): Boolean {
return oldItem.offerId == newItem.offerId
}
@SuppressLint("DiffUtilEquals")
override fun areContentsTheSame(
oldItem: IapViewModel.PlansListItem,
newItem: IapViewModel.PlansListItem
): Boolean {
return oldItem.period == newItem.period && oldItem.price == newItem.price
}
}
}