Bimba.git

commit dfccb646c9a2fa932c05c541f6b0fedeca6873d7

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

fix: transition from favourite to StopActivity

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


diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser
index 51d3d2e81267d403807e7b0595520f38030e5e6d..7ec50e8e551c320d324c16f1c16c13a7472db0ca 100644
Binary files a/.idea/caches/build_file_checksums.ser and b/.idea/caches/build_file_checksums.ser differ




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 683eab7c01f99fe84388609b99d4006e00fd4575..eb6692db50c2b41839ea460551e693504f07a724 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
@@ -389,7 +389,7 @@             return when (item.itemId) {
                 R.id.action_merge -> {
                     val selectedPositions = adapter.getSelectedItems()
                     val selectedNames = selectedPositions.map { favourites[it]?.name }.filter { it != null }.map { it!! }
-                    favourites.merge(selectedNames)
+                    favourites.merge(selectedNames, this@DashActivity)
 
                     adapter.notifyItemChanged(selectedPositions.min()!!)
                     (1 until selectedPositions.size).forEach {




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 6d3bf82f70e0f76eb6a68405b1e6d93742cf7f93..c96770aac28a6e666db0e77f4df263fad2d63654 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
@@ -127,7 +127,7 @@         fab.setOnClickListener {
             if (!favourites.has(stopSymbol)) {
                 val items = HashSet<StopSegment>()
                 items.add(stopSegment!!)
-                favourites.add(stopSymbol, items)
+                favourites.add(stopSymbol, 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)
@@ -153,6 +153,7 @@             favourite!!.registerOnVm(receiver, context)
     }
 
     override fun onVm(vmDepartures: Set<Departure>?, plateId: Plate.ID) {
+        //fixme do we give up too fast?
         if (vmDepartures == null && this.vmDepartures.isEmpty() && hasDepartures) {
             if (ticked()) {
                 refreshAdapterFromStop()
@@ -181,6 +182,7 @@         try {
             Snackbar.make(findViewById(R.id.drawer_layout), message, Snackbar.LENGTH_LONG).show()
         } catch (e: IllegalArgumentException) {
         }
+        timetable = Timetable.getTimetable(this, true)
         refreshAdapterFromStop()
     }
 




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 1128cf7b5426f3806e85f28d6680668bd3022b28..2c8e92efd8f69559ae0ec5cc2eadda253d49c80f 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/collections/FavouriteStorage.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/collections/FavouriteStorage.kt
@@ -44,7 +44,7 @@                 }
                 stopSegment.plates = plates
                 stopSegment
             }
-            favourites[name] = Favourite(name, timetables)
+            favourites[name] = Favourite(name, timetables, context)
             positionIndex.add(name)
         }
     }
@@ -53,9 +53,9 @@     override fun iterator(): Iterator = favourites.values.iterator()
 
     fun has(name: String): Boolean = favourites.contains(name)
 
-    fun add(name: String, timetables: HashSet<StopSegment>) {
+    fun add(name: String, timetables: HashSet<StopSegment>, context: Context) {
         if (favourites[name] == null) {
-            favourites[name] = Favourite(name, timetables)
+            favourites[name] = Favourite(name, timetables, context)
             addIndex(name)
             serialize()
         }
@@ -111,7 +111,7 @@         editor.apply()
 
     }
 
-    fun merge(names: List<String>) {
+    fun merge(names: List<String>, context: Context) {
         if (names.size < 2)
             return
 
@@ -127,7 +127,7 @@         val now = Calendar.getInstance().secondsAfterMidnight()
         newCache.forEach {
             it.value.sortBy { it.timeTill(now) }
         }
-        val newFavourite = Favourite(names[0], HashSet(), newCache)
+        val newFavourite = Favourite(names[0], HashSet(), newCache, context)
         for (name in names) {
             newFavourite.segments.addAll(favourites[name]!!.segments)
             favourites.remove(name)




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 95e06d891e869f55f662beab1a639106dee8caf0..066091d80e19d3e01ae91d4914a28ab4758ad537 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt
@@ -5,11 +5,15 @@ import android.os.*
 import ml.adamsprogs.bimba.*
 import ml.adamsprogs.bimba.datasources.VmClient
 import ml.adamsprogs.bimba.models.gtfs.AgencyAndId
+import java.io.File
+import java.math.BigInteger
+import java.security.SecureRandom
 import java.util.Calendar
 import kotlin.collections.*
 
 class Favourite : Parcelable, MessageReceiver.OnVmListener {
     private var isRegisteredOnVmListener: Boolean = false
+    private val cacheDir: File
     var name: String
         private set
     var segments: HashSet<StopSegment>
@@ -45,24 +49,37 @@         array.forEach {
             set.add(it as StopSegment)
         }
         this.segments = set
-        val mapString = parcel.readString()
+        this.cacheDir = File(parcel.readString())
+        val mapDir = File(parcel.readString())
+
+        val mapString = mapDir.readText()
+        val ile = mapString.length / 1024
+
+        for (i in 0 until ile) {
+            println(mapString.slice(1024 * i until 1024 * (i+1)))
+        }
+
+        println(mapString)
         val map = HashMap<AgencyAndId, List<Departure>>()
-        mapString.safeSplit("\n").forEach {
-            val (k, v) = it.split(":")
-            map[AgencyAndId(k)] = v.split(",").map { Departure.fromString(it) }
+        mapString.safeSplit("%").forEach {
+            val (k, v) = it.split("#")
+            map[AgencyAndId(k)] = v.split("&").map { Departure.fromString(it) }
         }
         this.fullDepartures = map
+        mapDir.delete()
     }
 
-    constructor(name: String, segments: HashSet<StopSegment>, cache: Map<AgencyAndId, List<Departure>>) {
+    constructor(name: String, segments: HashSet<StopSegment>, cache: Map<AgencyAndId, List<Departure>>, context: Context) {
         this.fullDepartures = cache
         this.name = name
         this.segments = segments
+        this.cacheDir = context.cacheDir
     }
 
-    constructor(name: String, timetables: HashSet<StopSegment>) {
+    constructor(name: String, timetables: HashSet<StopSegment>, context: Context) {
         this.name = name
         this.segments = timetables
+        this.cacheDir = context.cacheDir
 
     }
 
@@ -74,17 +91,24 @@     override fun writeToParcel(dest: Parcel?, flags: Int) {
         dest?.writeString(name)
         val parcelableSegments = segments.map { it }.toTypedArray()
         dest?.writeParcelableArray(parcelableSegments, flags)
+        dest?.writeString(cacheDir.absolutePath)
+
+        val bytes = ByteArray(4) { 0 }
+        SecureRandom().nextBytes(bytes)
+        val mapFile = File(cacheDir, BigInteger(1, bytes).toString(16))
+        dest?.writeString(mapFile.absolutePath)
+
         var isFirst = true
         var map = ""
         fullDepartures.forEach {
             if (isFirst)
                 isFirst = false
             else
-                map += '\n'
+                map += '%'
 
-            map += "${it.key}:${it.value.joinToString(",") { it.toString() }}"
+            map += "${it.key}#${it.value.joinToString("&") { it.toString() }}"
         }
-        dest?.writeString(map)
+        mapFile.writeText(map)
     }
 
     private fun filterVmDepartures() {