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
|
package mx.trackermap.TrackerMap.android.details.commands
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.appcompat.app.AlertDialog
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
import kotlinx.coroutines.DelicateCoroutinesApi
import mx.trackermap.TrackerMap.android.R
import mx.trackermap.TrackerMap.android.databinding.UnitDetailsCommandsBinding
import mx.trackermap.TrackerMap.android.details.UnitDetailsAdapter
import mx.trackermap.TrackerMap.android.shared.MarkerTransformations
import org.koin.androidx.viewmodel.ext.android.viewModel
import kotlin.time.ExperimentalTime
@DelicateCoroutinesApi
@ExperimentalTime
class UnitCommandsFragment: Fragment() {
private var _binding: UnitDetailsCommandsBinding? = null
private val binding get() = _binding!!
private var deviceName: String? = null
private var deviceCategory: String? = null
private val unitCommandsViewModel: UnitCommandsViewModel by viewModel()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = UnitDetailsCommandsBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.reportLoading.root.visibility = View.VISIBLE
setupEvents()
}
override fun onResume() {
super.onResume()
setupObservers()
}
override fun onStop() {
super.onStop()
removeObservers()
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
private fun setupObservers() {
unitCommandsViewModel.commands.observe(viewLifecycleOwner) { commands ->
Log.d("UnitCommandsFragment", "Device commands - $commands")
binding.reportLoading.root.visibility = View.GONE
val context = activity!!.applicationContext
val adapter = ArrayAdapter<String>(context, R.layout.simple_list_item_checked)
adapter.addAll(commands.map { it.description })
binding.commandsList.adapter = adapter
}
val id = arguments?.getInt(UnitDetailsAdapter.DEVICE_ID_ARG)
deviceName = arguments?.getString(UnitDetailsAdapter.DEVICE_NAME_ARG)
deviceCategory = arguments?.getString(UnitDetailsAdapter.DEVICE_CATEGORY_ARG)
binding.nameDetail.text = deviceName
context?.let {
val metrics = it.resources.displayMetrics
val icon = ResourcesCompat.getDrawable(
it.resources,
MarkerTransformations.categoryToResourceId(deviceCategory),
it.theme
)
icon?.setBounds(0, 0,
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40f, metrics).toInt(),
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40f, metrics).toInt()
)
binding.nameDetail.setCompoundDrawables(
icon, null, null, null
)
}
binding.nameDetail.compoundDrawablePadding =
resources.getDimensionPixelSize(R.dimen.card_icon_margin)
unitCommandsViewModel.fetchCommands(id)
}
private fun removeObservers() {
unitCommandsViewModel.commands.removeObservers(viewLifecycleOwner)
}
private fun setupEvents() {
binding.commandsList.setOnItemClickListener { _, _, index, _ ->
Log.d("UnitCommandsFragment", "Selected item at index - $index")
binding.sendCommandButton.isEnabled = true
unitCommandsViewModel.selectCommand(index)
}
binding.sendCommandButton.setOnClickListener {
activity?.let {
val builder = AlertDialog.Builder(it)
builder.apply {
setMessage(getString(R.string.send_command_confirmation_text, deviceName))
setPositiveButton(R.string.shared_cancel) { dialogInterface, _ ->
dialogInterface.dismiss()
}
setNegativeButton(R.string.shared_send) { dialogInterface, _ ->
unitCommandsViewModel.sendCommand()
dialogInterface.dismiss()
}
}
builder.create().show()
}
}
}
}
|