Commit a58c00dd authored by Christophe Henry's avatar Christophe Henry

Handle late service initialization

parent 7798e571
......@@ -6,6 +6,8 @@ import android.os.Handler
import android.os.IBinder
import androidx.core.app.NotificationManagerCompat
import androidx.core.os.postDelayed
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import fr.chenry.android.freshrss.RefresherService.RefresherBinder
import fr.chenry.android.freshrss.store.Store
import fr.chenry.android.freshrss.store.database.FreshRSSDabatabase
......@@ -14,12 +16,12 @@ import fr.chenry.android.freshrss.utils.whenNotNull
import nl.komponents.kovenant.android.startKovenant
import nl.komponents.kovenant.android.stopKovenant
import nl.komponents.kovenant.deferred
import java.util.*
import java.util.Properties
class FreshRSSApplication: Application() {
private val refreshDelay: Long get() = 30
var refresherService: RefresherService? = null
private set
private val _refresherService = MutableLiveData<RefresherService>()
val refresherService: LiveData<RefresherService> get() = _refresherService
private val serviceConnection = RefresherServiceConnection()
......@@ -56,8 +58,9 @@ class FreshRSSApplication: Application() {
val notificationManager: NotificationManagerCompat
get() = NotificationManagerCompat.from(FreshRSSApplication.application)
val stateSharedPreferences: SharedPreferences get() =
context.getSharedPreferences("STATE", Context.MODE_PRIVATE)
val stateSharedPreferences: SharedPreferences
get() =
context.getSharedPreferences("STATE", Context.MODE_PRIVATE)
val database get() = FreshRSSDabatabase.instance
......@@ -70,17 +73,19 @@ class FreshRSSApplication: Application() {
override fun onServiceDisconnected(name: ComponentName?) {
handler.removeCallbacksAndMessages(token)
refresherService = null
_refresherService.value = null
}
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
refresherService = (service as RefresherBinder).service
postDelayedRefesh()
(service as RefresherBinder).service.let {
_refresherService.value = it
postDelayedRefesh()
}
}
private fun postDelayedRefesh() {
handler.postDelayed(this@FreshRSSApplication.refreshDelay * 60 * 1000, token) {
this@FreshRSSApplication.refresherService.whenNotNull {
this@FreshRSSApplication.refresherService.value.whenNotNull {
it.refresh()
postDelayedRefesh()
}
......
......@@ -3,6 +3,7 @@ package fr.chenry.android.freshrss.activities
import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.ui.AppBarConfiguration
......@@ -13,8 +14,7 @@ import fr.chenry.android.freshrss.store.Store
import fr.chenry.android.freshrss.utils.*
import nl.komponents.kovenant.deferred
import nl.komponents.kovenant.resolve
import nl.komponents.kovenant.ui.failUi
import nl.komponents.kovenant.ui.successUi
import nl.komponents.kovenant.ui.*
class MainActivity: AppCompatActivity() {
......@@ -26,11 +26,19 @@ class MainActivity: AppCompatActivity() {
private val appBarConfiguration by lazy {AppBarConfiguration(navigation.graph)}
init {
FreshRSSApplication.application.refresherService.whenNotNull {
it.refresh().successUi {deferred.resolve()}.failUi(deferred::reject)
}.whenNull {
deferred.reject(Exception("${FreshRSSApplication::class.qualifiedName}: Service ${RefresherService::class.qualifiedName} not bound"))
// TODO: Do refresh in the application when database ans Store are also handled within
val observer = object: Observer<RefresherService> {
override fun onChanged(t: RefresherService?) {
t.whenNotNull {
it.refresh()
.successUi {deferred.resolve()}
.failUi(deferred::reject)
.alwaysUi {FreshRSSApplication.application.refresherService.removeObserver(this)}
}
}
}
FreshRSSApplication.application.refresherService.observeForever(observer)
}
override fun onStart() {
......
......@@ -23,7 +23,7 @@ class MainSubscriptionFragment: Fragment(), BottomNavigationView.OnNavigationIte
super.onActivityCreated(savedInstanceState)
subcription_pull_to_refresh.setOnRefreshListener {
FreshRSSApplication.application.refresherService.whenNotNull {
FreshRSSApplication.application.refresherService.value.whenNotNull {
it.refresh().alwaysUi {subcription_pull_to_refresh.isRefreshing = false}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment