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
|
/**
* 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.units
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.TooltipCompat
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.commit
import kotlinx.coroutines.DelicateCoroutinesApi
import mx.trackermap.TrackerMap.android.R
import mx.trackermap.TrackerMap.android.databinding.UnitsActivityBinding
import mx.trackermap.TrackerMap.android.devices.DevicesFragment
import mx.trackermap.TrackerMap.android.map.UnitMapFragment
import mx.trackermap.TrackerMap.android.session.UserInformationActivity
import mx.trackermap.TrackerMap.android.shared.Utils
import org.koin.androidx.viewmodel.ext.android.viewModel
import kotlin.time.ExperimentalTime
@DelicateCoroutinesApi
@ExperimentalTime
class UnitsActivity : AppCompatActivity() {
private var _binding: UnitsActivityBinding? = null
private val binding get() = _binding!!
private val unitsViewModel: UnitsViewModel by viewModel()
private val mapFragment: UnitMapFragment by lazy {
UnitMapFragment()
}
private val devicesFragment: DevicesFragment by lazy {
DevicesFragment()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = UnitsActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
setupViews()
setupEvents()
}
override fun onResume() {
super.onResume()
setupObservers()
}
override fun onPause() {
super.onPause()
removeObservers()
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
private fun setupViews() {
supportFragmentManager.commit {
add(R.id.displayContainer, mapFragment)
add(R.id.displayContainer, devicesFragment)
}
TooltipCompat.setTooltipText(binding.userButton, getString(R.string.open_profile))
TooltipCompat.setTooltipText(binding.mapLayerToggle, getString(R.string.switch_layer))
}
private fun setupEvents() {
binding.userButton.setOnClickListener {
val intent = Intent(applicationContext, UserInformationActivity::class.java)
startActivity(intent)
}
binding.displayModeToggle.setOnClickListener {
unitsViewModel.toggleDisplayMode()
}
binding.mapLayerToggle.setOnClickListener {
showLayersPopUp(it)
}
binding.searchInput.doAfterTextChanged {
Log.d("UnitsActivity", "Search changed to ${it.toString()}")
if (unitsViewModel.unitsDisplayMode.value == UnitsViewModel.UnitsDisplayMode.MAP) {
unitsViewModel.setDisplayMode(UnitsViewModel.UnitsDisplayMode.LIST)
}
unitsViewModel.search(it.toString())
}
binding.searchInputLayout.setEndIconOnClickListener {
unfocusSearch(true)
}
}
private fun showLayersPopUp(view: View) {
Utils.showLayersPopUp(this, view) {
unitsViewModel.setMapLayerType(it)
}
}
private fun unfocusSearch(clearText: Boolean = false) {
val manager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
manager.hideSoftInputFromWindow(binding.searchInput.windowToken, 0)
if (clearText) {
binding.searchInput.text?.clear()
}
binding.searchInput.clearFocus()
}
private fun setupObservers() {
unitsViewModel.unitsDisplayMode.observe(this) { displayMode ->
binding.displayModeToggle.setImageResource(
when (displayMode) {
UnitsViewModel.UnitsDisplayMode.LIST -> R.drawable.icon_map
UnitsViewModel.UnitsDisplayMode.MAP -> R.drawable.icon_list
else -> R.drawable.icon_list
}
)
binding.displayModeToggle.contentDescription = getString(
when (displayMode) {
UnitsViewModel.UnitsDisplayMode.LIST -> R.string.toggle_map
UnitsViewModel.UnitsDisplayMode.MAP -> R.string.toggle_list
else -> R.string.toggle_list
}
)
TooltipCompat.setTooltipText(binding.displayModeToggle,
getString(when (displayMode) {
UnitsViewModel.UnitsDisplayMode.LIST -> R.string.toggle_map
UnitsViewModel.UnitsDisplayMode.MAP -> R.string.toggle_list
else -> R.string.toggle_list
})
)
binding.mapLayerToggle.visibility =
when (displayMode) {
UnitsViewModel.UnitsDisplayMode.LIST -> View.GONE
UnitsViewModel.UnitsDisplayMode.MAP -> View.VISIBLE
else -> View.VISIBLE
}
val newFragment =
when (displayMode) {
UnitsViewModel.UnitsDisplayMode.LIST -> devicesFragment
UnitsViewModel.UnitsDisplayMode.MAP -> mapFragment
else -> mapFragment
}
val oldFragment =
when (displayMode) {
UnitsViewModel.UnitsDisplayMode.LIST -> mapFragment
UnitsViewModel.UnitsDisplayMode.MAP -> devicesFragment
else -> devicesFragment
}
supportFragmentManager.commit {
show(newFragment)
hide(oldFragment)
}
}
unitsViewModel.selectedUnit.observe(this) {
unfocusSearch(clearText = true)
}
}
private fun removeObservers() {
unitsViewModel.units.removeObservers(this)
unitsViewModel.unitsDisplayMode.removeObservers(this)
unitsViewModel.selectedUnit.removeObservers(this)
}
override fun onBackPressed() {
if (unitsViewModel.unitsDisplayMode.value == UnitsViewModel.UnitsDisplayMode.LIST) {
unitsViewModel.setDisplayMode(UnitsViewModel.UnitsDisplayMode.MAP)
} else {
super.onBackPressed()
}
}
}
|