By using Kotlin i created a networkModule, code below
val networkModule = module {
......
single {
return@single AndroidLifecycle.ofApplicationForeground(androidApplication())
}
// Scarlet WebSocket library for Tinder
single {
return@single Scarlet.Builder()
.webSocketFactory(get<OkHttpClient>().newWebSocketFactory(Wss))
.lifecycle(get()) // I can create my own lifeCycle and combineWith(), for example accounts.isEmpty
.backoffStrategy(LinearBackoffStrategy(5000))
.addMessageAdapterFactory(GsonMessageAdapter.Factory(get()))
.addStreamAdapterFactory(CoroutinesStreamAdapterFactory())
.build()
.create(FlowSocketApi::class.java)
}
}
val lifecycleModule = module {
single {
return@single AppLifeCycleObserver()
}
}
I also have an Observer class that is registered to the Application lifecycle.
class AppLifeCycleObserver :
LifecycleObserver, KoinComponent {
private val flowEventsObserver: LiveData<WebSocket.Event> =
flowSocketService.observeEvents().consumeAsFlow().asLiveData()
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onEnterForeground() {
flowEventsObserver.observeForever { event ->
when(event) {
is WebSocket.Event.OnConnectionOpened<*> -> {
println("WebSocket.Event.OnConnectionOpened")
println(event)
}
is WebSocket.Event.OnMessageReceived -> {
println("WebSocket.Event.OnMessageReceived")
println(event.message.toString())
}
is WebSocket.Event.OnConnectionClosing -> {
println("WebSocket.Event.OnConnectionClosing")
println(event)
}
is WebSocket.Event.OnConnectionClosed -> {
println("WebSocket.Event.OnConnectionClosed")
println(event)
}
is WebSocket.Event.OnConnectionFailed -> {
println("WebSocket.Event.OnConnectionFailed")
println(event)
}
}
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onEnterBackground() {
}
}
which of course is initialized in the Application class
class MyApplication : Application() {
private val appLifeCycleObserver: AppLifeCycleObserver by inject()
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@MyApplication)
modules(
arrayListOf(networkModule,databaseModule,managersModule,repositoryModule,lifecycleModule)
)
}
ProcessLifecycleOwner.get().lifecycle.addObserver(appLifeCycleObserver)
}
When the app starts, Scarlet creates a connection and onEnterForeground is triggered. So the LiveData outputs
OnConnectionOpened(webSocket=okhttp3.internal.ws.RealWebSocket@6795837)
After that i press the home button so the application enters background, so the Scarlet has to cancel the current socket connection. However after relaunch the app, app gets in Foreground start, Scarlet has to reopen the connection as it does but the problem is that in my Logcat i see double times the print
OnConnectionOpened(webSocket=okhttp3.internal.ws.RealWebSocket@b87493b)
OnConnectionOpened(webSocket=okhttp3.internal.ws.RealWebSocket@b87493b)
And as i go on and do the same thing the onConnectionOpened gets multiplied...!! What is going wrong??
@Gabiele Petrioli
So onEnterForeground is like that
and onEnterBackground is
However when i pause and relaunch the app i get an exception that the livedata can be observed only once or something.