Bimba.git

commit 68ce87cc826a5c4c649ea1eb300eb20acfa5faba

Author: Adam Pioterek <adam.pioterek@protonmail.ch>

fix: deserialising favourites

%!v(PANIC=String method: strings: negative Repeat count)


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 daec3aed4d3ab117418b0dd364102ab657f2b2cb..b7d96b89b3fd738796ade8db57b8539e794ba59e 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/collections/FavouriteStorage.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/collections/FavouriteStorage.kt
@@ -29,28 +29,23 @@     private val preferences: SharedPreferences = context.getSharedPreferences("ml.adamsprogs.bimba.prefs", Context.MODE_PRIVATE)
 
     init {
         val favouritesString = preferences.getString("favourites", "{}")
-        val favouritesMap = Gson().fromJson(favouritesString, JsonObject::class.java)
-        for ((name, jsonTimetables) in favouritesMap.entrySet()) {
-            val timetables = HashSet<StopSegment>()
-            jsonTimetables.asJsonArray.mapTo(timetables) {
-                val stopSegment = StopSegment(it.asJsonObject["stop"].asString, null)
-                val plates = it.asJsonObject["plates"].let { jsonPlates ->
-                    if (jsonPlates == null || jsonPlates.isJsonNull)
+        JsonParser().parse(favouritesString).asJsonObject.entrySet().forEach { (name, timetables) ->
+            timetables.asJsonArray.map {
+                val plates = it.asJsonObject["plates"].let {
+                    if (it == null || it.isJsonNull)
                         null
                     else {
-                        HashSet<Plate.ID>().apply {
-                            jsonPlates.asJsonArray.map {
-                                Plate.ID(it.asJsonObject["line"].asString,
-                                        it.asJsonObject["stop"].asString,
-                                        it.asJsonObject["headsign"].asString)
+                        it.asJsonArray.map {
+                            it.asJsonObject.let {
+                                Plate.ID(it["line"].asString, it["stop"].asString, it["headsign"].asString)
                             }
-                        }
+                        }.toHashSet()
                     }
                 }
-                stopSegment.plates = plates
-                stopSegment
+                StopSegment(it.asJsonObject["stop"].asString, plates)
+            }.toHashSet().let {
+                favourites[name] = Favourite(name, it, context)
             }
-            favourites[name] = Favourite(name, timetables, context)
             positionIndex.add(name)
         }
     }




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 12096f90b1067602d0e911ae37e1c03e92b2928d..e6ef8860315c2d2aca5a481267fb924954bf9ceb 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/StopSegment.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/StopSegment.kt
@@ -67,10 +67,6 @@         if (plates == null)
             return false
 
         plates = plates!!.asSequence().filter { it != plateId }.toSet()
-        println("after remove:")
-        plates!!.forEach {
-            println(it)
-        }
         return true
     }