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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/**
* 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.map
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.text.HtmlCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import mx.trackermap.TrackerMap.android.R
import mx.trackermap.TrackerMap.android.databinding.FragmentMapWrapperBinding
import mx.trackermap.TrackerMap.android.shared.MarkerTransformations
import mx.trackermap.TrackerMap.android.shared.Utils
import mx.trackermap.TrackerMap.client.models.*
class MapWrapperFragment: Fragment() {
private var _binding: FragmentMapWrapperBinding? = null
private val binding get() = _binding!!
private val mapFragment = MapFragment()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentMapWrapperBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupEvents()
setupViews()
}
override fun onResume() {
super.onResume()
initializeMap()
}
override fun onPause() {
super.onPause()
removeMap()
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
private fun initializeMap() {
Log.d("MapWrapperFragment", "initializeMap()")
childFragmentManager.commit {
replace(R.id.mapContainer, mapFragment)
}
val layer = MapLayer.defaultLayer
binding.attributionText.text = HtmlCompat.fromHtml(layer.attribution, 0)
if (mapFragment.hasStarted) {
binding.mapLoading.root.visibility = View.GONE
} else {
binding.mapLoading.root.visibility = View.VISIBLE
mapFragment.setupCallbacks.add {
binding.mapLoading.root.visibility = View.GONE
}
}
}
private fun removeMap() {
Log.d("MapWrapperFragment", "removeMap()")
childFragmentManager.commit {
remove(mapFragment)
}
}
private fun setupEvents() {
binding.zoomInButton.setOnClickListener {
if (mapFragment.hasStarted) {
mapFragment.zoomIn()
} else {
mapFragment.setupCallbacks.add {
binding.zoomInButton.performClick()
}
}
}
binding.zoomOutButton.setOnClickListener {
if (mapFragment.hasStarted) {
mapFragment.zoomOut()
} else {
mapFragment.setupCallbacks.add {
binding.zoomOutButton.performClick()
}
}
}
binding.mapLayerToggle.setOnClickListener {
context?.let { context ->
if (mapFragment.hasStarted) {
Utils.showLayersPopUp(context, it) { type ->
mapFragment.updateLayer(type)
MapLayer.layers[type]?.attribution?.let { attribution ->
binding.attributionText.text =
HtmlCompat.fromHtml(attribution, 0)
}
}
} else {
binding.mapLayerToggle.performClick()
}
}
}
}
private fun setupViews() {
val overlayMarginTop = arguments?.getInt(OVERLAY_MARGIN_TOP_ARG)
val showLayerToggle = arguments?.getBoolean(SHOW_LAYER_TOGGLE_ARG)
overlayMarginTop?.let { top ->
binding.overlay.setPadding(0, top, 0, 0)
}
showLayerToggle?.let { show ->
binding.mapLayerToggle.visibility = if (show) View.VISIBLE else View.GONE
}
}
fun setMarkerCallback(callback: MarkerCallback) {
mapFragment.markerCallback = callback
}
fun focusOn(
latitude: Double,
longitude: Double,
height: Double = 0.0000264,
animated: Boolean = true
) {
Log.d("MapWrapperFragment", "Focusing on $latitude, $longitude")
if (mapFragment.hasStarted) {
mapFragment.focusOn(latitude, longitude, height, animated)
} else {
mapFragment.setupCallbacks.add {
focusOn(latitude, longitude, height, animated)
}
}
}
fun display(positions: Array<Position>, isReport: Boolean, center: Boolean) {
if (mapFragment.hasStarted) {
Log.d("MapWrapperFragment", "Displaying positions: $positions")
mapFragment.display(
positions.mapNotNull(Marker::fromPosition).toTypedArray(),
isReport = isReport,
center = center
)
} else {
mapFragment.setupCallbacks.add {
display(positions, isReport, center)
}
}
}
fun display(units: Array<UnitInformation>, isReport: Boolean, center: Boolean) {
if (mapFragment.hasStarted) {
Log.d("MapWrapperFragment", "Displaying units: $units")
mapFragment.display(
units.mapNotNull(Marker::fromUnit).toTypedArray(),
isReport = isReport,
center = center
)
} else {
mapFragment.setupCallbacks.add {
display(units, isReport, center)
}
}
}
fun display(stops: Array<Stop>) {
if (mapFragment.hasStarted) {
Log.d("MapWrapperFragment", "Displaying stops: $stops")
mapFragment.display(
stops.mapNotNull(Marker::fromStop).toTypedArray(),
isReport = true
)
} else {
mapFragment.setupCallbacks.add {
display(stops)
}
}
}
fun display(geofences: Array<Geofence>) {
if (mapFragment.hasStarted) {
Log.d("MapWrapperFragment", "Displaying geofences: $geofences")
mapFragment.displayGeofences(geofences)
} else {
mapFragment.setupCallbacks.add {
display(geofences)
}
}
}
fun updateLayer(layer: MapLayer.Type) {
if (mapFragment.hasStarted) {
mapFragment.updateLayer(layer)
MapLayer.layers[layer]?.attribution?.let { attribution ->
binding.attributionText.text =
HtmlCompat.fromHtml(attribution, 0)
}
} else {
mapFragment.setupCallbacks.add {
updateLayer(layer)
}
}
}
companion object {
const val SHOW_LAYER_TOGGLE_ARG = "show_layer_toggle"
const val OVERLAY_MARGIN_TOP_ARG = "overlay_margin_top"
fun newInstance(
showLayerToggle: Boolean = false,
overlayMarginTop: Int = 0
): MapWrapperFragment {
val args = Bundle()
args.putBoolean(SHOW_LAYER_TOGGLE_ARG, showLayerToggle)
args.putInt(OVERLAY_MARGIN_TOP_ARG, overlayMarginTop)
val fragment = MapWrapperFragment()
fragment.arguments = args
return fragment
}
}
}
|