Bimba.git

commit 7edacbc43c53da3cb86c9adc80c61c480fddd5ca

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

full favourites show faster

 app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt | 1 
 app/src/main/java/ml/adamsprogs/bimba/activities/NoDbActivity.kt | 1 
 app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt | 60 
 app/src/main/java/ml/adamsprogs/bimba/datasources/VmClient.kt | 1 
 app/src/main/java/ml/adamsprogs/bimba/models/DeparturesAdapter.kt | 1 
 app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt | 23 


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 eb8293a96bbe4e7a59e9e6f42ff67e1618779f0c..29d8b1cecf62d180a40890da4190c8f48404098f 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
@@ -26,6 +26,7 @@ import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion
 
 //todo<p:1> searchView integration
 //todo something devours RAM
+//todo onResume -> refresh favourites
 class DashActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadListener,
         FavouritesAdapter.OnMenuItemClickListener, Favourite.OnVmPreparedListener,
         FavouritesAdapter.ViewHolder.OnClickListener {




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 c08d5d5737d3ea5da8ffdae795c4fa010f62737f..28d24f6d832eb6a185ea90d8720fb432282ff607 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/NoDbActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/NoDbActivity.kt
@@ -8,6 +8,7 @@ import ml.adamsprogs.bimba.*
 import kotlinx.android.synthetic.main.activity_nodb.*
 import ml.adamsprogs.bimba.datasources.TimetableDownloader
 
+//todo onResume check again
 class NoDbActivity : AppCompatActivity(), NetworkStateReceiver.OnConnectivityChangeListener, MessageReceiver.OnTimetableDownloadListener {
     private val networkStateReceiver = NetworkStateReceiver()
     private val timetableDownloadReceiver = MessageReceiver.getMessageReceiver()




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 ef44e2ef84cdfb1f893105b565177592f11dfa05..d5c41453695eee6011f0541d8028e1a3a4afda8a 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
@@ -1,42 +1,22 @@
 package ml.adamsprogs.bimba.activities
 
-import android.content.Context
-import android.content.Intent
-import android.content.IntentFilter
-import android.support.design.widget.TabLayout
-import android.support.design.widget.Snackbar
-import android.support.v7.app.AppCompatActivity
-
-import android.support.v4.app.Fragment
-import android.support.v4.app.FragmentManager
+import android.content.*
+import android.support.design.widget.*
 import android.os.Bundle
-import android.support.v4.app.FragmentStatePagerAdapter
-import android.support.v4.content.res.ResourcesCompat
+import android.view.*
+import android.support.v4.app.*
 import android.support.v4.view.PagerAdapter
-import android.support.v7.widget.DividerItemDecoration
-import android.support.v7.widget.LinearLayoutManager
-import android.support.v7.widget.RecyclerView
-import android.view.LayoutInflater
-import android.view.Menu
-import android.view.MenuItem
-import android.view.View
-import android.view.ViewGroup
+import android.support.v4.content.res.ResourcesCompat
+import android.support.v7.app.AppCompatActivity
+import android.support.v7.widget.*
 
+import java.util.Calendar
 import kotlinx.android.synthetic.main.activity_stop.*
-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.*
+import ml.adamsprogs.bimba.datasources.*
 import ml.adamsprogs.bimba.models.gtfs.AgencyAndId
 import ml.adamsprogs.bimba.models.*
-import ml.adamsprogs.bimba.secondsAfterMidnight
-import java.util.*
-import kotlin.collections.ArrayList
-import kotlin.concurrent.thread
 
-//todo<p:1> on click show time (HH:MM)
-//todo does not show departures when favourite (force works, not by default)
 class StopActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadListener, MessageReceiver.OnVmListener, Favourite.OnVmPreparedListener {
 
     private var sectionsPagerAdapter: SectionsPagerAdapter? = null
@@ -101,9 +81,7 @@         prepareOnDownloadListener()
     }
 
     private fun getFavouriteDepartures() {
-        thread {
-            refreshAdapter(favourite!!.allDepartures())
-        }
+        refreshAdapter(favourite!!.allDepartures())
     }
 
     private fun refreshAdapterFromStop() {
@@ -121,15 +99,15 @@
     private fun refreshAdapter(departures: Map<AgencyAndId, List<Departure>>?) {
         if (departures != null)
             sectionsPagerAdapter?.departures = departures
-        runOnUiThread {
-            sectionsPagerAdapter?.notifyDataSetChanged()
-            selectTodayPage()
-            lastUpdated = Calendar.getInstance().timeInMillis
-        }
+        sectionsPagerAdapter?.notifyDataSetChanged()
+        selectTodayPage()
+        lastUpdated = Calendar.getInstance().timeInMillis
     }
 
     override fun onVmPrepared() {
-        getFavouriteDepartures()
+        if (favourite!!.isBackedByVm || ticked()) {
+            getFavouriteDepartures()
+        }
     }
 
     private fun showFab() {
@@ -173,10 +151,8 @@             favourite!!.registerOnVm(receiver, context)
     }
 
     override fun onVm(vmDepartures: Set<Departure>?, plateId: Plate.ID) {
-        println("OnVM")
         if (vmDepartures == null && this.vmDepartures.isEmpty() && hasDepartures) {
             if (ticked()) {
-                println("tick")
                 refreshAdapterFromStop()
             }
             return
@@ -190,7 +166,7 @@             refreshAdapterFromStop()
         }
     }
 
-    private fun ticked() = Calendar.getInstance().timeInMillis - lastUpdated >= VmClient.TICK_6_ZINA_TIM
+    private fun ticked() = Calendar.getInstance().timeInMillis - lastUpdated >= VmClient.TICK_6_ZINA_TIM_WITH_MARGIN
 
     override fun onTimetableDownload(result: String?) {
         val message: String = when (result) {




diff --git a/app/src/main/java/ml/adamsprogs/bimba/datasources/VmClient.kt b/app/src/main/java/ml/adamsprogs/bimba/datasources/VmClient.kt
index 0c1acdd386bc2ab37ca55ab20d16371c1b5a5cc8..7847c18a8bf0194b69f5cec83d6d2bbc24aaf0bf 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/datasources/VmClient.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/datasources/VmClient.kt
@@ -26,6 +26,7 @@         const val ACTION_READY = "ml.adamsprogs.bimba.action.vm.ready"
         const val EXTRA_DEPARTURES = "ml.adamsprogs.bimba.extra.vm.departures"
         const val EXTRA_PLATE_ID = "ml.adamsprogs.bimba.extra.vm.plate"
         const val TICK_6_ZINA_TIM = 12500L
+        const val TICK_6_ZINA_TIM_WITH_MARGIN = TICK_6_ZINA_TIM * 3 / 4
     }
 
     private var handler: Handler? = null




diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/DeparturesAdapter.kt b/app/src/main/java/ml/adamsprogs/bimba/models/DeparturesAdapter.kt
index 93ffa0f16a9f84693cc0a70e4706d11108356e98..457e6b09f404b66d2aefad7d99d3950fa14bf685 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/DeparturesAdapter.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/DeparturesAdapter.kt
@@ -15,6 +15,7 @@ import ml.adamsprogs.bimba.Declinator
 import ml.adamsprogs.bimba.rollTime
 import java.util.*
 
+//todo<p:1> on click show time (HH:MM)
 class DeparturesAdapter(val context: Context, private val departures: List<Departure>?, private val relativeTime: Boolean) :
         RecyclerView.Adapter<DeparturesAdapter.ViewHolder>() {
 




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 750f193d77c56b994749737190f24ecfe46bf5b5..f0193394cd57e83e74b15979c5dfc843520ba15a 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt
@@ -26,6 +26,8 @@     val size
         get() = segments.sumBy {
             it.size
         }
+    val isBackedByVm
+        get() = vmDepartures.isNotEmpty()
 
     private val onVmPreparedListeners = HashSet<OnVmPreparedListener>()
 
@@ -136,36 +138,21 @@                     }
         }
 
         val full = fullTimetable()
-        /*println("full:")
-        full.forEach {
-            println("${it.key}:")
-            it.value.forEach {
-                println("\t$it")
-            }
-        }*/
 
         val twoDayDepartures = Departure.rollDepartures(full)[timetable.getServiceForToday()]
-
-        println("NextDep: is empty? ${twoDayDepartures?.isEmpty()}")
 
         if (twoDayDepartures?.isEmpty() != false)
             return null
 
-        println("NextDep: twoDays:")
-        twoDayDepartures.forEach {
-            println("\t$it")
-        }
-
-        println("NextDep: ${twoDayDepartures[0]}")
-
         return twoDayDepartures[0]
     }
 
     fun allDepartures(): Map<AgencyAndId, List<Departure>> {
         if (vmDepartures.isNotEmpty()) {
-            val departures = HashMap<AgencyAndId, ArrayList<Departure>>()
+            val now = Calendar.getInstance().secondsAfterMidnight()
+            val departures = HashMap<AgencyAndId, List<Departure>>()
             val today = timetable.getServiceForToday()
-            departures[today] = vmDepartures.flatMap { it.value } as ArrayList<Departure>
+            departures[today] = vmDepartures.flatMap { it.value }.sortedBy { it.timeTill(now) }
             return departures
         }