Bimba.git

commit 6aa58435b7ad5e0f726a90a7c4f7e286c3913e94

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

fix night bg on first run & fix no db when there is & begin trip graphs

 app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt | 2 
 app/src/main/java/ml/adamsprogs/bimba/activities/NoDbActivity.kt | 12 
 app/src/main/java/ml/adamsprogs/bimba/activities/SplashActivity.kt | 14 
 app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt | 74 
 app/src/main/java/ml/adamsprogs/bimba/models/gtfs/Stop.kt | 5 


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 c640aa2fcdca835b67a8d7842f935e972950c2b9..57cfa34269959a7d8366d5dba4979cb0b9378c4c 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
@@ -49,7 +49,6 @@     }
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
-        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO) //fixme on first run after kill Dash is night (but just background)
         setContentView(R.layout.activity_dash)
 
         setSupportActionBar(toolbar)
@@ -153,6 +152,7 @@         }
     }
 
     private fun warnTimetableValidity() {
+        //todo not on turn
         val validTill = timetable.getValidTill()
         val today = Calendar.getInstance().toIsoDate()
         val tomorrow = Calendar.getInstance().apply {




diff --git a/app/src/main/java/ml/adamsprogs/bimba/activities/NoDbActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/activities/NoDbActivity.kt
index 91c215fc7f8a067541bcc9c03258c8f4ecd07ba7..1108febb0d070c2a7bf6af3a11e75250c77f17ee 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/NoDbActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/NoDbActivity.kt
@@ -36,11 +36,13 @@     }
 
     override fun onResume() {
         super.onResume()
-        val timetable = Timetable.getTimetable(this)
-        if (!timetable.isEmpty()){
-            startActivity(Intent(this, DashActivity::class.java))
-            finish()
-        }
+        try {
+            val timetable = Timetable.getTimetable(this, true)
+            if (!timetable.isEmpty()) {
+                startActivity(Intent(this, DashActivity::class.java))
+                finish()
+            }
+        } catch (e:Exception){}
         var filter = IntentFilter(TimetableDownloader.ACTION_DOWNLOADED)
         filter.addCategory(Intent.CATEGORY_DEFAULT)
         registerReceiver(timetableDownloadReceiver, filter)




diff --git a/app/src/main/java/ml/adamsprogs/bimba/activities/SplashActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/activities/SplashActivity.kt
index 1a64e08790453363e9d7db46c4ca24b58a7a6e20..5a2d486aa09d33eae44c5d3d1686830a8adcbb2d 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/SplashActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/SplashActivity.kt
@@ -4,6 +4,7 @@ import android.support.v7.app.AppCompatActivity
 import android.os.Bundle
 import android.content.Intent
 import android.database.sqlite.SQLiteCantOpenDatabaseException
+import android.support.v7.app.AppCompatDelegate
 import ml.adamsprogs.bimba.models.Timetable
 import java.io.FileNotFoundException
 
@@ -12,11 +13,16 @@ class SplashActivity : AppCompatActivity() {
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
-        val timetable = Timetable.getTimetable(this)
-        if (timetable.isEmpty())
+        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO)
+        try {
+            val timetable = Timetable.getTimetable(this)
+            if (timetable.isEmpty())
+                startActivity(Intent(this, NoDbActivity::class.java))
+            else
+                startActivity(Intent(this, DashActivity::class.java))
+        } catch (e:Exception) {
             startActivity(Intent(this, NoDbActivity::class.java))
-        else
-            startActivity(Intent(this, DashActivity::class.java))
+        }
         finish()
     }
 }




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 a33feb5486c9a061992e83551b40ed8c95096833..04fd54447771f7e55a82d4992c10f7d00462a156 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt
@@ -305,7 +305,7 @@             result = !cursor.moveToNext()
         } catch (e: Exception) {
             result = true
         } finally {
-            cursor!!.close()
+            cursor?.close()
         }
         return result
     }
@@ -373,11 +373,12 @@         cursor.close()
         return plates
     }
 
-    fun getTripGraphs(id: AgencyAndId): Array<Pair<HashMap<Int, HashSet<Int>>, String>> {
-        val trips = arrayOf(HashMap<String, HashMap<Int, Int>>(), HashMap())
-        val headsigns = arrayOf("", "")
-        val cursor = db.rawQuery("select trip_id, trip_headsign, direction_id, stop_id, stop_sequence " +
-                "from stop_times natural join trips " +
+    fun getTripGraphs(id: AgencyAndId): Array<TripGraph> {
+        val graphs = arrayOf(TripGraph(), TripGraph())
+
+        val cursor = db.rawQuery("select trip_id, trip_headsign, direction_id, stop_id, " +
+                "stop_sequence, pickup_type, stop_name, zone_id " +
+                "from stop_times natural join trips natural join stops" +
                 "where route_id = ?", arrayOf(id.id))
 
         while (cursor.moveToNext()) {
@@ -386,39 +387,50 @@             val headsign = cursor.getString(1)
             val direction = cursor.getInt(2)
             val stopId = cursor.getInt(3)
             val sequence = cursor.getInt(4)
-            if (trips[direction][trip] == null)
-                trips[direction][trip] = HashMap()
-            trips[direction][trip]!![sequence] = stopId
-            if (trip[trip.length - 1] == '+')
-                headsigns[direction] = headsign
+            val pickupType = cursor.getInt(5)
+            val stopName = cursor.getString(6)
+            val zone = cursor.getString(7)
+
+            if (trip.contains('+')) {
+                graphs[direction].mainTrip[stopId] = sequence
+                graphs[direction].headsign = headsign
+            }
+
+            if (graphs[direction].otherTrips[trip] == null)
+                graphs[direction].otherTrips[trip] = HashMap()
+            graphs[direction].otherTrips[trip]?.put(sequence, Stop(stopId, null, stopName, null, null, zone[0], pickupType == 3))
         }
 
         cursor.close()
 
-        val result = arrayOf(Pair<HashMap<Int, HashSet<Int>>, String>(HashMap(), headsigns[0]), Pair(HashMap(), headsigns[1]))
-
-        var tripNo = 0
-        trips.forEach {
-            it.forEach {
-                var i = 0
-                val size = it.value.size
-                val list = it.value
-                it.value.toSortedMap(
-                        Comparator { o1, o2 ->
-                            o1.compareTo(o2)
+        graphs.forEach {
+            val thisTripGraph = it
+            it.otherTrips.forEach {
+                val tripId = it.key
+                val trip = it.value
+                it.value.keys.sortedBy { it }.forEach {
+                    if (thisTripGraph.tripsMetadata[tripId] == "" || thisTripGraph.tripsMetadata[tripId] == "o")
+                        if (thisTripGraph.mainTrip[trip[it]!!.id] != null) {
+                            val mainLayer = thisTripGraph.mainTrip[trip[it]!!.id]!!
+                            if (it == 0 || thisTripGraph.tripsMetadata[tripId]!![0] == 'o') {
+                                thisTripGraph.tripsMetadata[tripId] = "o|$mainLayer|$it"
+                            } else {
+                                val startingLayer = mainLayer - it + 1
+                                thisTripGraph.tripsMetadata[tripId] = "i|$startingLayer|$it"
+                            }
                         }
-                ).values.forEach {
-                    if (i < size - 1) {
-                        if (result[tripNo].first[it] == null)
-                            result[tripNo].first[it] = HashSet()
-                        result[tripNo].first[it]!!.add(list[i + 1]!!)
-                    }
-                    i++
                 }
             }
-            tripNo++
         }
-        return result
+
+        return graphs
+    }
+
+    class TripGraph {
+        var headsign = ""
+        val mainTrip = HashMap<Int, Int>()
+        val otherTrips = HashMap<String, HashMap<Int, Stop>>()
+        val tripsMetadata = HashMap<String, String>()
     }
 }
 




diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/gtfs/Stop.kt b/app/src/main/java/ml/adamsprogs/bimba/models/gtfs/Stop.kt
new file mode 100644
index 0000000000000000000000000000000000000000..a98a2517e0fe34ae0e559e46c9d398ae51605de1
--- /dev/null
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/gtfs/Stop.kt
@@ -0,0 +1,5 @@
+package ml.adamsprogs.bimba.models.gtfs
+
+data class Stop(val id: Int, val code: String?, val name: String, val latitude: Float?, val Longitude: Float?, val zone: Char, val onDemand: Boolean = false) {
+    override fun toString() = "$id: $name ($zone)"
+}
\ No newline at end of file