AppShareAdapter.kt 2.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.share.listadapters

import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.ViewGroup
10
11
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
12
13
14
import org.mozilla.fenix.share.ShareToAppsInteractor
import org.mozilla.fenix.share.viewholders.AppViewHolder

15
16
17
/**
 * Adapter for a list of apps that can be shared to.
 */
18
class AppShareAdapter(
19
    private val interactor: ShareToAppsInteractor
20
) : ListAdapter<AndroidShareOption, AppViewHolder>(DiffCallback) {
21
22
23
24
25
26
27
28
29

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AppViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(AppViewHolder.LAYOUT_ID, parent, false)

        return AppViewHolder(view, interactor)
    }

    override fun onBindViewHolder(holder: AppViewHolder, position: Int) {
30
        holder.bind(getItem(position))
31
    }
32

33
34
35
36
37
38
39
    private object DiffCallback : DiffUtil.ItemCallback<AndroidShareOption>() {
        override fun areItemsTheSame(oldItem: AndroidShareOption, newItem: AndroidShareOption) =
            when (oldItem) {
                AndroidShareOption.Invisible -> oldItem === newItem
                is AndroidShareOption.App ->
                    newItem is AndroidShareOption.App && oldItem.packageName == newItem.packageName
            }
40

41
42
43
44
        @Suppress("DiffUtilEquals")
        override fun areContentsTheSame(oldItem: AndroidShareOption, newItem: AndroidShareOption) =
            oldItem == newItem
    }
45
46
}

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
 * Represents an app that can be shared to.
 */
sealed class AndroidShareOption {
    object Invisible : AndroidShareOption()
    /**
     * Represents an app that can be shared to.
     *
     * @property name Name of the app.
     * @property icon Icon representing the share target.
     * @property packageName Package of the app.
     * @property activityName Activity that will be shared to.
     */
    data class App(
        val name: String,
        val icon: Drawable,
        val packageName: String,
        val activityName: String
    ) : AndroidShareOption()
}