Bimba.git

ref: 83525b8c15e6d18f23e32ee2ba83f013a92368c3

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
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.constraintlayout.widget.Group
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"
	}

	private fun showVehicle(content: View, vehicle: Vehicle) {
		content.findViewById<Group>(R.id.stop_group).visibility = View.GONE
		content.findViewById<Group>(R.id.vehicle_group).visibility = View.VISIBLE

		context?.let { ctx ->
			content.findViewById<TextView>(R.id.title).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
				)
			}
			// todo units -- [3.1] settings or system-based
			content.findViewById<TextView>(R.id.speed_text).text =
				ctx.getString(R.string.speed_in_km_per_h, vehicle.Speed * 3.6)
			content.findViewById<TextView>(R.id.congestion_text).text = vehicle.congestion(ctx)
			content.findViewById<TextView>(R.id.occupancy_text).text = vehicle.occupancy(ctx)
			content.findViewById<ImageView>(R.id.ac).visibility =
				if (vehicle.getCapability(Vehicle.Capability.AC)) {
					View.VISIBLE
				} else {
					View.GONE
				}
			content.findViewById<ImageView>(R.id.bike).visibility =
				if (vehicle.getCapability(Vehicle.Capability.BIKE)) {
					View.VISIBLE
				} else {
					View.GONE
				}
			content.findViewById<ImageView>(R.id.voice).visibility =
				if (vehicle.getCapability(Vehicle.Capability.VOICE)) {
					View.VISIBLE
				} else {
					View.GONE
				}
			content.findViewById<ImageView>(R.id.ticket).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).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<Group>(R.id.stop_group).visibility = View.VISIBLE
			content.findViewById<Group>(R.id.vehicle_group).visibility = View.GONE
			content.findViewById<TextView>(R.id.title).text =
				context?.getString(R.string.stop_title, stop.name, stop.code)
			content.findViewById<Button>(R.id.departures_button).setOnClickListener {
				val intent = Intent(ctx, DeparturesActivity::class.java).apply {
					putExtra("code", stop.code)
					putExtra("name", stop.name)
				}
				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 {
		val content = inflater.inflate(R.layout.map_bottom_sheet, container, false)
		content.apply {
			when (locatable) {
				is Vehicle -> {
					showVehicle(this, locatable)
				}
				is Stop -> {
					showStop(this, locatable)
				}
			}
		}
		//(dialog as BottomSheetDialog).behavior.peekHeight = dpToPixelI(90f)

		return content
	}
}