Bimba.git

commit 0e8ee29457e0bf8bcc52b8375ed84383903dadd6

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

peeking favourite

 app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt | 7 
 app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt | 23 
 app/src/main/java/ml/adamsprogs/bimba/models/FavouritesAdapter.kt | 8 
 app/src/main/java/ml/adamsprogs/bimba/models/Plate.kt | 26 


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 4867eed2fb2192b0728f2ae2b3acefd6ebd792df..50d4709b5dbfc188bc9fcddd85badf7252be8eaf 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt
@@ -1,5 +1,6 @@
 package ml.adamsprogs.bimba.models
 
+import android.util.Log
 import java.util.*
 import kotlin.collections.ArrayList
 
@@ -39,6 +40,10 @@
         fun createDepartures(stopId: String): HashMap<String, ArrayList<Departure>> {
             val timetable = Timetable.getTimetable()
             val departures = timetable.getStopDepartures(stopId)
+            return createDepartures(departures)
+        }
+
+        fun createDepartures(departures: HashMap<String, ArrayList<Departure>>): HashMap<String, ArrayList<Departure>> {
             val moreDepartures = HashMap<String, ArrayList<Departure>>()
             for ((k, v) in departures) {
                 moreDepartures[k] = ArrayList()
@@ -62,6 +67,8 @@         }
 
         fun fromString(string: String): Departure {
             val array = string.split("|")
+            if (array.size != 9)
+                throw IllegalArgumentException()
             return Departure(array[0], array[1], array[2], array[3] == "true", array[4], array[5],
                     array[6] == "true", array[7] == "true", array[8] == "true")
         }




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 eb89b9b2a1cebfecf7d681916cee29b6dd66e747..3a96799815f5e01b97c2f4aba3cc008372308c6b 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt
@@ -86,6 +86,17 @@         if (vmDepartures.isNotEmpty()) {
             return vmDepartures.minBy { it.timeTill() }
         }
 
+        val twoDayDepartures = nowDepartures()
+
+        if (twoDayDepartures.isEmpty())
+            return null
+
+        return twoDayDepartures
+                .filter { it.timeTill() >= 0 }
+                .minBy { it.timeTill() }
+    }
+
+    private fun nowDepartures(): ArrayList<Departure> {
         val today = Calendar.getInstance().getMode()
         val tomorrowCal = Calendar.getInstance()
         tomorrowCal.add(Calendar.DAY_OF_MONTH, 1)
@@ -100,21 +111,15 @@         tomorrowDepartures.forEach {it.tomorrow = true}
 
         todayDepartures.forEach {twoDayDepartures.add(it)}
         tomorrowDepartures.forEach {twoDayDepartures.add(it)}
-
-        if (twoDayDepartures.isEmpty())
-            return null
-
         return twoDayDepartures
-                .filter { it.timeTill() >= 0 }
-                .minBy { it.timeTill() }
     }
 
-    fun allDepartures(): HashMap<String, ArrayList<Departure>>? {
-        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
+    fun allDepartures(): HashMap<String, ArrayList<Departure>> {
+        return Departure.createDepartures(timetable.getStopDepartures(timetables))
     }
 
     fun fullTimetable(): HashMap<String, ArrayList<Departure>>? {
-        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
+        return timetable.getStopDepartures(timetables)
     }
 
     override fun onVm(vmDepartures: ArrayList<Departure>?, requester: String) {




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 ebbebcbddff627f6d8cf6f741e6adef300005e8b..d05420a990e70fa4ba27780b7e72003abb0125a2 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/FavouritesAdapter.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/FavouritesAdapter.kt
@@ -2,6 +2,7 @@ package ml.adamsprogs.bimba.models
 
 import android.app.Activity
 import android.content.Context
+import android.content.Intent
 import android.os.Build
 import android.support.design.widget.AppBarLayout
 import android.support.v4.content.res.ResourcesCompat
@@ -16,6 +17,7 @@ import kotlin.concurrent.thread
 import android.util.TypedValue
 import com.arlib.floatingsearchview.FloatingSearchView
 import ml.adamsprogs.bimba.Declinator
+import ml.adamsprogs.bimba.activities.StopActivity
 import kotlin.collections.ArrayList
 
 //todo list to storage
@@ -74,6 +76,12 @@             (context as Activity).runOnUiThread {
                 holder?.root?.setOnLongClickListener {
                     toggleSelected(it as CardView, position)
                     true
+                }
+                holder?.root?.setOnClickListener {
+                    val intent = Intent(context, StopActivity::class.java)
+                    intent.putExtra(StopActivity.SOURCE_TYPE, StopActivity.SOURCE_TYPE_FAV)
+                    intent.putExtra(StopActivity.EXTRA_FAVOURITE, favourite)
+                    context.startActivity(intent)
                 }
                 holder?.nameTextView?.text = favourite.name
                 holder?.timeTextView?.text = nextDepartureText




diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/Plate.kt b/app/src/main/java/ml/adamsprogs/bimba/models/Plate.kt
index c62e6982c1416e0b414e513d1be5d9563e2f0dd6..b50d8b0bbd84bcf7f2a37c11a8978072f9f58a27 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/Plate.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/Plate.kt
@@ -1,7 +1,5 @@
 package ml.adamsprogs.bimba.models
 
-import android.util.Log
-
 data class Plate(val line: String, val stop: String, val departures: HashMap<String, HashSet<Departure>>?) {
     override fun toString(): String {
         var result = "$line=$stop={"
@@ -19,14 +17,17 @@     companion object {
         fun fromString(string: String): Plate {
             val s = string.split("=")
             val departures = HashMap<String, HashSet<Departure>>()
-            for (d in s[2].replace("{", "").replace("}", "").split(";")) {
-                if (d == "")
-                    continue
-                val dep = Departure.fromString(d)
-                if (departures[dep.mode] == null)
-                    departures[dep.mode] = HashSet()
-                departures[dep.mode]!!.add(dep)
-            }
+            s[2].replace("{", "").replace("}", "").split(";")
+                    .filter { it != "" }
+                    .forEach {
+                        try {
+                            val dep = Departure.fromString(it)
+                            if (departures[dep.mode] == null)
+                                departures[dep.mode] = HashSet()
+                            departures[dep.mode]!!.add(dep)
+                        } catch (e: IllegalArgumentException) {
+                        }
+                    }
             return Plate(s[0], s[1], departures)
         }
 
@@ -36,8 +37,11 @@             for (plate in set) {
                 for ((mode, d) in plate.departures!!) {
                     if (departures[mode] == null)
                         departures[mode] = ArrayList()
-                    departures[mode]!!.addAll(d.sortedBy { it.time })
+                    departures[mode]!!.addAll(d)
                 }
+            }
+            for ((mode, _) in departures) {
+                departures[mode]?.sortBy { it.time }
             }
             return departures
         }