ref: 28e71396b2ed6cee7c88b62de6788b39344689ff
app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/map/MapFragment.kt
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 |
package ml.adamsprogs.bimba.dashboard.ui.map import android.annotation.SuppressLint import android.content.Context import android.content.Context.MODE_PRIVATE import android.content.SharedPreferences import android.content.res.Configuration.* import android.graphics.Bitmap import android.net.ConnectivityManager import android.os.Bundle import android.os.Handler import android.os.Looper import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.appcompat.content.res.AppCompatResources import androidx.core.content.edit import androidx.core.graphics.drawable.toBitmap import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider import com.google.android.material.snackbar.Snackbar import ml.adamsprogs.bimba.R import ml.adamsprogs.bimba.api.Position import ml.adamsprogs.bimba.api.Server import ml.adamsprogs.bimba.dashboard.MainActivity import ml.adamsprogs.bimba.databinding.FragmentMapBinding import ml.adamsprogs.bimba.dpToPixelI import org.osmdroid.config.Configuration import org.osmdroid.events.MapListener import org.osmdroid.events.ScrollEvent import org.osmdroid.events.ZoomEvent import org.osmdroid.tileprovider.tilesource.TileSourceFactory import org.osmdroid.util.GeoPoint import org.osmdroid.views.CustomZoomButtonsController import org.osmdroid.views.overlay.Marker import org.osmdroid.views.overlay.TilesOverlay import org.osmdroid.views.overlay.gestures.RotationGestureOverlay import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay // todo empty state on no network class MapFragment : Fragment() { private var maybeBinding: FragmentMapBinding? = null private val binding get() = maybeBinding!! private lateinit var locationOverlay: MyLocationNewOverlay private lateinit var mapViewModel: MapViewModel private val handler = Handler(Looper.getMainLooper()) private var workRunnable = Runnable {} private var snack: Snackbar? = null @SuppressLint("ClickableViewAccessibility") override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { mapViewModel = ViewModelProvider(this)[MapViewModel::class.java] observeLocatables() maybeBinding = FragmentMapBinding.inflate(inflater, container, false) val root: View = binding.root binding.map.setTileSource(TileSourceFactory.MAPNIK) if (((context?.resources?.configuration?.uiMode ?: UI_MODE_NIGHT_UNDEFINED) and UI_MODE_NIGHT_MASK) == UI_MODE_NIGHT_YES ) { binding.map.overlayManager.tilesOverlay.setColorFilter(TilesOverlay.INVERT_COLORS) } binding.map.zoomController.setVisibility(CustomZoomButtonsController.Visibility.NEVER) binding.map.setMultiTouchControls(true) binding.map.overlays.add(RotationGestureOverlay(binding.map).apply { isEnabled = true }) locationOverlay = MyLocationNewOverlay(GpsMyLocationProvider(context), binding.map) context?.let { centreMap(it.getSharedPreferences("shp", MODE_PRIVATE)) locationOverlay.setDirectionIcon( AppCompatResources.getDrawable(it, R.drawable.navigation_arrow)?.mutate() ?.toBitmap(dpToPixelI(36f), dpToPixelI(36f), Bitmap.Config.ARGB_8888) ) locationOverlay.setDirectionAnchor(.5f, .5f) locationOverlay.setPersonIcon( AppCompatResources.getDrawable(it, R.drawable.navigation_circle)?.mutate() ?.toBitmap(dpToPixelI(24f), dpToPixelI(24f), Bitmap.Config.ARGB_8888) ) locationOverlay.setPersonAnchor(.5f, .5f) } binding.floatingActionButton.setOnClickListener { (context as MainActivity).onGpsClicked(this) } binding.map.addMapListener(object : MapListener { override fun onScroll(event: ScrollEvent?): Boolean { return onMapMove() } override fun onZoom(event: ZoomEvent?): Boolean { return onMapMove() } }) binding.map.setOnTouchListener { _, _ -> binding.floatingActionButton.show() false } return root } private fun onMapMove(): Boolean { snack?.dismiss() return delayGetLocatables() } private fun delayGetLocatables(delay: Long = 1000): Boolean { handler.removeCallbacks(workRunnable) workRunnable = Runnable { getLocatables() } handler.postDelayed(workRunnable, delay) return true } private fun observeLocatables() { mapViewModel.items.observe(viewLifecycleOwner) { binding.map.overlays.removeAll { marker -> marker is Marker } it.forEach { locatable -> val marker = Marker(binding.map) marker.position = GeoPoint(locatable.location().latitude, locatable.location().longitude) marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER) marker.icon = context?.let { ctx -> locatable.icon(ctx, 2f) } context?.let { ctx -> marker.setOnMarkerClickListener { _, _ -> MapBottomSheet(locatable).apply { (ctx as MainActivity?)?.supportFragmentManager?.let { fm -> show(fm, MapBottomSheet.TAG) } } true } } binding.map.overlays.add(marker) } binding.map.invalidate() } } fun showLocation() { snack = Snackbar.make(binding.root, getString(R.string.waiting_position), Snackbar.LENGTH_INDEFINITE) snack!!.show() binding.floatingActionButton.hide() binding.map.overlays.removeAll { it is MyLocationNewOverlay } locationOverlay.enableFollowLocation() binding.map.overlays.add(locationOverlay) locationOverlay.runOnFirstFix { snack?.dismiss() } } private fun getLocatables() { maybeBinding?.let { binding -> val (bl, tr) = binding.map.boundingBox.let { Pair( Position(it.latSouth, it.lonWest), Position(it.latNorth, it.lonEast) ) } context?.let { val cm = it.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager mapViewModel.getLocatablesIn( cm, Server.get(it), bl, tr ) } delayGetLocatables(30000) } } private fun centreMap(preferences: SharedPreferences) { maybeBinding?.map?.controller?.apply { setZoom(preferences.getFloat("mapZoom", 17.0f).toDouble()) val startPoint = GeoPoint( preferences.getFloat("mapCentreLat", 52.39511f).toDouble(), preferences.getFloat("mapCentreLon", 16.89506f).toDouble() ) setCenter(startPoint) } } override fun onResume() { super.onResume() binding.map.onResume() locationOverlay.enableMyLocation() context?.let { ctx -> ctx.getSharedPreferences("shp", MODE_PRIVATE).let { Configuration.getInstance() .load(ctx, it) centreMap(it) } } } override fun onPause() { super.onPause() binding.map.onPause() locationOverlay.disableMyLocation() val centre = binding.map.mapCenter context?.let { ctx -> ctx.getSharedPreferences("shp", MODE_PRIVATE).edit { this.putFloat("mapCentreLat", centre.latitude.toFloat()) this.putFloat("mapCentreLon", centre.longitude.toFloat()) this.putFloat("mapZoom", binding.map.zoomLevelDouble.toFloat()) } } handler.removeCallbacks(workRunnable) } override fun onDestroyView() { super.onDestroyView() maybeBinding = null } } |