Bimba.git

commit 90e83e3eef9a25faf7344f22b8404b7accc79db0

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

VmClient and CacheManager fixed

 app/src/main/java/ml/adamsprogs/bimba/CacheManager.kt | 11 ++
 app/src/main/java/ml/adamsprogs/bimba/VmClient.kt | 33 ++++----
 app/src/main/java/ml/adamsprogs/bimba/extensions.kt | 19 +++++
 app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt | 6 +


diff --git a/app/src/main/java/ml/adamsprogs/bimba/CacheManager.kt b/app/src/main/java/ml/adamsprogs/bimba/CacheManager.kt
index 53d2b0f13580126382cd493b89c763f0ea830674..8626c0c09ed242176281072d9918e86ea60c11a3 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/CacheManager.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/CacheManager.kt
@@ -3,6 +3,7 @@
 import android.content.Context
 import android.content.SharedPreferences
 import ml.adamsprogs.bimba.models.Plate
+import org.onebusaway.gtfs.model.AgencyAndId
 
 class CacheManager private constructor(context: Context) {
     companion object {
@@ -25,7 +26,13 @@     private var cache: HashMap = HashMap()
     private var cacheHits: HashMap<String, Int> = HashMap()
 
     fun keys(): List<Plate> {
-        return cache.map { Plate(it.key.split("@")[0], it.key.split("@")[1], null) }
+        return cache.map {
+            Plate(
+                    AgencyAndId.convertFromString(it.key.split("@")[0]),
+                    AgencyAndId.convertFromString(it.key.split("@")[1].split(">")[0]),
+                    it.key.split(">")[1],
+                    null)
+        }
     }
 
     fun hasAll(plates: HashSet<Plate>): Boolean {
@@ -125,5 +132,5 @@         }
         return result
     }
 
-    private fun key(plate: Plate) = "${plate.line}@${plate.stop}"
+    private fun key(plate: Plate) = "${plate.line}@${plate.stop}>${plate.headsign}"
 }
\ No newline at end of file




diff --git a/app/src/main/java/ml/adamsprogs/bimba/VmClient.kt b/app/src/main/java/ml/adamsprogs/bimba/VmClient.kt
index d7dac071c28df4411be94e16da83fa5c88fea83e..d5fcc985e20d26cea9e0b85f8c508045a2c6efa1 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/VmClient.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/VmClient.kt
@@ -6,20 +6,21 @@ import android.util.Log
 import ml.adamsprogs.bimba.models.*
 import okhttp3.*
 import com.google.gson.Gson
+import org.onebusaway.gtfs.model.AgencyAndId
 import java.io.IOException
 import java.util.*
 
 
 class VmClient : IntentService("VmClient") {
     companion object {
-        val ACTION_DEPARTURES_CREATED = "ml.adamsprogs.bimba.departuresCreated"
-        val ACTION_NO_DEPARTURES = "ml.adamsprogs.bimba.noVM"
-        val EXTRA_STOP_SYMBOL = "stopSymbol"
-        val EXTRA_LINE_NUMBER = "lineNumber"
-        val EXTRA_REQUESTER = "requester"
-        val EXTRA_DEPARTURES = "departures"
-        val EXTRA_SIZE = "size"
-        val EXTRA_ID = "id"
+        const val ACTION_DEPARTURES_CREATED = "ml.adamsprogs.bimba.departuresCreated"
+        const val ACTION_NO_DEPARTURES = "ml.adamsprogs.bimba.noVM"
+        const val EXTRA_STOP_SYMBOL = "stopSymbol"
+        const val EXTRA_LINE_NUMBER = "lineNumber"
+        const val EXTRA_REQUESTER = "requester"
+        const val EXTRA_DEPARTURES = "departures"
+        const val EXTRA_SIZE = "size"
+        const val EXTRA_ID = "id"
     }
 
     override fun onHandleIntent(intent: Intent?) {
@@ -39,7 +40,7 @@             if (stopSymbol == null) {
                 sendNullResult(requester, id, size)
                 return
             }
-            val lineNumber = intent.getStringExtra(EXTRA_LINE_NUMBER)
+            val lineId = intent.getSerializableExtra(EXTRA_LINE_NUMBER) as AgencyAndId?
 
             val client = OkHttpClient()
             val url = "http://www.peka.poznan.pl/vm/method.vm?ts=${Calendar.getInstance().timeInMillis}"
@@ -73,16 +74,18 @@             val javaRootMapObject = Gson().fromJson(responseBody, HashMap::class.java)
             val times = (javaRootMapObject["success"] as Map<*, *>)["times"] as List<*>
             val date = Calendar.getInstance()
             val todayDay = "${date.get(Calendar.DATE)}".padStart(2, '0')
-            val todayMode = date.getMode()
+            val timetable = Timetable.getTimetable()
+            val todayMode = timetable.calendarToMode(timetable.getServiceForToday().id.toInt())
             val departuresToday = ArrayList<Departure>()
+            val lineNumber = if (lineId != null) timetable.getLineNumber(lineId) else null
             for (time in times) {
                 val t = time as Map<*, *>
-                if (lineNumber == null || t["line"] == lineNumber) {
+                if (lineId == null || t["line"] == lineNumber) {
                     val departureDay = (t["departure"] as String).split("T")[0].split("-")[2]
-                    val departureTimeRaw = (t["departure"] as String).split("T")[1].split(":")
-                    val departureTime = "${departureTimeRaw[0]}:${departureTimeRaw[1]}"
-                    val departure = Departure(t["line"] as String, todayMode, departureTime, false,
-                            null, t["direction"] as String, t["realTime"] as Boolean,
+                    val departureTime = calendarFromIso(t["departure"] as String).secondsAfterMidnight()
+                    val departure = Departure(lineId?: timetable.getLineForNumber(t["line"] as String)!!
+                            , todayMode, departureTime, false,
+                            ArrayList(), t["direction"] as String, t["realTime"] as Boolean,
                             departureDay != todayDay, t["onStopPoint"] as Boolean)
                     departuresToday.add(departure)
                 }




diff --git a/app/src/main/java/ml/adamsprogs/bimba/extensions.kt b/app/src/main/java/ml/adamsprogs/bimba/extensions.kt
index 245856cb99111d38675fc1780a5dd7047b9e9095..f3498e7ae51d5074c62ffb8373c67bbc49847d12 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/extensions.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/extensions.kt
@@ -1,6 +1,7 @@
 package ml.adamsprogs.bimba
 
 import ml.adamsprogs.bimba.models.Timetable
+import java.text.SimpleDateFormat
 import java.util.*
 
 internal fun Calendar.getMode(): String {
@@ -33,4 +34,22 @@     this.set(Calendar.MINUTE, minute)
     this.set(Calendar.SECOND, second)
     this.set(Calendar.MILLISECOND, 0)
     return this
+}
+
+internal fun Calendar.secondsAfterMidnight(): Int {
+    val hour = this.get(Calendar.HOUR_OF_DAY)
+    val minute = this.get(Calendar.MINUTE)
+    val second = this.get(Calendar.SECOND)
+    return hour * 3600 + minute * 60 + second
+}
+
+const val ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
+
+fun calendarFromIso(iso: String): Calendar { // check
+    val calendar = Calendar.getInstance()
+    val dateFormat = SimpleDateFormat(ISO_8601_DATE_FORMAT)
+    val date = dateFormat.parse(iso)
+    //date.hours = date.getHours() - 1 //fixme why?
+    calendar.time = date
+    return calendar
 }
\ No newline at end of file




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 934fcdf08d2affbcc102072fd35a2a90aa2601ab..1333e60a1231feb2fc795974727b8a8e89a01520 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt
@@ -168,7 +168,7 @@                 }
         return p
     }
 
-    private fun calendarToMode(serviceId: Int): List<Int> {
+    fun calendarToMode(serviceId: Int): List<Int> {
         val calendar = store.getCalendarForId(serviceId)
         val days = ArrayList<Int>()
         if (calendar.monday == 1) days.add(0)
@@ -249,6 +249,10 @@             if (it.saturday == 1 && day == Calendar.SATURDAY) return it.serviceId
             if (it.sunday == 1 && day == Calendar.SUNDAY) return it.serviceId
         }
         throw IllegalArgumentException()
+    }
+
+    fun getLineForNumber(number: String): AgencyAndId? {
+            return store.allRoutes.find { it.shortName == number }?.id
     }
 }