srctree

Robin Linden parent e2a147de 495eb93c
Don't use LiveData outside of the GUI classes

LiveData in general should only ever be used in GUIs. They always run onthe main thread, and they don't support things like suspension points.This patch nukes the only instance of us using LiveData outside of theGUI bits.

inlinesplit
atox/build.gradle.kts added: 30, removed: 21, total 9
@@ -70,6 +70,7 @@ dependencies {
implementation(AndroidX.preference)
 
implementation(AndroidX.Lifecycle.livedataKtx)
implementation(AndroidX.Lifecycle.runtimeKtx)
implementation(AndroidX.Lifecycle.service)
implementation(AndroidX.Lifecycle.viewmodelKtx)
 
 
atox/src/main/kotlin/ToxService.kt added: 30, removed: 21, total 9
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2021 aTox contributors
// SPDX-FileCopyrightText: 2019-2022 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only
 
@@ -13,15 +13,18 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.res.ResourcesCompat
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.asLiveData
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import java.util.Timer
import javax.inject.Inject
import kotlin.concurrent.schedule
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.launch
import ltd.evilcorp.atox.tox.ToxStarter
import ltd.evilcorp.core.repository.UserRepository
import ltd.evilcorp.core.vo.ConnectionStatus
import ltd.evilcorp.core.vo.User
import ltd.evilcorp.domain.tox.Tox
import ltd.evilcorp.domain.tox.ToxSaveStatus
 
@@ -90,23 +93,27 @@ class ToxService : LifecycleService() {
createNotificationChannel()
startForeground(NOTIFICATION_ID, notificationFor(connectionStatus))
 
userRepository.get(tox.publicKey.string())
.filter { user: User? -> user != null }.asLiveData().observe(this) { user ->
if (user.connectionStatus == connectionStatus) return@observe
connectionStatus = user.connectionStatus
notifier.notify(NOTIFICATION_ID, notificationFor(connectionStatus))
if (connectionStatus == ConnectionStatus.None) {
Log.i(TAG, "Gone offline, scheduling bootstrap")
bootstrapTimer.schedule(BOOTSTRAP_INTERVAL_MS, BOOTSTRAP_INTERVAL_MS) {
Log.i(TAG, "Been offline for too long, bootstrapping")
tox.isBootstrapNeeded = true
lifecycleScope.launch(Dispatchers.Default) {
userRepository.get(tox.publicKey.string())
.filterNotNull()
.filter { it.connectionStatus != connectionStatus }
.flowWithLifecycle(lifecycle)
.collect { user ->
connectionStatus = user.connectionStatus
notifier.notify(NOTIFICATION_ID, notificationFor(connectionStatus))
if (connectionStatus == ConnectionStatus.None) {
Log.i(TAG, "Gone offline, scheduling bootstrap")
bootstrapTimer.schedule(BOOTSTRAP_INTERVAL_MS, BOOTSTRAP_INTERVAL_MS) {
Log.i(TAG, "Been offline for too long, bootstrapping")
tox.isBootstrapNeeded = true
}
} else {
Log.i(TAG, "Online, cancelling bootstrap")
bootstrapTimer.cancel()
bootstrapTimer = Timer()
}
} else {
Log.i(TAG, "Online, cancelling bootstrap")
bootstrapTimer.cancel()
bootstrapTimer = Timer()
}
}
}
}
 
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
 
buildSrc/src/main/kotlin/Dependencies.kt added: 30, removed: 21, total 9
@@ -58,6 +58,7 @@ object AndroidX {
object Lifecycle {
private const val version = "2.4.0"
const val livedataKtx = "androidx.lifecycle:lifecycle-livedata-ktx:$version"
const val runtimeKtx = "androidx.lifecycle:lifecycle-runtime-ktx:$version"
const val service = "androidx.lifecycle:lifecycle-service:$version"
const val viewmodelKtx = "androidx.lifecycle:lifecycle-viewmodel-ktx:$version"
}