Author: Adam Pioterek <adam.pioterek@protonmail.ch>
quicker VM
app/src/main/java/ml/adamsprogs/bimba/MessageReceiver.kt | 2 app/src/main/java/ml/adamsprogs/bimba/VmClient.kt | 9 app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt | 17 + app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt | 11 app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt | 1 app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt | 20 +
diff --git a/app/src/main/java/ml/adamsprogs/bimba/MessageReceiver.kt b/app/src/main/java/ml/adamsprogs/bimba/MessageReceiver.kt index 0332f77894fdbf108b0b8b4f8dfaff92159c4e34..64287c3ea35f579dbc249cbc22a7892b446c922d 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/MessageReceiver.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/MessageReceiver.kt @@ -3,6 +3,7 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import android.util.Log import ml.adamsprogs.bimba.models.Departure class MessageReceiver private constructor() : BroadcastReceiver() { @@ -35,6 +36,7 @@ listener.onVm(departures, requester, id, size) } } if (intent?.action == VmClient.ACTION_NO_DEPARTURES) { + Log.i("VM", "Response received") val requester = intent.getStringExtra(VmClient.EXTRA_REQUESTER) val id = intent.getStringExtra(VmClient.EXTRA_ID) val size = intent.getIntExtra(VmClient.EXTRA_SIZE, -1) diff --git a/app/src/main/java/ml/adamsprogs/bimba/VmClient.kt b/app/src/main/java/ml/adamsprogs/bimba/VmClient.kt index ec68bd9916247c8a329d1a2211cd5de1110bbf41..d7dac071c28df4411be94e16da83fa5c88fea83e 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/VmClient.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/VmClient.kt @@ -2,6 +2,7 @@ package ml.adamsprogs.bimba import android.app.IntentService import android.content.Intent +import android.util.Log import ml.adamsprogs.bimba.models.* import okhttp3.* import com.google.gson.Gson @@ -24,6 +25,7 @@ override fun onHandleIntent(intent: Intent?) { if (intent != null) { val requester = intent.getStringExtra(EXTRA_REQUESTER) + Log.i("VM", "Request for $requester received") val id = intent.getStringExtra(EXTRA_ID) val size = intent.getIntExtra(EXTRA_SIZE, -1) @@ -50,6 +52,8 @@ .url(url) .post(formBody) .build() + Log.i("VM", "created http request") + val responseBody: String? try { responseBody = client.newCall(request).execute().body()?.string() @@ -58,6 +62,8 @@ sendNullResult(requester, id, size) return } + Log.i("VM", "received http response") + if (responseBody?.get(0) == '<') { sendNullResult(requester, id, size) return @@ -81,6 +87,8 @@ departureDay != todayDay, t["onStopPoint"] as Boolean) departuresToday.add(departure) } } + + Log.i("VM", "parsed http response") if (departuresToday.isEmpty()) sendNullResult(requester, id, size) else @@ -107,5 +115,6 @@ broadcastIntent.putExtra(EXTRA_REQUESTER, requester) broadcastIntent.putExtra(EXTRA_ID, id) broadcastIntent.putExtra(EXTRA_SIZE, size) sendBroadcast(broadcastIntent) + Log.i("VM", "sent response") } } 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 304f5a91338cc47924b9eb8a8f63babc9224e495..2ac8dd863f1cfb250db50cc77115c67ff4e68247 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt @@ -19,11 +19,11 @@ import android.view.inputmethod.InputMethodManager import ml.adamsprogs.bimba.* import java.util.* import android.os.Bundle +import android.util.Log import kotlinx.android.synthetic.main.activity_dash.* class DashActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadListener, - FavouritesAdapter.OnMenuItemClickListener { - + FavouritesAdapter.OnMenuItemClickListener, Favourite.OnVmPreparedListener { val context: Context = this val receiver = MessageReceiver.getMessageReceiver() lateinit var timetable: Timetable @@ -156,6 +156,7 @@ timerTask = object : TimerTask() { override fun run() { for (fav in favourites) { fav.registerOnVm(receiver) + fav.addOnVmPreparedListener(this@DashActivity) for (t in fav.timetables) { val symbol = timetable.getStopSymbol(t.stop) val line = timetable.getLineNumber(t.line) @@ -169,13 +170,15 @@ "${fav.name};${t.stop}${t.line}") context.startService(intent) } vmRequestId++ - } - - runOnUiThread { - favouritesList.adapter.notifyDataSetChanged() + Log.i("VM", "Sent request for ${fav.name}") } } } + } + + override fun onVmPrepared() { + Log.i("VM", "DataSetChange") + favouritesList.adapter.notifyDataSetChanged() } private fun getStops() { @@ -259,8 +262,8 @@ TimetableDownloader.RESULT_VALIDITY_FAILED -> getString(R.string.validity_failed) else -> getString(R.string.error_try_later) } Snackbar.make(findViewById(R.id.drawer_layout), message, Snackbar.LENGTH_LONG).show() - Snackbar.make(findViewById(R.id.drawer_layout), getString(R.string.refreshing_cache), Snackbar.LENGTH_LONG).show() if (result == TimetableDownloader.RESULT_DOWNLOADED) { + Snackbar.make(findViewById(R.id.drawer_layout), getString(R.string.refreshing_cache), Snackbar.LENGTH_LONG).show() timetable.refresh(context) stops = timetable.getStops() as ArrayList<StopSuggestion> } 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 3276e3a99ba49d2965c73a1141a1066b805518b0..930d22eb15b467361c5e3ece567387ec72726189 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt @@ -18,8 +18,7 @@ import ml.adamsprogs.bimba.* import kotlin.concurrent.thread import kotlinx.android.synthetic.main.activity_stop.* -class StopActivity : AppCompatActivity(), MessageReceiver.OnVmListener { - +class StopActivity : AppCompatActivity(), MessageReceiver.OnVmListener, Favourite.OnVmPreparedListener { companion object { val EXTRA_STOP_ID = "stopId" val EXTRA_STOP_SYMBOL = "stopSymbol" @@ -67,7 +66,7 @@ } SOURCE_TYPE_FAV -> { favourite = intent.getParcelableExtra(EXTRA_FAVOURITE) supportActionBar?.title = favourite!!.name - + favourite!!.addOnVmPreparedListener(this) } } @@ -118,6 +117,10 @@ sectionsPagerAdapter?.notifyDataSetChanged() } } + override fun onVmPrepared() { + getFavouriteDepartures() + } + private fun showFab() { if (sourceType == SOURCE_TYPE_FAV) return @@ -193,7 +196,7 @@ private fun scheduleRefresh() { timer.cancel() timer = Timer() createTimerTask() - timer.scheduleAtFixedRate(timerTask, 15000, 15000) + timer.scheduleAtFixedRate(timerTask, 0, 15000) } override fun onCreateOptionsMenu(menu: Menu): Boolean { 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 778d3ef55968479a22f8d0b70ca64042c2d55281..464217e69dc44aafd37b0cec470b1090cc281651 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/models/Departure.kt @@ -1,6 +1,5 @@ package ml.adamsprogs.bimba.models -import android.util.Log import java.util.* import kotlin.collections.ArrayList import kotlin.collections.HashMap 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 1d4ccfd33f58e46ab35d3a54ce615dbc81dacb60..91817eaa814f7d1bf9206b783fc5c7d11d887820 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/models/Favourite.kt @@ -25,6 +25,16 @@ private val requestValidityNumber = HashMap<String, Int>() private val requestValidity = HashMap<String, Boolean>() + private val onVmPreparedListeners = HashSet<OnVmPreparedListener>() + + fun addOnVmPreparedListener(listener: OnVmPreparedListener) { + onVmPreparedListeners.add(listener) + } + + fun removeOnVmPreparedListener(listener: OnVmPreparedListener) { + onVmPreparedListeners.remove(listener) + } + constructor(parcel: Parcel) { val array = ArrayList<String>() parcel.readStringList(array) @@ -121,7 +131,6 @@ fun allDepartures(): Map<String, List<Departure>> { val departures = timetable.getStopDepartures(timetables) as HashMap<String, ArrayList<Departure>> - Log.i("Fav", "$vmDepartures") if (vmDepartures.isNotEmpty()) { val today = Calendar.getInstance().getMode() departures[today] = vmDepartures @@ -135,6 +144,7 @@ return timetable.getStopDepartures(timetables) } override fun onVm(vmDepartures: ArrayList<Departure>?, requester: String, id: String, size: Int) { + Log.i("VM", "onVM fired") val requesterName = requester.split(";")[0] val requesterTimetable: String = try { requester.split(";")[1] @@ -155,6 +165,10 @@ } else if (requesterName == name) { requestValidityNumber[id] = requestValidityNumber[id]!! + 1 } if (requestValidityNumber[id] == size) { + Log.i("VM", "All onVm’s collected") + for (listener in onVmPreparedListeners) { + listener.onVmPrepared() + } if (!requestValidity[id]!!) { this.vmDepartures = ArrayList() } @@ -162,5 +176,9 @@ requestValidity.remove(id) requestValidityNumber.remove(id) } filterVmDepartures() + } + + interface OnVmPreparedListener { + fun onVmPrepared() } }