AbstractSubscriptionViewItem.kt 2.93 KB
Newer Older
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 69
package fr.chenry.android.freshrss.components.subscriptions.adapters

import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.davidea.flexibleadapter.items.AbstractSectionableItem
import eu.davidea.flexibleadapter.items.IFlexible
import eu.davidea.viewholders.FlexibleViewHolder
import fr.chenry.android.freshrss.BR
import fr.chenry.android.freshrss.R
import fr.chenry.android.freshrss.components.subscriptions.adapters.AbstractSubscriptionViewItem.SubscriptionViewHolder
import fr.chenry.android.freshrss.databinding.FragmentSubscriptionBinding
import fr.chenry.android.freshrss.store.database.models.Subscription
import fr.chenry.android.freshrss.store.database.models.SubscriptionCategory

sealed class AbstractSubscriptionViewItem(header: SubscriptionViewHeaderItem):
    AbstractSectionableItem<SubscriptionViewHolder, SubscriptionViewHeaderItem>(header) {
    abstract val virtualSubscription: Subscription

    init {
        isDraggable = false
        isSelectable = false
        isSwipeable = false
    }

    override fun createViewHolder(
        view: View?,
        adapter: FlexibleAdapter<IFlexible<ViewHolder>>?
    ): SubscriptionViewHolder = SubscriptionViewHolder(DataBindingUtil.bind(view!!)!!, adapter)

    override fun bindViewHolder(
        adapter: FlexibleAdapter<IFlexible<ViewHolder>>?,
        holder: SubscriptionViewHolder?, position: Int,
        payloads: MutableList<Any>?
    ) = holder?.bind(virtualSubscription).let {Unit}

    override fun getLayoutRes() = R.layout.fragment_subscription

    inner class SubscriptionViewHolder(
        private val binding: FragmentSubscriptionBinding,
        adapter: FlexibleAdapter<IFlexible<ViewHolder>>?
    ): FlexibleViewHolder(binding.root, adapter) {
        fun bind(subscription: Subscription) {
            binding.setVariable(BR.subscription, subscription)
            binding.executePendingBindings()
        }
    }
}

class SubscriptionViewItem(val subscription: Subscription, header: String):
    AbstractSubscriptionViewItem(SubscriptionViewHeaderItem(header)) {
    override val virtualSubscription: Subscription get() = subscription

    override fun equals(other: Any?): Boolean =
        if(other !is SubscriptionViewItem) false else subscription == other.subscription

    override fun hashCode(): Int = subscription.hashCode()
}

class SubscriptionCategoryViewItem(val subscriptionCategory: SubscriptionCategory):
    AbstractSubscriptionViewItem(SubscriptionCategoryViewHeaderItem) {
    override val virtualSubscription: Subscription =
        Subscription(subscriptionCategory.id, subscriptionCategory.label, "", unreadCount = -1)

    override fun equals(other: Any?): Boolean =
        if(other !is SubscriptionCategoryViewItem) false else subscriptionCategory == other.subscriptionCategory

    override fun hashCode(): Int = subscriptionCategory.hashCode()
Christophe Henry's avatar
Christophe Henry committed
70
}