Bimba.git

commit defd8f9bb4c6041d669f106d49887f88afd3e91c

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

timetable validity guard

 app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt | 55 +
 app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt | 4 
 app/src/main/java/ml/adamsprogs/bimba/extensions.kt | 7 
 app/src/main/java/ml/adamsprogs/bimba/models/FavouriteStorage.kt | 1 
 app/src/main/res/values-de/strings.xml | 4 
 app/src/main/res/values-it/strings.xml | 2 
 app/src/main/res/values-pl/strings.xml | 2 
 app/src/main/res/values/strings.xml | 2 


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 648ec173b99655b90e453de672ced8c44999e08f..cb6051e824467c2c6afcd0a33304abfbe468d054 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
@@ -27,6 +27,8 @@ import ml.adamsprogs.bimba.models.suggestions.LineSuggestion
 import ml.adamsprogs.bimba.models.suggestions.StopSuggestion
 import android.support.v7.widget.DefaultItemAnimator
 import android.content.Intent
+import java.util.*
+import kotlin.collections.ArrayList
 
 //todo cards https://enoent.fr/blog/2015/01/18/recyclerview-basics/
 //todo searchView integration
@@ -57,7 +59,11 @@
         AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO)
         setSupportActionBar(toolbar)
 
+        timetable = Timetable.getTimetable(this)
+
         getSuggestions()
+
+        warnTimetableValidity()
 
         prepareFavourites()
 
@@ -150,6 +156,54 @@
         searchView.attachNavigationDrawerToMenuButton(drawer_layout as DrawerLayout)
     }
 
+    private fun warnTimetableValidity() {
+        val validTill = timetable.getValidTill()
+        val today = Calendar.getInstance().toIsoDate()
+        val tomorrow = Calendar.getInstance().apply {
+            this.add(Calendar.DAY_OF_MONTH, 1)
+        }.toIsoDate()
+
+        println(today)
+        println(tomorrow)
+        println(validTill)
+        try {
+            timetable.getServiceForToday()
+            if (today >= validTill) {
+                notifyTimetableValidity()
+                suggestions = ArrayList()
+                return
+            }
+        } catch (e: IllegalArgumentException) {
+            notifyTimetableValidity()
+            suggestions = ArrayList()
+            return
+        }
+
+        try {
+            timetable.getServiceForTomorrow()
+            if (tomorrow == validTill) {
+                notifyTimetableValidity(true)
+                return
+            }
+        } catch (e: IllegalArgumentException) {
+            notifyTimetableValidity(true)
+            return
+        }
+    }
+
+    private fun notifyTimetableValidity(warning: Boolean = false) {
+        val message = if (warning)
+            getString(R.string.timetable_validity_warning)
+        else
+            getString(R.string.timetable_validity_finished)
+        AlertDialog.Builder(context)
+                .setPositiveButton(context.getText(android.R.string.ok),
+                        { dialog: DialogInterface, _: Int -> dialog.cancel() })
+                .setCancelable(true)
+                .setMessage(message)
+                .create().show()
+    }
+
     private fun prepareFavourites() {
         favourites = FavouriteStorage.getFavouriteStorage(context)
         val layoutManager = LinearLayoutManager(context)
@@ -166,7 +220,6 @@         favouritesList.adapter.notifyDataSetChanged()
     }
 
     private fun getSuggestions() {
-        timetable = Timetable.getTimetable(this)
         suggestions = (timetable.getStopSuggestions(context) + timetable.getLineSuggestions()).sorted() //todo<p:v+1> + bike stations, &c
     }
 




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 79ee257a0ec0231d16868e4e484c4eb4764a1e62..cdf185e0a4daef0c479be5834e16551845eb8572 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
@@ -254,8 +254,8 @@             if (departures == null)
                 return PlaceholderFragment.newInstance(null, relativeTime)
             if (departures!!.isEmpty())
                 return PlaceholderFragment.newInstance(ArrayList(), relativeTime)
-            val sat = timetable.getServiceFor(Calendar.getInstance().get(Calendar.DAY_OF_WEEK))
-            val sun = timetable.getServiceFor(Calendar.getInstance().get(Calendar.DAY_OF_WEEK))
+            val sat = timetable.getServiceFor(Calendar.SATURDAY)
+            val sun = timetable.getServiceFor(Calendar.SUNDAY)
             val list: List<Departure> = when (position) {
                 1 -> departures!![sat] ?: ArrayList()
                 2 -> departures!![sun] ?: ArrayList()




diff --git a/app/src/main/java/ml/adamsprogs/bimba/extensions.kt b/app/src/main/java/ml/adamsprogs/bimba/extensions.kt
index cbfd948ff1eb54bffaccb8a4ba2cb1749d6f9ee8..f15b28914aa2591f5cea4924dd1776c7d926cc27 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/extensions.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/extensions.kt
@@ -26,6 +26,13 @@     val second = this.get(Calendar.SECOND)
     return hour * 3600 + minute * 60 + second
 }
 
+internal fun Calendar.toIsoDate(): String {
+    val year = this.get(Calendar.YEAR)
+    val month = String.format("%02d", this.get(Calendar.MONTH)+1)
+    val day = String.format("%02d", this.get(Calendar.DAY_OF_MONTH))
+    return "$year$month$day"
+}
+
 const val ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
 
 fun calendarFromIso(iso: String): Calendar { // check




diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/FavouriteStorage.kt b/app/src/main/java/ml/adamsprogs/bimba/models/FavouriteStorage.kt
index fc3f1e7a09584272032fc4d917b7b6f7eb362d33..0dbb60de3e15c7f9188a53bbc9f41de42ea939db 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/FavouriteStorage.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/FavouriteStorage.kt
@@ -4,7 +4,6 @@ import android.content.Context
 import android.content.SharedPreferences
 import com.google.gson.Gson
 import com.google.gson.JsonArray
-import com.google.gson.JsonElement
 import com.google.gson.JsonObject
 import ml.adamsprogs.bimba.MessageReceiver
 import ml.adamsprogs.bimba.models.gtfs.AgencyAndId




diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index f7c93fd89641366e223e94a97a7f760af3202e5f..958e22c9bfc3f10cc768bfac26ac8bc625eaac30 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -78,4 +78,6 @@     No departures
     <string name="title_activity_main" translatable="false">LineSpecifyActivity</string>
     <string name="tab_text_line_to">To</string>
     <string name="tab_text_line_fro">Fro</string>
+    <string name="timetable_validity_finished">Timetable validity has ended. Connect to the Internet to download a new one in order to continue.</string>
+    <string name="timetable_validity_warning">Timetable validity ends today.</string>
 </resources>




diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml
index 57eb4de2acbb7a1a98edff490f76b5b0bd403e0f..d74f1332adb4471a500c9216f764ff84302c2799 100644
--- a/app/src/main/res/values-de/strings.xml
+++ b/app/src/main/res/values-de/strings.xml
@@ -13,7 +13,7 @@     Arbeitstage
     <string name="tab_saturday_text">Samstags</string>
     <string name="tab_sunday_text">Sonntags</string>
     <string name="action_change_type">Typ</string>
-    <string name="no_db_connect">Verbind mit dem Internet um zu den Fahrplan herunterladen</string>
+    <string name="no_db_connect">Verbind mit dem Internet, um den Fahrplan herunterzuladen</string>
     <string name="no_db_downloading">Fahrplan wird heruntergeladen…</string>
     <string name="timetable_downloading">Fahrplan wird heruntergeladen</string>
     <string name="search_placeholder">Haltestelle oder Linie…</string>
@@ -64,4 +64,6 @@     Heute
     <string name="no_departures">Keine Abfahrten</string>
     <string name="tab_text_line_to">Hin</string>
     <string name="tab_text_line_fro">Her</string>
+    <string name="timetable_validity_warning">Fahrplan gilt nur bis heute.</string>
+    <string name="timetable_validity_finished">Die Gültigkeit des Zeitplans ist beendet. Verbind mit dem Internet, um eine neue herunterzuladen und um fortzufahren.</string>
 </resources>




diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml
index 07f7847e12350b893b3463e917476f6a9147f49e..6a7aa064786e68c9c4186348dfbee07e5e2eaa39 100644
--- a/app/src/main/res/values-it/strings.xml
+++ b/app/src/main/res/values-it/strings.xml
@@ -62,4 +62,6 @@     Oggi
     <string name="no_departures">Nessune partenze</string>
     <string name="tab_text_line_to">Avanti</string>
     <string name="tab_text_line_fro">Indietro</string>
+    <string name="timetable_validity_warning">L’orario è valido solo fino ad oggi.</string>
+    <string name="timetable_validity_finished">"La validità dell’orario è terminata. Connetti a Internet per scaricarne uno nuovo e continuare. "</string>
 </resources>




diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml
index 78e822894e90a2d74b91fd75675bfe0659407664..83866dcc428e2d63c4de098aaa63e252eb90cd6c 100644
--- a/app/src/main/res/values-pl/strings.xml
+++ b/app/src/main/res/values-pl/strings.xml
@@ -64,4 +64,6 @@     Dzisiaj
     <string name="no_departures">Brak odjazdów</string>
     <string name="tab_text_line_to">Tam</string>
     <string name="tab_text_line_fro">Z powrotem</string>
+    <string name="timetable_validity_warning">Rozkład obowiązuje tylko do dzisiaj.</string>
+    <string name="timetable_validity_finished">Rozkład przestał obowiązywać. Połącz się z Internetem, aby pobrać nowy i kontynuować.</string>
 </resources>
\ No newline at end of file