Author: Adam Pioterek <adam.pioterek@protonmail.ch>
sheds from VM without offline timetable
%!v(PANIC=String method: strings: negative Repeat count)
diff --git a/app/src/main/java/ml/adamsprogs/bimba/ProviderProxy.kt b/app/src/main/java/ml/adamsprogs/bimba/ProviderProxy.kt index 9f8e1300ab3a7d260f9feaf34af0d674e668ed85..80a47f9442b8b6dec1b40f7acdbf16ad2e34d59e 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/ProviderProxy.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/ProviderProxy.kt @@ -26,11 +26,8 @@ val vmSuggestions = withContext(DefaultDispatcher) { vmStopsClient.getStops(query) } - return if (vmSuggestions.isEmpty()) { - if (timetable.isEmpty()) - emptyList() - else - timetable.getStopSuggestions() + return if (vmSuggestions.isEmpty() and !timetable.isEmpty()) { + timetable.getStopSuggestions() } else { vmSuggestions } @@ -53,5 +50,21 @@ result = result.replace('ź', 'z', true) result = result.replace('ć', 'c', true) result = result.replace('ń', 'n', true) return result + } + + fun getSheds(name: String, callback: (Map<String, Set<String>>) -> Unit) { + launch(UI) { + val vmSheds = withContext(DefaultDispatcher) { + vmStopsClient.getSheds(name) + } + + val sheds = if (vmSheds.isEmpty() and !timetable.isEmpty()) { + timetable.getHeadlinesForStop(name) + } else { + vmSheds + } + + callback(sheds) + } } } \ No newline at end of file diff --git a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt index e66054a84ced3c7d34d383ff10b4e251925955cc..0c1c1c85ec7488adba5ff3146321fc3397148d18 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt @@ -124,7 +124,6 @@ } imm.hideSoftInputFromWindow(view.windowToken, 0) if (searchSuggestion is StopSuggestion) { val intent = Intent(context, StopSpecifyActivity::class.java) - intent.putExtra(StopSpecifyActivity.EXTRA_STOP_IDS, searchSuggestion.ids.joinToString(",") { it.id }) intent.putExtra(StopSpecifyActivity.EXTRA_STOP_NAME, searchSuggestion.name) startActivity(intent) } else if (searchSuggestion is LineSuggestion) { diff --git a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt index a369cc81ddece00c5313ef0e19482d13785f74a1..18d6938b4f6cf5b79ed02117238c287f9ec3ecc9 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt @@ -25,6 +25,7 @@ private var sectionsPagerAdapter: SectionsPagerAdapter? = null companion object { const val EXTRA_STOP_ID = "stopId" + const val EXTRA_STOP_CODE = "stopCode" const val EXTRA_FAVOURITE = "favourite" const val SOURCE_TYPE = "sourceType" const val SOURCE_TYPE_STOP = "stop" diff --git a/app/src/main/java/ml/adamsprogs/bimba/activities/StopSpecifyActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/activities/StopSpecifyActivity.kt index eacf1d6d40244cf2c40982f9514211603d2c683f..9562d8d1b686e77cf39bd9428497a6a05231801d 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopSpecifyActivity.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopSpecifyActivity.kt @@ -7,18 +7,16 @@ import android.view.View import android.view.ViewGroup import kotlinx.android.synthetic.main.activity_stop_specify.* import ml.adamsprogs.bimba.R -import ml.adamsprogs.bimba.models.gtfs.AgencyAndId -import ml.adamsprogs.bimba.models.Timetable import android.content.Context import android.widget.TextView import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.view.LayoutInflater +import ml.adamsprogs.bimba.ProviderProxy class StopSpecifyActivity : AppCompatActivity() { companion object { - const val EXTRA_STOP_IDS = "stopIds" const val EXTRA_STOP_NAME = "stopName" } @@ -26,22 +24,24 @@ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_stop_specify) - val ids = intent.getStringExtra(EXTRA_STOP_IDS).split(",").map { AgencyAndId(it) }.toSet() val name = intent.getStringExtra(EXTRA_STOP_NAME) - val timetable = Timetable.getTimetable(this) - val headlines = timetable.getHeadlinesForStop(ids) + val providerProxy = ProviderProxy(this) + providerProxy.getSheds(name) { + val layoutManager = LinearLayoutManager(this) + val departuresList: RecyclerView = list_view - val layoutManager = LinearLayoutManager(this) - val departuresList: RecyclerView = list_view + departuresList.adapter = ShedAdapter(this, it) + departuresList.layoutManager = layoutManager + } + /*val timetable = Timetable.getTimetable(this) + val headlines = timetable.getHeadlinesForStop(name)*/ - departuresList.adapter = ShedAdapter(this, headlines) - departuresList.layoutManager = layoutManager setSupportActionBar(toolbar) supportActionBar?.title = name } - class ShedAdapter(val context: Context, private val values: Map<AgencyAndId, Pair<String, Set<String>>>) : + class ShedAdapter(val context: Context, private val values: Map<String, Set<String>>) : RecyclerView.Adapter<ShedAdapter.ViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val context = parent.context @@ -55,15 +55,15 @@ override fun getItemCount(): Int = values.size override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.root.setOnClickListener { - val id = values.entries.sortedBy { it.value.first }[position].key + val code = values.keys.sorted()[position] val intent = Intent(context, StopActivity::class.java) intent.putExtra(StopActivity.SOURCE_TYPE, StopActivity.SOURCE_TYPE_STOP) - intent.putExtra(StopActivity.EXTRA_STOP_ID, id) + intent.putExtra(StopActivity.EXTRA_STOP_CODE, code) context.startActivity(intent) } - holder.stopCode.text = values.values.sortedBy { it.first }[position].first - holder.stopHeadlines.text = values.values.sortedBy { it.first }[position].second - .sortedBy { it } // fixme<p:1> natural sort + holder.stopCode.text = values.keys.sorted()[position] + holder.stopHeadlines.text = values.entries.sortedBy { it.key }[position].value + .sortedBy { it.split(" → ")[0].toInt() } // fixme<p:1> natural sort .joinToString() } diff --git a/app/src/main/java/ml/adamsprogs/bimba/datasources/VmStopsClient.kt b/app/src/main/java/ml/adamsprogs/bimba/datasources/VmStopsClient.kt index a2d47d4e31c9d91aada6130f5b58a2dbcb0303b1..7f9d5e8562a5c1f064dbc9c7fb6c4f9b422cdf53 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/datasources/VmStopsClient.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/datasources/VmStopsClient.kt @@ -22,9 +22,10 @@ return vmStopsClient!! } } - /*suspend fun getBollardsByStopPoint(name: String): Map<String, Set<String>> { + suspend fun getSheds(name: String): Map<String, Set<String>> { val response = makeRequest("getBollardsByStopPoint", """{"name": "$name"}""") - println("asked for $name and got $response") + if (!response.has("success")) + return emptyMap() val rootObject = response["success"].asJsonObject["bollards"].asJsonArray val result = HashMap<String, Set<String>>() rootObject.forEach { @@ -36,6 +37,7 @@ } return result } + /* suspend fun getPlatesByStopPoint(code: String): Set<Plate.ID>? { val getTimesResponse = makeRequest("getTimes", """{"symbol": "$code"}""") val name = getTimesResponse["success"].asJsonObject["bollard"].asJsonObject["name"].asString @@ -56,6 +58,9 @@ val response = withContext(DefaultDispatcher) { makeRequest("getStopPoints", """{"pattern": "$pattern"}""") } + if (!response.has("success")) + return emptyList() + val points = response["success"].asJsonArray.map { it.asJsonObject } val names = HashSet<String>() @@ -65,7 +70,7 @@ val name = it["name"].asString names.add(name) } - return names.map { StopSuggestion(it, emptySet(), "", "") } + return names.map { StopSuggestion(it, "", "") } } private suspend fun makeRequest(method: String, data: String): JsonObject { diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt b/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt index 4f5762e92ce634e5e4275ef8558da5615a6a1ec9..f09c969c9554054ce02e4ffd3b653422e795d39a 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt @@ -57,24 +57,19 @@ fun getStopSuggestions(/*context: Context, */force: Boolean = false): List{ if (_stops != null && !force) return _stops!! - val ids = HashMap<String, HashSet<AgencyAndId>>() val zones = HashMap<String, String>() - val cursor = db!!.rawQuery("select stop_name, stop_id, zone_id from stops", null) + val cursor = db!!.rawQuery("select stop_name, zone_id from stops", null) while (cursor.moveToNext()) { val name = cursor.getString(0) - val id = cursor.getInt(1) - val zone = cursor.getString(2) - if (name !in ids) - ids[name] = HashSet() - ids[name]!!.add(AgencyAndId(id.toString())) + val zone = cursor.getString(1) zones[name] = zone } cursor.close() - _stops = ids.map { + _stops = zones.map { /*todo val colour = when (zones[it.key]) { "A" -> "#${getColour(R.color.zoneA, context).toString(16)}" @@ -83,7 +78,7 @@ "C" -> "#${getColour(R.color.zoneC, context).toString(16)}" else -> "#000000" } */ - StopSuggestion(it.key, it.value, zones[it.key]!!, "#000000") + StopSuggestion(it.key, it.value, "#000000") }.sorted() return _stops!! } @@ -102,31 +97,31 @@ return routes.sortedBy { it.name } } - fun getHeadlinesForStop(stops: Set<AgencyAndId>): Map<AgencyAndId, Pair<String, Set<String>>> { - val headsigns = HashMap<AgencyAndId, Pair<String, HashSet<String>>>() - - val stopsIndex = HashMap<Int, String>() - val where = stops.joinToString(" or ", "where ") { "stop_id = ?" } - var cursor = db!!.rawQuery("select stop_id, stop_code from stops $where", stops.map { it.toString() }.toTypedArray()) + fun getHeadlinesForStop(stop: String): Map<String, Set<String>> { + val headsigns = HashMap<String, HashSet<String>>() + var cursor = db!!.rawQuery("select stop_code from stops where stop_name = ?", + arrayOf(stop)) + val stopCodes = ArrayList<String>() while (cursor.moveToNext()) { - stopsIndex[cursor.getInt(0)] = cursor.getString(1) + stopCodes.add(cursor.getString(0)) } cursor.close() - cursor = db!!.rawQuery("select stop_id, route_id, trip_headsign " + + val where = stopCodes.joinToString(" or ", "where ") { "stop_code = ?" } + + cursor = db!!.rawQuery("select stop_code, route_id, trip_headsign " + "from stop_times natural join trips " + - where, stops.map { it.toString() }.toTypedArray()) + where, stopCodes.toTypedArray()) while (cursor.moveToNext()) { - val stop = cursor.getInt(0) - val stopId = AgencyAndId(stop.toString()) + val stopCode = cursor.getString(0) val route = cursor.getString(1) val headsign = cursor.getString(2) - if (stopId !in headsigns) - headsigns[stopId] = Pair(stopsIndex[stop]!!, HashSet()) - headsigns[stopId]!!.second.add("$route → $headsign") + if (stopCode !in headsigns) + headsigns[stopCode] = HashSet() + headsigns[stopCode]!!.add("$route → $headsign") } cursor.close() @@ -134,13 +129,13 @@ return headsigns /* - 1435 -> (AWF03, {232 → Os. Rusa}) - 1436 -> (AWF04, {232 → Rondo Kaponiera}) - 1437 -> (AWF02, {76 → Pl. Bernardyński, 74 → Os. Sobieskiego, 603 → Pl. Bernardyński}) - 1634 -> (AWF01, {76 → Os. Dębina, 603 → Łęczyca/Dworcowa}) - 171 -> (AWF42, {29 → Pl. Wiosny Ludów}) - 172 -> (AWF41, {10 → Połabska, 29 → Dębiec, 15 → Budziszyńska, 10 → Dębiec, 15 → Os. Sobieskiego, 12 → Os. Sobieskiego, 6 → Junikowo, 18 → Ogrody, 2 → Ogrody}) - 4586 -> (AWF73, {10 → Franowo, 29 → Franowo, 6 → Miłostowo, 5 → Stomil, 18 → Franowo, 15 → Franowo, 12 → Starołęka, 74 → Os. Orła Białego}) + AWF03 -> {232 → Os. Rusa} + AWF04 -> {232 → Rondo Kaponiera} + AWF02 -> {76 → Pl. Bernardyński, 74 → Os. Sobieskiego, 603 → Pl. Bernardyński} + AWF01 ->{76 → Os. Dębina, 603 → Łęczyca/Dworcowa} + AWF42 -> {29 → Pl. Wiosny Ludów} + AWF41 -> {10 → Połabska, 29 → Dębiec, 15 → Budziszyńska, 10 → Dębiec, 15 → Os. Sobieskiego, 12 → Os. Sobieskiego, 6 → Junikowo, 18 → Ogrody, 2 → Ogrody} + AWF73 -> {10 → Franowo, 29 → Franowo, 6 → Miłostowo, 5 → Stomil, 18 → Franowo, 15 → Franowo, 12 → Starołęka, 74 → Os. Orła Białego} */ } diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/suggestions/StopSuggestion.kt b/app/src/main/java/ml/adamsprogs/bimba/models/suggestions/StopSuggestion.kt index 934a6e0e2ef25631fe6fde8162becd1260b8a8d8..af9fc5a346c5ef4d8dea9e54a4ef0e3a635cc836 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/models/suggestions/StopSuggestion.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/models/suggestions/StopSuggestion.kt @@ -5,9 +5,9 @@ import android.os.Parcelable import ml.adamsprogs.bimba.R import ml.adamsprogs.bimba.models.gtfs.AgencyAndId -class StopSuggestion(name: String, val ids: Set<AgencyAndId>, private val zone: String, private val zoneColour: String) : GtfsSuggestion(name){ +class StopSuggestion(name: String, private val zone: String, private val zoneColour: String) : GtfsSuggestion(name){ @Suppress("UNCHECKED_CAST") - constructor(parcel: Parcel) : this(parcel.readString(), parcel.readString().split(",").map { AgencyAndId(it) }.toSet(), parcel.readString(), parcel.readString()) + constructor(parcel: Parcel) : this(parcel.readString(), parcel.readString(), parcel.readString()) override fun describeContents(): Int { return Parcelable.CONTENTS_FILE_DESCRIPTOR @@ -15,7 +15,6 @@ } override fun writeToParcel(dest: Parcel?, flags: Int) { dest?.writeString(name) - dest?.writeString(ids.joinToString(",") { it.toString() }) dest?.writeString(zone) dest?.writeString(zoneColour) }