Bimba.git

ref: v3.7.0

app/src/main/java/xyz/apiote/bimba/czwek/repo/Departure.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.repo

import android.content.Context
import android.text.format.DateUtils
import xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.api.AlertCauseV1
import xyz.apiote.bimba.czwek.api.AlertEffectV1
import xyz.apiote.bimba.czwek.api.AlertV1
import xyz.apiote.bimba.czwek.api.DepartureV1
import xyz.apiote.bimba.czwek.api.DepartureV2
import xyz.apiote.bimba.czwek.api.DepartureV3
import xyz.apiote.bimba.czwek.api.DepartureV4
import xyz.apiote.bimba.czwek.api.DepartureV5
import xyz.apiote.bimba.czwek.api.Time
import xyz.apiote.bimba.czwek.api.UnknownResourceVersionException
import xyz.apiote.bimba.czwek.units.Second
import xyz.apiote.bimba.czwek.units.TGM
import xyz.apiote.bimba.czwek.units.UnitSystem
import java.time.Instant
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit


class DepartureItem {
	private constructor(d: Departure?, a: List<Alert>) {
		departure = d
		alert = a
	}

	constructor(d: Departure) : this(d, emptyList())
	constructor(a: List<Alert>) : this(null, a)

	val departure: Departure?
	val alert: List<Alert>
}

enum class AlertCause {
	UNKNOWN, OTHER, TECHNICAL_PROBLEM, STRIKE, DEMONSTRATION, ACCIDENT, HOLIDAY, WEATHER, MAINTENANCE,
	CONSTRUCTION, POLICE_ACTIVITY, MEDICAL_EMERGENCY;

	companion object {
		fun of(type: AlertCauseV1): AlertCause {
			return when (type) {
				AlertCauseV1.UNKNOWN -> valueOf("UNKNOWN")
				AlertCauseV1.OTHER -> valueOf("OTHER")
				AlertCauseV1.TECHNICAL_PROBLEM -> valueOf("TECHNICAL_PROBLEM")
				AlertCauseV1.STRIKE -> valueOf("STRIKE")
				AlertCauseV1.DEMONSTRATION -> valueOf("DEMONSTRATION")
				AlertCauseV1.ACCIDENT -> valueOf("ACCIDENT")
				AlertCauseV1.HOLIDAY -> valueOf("HOLIDAY")
				AlertCauseV1.WEATHER -> valueOf("WEATHER")
				AlertCauseV1.MAINTENANCE -> valueOf("MAINTENANCE")
				AlertCauseV1.CONSTRUCTION -> valueOf("CONSTRUCTION")
				AlertCauseV1.POLICE_ACTIVITY -> valueOf("POLICE_ACTIVITY")
				AlertCauseV1.MEDICAL_EMERGENCY -> valueOf("MEDICAL_EMERGENCY")
			}
		}
	}
}

enum class AlertEffect {
	UNKNOWN, OTHER, NO_SERVICE, REDUCED_SERVICE, SIGNIFICANT_DELAYS, DETOUR, ADDITIONAL_SERVICE,
	MODIFIED_SERVICE, STOP_MOVED, NONE, ACCESSIBILITY_ISSUE;

	companion object {
		fun of(type: AlertEffectV1): AlertEffect {
			return when (type) {
				AlertEffectV1.UNKNOWN -> valueOf("UNKNOWN")
				AlertEffectV1.OTHER -> valueOf("OTHER")
				AlertEffectV1.NO_SERVICE -> valueOf("NO_SERVICE")
				AlertEffectV1.REDUCED_SERVICE -> valueOf("REDUCED_SERVICE")
				AlertEffectV1.SIGNIFICANT_DELAYS -> valueOf("SIGNIFICANT_DELAYS")
				AlertEffectV1.DETOUR -> valueOf("DETOUR")
				AlertEffectV1.ADDITIONAL_SERVICE -> valueOf("ADDITIONAL_SERVICE")
				AlertEffectV1.MODIFIED_SERVICE -> valueOf("MODIFIED_SERVICE")
				AlertEffectV1.STOP_MOVED -> valueOf("STOP_MOVED")
				AlertEffectV1.NONE -> valueOf("NONE")
				AlertEffectV1.ACCESSIBILITY_ISSUE -> valueOf("ACCESSIBILITY_ISSUE")
			}
		}
	}
}

data class Alert(
	val header: String,
	val description: String,
	val url: String,
	val cause: AlertCause,
	val effect: AlertEffect
) {
	constructor(a: AlertV1) : this(
		a.header,
		a.Description,
		a.Url,
		AlertCause.of(a.Cause),
		AlertEffect.of(a.Effect)
	)
}

data class StopDepartures(
	val departures: List<Departure>,
	val stop: Stop,
	val alerts: List<Alert>
)

data class Departure(
	val ID: String,
	val time: Time,
	val status: ULong,
	val isRealtime: Boolean,
	val vehicle: Vehicle,
	val boarding: UByte,
	val alerts: List<Alert>,
	val exact: Boolean,
	val terminusArrival: Boolean
) {

	constructor(d: DepartureV1) : this(
		d.ID,
		d.time,
		d.status,
		d.isRealtime,
		Vehicle(d.vehicle),
		d.boarding,
		emptyList(),
		true,
		false
	)

	constructor(d: DepartureV2) : this(
		d.ID,
		d.time,
		d.status,
		d.isRealtime,
		Vehicle(d.vehicle),
		d.boarding,
		emptyList(),
		true,
		false
	)

	constructor(d: DepartureV3) : this(
		d.ID,
		d.time,
		d.status.ordinal.toULong(), // TODO VehicleStatus
		d.isRealtime,
		Vehicle(d.vehicle),
		d.boarding,
		emptyList(),
		true,
		false
	)

	constructor(d: DepartureV4) : this(
		d.ID,
		d.time,
		d.status.ordinal.toULong(), // TODO VehicleStatus
		d.isRealtime,
		Vehicle(d.vehicle),
		d.boarding,
		d.alerts.map { Alert(it) },
		true,
		false
	)

	constructor(d: DepartureV5) : this(
		d.ID,
		d.time,
		d.status.ordinal.toULong(), // TODO VehicleStatus
		d.isRealtime,
		Vehicle(d.vehicle),
		d.boarding,
		d.alerts.map { Alert(it) },
		d.exact,
		d.terminusArrival
	)

	fun statusText(context: Context?, showAsTime: Boolean, at: ZonedDateTime? = null): String {
		val now = at ?: Instant.now().atZone(ZoneId.systemDefault())
		val departureTime = ZonedDateTime.of(
			now.year, now.monthValue, now.dayOfMonth,
			time.Hour.toInt(), time.Minute.toInt(), time.Second.toInt(), 0, ZoneId.of(time.Zone)
		).plus(time.DayOffset.toLong(), ChronoUnit.DAYS)
		if (showAsTime) {
			return departureTime.format(DateTimeFormatter.ofPattern("HH:mm"))
		}
		var r = status.toUInt()
		if (departureTime.isBefore(now) && r < 3u) {
			r = 0u
		}
		return when (r) {
			0u -> if (context != null && UnitSystem.getSelected(context) is TGM) {
				val us = UnitSystem.getSelected(context)
				us.toString(
					context,
					us.timeUnit(Second((departureTime.toEpochSecond() - now.toEpochSecond()).toInt()))
				)
			} else {
				DateUtils.getRelativeTimeSpanString(
					departureTime.toEpochSecond() * 1000,
					now.toEpochSecond() * 1000,
					DateUtils.MINUTE_IN_MILLIS,
					DateUtils.FORMAT_ABBREV_RELATIVE
				).toString()
			}

			1u -> context?.getString(R.string.departure_momentarily) ?: "momentarily"
			2u -> context?.getString(R.string.departure_now) ?: "now"
			3u -> context?.getString(R.string.departure_departed) ?: "departed"
			else -> throw UnknownResourceVersionException("VehicleStatus/$r", 1u)
		}
	}

	fun timeString(context: Context): String {
		return when {
			isRealtime -> context.getString(
				R.string.at_time_realtime, time.Hour.toInt(), time.Minute.toInt(), time.Second.toInt()
			)

			exact -> context.getString(R.string.at_time, time.Hour.toInt(), time.Minute.toInt())
			else -> context.getString(R.string.about_time, time.Hour.toInt(), time.Minute.toInt())
		}
	}

	fun boardingText(context: Context): String {
		// todo [3.x] probably should take into account (on|off)-boarding only, on demand
		return when {
			boarding == (0b0000_0000).toUByte() -> context.getString(R.string.no_boarding)
			boarding == (0b1111_1111).toUByte() -> "" // unknown
			boarding.and(0b0011_0011u) == (0b0000_0001).toUByte() -> context.getString(R.string.on_boarding)
			boarding.and(0b0011_0011u) == (0b0001_0000).toUByte() -> context.getString(R.string.off_boarding)
			boarding.and(0b0011_0011u) == (0b0001_0001).toUByte() -> context.getString(R.string.boarding)
			else -> context.getString(R.string.on_demand)
		}
	}
}