Bimba.git

ref: f7001336c32940112f2229735bd2b5135826b8ba

app/src/main/java/xyz/apiote/bimba/czwek/journeys/JourneysActivity.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
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.journeys

import android.content.Context
import android.content.Intent
import android.content.res.Configuration.UI_MODE_NIGHT_MASK
import android.content.res.Configuration.UI_MODE_NIGHT_UNDEFINED
import android.content.res.Configuration.UI_MODE_NIGHT_YES
import android.graphics.DashPathEffect
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import org.osmdroid.tileprovider.tilesource.TileSourceFactory
import org.osmdroid.util.BoundingBox
import org.osmdroid.util.GeoPoint
import org.osmdroid.views.CustomZoomButtonsController
import org.osmdroid.views.overlay.Marker
import org.osmdroid.views.overlay.Polyline
import org.osmdroid.views.overlay.TilesOverlay
import org.osmdroid.views.overlay.gestures.RotationGestureOverlay
import xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.databinding.ActivityJourneysBinding
import xyz.apiote.bimba.czwek.dpToPixelI
import xyz.apiote.bimba.czwek.repo.JourneyParams
import xyz.apiote.bimba.czwek.repo.Place
import kotlin.math.max
import kotlin.math.min

class JourneysActivity : AppCompatActivity() {
	private lateinit var binding: ActivityJourneysBinding

	companion object {
		const val ORIGIN_PARAM = "origin"
		const val DESTINATION_PARAM = "destination"
		const val PARAMS_PARAM = "params"

		fun getIntent(
			context: Context,
			origin: Place,
			destination: Place,
			params: JourneyParams
		) = Intent(context, JourneysActivity::class.java).apply {
			putExtra(ORIGIN_PARAM, origin)
			putExtra(DESTINATION_PARAM, destination)
			putExtra(PARAMS_PARAM, params)
		}
	}

	private fun getOrigin(): Place = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) {
		intent.getParcelableExtra(ORIGIN_PARAM, Place::class.java)
	} else {
		@Suppress("DEPRECATION")
		intent.getParcelableExtra(ORIGIN_PARAM)
	} ?: throw Exception("Origin not given")

	private fun getDestination(): Place = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) {
		intent.getParcelableExtra(DESTINATION_PARAM, Place::class.java)
	} else {
		@Suppress("DEPRECATION")
		intent.getParcelableExtra(DESTINATION_PARAM)
	} ?: throw Exception("Destination not given")

	private fun getJourneyParams(): JourneyParams = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) {
		intent.getParcelableExtra(PARAMS_PARAM, JourneyParams::class.java)
	} else {
		@Suppress("DEPRECATION")
		intent.getParcelableExtra(PARAMS_PARAM)
	} ?: throw Exception("Params not given")

	override fun onCreate(savedInstanceState: Bundle?) {
		enableEdgeToEdge()
		super.onCreate(savedInstanceState)
		binding = ActivityJourneysBinding.inflate(layoutInflater)
		setContentView(binding.root)
		val journeysViewModel =
			ViewModelProvider(this)[JourneysViewModel::class.java]

		// TODO check upside-down
		// TODO for horizontal make side sheet
		ViewCompat.setOnApplyWindowInsetsListener(binding.journeys) { v, windowInsets ->
			val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
			v.updatePadding(bottom = insets.bottom + dpToPixelI(16f))
			WindowInsetsCompat.CONSUMED
		}

		binding.map.setTileSource(TileSourceFactory.MAPNIK)
		if (((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.maxZoomLevel = 21.5
		binding.map.minZoomLevel = 5.5
		binding.map.setMultiTouchControls(true)
		binding.map.overlays.add(RotationGestureOverlay(binding.map).apply { isEnabled = true })

		binding.journeys.layoutManager = LinearLayoutManager(this)

		val origin = getOrigin()
		val destination = getDestination()
		val params = getJourneyParams()

		journeysViewModel.journeys.observe(this) {
			zoomMap(100)
			binding.journeysProgress.visibility = View.GONE
			if (it.isEmpty()) {
				binding.emptyText.visibility = View.VISIBLE
				binding.emptyImage.visibility = View.VISIBLE
			} else {
				binding.journeys.visibility = View.VISIBLE
				binding.journeys.adapter = JourneysAdapter(layoutInflater, this, it) {
					binding.map.overlays.removeAll { true }

					// TODO show depending on open/close card
					// TODO close other cards

					// TODO show without journey
					val originMarker = Marker(binding.map).apply {
						position = GeoPoint(it.legs[0].origin.latitude, it.legs[0].origin.longitude)
						setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM)
						icon = AppCompatResources.getDrawable(
							this@JourneysActivity,
							R.drawable.pin // TODO R.drawable.legOrigin
						) // TODO colour
						setOnMarkerClickListener { marker, map ->
							true
						}
					}
					binding.map.overlays.add(originMarker)

					// TODO show without journey
					val destinationMarker = Marker(binding.map).apply {
						position = GeoPoint(it.legs.last().destination.latitude, it.legs.last().destination.longitude)
						setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM)
						icon = AppCompatResources.getDrawable(
							this@JourneysActivity,
							R.drawable.pin // TODO R.drawable.legDestination
						) // TODO colour
						setOnMarkerClickListener { marker, map ->
							true
						}
					}
					binding.map.overlays.add(destinationMarker)

					it.legs.forEachIndexed { i, leg ->
						val shapePoints = leg.shape.map {
							GeoPoint(it.latitude, it.longitude)
						}
						val shape = Polyline()
						val paint = shape.outlinePaint
						paint.color = leg.start.vehicle.Line.colour.toInt() // TODO contrast
						if (leg.start.vehicle.Line.kind.isActive()) {
							paint.setPathEffect(DashPathEffect(floatArrayOf(10f, 10f), 0f))
						}
						shape.setPoints(shapePoints)
						shape.isVisible = true
						binding.map.overlays.add(shape)
					}
					binding.map.invalidate()
					// todo move map accordingly
				}
			}
		}
		journeysViewModel.getJourneys(this, origin, destination, params)
	}

	override fun onStart() {
		super.onStart()
		zoomMap()
	}

	fun zoomMap(margin: Int = 0, box: BoundingBox? = null) {
		val origin = getOrigin()
		val destination = getDestination()
		// TODO offset bottom sheet
		val bb = box ?: BoundingBox(
			max(origin.latitude, destination.latitude),
			max(origin.longitude, destination.longitude),
			min(origin.latitude, destination.latitude),
			min(origin.longitude, destination.longitude),
		)
		binding.map.zoomToBoundingBox(bb, false, margin)
	}
}