blob: 4aa210d8634bfa480aab667a8b71508a92896207 (
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
|
package mx.trackermap.TrackerMap.android.devices
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import mx.trackermap.TrackerMap.android.R
import mx.trackermap.TrackerMap.android.databinding.UnitItemBinding
import mx.trackermap.TrackerMap.client.models.UnitInformation
enum class Action {
DETAILS, REPORTS, COMMANDS
}
typealias ActionCallback = (unit: UnitInformation, action: Action) -> Unit
class DevicesAdapter(
private val units: List<UnitInformation>,
private val actionCallback: ActionCallback?
) : RecyclerView.Adapter<DevicesAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = UnitItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.toggleOptions(false)
val unit = units[position]
holder.binding.apply {
unitName.text = unit.device.name
driverName.text = unit.device.contact
unitSpeed.text = "${unit.position?.speed ?: "--"} Km/h"
lastAddress.text = unit.position?.address ?: "Unknown location"
lastDate.text = "yyyy/mm/dd, hh:mm"
actionCallback?.let { callback ->
detailsButton.setOnClickListener { callback(unit, Action.DETAILS) }
reportsButton.setOnClickListener { callback(unit, Action.REPORTS) }
commandsButton.setOnClickListener { callback(unit, Action.COMMANDS) }
}
}
}
override fun getItemCount(): Int = units.size
inner class ViewHolder(val binding: UnitItemBinding) : RecyclerView.ViewHolder(binding.root) {
init {
binding.unitCard.setOnClickListener {
toggleOptions(binding.itemOptions.visibility == View.GONE)
}
}
fun toggleOptions(shouldExpand: Boolean) {
val context = binding.root.context
binding.unitCard.setCardBackgroundColor(
context
.resources
.getColor(if (shouldExpand) R.color.background else R.color.darkBackground)
)
binding.itemOptions.visibility = if (shouldExpand) View.VISIBLE else View.GONE
binding.unitCard.cardElevation = if (shouldExpand) 5.0F else 0.0F
}
}
}
|