Author: Adam Pioterek <adam.pioterek@protonmail.ch>
favourites (without editing)
%!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 da2601cf9bf8bcf25a49956282b825c3730daff1..f45c01a976e60d588a49bce3749a06e20b9ee428 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/ProviderProxy.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/ProviderProxy.kt @@ -189,8 +189,9 @@ if (plateId !in receivedPlates) receivedPlates.add(plateId) } else { receivedPlates.remove(plateId) - if (receivedPlates.isEmpty()) + if (receivedPlates.isEmpty()) { listener.onDeparturesReady(filterDepartures(cache!!.await()), null) + } } } } 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 6c66c23ee44a8cd848b46d5f53c4130f8624a468..a3bcc6e7dc5caf1b31a1d20b76a1062f914329b0 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt @@ -45,7 +45,6 @@ private val actionModeCallback = ActionModeCallback() private var actionMode: ActionMode? = null private var isWarned = false private lateinit var providerProxy: ProviderProxy - private val listenersIds = HashSet<String>() companion object { const val REQUEST_EDIT_FAVOURITE = 1 @@ -153,6 +152,21 @@ searchView.attachNavigationDrawerToMenuButton(drawer_layout as DrawerLayout) } + override fun onRestart() { + super.onRestart() + favourites = FavouriteStorage.getFavouriteStorage(context) + favourites.forEach { + it.subscribeForDepartures(this, this) + } + } + + override fun onStop() { + super.onStop() + favourites.forEach { + it.unsubscribeFromDepartures(this) + } + } + private fun showValidityInDrawer() { if (timetable.isEmpty()) { drawerView.menu.findItem(R.id.drawer_validity_since).title = getString(R.string.validity_offline_unavailable) @@ -221,12 +235,14 @@ ) { dialog: DialogInterface, _: Int -> dialog.cancel() } .setCancelable(true) .setMessage(message) .create().show() + + //todo if days == -1 -> delete timetable } private fun prepareFavourites() { favourites = FavouriteStorage.getFavouriteStorage(context) favourites.forEach { - listenersIds.add(it.subscribeForDepartures(this, this)) + it.subscribeForDepartures(this, this) } val layoutManager = LinearLayoutManager(context) favouritesList = favourites_list @@ -277,7 +293,6 @@ } override fun onDestroy() { super.onDestroy() - listenersIds.forEach { providerProxy.unsubscribeFromDepartures(it, this) } receiver.removeOnTimetableDownloadListener(context as MessageReceiver.OnTimetableDownloadListener) unregisterReceiver(receiver) } 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 4fb95b7576a4f220f6a667fc0a1ed2dd38c493fa..047a1243509f73149635309d43770b932d9215b5 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt @@ -98,17 +98,15 @@ fab.setImageDrawable(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_favourite_empty, this.theme)) } fab.setOnClickListener { - /* todo - if (!favourites.has(stopSymbol)) { + if (!favourites.has(stopCode)) { val items = HashSet<StopSegment>() - items.add(stopSegment!!) - favourites.add(stopSymbol, items, this@StopActivity) + items.add(StopSegment(stopCode, null)) + favourites.add(stopCode, items, this@StopActivity) fab.setImageDrawable(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_favourite, this.theme)) } else { Snackbar.make(it, getString(R.string.stop_already_fav), Snackbar.LENGTH_LONG) .setAction("Action", null).show() } - */ } } @@ -214,7 +212,7 @@ receiver.removeOnTimetableDownloadListener(context) if (sourceType == SOURCE_TYPE_STOP) providerProxy.unsubscribeFromDepartures(subscriptionId, this) else - favourite!!.unsubscribeFromDepartures(subscriptionId, this) + favourite!!.unsubscribeFromDepartures(this) unregisterReceiver(receiver) } } diff --git a/app/src/main/java/ml/adamsprogs/bimba/collections/FavouriteStorage.kt b/app/src/main/java/ml/adamsprogs/bimba/collections/FavouriteStorage.kt index 2c772c3a6ebe559d74358439cc0db557ae01621e..948b545155f4a33101d0b41d762abe2782467422 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/collections/FavouriteStorage.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/collections/FavouriteStorage.kt @@ -34,11 +34,18 @@ for ((name, jsonTimetables) in favouritesMap.entrySet()) { val timetables = HashSet<StopSegment>() jsonTimetables.asJsonArray.mapTo(timetables) { val stopSegment = StopSegment(it.asJsonObject["stop"].asString, null) - val plates = HashSet<Plate.ID>() - it.asJsonObject["plates"].asJsonArray.mapTo(plates) { - Plate.ID(it.asJsonObject["line"].asString, - it.asJsonObject["stop"].asString, - it.asJsonObject["headsign"].asString) + val plates = it.asJsonObject["plates"].let { jsonPlates -> + if (jsonPlates == null || jsonPlates.isJsonNull) + null + else { + HashSet<Plate.ID>().apply { + jsonPlates.asJsonArray.map { + Plate.ID(it.asJsonObject["line"].asString, + it.asJsonObject["stop"].asString, + it.asJsonObject["headsign"].asString) + } + } + } } stopSegment.plates = plates stopSegment @@ -90,14 +97,19 @@ val timetables = JsonArray() for (timetable in favourite.segments) { val segment = JsonObject() segment.addProperty("stop", timetable.stop) - val plates = JsonArray() - for (plate in timetable.plates ?: HashSet()) { - val element = JsonObject() - element.addProperty("stop", plate.stop) - element.addProperty("line", plate.line) - element.addProperty("headsign", plate.headsign) - plates.add(element) - } + val plates = + if (timetable.plates == null) + JsonNull.INSTANCE + else + JsonArray().apply { + for (plate in timetable.plates ?: HashSet()) { + val element = JsonObject() + element.addProperty("stop", plate.stop) + element.addProperty("line", plate.line) + element.addProperty("headsign", plate.headsign) + add(element) + } + } segment.add("plates", plates) timetables.add(segment) } diff --git a/app/src/main/java/ml/adamsprogs/bimba/datasources/TimetableDownloader.kt b/app/src/main/java/ml/adamsprogs/bimba/datasources/TimetableDownloader.kt index 65495c91d65aefb89125ea4037822a40cdb2369c..8c39cb5c95f88327c5aa654d3d4b84e701fdcad6 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/datasources/TimetableDownloader.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/datasources/TimetableDownloader.kt @@ -57,7 +57,6 @@ sendResult(RESULT_UP_TO_DATE) return } if (httpCon.responseCode != HttpsURLConnection.HTTP_OK) { - println(httpCon.responseMessage) sendResult(RESULT_NO_CONNECTIVITY) return } diff --git a/app/src/main/java/ml/adamsprogs/bimba/datasources/VmClient.kt b/app/src/main/java/ml/adamsprogs/bimba/datasources/VmClient.kt index a795febdae7c43b37f01e3c5eea0da38bdef61df..a13d730482d3b2484e636b4c993d24fe1abc9f29 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/datasources/VmClient.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/datasources/VmClient.kt @@ -84,7 +84,6 @@ val request = okhttp3.Request.Builder() .url(url) .post(body) .build() - println("makeRequest: $request") val responseBody: String? diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt b/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt index ebfe9480bedde0b7c07472f0df0e8cb27b2e01e8..a1e71f73cbed2a420f310de9ce9b85d03d31295c 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt @@ -17,6 +17,7 @@ var segments: HashSetprivate set private var fullDepartures: Map<String, List<Departure>> = HashMap() private var cache: List<Departure> = ArrayList() + private var listenerId = "" val size get() = segments.sumBy { @@ -138,7 +139,8 @@ } fun subscribeForDepartures(listener: ProviderProxy.OnDeparturesReadyListener, context: Context): String { this.listener = listener - return providerProxy.subscribeForDepartures(segments, this, context) + listenerId = providerProxy.subscribeForDepartures(segments, this, context) + return listenerId } override fun onDeparturesReady(departures: List<Departure>, plateId: Plate.ID?) { @@ -146,7 +148,7 @@ cache = departures listener.onDeparturesReady(departures, plateId) } - fun unsubscribeFromDepartures(uuid: String, context: Context) { - providerProxy.unsubscribeFromDepartures(uuid, context) + fun unsubscribeFromDepartures(context: Context) { + providerProxy.unsubscribeFromDepartures(listenerId, context) } } diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/StopSegment.kt b/app/src/main/java/ml/adamsprogs/bimba/models/StopSegment.kt index 07148cbe06fa0bc5f3d6074db888601dc8f0d469..22b8e0b9ef369a53affb2c3df27cdffc902f9372 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/models/StopSegment.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/models/StopSegment.kt @@ -66,6 +66,18 @@ fun remove(plateId: Plate.ID) { (plates as HashSet).remove(plateId) } + override fun toString(): String { + var s = "$stop: " + if (plates == null) + s += "NULL" + else { + s += "{" + s += plates!!.joinToString { it.toString() } + s += "}" + } + return s + } + val size: Int get() = plates?.size ?: 0 } \ No newline at end of file diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouriteEditRowAdapter.kt b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouriteEditRowAdapter.kt index 6814ada2b4f7f4e9ff219337c1a4e1e03c643fae..4b2a2eac4ee1f245a99982b9dc6cbfbd1940ed85 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouriteEditRowAdapter.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouriteEditRowAdapter.kt @@ -12,6 +12,8 @@ import ml.adamsprogs.bimba.models.Favourite import ml.adamsprogs.bimba.models.Plate import ml.adamsprogs.bimba.models.Timetable + +//todo when plates null -> get all plates from proxy class FavouriteEditRowAdapter(private var favourite: Favourite) : RecyclerView.Adapter<FavouriteEditRowAdapter.ViewHolder>() { override fun getItemCount(): Int {