Bimba.git

commit 401798218c0f07de6d0569546190cd84f735ed6b

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

some optimisation

 .idea/misc.xml | 2 
 app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt | 4 
 app/src/main/java/ml/adamsprogs/bimba/models/DeparturesAdapter.kt | 2 
 app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt | 7 
 app/src/main/java/ml/adamsprogs/bimba/models/FavouritesAdapter.kt | 2 
 app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt | 83 


diff --git a/.idea/misc.xml b/.idea/misc.xml
index 635999df1e86791ad3787e455b4524e4d8879b93..ba7052b8197ddf8ba8756022d905d03055c7ad60 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -24,7 +24,7 @@         
       </value>
     </option>
   </component>
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/build/classes" />
   </component>
   <component name="ProjectType">




diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt b/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt
index 0eb7397205931b86245248b00c12bde5eb2b3fa2..0fef8f5355d832c1888eb0fd17787f46d3e5b3c0 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt
@@ -9,7 +9,7 @@ import kotlin.collections.ArrayList
 import kotlin.collections.HashMap
 
 data class Departure(val line: AgencyAndId, val mode: List<Int>, val time: Int, val lowFloor: Boolean, //time in seconds since midnight
-                     val modification: List<String>, val direction: String, val vm: Boolean = false,
+                     val modification: List<String>, val headsign: String, val vm: Boolean = false,
                      var tomorrow: Boolean = false, val onStop: Boolean = false) {
 
     val isModified: Boolean
@@ -18,7 +18,7 @@             return modification.isNotEmpty()
         }
 
     override fun toString(): String {
-        return "$line|${mode.joinToString(";")}|$time|$lowFloor|${modification.joinToString(";")}|$direction|$vm|$tomorrow|$onStop"
+        return "$line|${mode.joinToString(";")}|$time|$lowFloor|${modification.joinToString(";")}|$headsign|$vm|$tomorrow|$onStop"
     }
 
     fun copy(): Departure {




diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/DeparturesAdapter.kt b/app/src/main/java/ml/adamsprogs/bimba/models/DeparturesAdapter.kt
index cbe2a96fbfe7eab93b3d26dbc52929d91843414d..d47a9ce9948e66a7f748dcf55297573e2f92ba9a 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/DeparturesAdapter.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/DeparturesAdapter.kt
@@ -67,7 +67,7 @@             context.getString(R.string.now)
 
         line?.text = departure.lineText
         time?.text = timeString
-        direction?.text = context.getString(R.string.departure_to, departure.direction)
+        direction?.text = context.getString(R.string.departure_to, departure.headsign)
         val icon = holder?.typeIcon
         if (departure.vm)
             icon?.setImageDrawable(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_departure_vm, context.theme))




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 c0b449335f8183356dce641f4aa616632b550b04..c3eec8df843bb466a90f94a2a2662a69602ad8ca 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt
@@ -167,14 +167,15 @@         tomorrowDepartures.forEach { twoDayDepartures.add(it) }
         return twoDayDepartures
     }
 
-    fun allDepartures(): Map<AgencyAndId, List<Departure>> { // fixme<c:optimisation> departures through vm not the other way around
-        val departures = timetable.getStopDepartures(timetables) as HashMap<AgencyAndId, ArrayList<Departure>>
-
+    fun allDepartures(): Map<AgencyAndId, List<Departure>> {
         if (vmDepartures.isNotEmpty()) {
+            val departures = HashMap<AgencyAndId, ArrayList<Departure>>()
             val today = timetable.getServiceForToday()
             departures[today] = vmDepartures.flatMap { it.value } as ArrayList<Departure>
+            return departures
         }
 
+        val departures = timetable.getStopDepartures(timetables)
         return Departure.rollDepartures(departures)
     }
 




diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/FavouritesAdapter.kt b/app/src/main/java/ml/adamsprogs/bimba/models/FavouritesAdapter.kt
index 76ae3f570fae1f4eb659ca1d82345507e86dfa0e..9ebfe9ef032d96f9637965f1e344239cae16643d 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/FavouritesAdapter.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/FavouritesAdapter.kt
@@ -65,7 +65,7 @@                 val interval = nextDeparture.timeTill(true)
                 if (interval < 0)
                     return@thread
                 nextDepartureText = context.getString(Declinator.decline(interval), interval.toString())
-                nextDepartureLineText = context.getString(R.string.departure_to_line, nextDeparture.line, nextDeparture.direction)
+                nextDepartureLineText = context.getString(R.string.departure_to_line, nextDeparture.line, nextDeparture.headsign)
             } else {
                 nextDepartureText = context.getString(R.string.no_next_departure)
                 nextDepartureLineText = ""




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 748fc1ee4cd3fbed00bdaa7f49db09b015a6eac3..709d5f55a6c5d6ed3f1d82a16b67e58343d4412e 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt
@@ -204,24 +204,54 @@         throw IllegalArgumentException("Route $lineId not in store")
     }
 
     fun getStopDepartures(stopId: AgencyAndId): Map<AgencyAndId, List<Departure>> {
-        val plates = HashSet<Plate>()
-        val toGet = HashSet<Plate>()
+        println("getStopDepartures: ${JCalendar.getInstance().timeInMillis}")
+        val trips = getTripsForStop(stopId)
+        val segment = StopSegment(stopId, null)
+        segment.fillPlates()
+        return getStopDeparturesBySegment(segment, trips)
+    }
+
+    private fun getStopDeparturesBySegment(segment: StopSegment, trips: Map<String, Trip>): HashMap<AgencyAndId, List<Departure>> {
+        println("getStopDeparturesBySegment: ${JCalendar.getInstance().timeInMillis}")
+        val departures = HashMap<AgencyAndId, ArrayList<Departure>>()
+        val settings = CsvParserSettings()
+        settings.format.quote = '"'
+        settings.format.setLineSeparator("\r\n")
+        settings.isHeaderExtractionEnabled = true
+        var file = File(filesDir, "gtfs_files/stop_times_${segment.stop.id}.txt")
+        val parser = CsvParser(settings)
+        val tripsInStop = HashMap<String, Pair<Int, Int>>()
+        parser.parseAll(file).forEach {
+            tripsInStop[it[0]] = Pair(parseTime(it[2]), it[4].toInt())
+        }
+        println("getStopDeparturesBySegment: ${JCalendar.getInstance().timeInMillis}")
 
-        val trips = getTripsForStop(stopId)
-        trips.values.map {
-            Plate(Plate.ID(it.routeId, stopId, it.headsign), null)
-        }.forEach {
-                    toGet.add(it)
-                }
+        file = File(filesDir, "gtfs_files/calendar.txt")
+        parser.parseAll(file).forEach {
+            departures[AgencyAndId(it[0])] = ArrayList()
+        }
 
-        getStopDeparturesByPlates(toGet, trips).forEach { plates.add(it) }
-        return Plate.join(plates)
+        tripsInStop.forEach { //fixme this part is long --- cache is the only option
+            departures[trips[it.key]!!.serviceId]!!.add(Departure(trips[it.key]!!.routeId,
+                    calendarToMode(trips[it.key]!!.serviceId),
+                    it.value.first, trips[it.key]!!.wheelchairAccessible,
+                    explainModification(trips[it.key]!!, it.value.second),
+                    trips[it.key]!!.headsign))
+        }
+        println("getStopDeparturesBySegment: ${JCalendar.getInstance().timeInMillis}")
+        val sortedDepartures = HashMap<AgencyAndId, List<Departure>>()
+        departures.keys.forEach {
+            sortedDepartures[it] = departures[it]!!.sortedBy { it.time }
+        }
+        println("</>: ${JCalendar.getInstance().timeInMillis}")
+        println("</>: ${JCalendar.getInstance().timeInMillis}")
+        return sortedDepartures
     }
 
     fun getStopDepartures(plates: Set<Plate>): Map<AgencyAndId, ArrayList<Departure>> {
         val trips = HashMap<String, Trip>()
-        //todo get trips
 
+        tripsCache.forEach { trips[it.key] = tripFromCache(it.key) }
         return Plate.join(getStopDeparturesByPlates(plates, trips))
     }
 
@@ -232,7 +262,7 @@
         return plates.map { getStopDeparturesByPlate(it, trips) }.toSet()
     }
 
-    private fun getStopDeparturesByPlate(plate: Plate, trips: Map<String, Trip>): Plate { //fixme<c:optimisation> takes too long
+    private fun getStopDeparturesByPlate(plate: Plate, trips: Map<String, Trip>): Plate { //fixme<c:optimisation> takes too long --- cache
         println("getStopDeparturesByPlate: ${JCalendar.getInstance().timeInMillis}")
         val resultPlate = Plate(Plate.ID(plate.id), HashMap())
         val stopTimes = HashMap<String, Map<String, Any>>()
@@ -251,12 +281,7 @@
         trips.forEach {
             if (it.value.routeId == plate.id.line &&
                     it.value.headsign == plate.id.headsign) {
-                val cal = JCalendar.getInstance()
-                val (h, m, s) = (stopTimes[it.key]!!["departure_time"] as String).split(":")
-                cal.set(JCalendar.HOUR_OF_DAY, h.toInt())
-                cal.set(JCalendar.MINUTE, m.toInt())
-                cal.set(JCalendar.SECOND, s.toInt())
-                val time = cal.secondsAfterMidnight()
+                val time = parseTime(stopTimes[it.key]!!["departure_time"] as String)
                 val serviceId = it.value.serviceId
                 val mode = calendarToMode(serviceId)
                 val lowFloor = it.value.wheelchairAccessible
@@ -271,6 +296,15 @@             }
         }
         println("</>: ${JCalendar.getInstance().timeInMillis}")
         return resultPlate
+    }
+
+    private fun parseTime(time: String): Int {
+        val cal = JCalendar.getInstance()
+        val (h, m, s) = time.split(":")
+        cal.set(JCalendar.HOUR_OF_DAY, h.toInt())
+        cal.set(JCalendar.MINUTE, m.toInt())
+        cal.set(JCalendar.SECOND, s.toInt())
+        return cal.secondsAfterMidnight()
     }
 
     fun calendarToMode(serviceId: AgencyAndId): List<Int> {
@@ -396,12 +430,17 @@
         val filteredTrips = HashMap<String, Trip>()
 
         tripIds.forEach {
-            filteredTrips[it] = Trip(AgencyAndId(tripsCache[it]!![0]),
-                    AgencyAndId(tripsCache[it]!![1]), createTripId(tripsCache[it]!![2]),
-                    tripsCache[it]!![3], Integer.parseInt(tripsCache[it]!![4]),
-                    AgencyAndId(tripsCache[it]!![5]), tripsCache[it]!![6] == "1")
+            filteredTrips[it] = tripFromCache(it)
         }
         return filteredTrips
+    }
+
+    private fun tripFromCache(id: String):Trip {
+        return Trip(AgencyAndId(tripsCache[id]!![0]),
+                AgencyAndId(tripsCache[id]!![1]), createTripId(tripsCache[id]!![2]),
+                tripsCache[id]!![3], Integer.parseInt(tripsCache[id]!![4]),
+                AgencyAndId(tripsCache[id]!![5]), tripsCache[id]!![6] == "1")
+
     }
 
     private fun createTripId(rawId: String): Trip.ID {