Bimba.git

commit 9129de753912d00e6c00a1dc2d179f58a1901c12

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

back to 3 tabs

 app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt | 49 +
 app/src/main/java/ml/adamsprogs/bimba/extensions.kt | 13 
 app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt | 2 
 app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt | 10 
 app/src/main/res/layout/activity_stop.xml | 18 
 app/src/main/res/values-de/strings.xml | 18 
 app/src/main/res/values-it/strings.xml | 18 
 app/src/main/res/values-pl/strings.xml | 18 
 app/src/main/res/values/strings.xml | 25 -


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 f8c95ab545ed1b4cad1842a348875cb4ef3b55e8..d5e42fcc62ad95bd3b1764994eef3a4da6f9491e 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
@@ -3,7 +3,6 @@
 import android.content.Context
 import android.content.Intent
 import android.content.IntentFilter
-import android.database.DataSetObserver
 import android.support.design.widget.TabLayout
 import android.support.design.widget.Snackbar
 import android.support.v7.app.AppCompatActivity
@@ -28,6 +27,7 @@ import ml.adamsprogs.bimba.MessageReceiver
 import ml.adamsprogs.bimba.R
 import ml.adamsprogs.bimba.datasources.TimetableDownloader
 import ml.adamsprogs.bimba.datasources.VmClient
+import ml.adamsprogs.bimba.getMode
 import ml.adamsprogs.bimba.gtfs.AgencyAndId
 import ml.adamsprogs.bimba.models.*
 import java.util.*
@@ -43,6 +43,10 @@         const val EXTRA_FAVOURITE = "favourite"
         const val SOURCE_TYPE = "sourceType"
         const val SOURCE_TYPE_STOP = "stop"
         const val SOURCE_TYPE_FAV = "favourite"
+
+        const val MODE_WORKDAYS = 0
+        const val MODE_SATURDAYS = 1
+        const val MODE_SUNDAYS = 2
     }
 
     private var stopSegment: StopSegment? = null
@@ -188,8 +192,7 @@         //todo refresh
     }
 
     private fun selectTodayPage() {
-        val today = (Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1) % 7
-        tabs.getTabAt(sectionsPagerAdapter!!.todayTab(today))
+        tabs.getTabAt(sectionsPagerAdapter!!.todayTab())
     }
 
     override fun onCreateOptionsMenu(menu: Menu): Boolean {
@@ -240,37 +243,35 @@     }
 
     inner class SectionsPagerAdapter(fm: FragmentManager, var departures: Map<AgencyAndId, List<Departure>>) : FragmentStatePagerAdapter(fm) {
         var relativeTime = true
-        private var modes = ArrayList<AgencyAndId>()
-
-        init {
-            this.registerDataSetObserver(object : DataSetObserver() {
-                override fun onChanged() {
-                    departures.keys.sortedBy { timetable.calendarToMode(it)[0] }.mapTo(modes) { it }
-                }
-            })
-        }
 
         override fun getItem(position: Int): Fragment {
-            departures.keys.sortedBy { timetable.calendarToMode(it)[0] }.mapTo(modes) { it }
-            val list = if (departures.isEmpty())
-                ArrayList()
-            else
-                departures[modes[position]]!!
+            if (departures.isEmpty())
+                return PlaceholderFragment.newInstance(ArrayList(), relativeTime)
+            val sat = timetable.getServiceFor("saturday")
+            val sun = timetable.getServiceFor("sunday")
+            val list: List<Departure> = when (position) {
+                1 -> departures[sat]!!
+                2 -> departures[sun]!!
+                0 -> try {
+                    departures
+                            .filter { it.key != sat && it.key != sun }
+                            .filter { it.value.isNotEmpty() }.toList()[0].second
+                } catch (e: IndexOutOfBoundsException) {
+                    departures.filter { it.key != sat && it.key != sun }.toList()[0].second
+                }
+                else -> throw IndexOutOfBoundsException("No tab at index $position")
+            }
             return PlaceholderFragment.newInstance(list, relativeTime)
         }
 
-        override fun getCount() = 5
+        override fun getCount() = 3
 
         override fun getItemPosition(obj: Any): Int {
             return PagerAdapter.POSITION_NONE
         }
 
-        fun todayTab(today: Int): Int {
-            if (modes.isEmpty())
-                return 0
-            return modes.indexOf(modes.filter {
-                timetable.calendarToMode(it).contains(today)
-            }[0])
+        fun todayTab(): Int {
+            return Calendar.getInstance().getMode()
         }
     }
 




diff --git a/app/src/main/java/ml/adamsprogs/bimba/extensions.kt b/app/src/main/java/ml/adamsprogs/bimba/extensions.kt
index 7a3713d35fc96dc9be71a438ca4ad26db214e2d4..ba33c95679e8135e10ee722929fbff1cf5ec7813 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/extensions.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/extensions.kt
@@ -2,6 +2,7 @@ package ml.adamsprogs.bimba
 
 import android.content.Context
 import android.os.Build
+import ml.adamsprogs.bimba.activities.StopActivity
 import java.text.SimpleDateFormat
 import java.util.*
 
@@ -50,7 +51,15 @@
 fun getColour(id: Int, context: Context): Int {
     @Suppress("DEPRECATION")
     (return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
-        context.resources.getColor(R.color.colorAccent, null)
+        context.resources.getColor(id, null)
     else
-        context.resources.getColor(R.color.colorAccent))
+        context.resources.getColor(id))
+}
+
+internal fun Calendar.getMode(): Int {
+    return when (this.get(Calendar.DAY_OF_WEEK)) {
+        Calendar.SUNDAY -> StopActivity.MODE_SUNDAYS
+        Calendar.SATURDAY -> StopActivity.MODE_SATURDAYS
+        else -> StopActivity.MODE_WORKDAYS
+    }
 }
\ No newline at end of file




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 a7a5816b52eda2272d833886f5685011bcb8e908..4b757e56de207909889e9d8f49dc7f606ff9cb56 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt
@@ -47,7 +47,7 @@
             return rollDepartures(departures)
         }
 
-        fun rollDepartures(departures: Map<AgencyAndId, List<Departure>>): Map<AgencyAndId, List<Departure>> { //todo if departure.timeTill < 0 -> show ‘just departed’
+        fun rollDepartures(departures: Map<AgencyAndId, List<Departure>>): Map<AgencyAndId, List<Departure>> { //fixme doesn't roll night properly: (23),(23,00)
             val rolledDepartures = HashMap<AgencyAndId, List<Departure>>()
             departures.keys.forEach {
                 val (filtered, isFull) = filterDepartures(departures[it]!!)




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 86f05318d4ee75a376e7977751aac467ba9e115f..3a6a0597bc8860940c607b5997b06b60cfa8e60c 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt
@@ -326,7 +326,7 @@ //        store.allStopTimes.filter { it.stop.id == stopId }.forEach { lines.add(it.trip.route.id) }
 //        return lines
 //    }
 
-    fun getTripsForStop(stopId: AgencyAndId): HashMap<String, Trip> { //todo actually, we have trips at the start. Why not cache?
+    fun getTripsForStop(stopId: AgencyAndId): HashMap<String, Trip> {
         val tripIds = HashSet<String>()
         val stopTimesFile = File(filesDir, "gtfs_files/stop_times_${stopId.id}.txt")
         val mapReader = CsvMapReader(FileReader(stopTimesFile), CsvPreference.STANDARD_PREFERENCE)
@@ -418,6 +418,11 @@     }
 
     private fun getServiceFor(day: Int): AgencyAndId {
         val dow = arrayOf("sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday")
+
+        return getServiceFor(dow[day - 1])
+    }
+
+    fun getServiceFor(day: String): AgencyAndId {
         val file = File(filesDir, "gtfs_files/calendar.txt")
         val mapReader = CsvMapReader(FileReader(file), CsvPreference.STANDARD_PREFERENCE)
         val header = mapReader.getHeader(true)
@@ -425,13 +430,14 @@
         var row: Map<String, Any>? = null
         val processors = Array<CellProcessor?>(header.size, { null })
         while ({ row = mapReader.read(header, processors); row }() != null) {
-            if ((row!![dow[day - 1]] as String) == "1") {
+            if ((row!![day] as String) == "1") {
                 mapReader.close()
                 return AgencyAndId(row!!["service_id"] as String)
             }
         }
         mapReader.close()
         throw IllegalArgumentException("Day $day not in calendar")
+
     }
 
     fun getLineForNumber(number: String): AgencyAndId {




diff --git a/app/src/main/res/layout/activity_stop.xml b/app/src/main/res/layout/activity_stop.xml
index 670a7d4526703b96c1085c7e07604c3f57960a6a..3fd9ff4d556f89b85a4509c2be916a7655673acb 100644
--- a/app/src/main/res/layout/activity_stop.xml
+++ b/app/src/main/res/layout/activity_stop.xml
@@ -36,31 +36,19 @@                              android:id="@+id/tabItem"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
-                android:text="@string/mon" />
-
-            <android.support.design.widget.TabItem
-                android:id="@+id/tabItem2"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:text="@string/tue__thu" />
-
-            <android.support.design.widget.TabItem
-                android:id="@+id/tabItem3"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:text="@string/fri" />
+                android:text="@string/tab_workday_text" />
 
             <android.support.design.widget.TabItem
                 android:id="@+id/tabItem4"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
-                android:text="@string/sat" />
+                android:text="@string/tab_saturday_text" />
 
             <android.support.design.widget.TabItem
                 android:id="@+id/tabItem5"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
-                android:text="@string/sun" />
+                android:text="@string/tab_sunday_text" />
         </android.support.design.widget.TabLayout>
     </android.support.design.widget.AppBarLayout>
 




diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index b8faa65871949ede9b611e8111910a00f227299a..6e4c6f496a82be3eee9b13139e06baea5ac15c9f 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -73,30 +73,5 @@     departure info icon
     <string name="refreshing_cache">Refreshing cache. May take some time…</string>
     <string name="timetable_converting">Converting timetable…</string>
 
-    <string-array name="daysOfWeek">
-        <item>Monday</item>
-        <item>Tuesday</item>
-        <item>Wednesday</item>
-        <item>Thursday</item>
-        <item>Friday</item>
-        <item>Saturday</item>
-        <item>Sunday</item>
-    </string-array>
-
-    <string-array name="daysOfWeekShort">
-        <item>Mon</item>
-        <item>Tue</item>
-        <item>Wed</item>
-        <item>Thu</item>
-        <item>Fri</item>
-        <item>Sat</item>
-        <item>Sun</item>
-    </string-array>
-
     <string name="today">Today</string>
-    <string name="mon">Mon</string>
-    <string name="tue__thu">Tue, …, Thu</string>
-    <string name="fri">Fri</string>
-    <string name="sat">Sat</string>
-    <string name="sun">Sun</string>
 </resources>




diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml
index 0ac6eb65d8ffdca102eac78605f52d186780b659..06a9df684010f6264c275f6113ac28c95cde4e0b 100644
--- a/app/src/main/res/values-de/strings.xml
+++ b/app/src/main/res/values-de/strings.xml
@@ -59,24 +59,6 @@         "heute zeigen (wenn es Dienstag ist, es wird auf „Arbeitstage“ Karte sein).\n"
         "Sei sicher, die Nachrichten auf\nhttps://www.ztm.poznan.pl/en\nkonsultieren.\n\n"
     </string>
     <string name="refreshing_cache">Cache wird aktualisiert. Es kann einige Zeit dauern…</string>
-    <string-array name="daysOfWeek">
-        <item>Montag</item>
-        <item>Dienstag</item>
-        <item>Mittwoch</item>
-        <item>Donerstag</item>
-        <item>Freitag</item>
-        <item>Samstag</item>
-        <item>Sontag</item>
-    </string-array>
-    <string-array name="daysOfWeekShort">
-        <item>Mon</item>
-        <item>Die</item>
-        <item>Mit</item>
-        <item>Don</item>
-        <item>Fre</item>
-        <item>Sam</item>
-        <item>Son</item>
-    </string-array>
     <string name="timetable_converting">Fahrplan wird umgewandelt…</string>
     <string name="today">Heute</string>
 </resources>




diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml
index fd5f16720a42519165056818678b1c28c809b79b..f521f9def826aa47e427231290bd60edb270d624 100644
--- a/app/src/main/res/values-it/strings.xml
+++ b/app/src/main/res/values-it/strings.xml
@@ -57,24 +57,6 @@         "(se è martedì, sarà nella scheda «giorni di lavoro»).\n"
         "Assicurati di consultare le notifiche su\nhttps://www.ztm.poznan.pl/en.\n\n"
     </string>
     <string name="refreshing_cache">Cache sta essendo aggiornato. Può richiedere un certo tempo…</string>
-    <string-array name="daysOfWeek">
-        <item>Lunedi</item>
-        <item>Martedi</item>
-        <item>Mercoledi</item>
-        <item>Giovedi</item>
-        <item>Venerdi</item>
-        <item>Sabato</item>
-        <item>Domenica</item>
-    </string-array>
-    <string-array name="daysOfWeekShort">
-        <item>Lu</item>
-        <item>Ma</item>
-        <item>Me</item>
-        <item>Gi</item>
-        <item>Ve</item>
-        <item>Sa</item>
-        <item>Do</item>
-    </string-array>
     <string name="timetable_converting">L’orario e stando convertito</string>
     <string name="today">Oggi</string>
 </resources>




diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml
index 6f77515832fe84b73f3c13be4b6486b09a819a54..7066cf0963d1e83859e531ced178e3abee0b4762 100644
--- a/app/src/main/res/values-pl/strings.xml
+++ b/app/src/main/res/values-pl/strings.xml
@@ -59,24 +59,6 @@     Zbieranie odjazdów…
     <string name="valid_since">Ważny od %1$s</string>
     <string name="valid_till">Ważny do %1$s</string>
     <string name="refreshing_cache">Odświeżanie pamięci podręcznej. Może chwilę potrwać…</string>
-    <string-array name="daysOfWeek">
-        <item>Poniedziałek</item>
-        <item>Wtorek</item>
-        <item>Środa</item>
-        <item>Czwartek</item>
-        <item>Piątek</item>
-        <item>Sobota</item>
-        <item>Niedziela</item>
-    </string-array>
-    <string-array name="daysOfWeekShort">
-        <item>Pn</item>
-        <item>Wt</item>
-        <item>Śr</item>
-        <item>Czw</item>
-        <item>Pt</item>
-        <item>S</item>
-        <item>N</item>
-    </string-array>
     <string name="timetable_converting">Konwertowanie rozkładu</string>
     <string name="today">Dzisiaj</string>
 </resources>
\ No newline at end of file