Commit a58c00dd authored by Christophe Henry's avatar Christophe Henry

Handle late service initialization

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