Bimba.git

commit c931a8b155b91f616b144030988e5478797ab5bc

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

refreshing db connection when possible

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


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 de7311f4afc699144bab997b1efed17f81366798..6d3bf82f70e0f76eb6a68405b1e6d93742cf7f93 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
@@ -50,7 +50,7 @@     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_stop)
 
-        timetable = Timetable.getTimetable()
+        timetable = Timetable.getTimetable(this)
 
         sourceType = intent.getStringExtra(SOURCE_TYPE)
 




diff --git a/app/src/main/java/ml/adamsprogs/bimba/activities/StopSpecifyActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/activities/StopSpecifyActivity.kt
index 091fa87ccc6f69955ce5ea3d5be52f139a928760..eacf1d6d40244cf2c40982f9514211603d2c683f 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopSpecifyActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopSpecifyActivity.kt
@@ -15,7 +15,6 @@ import android.support.v7.widget.LinearLayoutManager
 import android.support.v7.widget.RecyclerView
 import android.view.LayoutInflater
 
-//todo in night dark on dark
 class StopSpecifyActivity : AppCompatActivity() {
 
     companion object {
@@ -29,7 +28,7 @@         setContentView(R.layout.activity_stop_specify)
 
         val ids = intent.getStringExtra(EXTRA_STOP_IDS).split(",").map { AgencyAndId(it) }.toSet()
         val name = intent.getStringExtra(EXTRA_STOP_NAME)
-        val timetable = Timetable.getTimetable()
+        val timetable = Timetable.getTimetable(this)
         val headlines = timetable.getHeadlinesForStop(ids)
 
         val layoutManager = LinearLayoutManager(this)




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 04fd54447771f7e55a82d4992c10f7d00462a156..2f90bc4c61040997a02a5a7a4c227b27854a96e1 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt
@@ -18,21 +18,32 @@
         fun getTimetable(context: Context? = null, force: Boolean = false): Timetable {
             return if (timetable == null || force)
                 if (context != null) {
-                    timetable = Timetable()
-                    timetable!!.filesDir = context.getSecondaryExternalFilesDir()
-                    val dbFile = File(timetable!!.filesDir, "timetable.db")
-                    timetable!!.db = SQLiteDatabase.openDatabase(dbFile.path, null, SQLiteDatabase.OPEN_READONLY)
+                    constructTimetable(context)
                     timetable!!
                 } else
-                    throw IllegalArgumentException("new timetable requested and no appContext given")
-            else
+                    throw IllegalArgumentException("new timetable requested and no `context` given")
+            else if (context != null) {
+                try {
+                    constructTimetable(context)
+                    timetable!!
+                } catch (e: Exception) {
+                    timetable!!
+                }
+            } else
                 timetable!!
         }
+
+        private fun constructTimetable(context: Context) {
+            val timetable = Timetable()
+            val filesDir = context.getSecondaryExternalFilesDir()
+            val dbFile = File(filesDir, "timetable.db")
+            timetable.db = SQLiteDatabase.openDatabase(dbFile.path, null, SQLiteDatabase.OPEN_READONLY)
+            this.timetable = timetable
+        }
     }
 
     private lateinit var db: SQLiteDatabase
     private var _stops: List<StopSuggestion>? = null
-    private lateinit var filesDir: File
 
     fun refresh() {
     }