ref: 912f70db2ca99322470dde31169a941352694501
app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesViewModel.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 |
// SPDX-FileCopyrightText: Adam Evyčędo // // SPDX-License-Identifier: GPL-3.0-or-later package xyz.apiote.bimba.czwek.departures import android.app.Activity import android.content.Context import android.content.Intent import android.net.Uri import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import kotlinx.coroutines.MainScope import kotlinx.coroutines.launch import xyz.apiote.bimba.czwek.api.Error import xyz.apiote.bimba.czwek.api.mapHttpError import xyz.apiote.bimba.czwek.repo.FeedInfo import xyz.apiote.bimba.czwek.repo.OfflineRepository import xyz.apiote.bimba.czwek.repo.OnlineRepository import xyz.apiote.bimba.czwek.repo.QrLocation import xyz.apiote.bimba.czwek.repo.StopDepartures import xyz.apiote.bimba.czwek.repo.TrafficResponseException class DeparturesViewModel : ViewModel() { private val _departures = MutableLiveData<StopDepartures>() val departures: LiveData<StopDepartures> = _departures private val _error = MutableLiveData<Error>() val error: LiveData<Error> = _error var requestedItemsNumber = 12 var allItemsRequested = false private var feed: FeedInfo? = null private lateinit var code: String fun getDepartures(context: Context, line: String?) { MainScope().launch { try { if (feed == null) { feed = getFeed(context) code = getCode(context, feed!!) } val repository = OnlineRepository() val stopDepartures = repository.getDepartures( feed!!.id, code, line, context, requestedItemsNumber ) stopDepartures?.let { if (stopDepartures.departures.isEmpty()) { val (string, image) = mapHttpError(404) throw TrafficResponseException(404, "", Error(404, string, image)) } _departures.value = it } } catch (e: TrafficResponseException) { if (!departures.isInitialized) { _error.value = e.error } Log.w("Departures", "$e") } } } private suspend fun getFeed(context: Context): FeedInfo { val intent = (context as Activity).intent var feeds = OfflineRepository().getFeeds(context) if (feeds.isNullOrEmpty()) { feeds = OnlineRepository().getFeeds(context) } return when (intent.action) { Intent.ACTION_VIEW -> { val feed = feeds?.values?.find { it.qrHost == intent.data?.host } if (feed == null) { if (feeds?.values?.all { it.qrIn == QrLocation.UNKNOWN } == true) { return getFeedOld(intent.data?.host, feeds) ?: throw TrafficResponseException(41) } else { throw TrafficResponseException(41) } } else { return feed } } null -> { val feedID = intent.extras?.getString("feedID") feeds?.get(feedID) ?: throw TrafficResponseException(41) } else -> throw TrafficResponseException(41) } } // TODO [after removing FeedsResponseV1] remove this method private fun getFeedOld(host: String?, feeds: Map<String, FeedInfo?>): FeedInfo? { @Suppress("SpellCheckingInspection") return when (host) { "www.peka.poznan.pl" -> feeds["poznan_ztm"] "rj.metropoliaztm.pl" -> feeds["gzm_ztm"] else -> null } } private fun getCode(context: Context, feed: FeedInfo): String { val intent = (context as Activity).intent return when (intent.action) { Intent.ACTION_VIEW -> { when (feed.qrIn) { QrLocation.QUERY -> { intent.data?.getQueryParameter(feed.qrSelector) } QrLocation.PATH -> { feed.qrSelector.toRegex().find(intent.data?.path ?: "")?.value } QrLocation.UNKNOWN -> { getCodeOld(intent.data) } QrLocation.NONE -> { throw TrafficResponseException(41) } } ?: throw TrafficResponseException(41) } null -> intent?.extras?.getString("code") ?: throw TrafficResponseException(41) else -> throw TrafficResponseException(41) } } // TODO [after removing FeedsResponseV1] remove this method private fun getCodeOld(data: Uri?): String { @Suppress("SpellCheckingInspection") return when (data?.host) { "www.peka.poznan.pl" -> data.getQueryParameter("przystanek") ?: "" "rj.metropoliaztm.pl" -> data.lastPathSegment ?: "" else -> "" } } } |