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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/**
* TrackerMap
* Copyright (C) 2021-2022 Iván Ávalos <avalos@disroot.org>, Henoch Ojeda <imhenoch@protonmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package mx.trackermap.TrackerMap.android.shared
import android.content.Context
import android.graphics.Color
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.Button
import android.widget.GridLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import com.zerobranch.layout.SwipeLayout
import kotlinx.serialization.json.longOrNull
import mx.trackermap.TrackerMap.android.R
import mx.trackermap.TrackerMap.client.models.UnitInformation
import mx.trackermap.TrackerMap.utils.Formatter
import mx.trackermap.TrackerMap.utils.SpeedUnit
typealias ActionCallback = (unit: UnitInformation, action: UnitRenderData.Action) -> Unit
class UnitRenderData {
data class UnitRenderViewHolder(
val unitName: TextView,
val unitIcon: ImageView? = null,
val statusIcon: ImageView,
val engineStopIcon: ImageView,
val gridLayout: GridLayout,
val detailsButton: Button? = null,
val reportsButton: Button? = null,
val commandsButton: Button? = null,
val unitCard: CardView? = null,
val swipeLayout: SwipeLayout? = null
)
enum class Action {
CLICK, DETAILS, REPORTS, COMMANDS
}
companion object {
fun render(
viewHolder: UnitRenderViewHolder,
context: Context,
unit: UnitInformation,
actionCallback: ActionCallback?
) {
viewHolder.apply {
val details: MutableList<Triple<Int, String, String>> = mutableListOf()
/* Device name */
unitName.text = unit.device.name
/* Device icon */
unitIcon?.setImageResource(
MarkerTransformations.categoryToResourceId(unit.device.category)
)
unitIcon?.contentDescription = context.getString(
MarkerTransformations.categoryToStringId(unit.device.category)
)
/* Contact */
unit.device.contact?.let { contact ->
if (contact.isNotEmpty()) {
details.add(
Triple(
R.drawable.device_contact,
contact,
context.getString(R.string.unit_driver_name)
)
)
}
}
/* Status icon */
when (unit.getStatus()) {
UnitInformation.Status.ONLINE -> {
statusIcon.setColorFilter(
ContextCompat.getColor(context, R.color.colorOnline)
)
}
UnitInformation.Status.OFFLINE -> {
statusIcon.setColorFilter(
ContextCompat.getColor(context, R.color.colorOffline)
)
}
else -> {
statusIcon.setColorFilter(Color.GRAY)
}
}
/* Engine stop */
when(unit.getEngineStop()) {
UnitInformation.EngineStop.ON -> {
engineStopIcon.visibility = View.VISIBLE
engineStopIcon.setImageResource(R.drawable.device_unlocked)
engineStopIcon.contentDescription = context.getString(R.string.unit_lock_on)
}
UnitInformation.EngineStop.OFF -> {
engineStopIcon.visibility = View.VISIBLE
engineStopIcon.setImageResource(R.drawable.device_locked)
engineStopIcon.contentDescription = context.getString(R.string.unit_lock_off)
}
UnitInformation.EngineStop.UNKNOWN -> {
engineStopIcon.visibility = View.GONE
}
}
unit.position?.let { position ->
/* Speed */
position.speed?.let { speed ->
details.add(
Triple(
R.drawable.position_speed,
Formatter.formatSpeed(speed, SpeedUnit.KMH),
context.getString(R.string.unit_speed)
)
)
}
/* Address */
position.address?.let { address ->
details.add(
Triple(
R.drawable.position_address,
address,
context.getString(R.string.unit_last_address)
)
)
}
/* Hourmeter */
unit.getHourmeter()?.let {
if (it > 0) {
details.add(
Triple(
R.drawable.position_hourmeter,
Formatter.formatHours(it),
context.getString(R.string.unit_hourmeter)
)
)
}
}
/* Date time */
position.fixTime?.let { fixTime ->
details.add(
Triple(
R.drawable.position_datetime,
Formatter.formatDate(fixTime),
context.getString(R.string.unit_last_date)
)
)
}
}
gridLayout.removeAllViewsInLayout()
val metrics = context.resources.displayMetrics
details.forEachIndexed { i, detail ->
/* Property icon */
val imageView = ImageView(gridLayout.context)
imageView.setImageResource(detail.first)
imageView.contentDescription = detail.third
imageView.setColorFilter(
ContextCompat.getColor(
context,
androidx.appcompat.R.color.secondary_text_default_material_light
)
)
val imageLayout = GridLayout.LayoutParams()
imageLayout.width = GridLayout.LayoutParams.WRAP_CONTENT
imageLayout.height = GridLayout.LayoutParams.WRAP_CONTENT
imageLayout.marginEnd =
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6f, metrics).toInt()
imageLayout.rowSpec = GridLayout.spec(i)
imageLayout.columnSpec = GridLayout.spec(0)
imageView.layoutParams = imageLayout
/* Property text */
val textView = TextView(gridLayout.context)
textView.text = detail.second
val textLayout = GridLayout.LayoutParams()
textLayout.width = 0
textLayout.height = GridLayout.LayoutParams.MATCH_PARENT
textLayout.rowSpec = GridLayout.spec(i)
textLayout.columnSpec = GridLayout.spec(1, 1, 5f)
textView.layoutParams = textLayout
gridLayout.addView(imageView)
gridLayout.addView(textView)
imageLayout.setGravity(Gravity.CENTER)
}
actionCallback?.let { callback ->
unitCard?.setOnClickListener {
swipeLayout?.close()
callback(unit, Action.CLICK)
}
detailsButton?.setOnClickListener {
swipeLayout?.close()
callback(unit, Action.DETAILS)
}
reportsButton?.setOnClickListener {
swipeLayout?.close()
callback(unit, Action.REPORTS)
}
commandsButton?.setOnClickListener {
swipeLayout?.close()
callback(unit, Action.COMMANDS)
}
}
}
}
}
}
|