Bimba.git

ref: c66d69c69f5f0ed6426d6abc6638701e75f16064

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

package xyz.apiote.bimba.czwek.dashboard.ui.map

import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
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.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.TooltipCompat
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.coroutines.launch
import org.osmdroid.views.MapView
import xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.departures.DeparturesActivity
import xyz.apiote.bimba.czwek.repo.CongestionLevel
import xyz.apiote.bimba.czwek.repo.Locatable
import xyz.apiote.bimba.czwek.repo.OccupancyStatus
import xyz.apiote.bimba.czwek.repo.OnlineRepository
import xyz.apiote.bimba.czwek.repo.Position
import xyz.apiote.bimba.czwek.repo.Stop
import xyz.apiote.bimba.czwek.repo.TrafficResponseException
import xyz.apiote.bimba.czwek.repo.Vehicle
import xyz.apiote.bimba.czwek.units.UnitSystem


class MapViewModel : ViewModel() {

	private val _locatables = MutableLiveData<List<Locatable>>()
	val locatables: MutableLiveData<List<Locatable>> = _locatables

	fun getLocatablesIn(bl: Position, tr: Position, context: Context) {
		viewModelScope.launch {
			viewModelScope.launch {
				try {
					val repository = OnlineRepository()
					_locatables.value = repository.getLocatablesIn(context, bl, tr) ?: emptyList()
				} catch (e: TrafficResponseException) {
					Log.w("Map", "$e")
				}
			}
		}
	}
}

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

	private fun showVehicle(content: View, vehicle: Vehicle) {
		context?.let { ctx ->
			content.findViewById<TextView>(R.id.line).apply {
				text = ctx.getString(R.string.vehicle_headsign, vehicle.Line.name, vehicle.Headsign)
				contentDescription = ctx.getString(
					R.string.vehicle_headsign_content_description,
					vehicle.Line.name,
					vehicle.Headsign
				)
			}

			content.findViewById<TextView>(R.id.time).visibility = View.GONE

			content.findViewById<MapView>(R.id.map).visibility = View.GONE
			content.findViewById<LinearLayout>(R.id.boarding).visibility = View.GONE
			content.findViewById<ImageView>(R.id.rt_icon).visibility = View.GONE
			// TODO vehicle accessible
			content.findViewById<ImageView>(R.id.wheelchair_icon).visibility = View.GONE

			Log.i("unit", "${vehicle.Speed.mps}")
			UnitSystem.getSelected(requireContext()).let { us ->
				content.findViewById<TextView>(R.id.speed_text).apply {
					text =
						us.toString(requireContext(), us.speedUnit(vehicle.Speed))
					contentDescription =
						us.speedUnit(vehicle.Speed).contentDescription(requireContext(), us.base)
				}
			}

			content.findViewById<LinearLayout>(R.id.congestion).visibility =
				if (vehicle.congestionLevel == CongestionLevel.UNKNOWN) View.GONE else View.VISIBLE
			content.findViewById<TextView>(R.id.congestion_text).text = vehicle.congestion(ctx)

			content.findViewById<LinearLayout>(R.id.occupancy).visibility =
				if (vehicle.occupancyStatus == OccupancyStatus.UNKNOWN) View.GONE else View.VISIBLE
			content.findViewById<TextView>(R.id.occupancy_text).text = vehicle.occupancy(ctx)

			content.findViewById<ImageView>(R.id.ac).let {
				TooltipCompat.setTooltipText(
					it,
					getString(R.string.air_condition_content_description)
				)
				it.visibility =
					if (vehicle.getCapability(Vehicle.Capability.AC)) {
						View.VISIBLE
					} else {
						View.GONE
					}
			}
			content.findViewById<ImageView>(R.id.bike).let {
				TooltipCompat.setTooltipText(
					it,
					getString(R.string.bicycles_allowed_content_description)
				)
				it.visibility =
					if (vehicle.getCapability(Vehicle.Capability.BIKE)) {
						View.VISIBLE
					} else {
						View.GONE
					}
			}
			content.findViewById<ImageView>(R.id.voice).let {
				TooltipCompat.setTooltipText(
					it,
					getString(R.string.voice_announcements_content_description)
				)
				it.visibility =
					if (vehicle.getCapability(Vehicle.Capability.VOICE)) {
						View.VISIBLE
					} else {
						View.GONE
					}
			}
			content.findViewById<ImageView>(R.id.ticket).let { ticketImage ->
				TooltipCompat.setTooltipText(
					ticketImage,
					getString(R.string.tickets_sold_content_description)
				)
				ticketImage.visibility =
					if (vehicle.let {
							it.getCapability(Vehicle.Capability.TICKET_DRIVER) || it.getCapability(Vehicle.Capability.TICKET_MACHINE)
						}) {
						View.VISIBLE
					} else {
						View.GONE
					}
			}
			content.findViewById<ImageView>(R.id.usb).let {
				TooltipCompat.setTooltipText(
					it,
					getString(R.string.usb_charging_content_description)
				)
				it.visibility =
					if (vehicle.getCapability(Vehicle.Capability.USB_CHARGING)) {
						View.VISIBLE
					} else {
						View.GONE
					}
			}
		}
	}

	private fun showStop(content: View, stop: Stop) {
		context?.let { ctx ->
			content.findViewById<TextView>(R.id.stop_name).text = stop.name
			content.findViewById<Button>(R.id.departures_button).setOnClickListener {
				val intent = Intent(ctx, DeparturesActivity::class.java).apply {
					putExtra("code", stop.code)
					putExtra("name", stop.name)
					putExtra("feedID", stop.feedID)
				}
				startActivity(intent)
			}
			content.findViewById<Button>(R.id.navigation_button).setOnClickListener {
				try {
					startActivity(
						Intent(
							Intent.ACTION_VIEW,
							Uri.parse("geo:${stop.location().latitude},${stop.location().longitude}")
						)
					)
				} catch (_: ActivityNotFoundException) {
					Toast.makeText(context, ctx.getString(R.string.no_map_app), Toast.LENGTH_SHORT).show()
				}
			}

			stop.changeOptions(ctx).let { changeOptions ->
				content.findViewById<TextView>(R.id.change_options).apply {
					text = changeOptions.first
					contentDescription = changeOptions.second
				}
			}
		}
	}


	override fun onCreateView(
		inflater: LayoutInflater,
		container: ViewGroup?,
		savedInstanceState: Bundle?
	): View {
		return when (locatable) {
			is Vehicle -> {
				inflater.inflate(R.layout.departure_bottom_sheet, container, false).apply {
					showVehicle(this, locatable)
				}
			}

			is Stop -> {
				inflater.inflate(R.layout.stop_bottom_sheet, container, false).apply {
					showStop(this, locatable)
				}
			}

			else -> {
				throw IllegalStateException("locatable is neither a vehicle nor a stop")
			}
		}
	}
}