Bimba.git

ref: 8693f82819b7dc79e894edcff7d67d69c7f68b2b

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
// 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.os.Build
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import org.osmdroid.util.BoundingBox
import xyz.apiote.bimba.czwek.databinding.ActivityJourneysBinding
import xyz.apiote.bimba.czwek.repo.Place
import kotlin.math.max
import kotlin.math.min

class JourneysActivity : AppCompatActivity() {
	companion object {
		const val ORIGIN_PARAM = "origin"
		const val DESTINATION_PARAM = "destination"

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

	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 lateinit var binding: ActivityJourneysBinding
	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 insets
		ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
			val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
			v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
			insets
		}

		val origin = getOrigin()
		val destination = getDestination()
		// FIXME
		binding.map.zoomToBoundingBox(BoundingBox(
			max(origin.latitude, destination.latitude),
			max(origin.longitude, destination.longitude),
			min(origin.latitude, destination.latitude),
			min(origin.longitude, destination.longitude),
		), false)

		binding.journeys.layoutManager = LinearLayoutManager(this)

		journeysViewModel.journeys.observe(this) {
			binding.journeys.adapter = JourneysAdapter(layoutInflater, this, it) {
				// todo move map accordingly
				// todo show shape on map
			}
		}
		journeysViewModel.getJourneys(this, origin, destination)
	}
}