Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LiveItemsAdapter.kt « view « ocreader « schaal « email « java « main « src « app - github.com/schaal/ocreader.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e8dbd08938db87cebe3d56f22f7dcef033aeae33 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright © 2019. Daniel Schaal <daniel@schaal.email>
 *
 * This file is part of ocreader.
 *
 * ocreader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ocreader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
package email.schaal.ocreader.view

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.NO_ID
import email.schaal.ocreader.R
import email.schaal.ocreader.database.model.Item
import email.schaal.ocreader.databinding.ListItemBinding
import java.util.*

class LiveItemsAdapter(private var items: List<Item>?, private val clickListener: ItemViewHolder.OnClickListener) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    /**
     * Selected item ids, LinkedHashSet to preserve insertion order for getFirstSelectedItem()
     */
    private val selections: MutableSet<Int> = LinkedHashSet()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            R.id.viewtype_item -> {
                ItemViewHolder(
                    ListItemBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    ), clickListener
                )
            }
            else -> throw IllegalArgumentException("Unknown view type")
        }
    }

    override fun getItemId(position: Int): Long {
        return items?.get(position)?.id ?: NO_ID
    }

    fun toggleSelection(position: Int) {
        if (selections.contains(position)) selections.remove(position) else selections.add(position)
        notifyItemChanged(position)
    }

    fun clearSelection() {
        selections.clear()
        notifyDataSetChanged()
    }

    val selectedItemsCount: Int
        get() = selections.size

    val selectedItems: Array<Item?>
        get() {
            val itemsArray = arrayOfNulls<Item>(selections.size)
            var i = 0
            for (position in selections) {
                itemsArray[i++] = items?.get(position)
            }
            return itemsArray
        }

    val firstSelectedItem: Item?
        get() = if (selections.isEmpty())
            null
        else
            items?.get(selections.iterator().next())

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ItemViewHolder) {
            items?.get(position)?.let {
                holder.bindItem(it, position, selections.contains(position))
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return R.id.viewtype_item
    }

    override fun getItemCount(): Int {
        return items?.size ?: 0
    }

    fun updateItems(items: List<Item>?) {
        this.items = items
        notifyDataSetChanged()
    }

    fun onSaveInstanceState(bundle: Bundle) {
        bundle.putIntegerArrayList("adapter_selections", ArrayList(selections))
    }

    fun onRestoreInstanceState(bundle: Bundle) {
        bundle.getIntegerArrayList("adapter_selections")?.let {
            selections.addAll(it)
        }
    }

    init {
        setHasStableIds(true)
    }
}