Bimba.git

ref: 2dc257c17c5088dd00af6ab6c549b76a0b8bb617

app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/map/MapViewModel.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
package ml.adamsprogs.bimba.dashboard.ui.map

import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import ml.adamsprogs.bimba.R
import ml.adamsprogs.bimba.api.*
import ml.adamsprogs.bimba.departures.DeparturesActivity
import java.io.InputStream

class MapViewModel : ViewModel() {

	private val _items = MutableLiveData<List<Locatable>>()
	val items: MutableLiveData<List<Locatable>> = _items

	fun getLocatablesIn(cm: ConnectivityManager, server: Server, bl: Position, tr: Position) {
		viewModelScope.launch {
			val locatablesResult = ml.adamsprogs.bimba.api.getLocatablesIn(cm, server, bl, tr)
			val response = if (locatablesResult.stream != null) {
				unmarshallLocatablesResponse(locatablesResult.stream)
			} else {
				null
			}
			if (locatablesResult.error != null) {
				Log.e("Results.location", "$locatablesResult")
				Log.e("Results.location", "$response")
				//todo showError(itemsResult.error)
			} else {
				_items.value = (response as LocatablesSuccess).locatables
			}
		}
	}

	private suspend fun unmarshallLocatablesResponse(stream: InputStream): LocatablesResponse {
		return withContext(Dispatchers.IO) {
			LocatablesResponse.unmarshal(stream)
		}
	}
}

class MapBottomSheet(private val locatable: Locatable) : BottomSheetDialogFragment() {
	companion object {
		const val TAG = "MapBottomSheet"
	}

	override fun onCreateView(
		inflater: LayoutInflater,
		container: ViewGroup?,
		savedInstanceState: Bundle?
	): View {
		val content = inflater.inflate(R.layout.map_bottom_sheet, container, false)
		content.apply {
			when (locatable) {
				is Vehicle -> {
					findViewById<TextView>(R.id.title).apply {
						text = "${locatable.Line.name} » ${locatable.Headsign}"
						contentDescription = "${locatable.Line.name} towards ${locatable.Headsign}"
					}
					findViewById<TextView>(R.id.change_options).visibility = View.GONE
					findViewById<Button>(R.id.departures_button).visibility = View.GONE
					findViewById<Button>(R.id.navigation_button).visibility = View.GONE
					findViewById<TextView>(R.id.speed_text).apply {
						// todo units
						val speed = locatable.Speed * 1.703
						text = "%.3f Vl".format(speed)
					}
					findViewById<TextView>(R.id.congestion_text).apply {
						text = when (locatable.CongestionLevel.toUInt()) {
							0u -> "unknown"
							1u -> "smooth traffic"
							2u -> "stop and go"
							3u -> "congestion"
							4u -> "severe jams"
							else -> TODO("throw invalid congestion")
						}
					}
					findViewById<TextView>(R.id.occupancy_text).apply {
						text = when (locatable.OccupancyStatus.toUInt()) {
							0u -> "unknown"
							1u -> "empty"
							2u -> "many seats"
							3u -> "few seats"
							4u -> "standing only"
							5u -> "crowded"
							6u -> "full"
							7u -> "won’t accept passengers"  // todo shorten
							else -> TODO("throw invalid congestion")
						}
					}

					findViewById<ImageView>(R.id.ac).visibility =
						if (locatable.getCapability(Vehicle.Capability.AC)) {
							View.VISIBLE
						} else {
							View.GONE
						}
					findViewById<ImageView>(R.id.bike).visibility =
						if (locatable.getCapability(Vehicle.Capability.BIKE)) {
							View.VISIBLE
						} else {
							View.GONE
						}
					findViewById<ImageView>(R.id.voice).visibility =
						if (locatable.getCapability(Vehicle.Capability.VOICE)) {
							View.VISIBLE
						} else {
							View.GONE
						}
					findViewById<ImageView>(R.id.ticket).visibility =
						if (locatable.let {
								it.getCapability(Vehicle.Capability.TICKET_DRIVER) || it.getCapability(Vehicle.Capability.TICKET_MACHINE)
							}) {
							View.VISIBLE
						} else {
							View.GONE
						}
					findViewById<ImageView>(R.id.usb).visibility =
						if (locatable.getCapability(Vehicle.Capability.USB_CHARGING)) {
							View.VISIBLE
						} else {
							View.GONE
						}
				}
				is Stop -> {
					findViewById<TextView>(R.id.title).text = "${locatable.name} [${locatable.code}]"
					findViewById<Button>(R.id.departures_button).setOnClickListener {
						val intent = Intent(context, DeparturesActivity::class.java).apply {
							putExtra("code", locatable.code)
							putExtra("name", locatable.name)
						}
						startActivity(intent)
					}
					findViewById<Button>(R.id.navigation_button).setOnClickListener {
						try {
							startActivity(
								Intent(
									Intent.ACTION_VIEW,
									Uri.parse("geo:${locatable.location().latitude},${locatable.location().longitude}")
								)
							)
						} catch (_: ActivityNotFoundException) {
							Toast.makeText(context, "No maps app installed", Toast.LENGTH_SHORT).show()
						}
					}

					findViewById<TextView>(R.id.change_options).text = locatable.changeOptions()

					findViewById<ImageView>(R.id.speed_icon).visibility = View.GONE
					findViewById<TextView>(R.id.speed_text).visibility = View.GONE
					findViewById<ImageView>(R.id.congestion_icon).visibility = View.GONE
					findViewById<TextView>(R.id.congestion_text).visibility = View.GONE
					findViewById<ImageView>(R.id.occupancy_icon).visibility = View.GONE
					findViewById<TextView>(R.id.occupancy_text).visibility = View.GONE

					findViewById<ImageView>(R.id.ac).visibility = View.GONE
					findViewById<ImageView>(R.id.bike).visibility = View.GONE
					findViewById<ImageView>(R.id.voice).visibility = View.GONE
					findViewById<ImageView>(R.id.ticket).visibility = View.GONE
					findViewById<ImageView>(R.id.usb).visibility = View.GONE
				}
			}
		}
		//(dialog as BottomSheetDialog).behavior.peekHeight = dpToPixelI(90f)

		return content
	}
}