11package org.schabi.newpipe.settings.notifications
22
33import android.view.LayoutInflater
4- import android.view.View
54import android.view.ViewGroup
6- import android.widget.CheckedTextView
7- import androidx.recyclerview.widget.AsyncListDiffer
85import androidx.recyclerview.widget.DiffUtil
6+ import androidx.recyclerview.widget.ListAdapter
97import androidx.recyclerview.widget.RecyclerView
10- import org.schabi.newpipe.R
118import org.schabi.newpipe.database.subscription.NotificationMode
129import org.schabi.newpipe.database.subscription.SubscriptionEntity
10+ import org.schabi.newpipe.databinding.ItemNotificationConfigBinding
1311import org.schabi.newpipe.settings.notifications.NotificationModeConfigAdapter.SubscriptionHolder
1412
1513/* *
@@ -19,85 +17,46 @@ import org.schabi.newpipe.settings.notifications.NotificationModeConfigAdapter.S
1917 */
2018class NotificationModeConfigAdapter (
2119 private val listener : ModeToggleListener
22- ) : RecyclerView.Adapter<SubscriptionHolder>() {
23-
24- private val differ = AsyncListDiffer (this , DiffCallback ())
25-
26- init {
27- setHasStableIds(true )
28- }
29-
30- override fun onCreateViewHolder (viewGroup : ViewGroup , i : Int ): SubscriptionHolder {
31- val view = LayoutInflater .from(viewGroup.context)
32- .inflate(R .layout.item_notification_config, viewGroup, false )
33- return SubscriptionHolder (view, listener)
34- }
35-
36- override fun onBindViewHolder (subscriptionHolder : SubscriptionHolder , i : Int ) {
37- subscriptionHolder.bind(differ.currentList[i])
20+ ) : ListAdapter<SubscriptionItem, SubscriptionHolder>(DiffCallback ) {
21+ override fun onCreateViewHolder (parent : ViewGroup , i : Int ): SubscriptionHolder {
22+ return SubscriptionHolder (
23+ ItemNotificationConfigBinding
24+ .inflate(LayoutInflater .from(parent.context), parent, false )
25+ )
3826 }
3927
40- fun getItem (position : Int ): SubscriptionItem = differ.currentList[position]
41-
42- override fun getItemCount () = differ.currentList.size
43-
44- override fun getItemId (position : Int ): Long {
45- return differ.currentList[position].id
28+ override fun onBindViewHolder (holder : SubscriptionHolder , position : Int ) {
29+ holder.bind(currentList[position])
4630 }
4731
48- fun getCurrentList (): List <SubscriptionItem > = differ.currentList
49-
5032 fun update (newData : List <SubscriptionEntity >) {
51- differ.submitList(
52- newData.map {
53- SubscriptionItem (
54- id = it.uid,
55- title = it.name,
56- notificationMode = it.notificationMode,
57- serviceId = it.serviceId,
58- url = it.url
59- )
60- }
61- )
33+ val items = newData.map {
34+ SubscriptionItem (it.uid, it.name, it.notificationMode, it.serviceId, it.url)
35+ }
36+ submitList(items)
6237 }
6338
64- data class SubscriptionItem (
65- val id : Long ,
66- val title : String ,
67- @NotificationMode
68- val notificationMode : Int ,
69- val serviceId : Int ,
70- val url : String
71- )
72-
73- class SubscriptionHolder (
74- itemView : View ,
75- private val listener : ModeToggleListener
76- ) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
77-
78- private val checkedTextView = itemView as CheckedTextView
79-
39+ inner class SubscriptionHolder (
40+ private val itemBinding : ItemNotificationConfigBinding
41+ ) : RecyclerView.ViewHolder(itemBinding.root) {
8042 init {
81- itemView.setOnClickListener(this )
43+ itemView.setOnClickListener {
44+ val mode = if (itemBinding.root.isChecked) {
45+ NotificationMode .DISABLED
46+ } else {
47+ NotificationMode .ENABLED
48+ }
49+ listener.onModeChange(bindingAdapterPosition, mode)
50+ }
8251 }
8352
8453 fun bind (data : SubscriptionItem ) {
85- checkedTextView.text = data.title
86- checkedTextView.isChecked = data.notificationMode != NotificationMode .DISABLED
87- }
88-
89- override fun onClick (v : View ) {
90- val mode = if (checkedTextView.isChecked) {
91- NotificationMode .DISABLED
92- } else {
93- NotificationMode .ENABLED
94- }
95- listener.onModeChange(bindingAdapterPosition, mode)
54+ itemBinding.root.text = data.title
55+ itemBinding.root.isChecked = data.notificationMode != NotificationMode .DISABLED
9656 }
9757 }
9858
99- private class DiffCallback : DiffUtil .ItemCallback <SubscriptionItem >() {
100-
59+ private object DiffCallback : DiffUtil.ItemCallback<SubscriptionItem>() {
10160 override fun areItemsTheSame (oldItem : SubscriptionItem , newItem : SubscriptionItem ): Boolean {
10261 return oldItem.id == newItem.id
10362 }
@@ -107,18 +66,27 @@ class NotificationModeConfigAdapter(
10766 }
10867
10968 override fun getChangePayload (oldItem : SubscriptionItem , newItem : SubscriptionItem ): Any? {
110- if (oldItem.notificationMode != newItem.notificationMode) {
111- return newItem.notificationMode
69+ return if (oldItem.notificationMode != newItem.notificationMode) {
70+ newItem.notificationMode
11271 } else {
113- return super .getChangePayload(oldItem, newItem)
72+ super .getChangePayload(oldItem, newItem)
11473 }
11574 }
11675 }
11776
118- interface ModeToggleListener {
77+ fun interface ModeToggleListener {
11978 /* *
12079 * Triggered when the UI representation of a notification mode is changed.
12180 */
12281 fun onModeChange (position : Int , @NotificationMode mode : Int )
12382 }
12483}
84+
85+ data class SubscriptionItem (
86+ val id : Long ,
87+ val title : String ,
88+ @NotificationMode
89+ val notificationMode : Int ,
90+ val serviceId : Int ,
91+ val url : String
92+ )
0 commit comments