ScannerListAdapter.kt 2.19 KB
Newer Older
1
2
3
/*
 * Copyright by the original author or authors.
 *
4
5
6
7
8
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
10
 *
11
12
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
13
 *
14
15
16
17
18
19
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
21
22
 */

23
package de.ccc.events.badge.card10.scanner
24
25
26
27
28

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import java.util.*
Anon's avatar
Anon committed
29
import kotlin.collections.HashSet
30

31
class ScannerListAdapter(val clickListener: (Device) -> Unit) : RecyclerView.Adapter<DeviceViewHolder>() {
32
33

    private val list = LinkedList<Device>()
Anon's avatar
Anon committed
34
    private val foundDevices = mutableSetOf<String>()
35
36
37
38
39
40
41
42

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DeviceViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return DeviceViewHolder(inflater, parent)
    }

    override fun onBindViewHolder(holder: DeviceViewHolder, position: Int) {
        val device: Device = list[position]
43
        holder.bind(device, clickListener)
44
45
46
47
    }

    override fun getItemCount(): Int = list.size

48
    fun put(device: Device) {
Anon's avatar
Anon committed
49
50
51
52
53
54
        if (foundDevices.contains(device.btMac)) {
            return
        } else {
            foundDevices.add(device.btMac)
        }

55
56
57
        list.remove(device)
        list.add(device)
        notifyDataSetChanged()
58
59
    }
}