Author: Adam <git@apiote.xyz>
refresh departures every 30s
%!v(PANIC=String method: strings: negative Repeat count)
diff --git a/app/src/main/java/ml/adamsprogs/bimba/departures/DeparturesActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/departures/DeparturesActivity.kt index a87fb47ec4ee057778fd84eecb242155e1c769fd..b64e65eb48f499695f9a5edb77d9e00536360921 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/departures/DeparturesActivity.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/departures/DeparturesActivity.kt @@ -6,6 +6,8 @@ import android.content.SharedPreferences import android.net.ConnectivityManager import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.os.Handler +import android.os.Looper import android.util.Log import android.view.View import androidx.appcompat.content.res.AppCompatResources @@ -14,6 +16,7 @@ import androidx.core.view.WindowCompat import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.MainScope +import kotlinx.coroutines.Runnable import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import ml.adamsprogs.bimba.R @@ -29,6 +32,9 @@ private lateinit var preferences: SharedPreferences private lateinit var adapter: BimbaDeparturesAdapter + private val handler = Handler(Looper.getMainLooper()) + private var runnable = Runnable {} + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) _binding = ActivityDeparturesBinding.inflate(layoutInflater) @@ -53,7 +59,6 @@ WindowCompat.setDecorFitsSystemWindows(window, false) preferences = getSharedPreferences("shp", MODE_PRIVATE) - // todo check every 30s getDepartures() } @@ -98,6 +103,9 @@ } else { updateItems((response as DeparturesSuccess)) } } + handler.removeCallbacks(runnable) + runnable = Runnable { getDepartures() } + handler.postDelayed(runnable, 30 * 1000) } private suspend fun unmarshallDepartureResponse(stream: InputStream): DeparturesResponse {