srctree

Robin Linden parent 203a8a8c a430fde1
Clean up coroutine creation in domain module

inlinesplit
domain/src/main/kotlin/feature/CallManager.kt added: 45, removed: 44, total 1
@@ -12,7 +12,6 @@ import im.tox.tox4j.av.exceptions.ToxavCallControlException
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -35,8 +34,9 @@ private const val AUDIO_SEND_INTERVAL_MS = 20
@Singleton
class CallManager @Inject constructor(
private val tox: Tox,
private val scope: CoroutineScope,
context: Context,
) : CoroutineScope by GlobalScope {
) {
private val _inCall = MutableStateFlow<CallState>(CallState.NotInCall)
val inCall: StateFlow<CallState> get() = _inCall
 
@@ -85,7 +85,7 @@ class CallManager @Inject constructor(
set(value) { audioManager?.isSpeakerphoneOn = value }
 
private fun startAudioSender(recorder: AudioCapture, to: PublicKey) {
launch {
scope.launch {
recorder.start()
_sendingAudio.value = true
while (inCall.value is CallState.InCall && sendingAudio.value) {
 
domain/src/main/kotlin/feature/ChatManager.kt added: 45, removed: 44, total 1
@@ -10,7 +10,6 @@ import java.nio.charset.StandardCharsets
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import ltd.evilcorp.core.repository.ContactRepository
@@ -43,14 +42,15 @@ private fun String.chunked(chunkSizeInBytes: Int): MutableList<String> {
 
@Singleton
class ChatManager @Inject constructor(
private val scope: CoroutineScope,
private val contactRepository: ContactRepository,
private val messageRepository: MessageRepository,
private val tox: Tox
) : CoroutineScope by GlobalScope {
) {
var activeChat = ""
set(value) {
field = value
if (value.isNotEmpty()) launch {
if (value.isNotEmpty()) scope.launch {
contactRepository.setHasUnreadMessages(value, false)
}
}
@@ -58,7 +58,7 @@ class ChatManager @Inject constructor(
fun messagesFor(publicKey: PublicKey) = messageRepository.get(publicKey.string())
 
fun sendMessage(publicKey: PublicKey, message: String, type: MessageType = MessageType.Normal) =
launch {
scope.launch {
if (contactRepository.get(publicKey.string()).first().connectionStatus == ConnectionStatus.None) {
queueMessage(publicKey, message, type)
return@launch
@@ -83,7 +83,7 @@ class ChatManager @Inject constructor(
private fun queueMessage(publicKey: PublicKey, message: String, type: MessageType) =
messageRepository.add(Message(publicKey.string(), message, Sender.Sent, type, Int.MIN_VALUE))
 
fun resend(messages: List<Message>) = launch {
fun resend(messages: List<Message>) = scope.launch {
for (message in messages) {
val msgs = message.message.chunked(MAX_MESSAGE_LENGTH)
 
@@ -98,11 +98,11 @@ class ChatManager @Inject constructor(
}
}
 
fun deleteMessage(id: Long) = launch {
fun deleteMessage(id: Long) = scope.launch {
messageRepository.deleteMessage(id)
}
 
fun clearHistory(publicKey: PublicKey) = launch {
fun clearHistory(publicKey: PublicKey) = scope.launch {
messageRepository.delete(publicKey.string())
contactRepository.setLastMessage(publicKey.string(), 0)
}
 
domain/src/main/kotlin/feature/ContactManager.kt added: 45, removed: 44, total 1
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2020 aTox contributors
// SPDX-FileCopyrightText: 2019-2021 Robin Lindén
//
// SPDX-License-Identifier: GPL-3.0-only
 
@@ -6,7 +6,6 @@ package ltd.evilcorp.domain.feature
 
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import ltd.evilcorp.core.repository.ContactRepository
import ltd.evilcorp.core.vo.Contact
@@ -15,23 +14,24 @@ import ltd.evilcorp.domain.tox.Tox
import ltd.evilcorp.domain.tox.ToxID
 
class ContactManager @Inject constructor(
private val scope: CoroutineScope,
private val contactRepository: ContactRepository,
private val tox: Tox
) : CoroutineScope by GlobalScope {
) {
fun get(publicKey: PublicKey) = contactRepository.get(publicKey.string())
fun getAll() = contactRepository.getAll()
 
fun add(toxID: ToxID, message: String) = launch {
fun add(toxID: ToxID, message: String) = scope.launch {
tox.addContact(toxID, message)
contactRepository.add(Contact(toxID.toPublicKey().string()))
}
 
fun delete(publicKey: PublicKey) = launch {
fun delete(publicKey: PublicKey) = scope.launch {
tox.deleteContact(publicKey)
contactRepository.delete(Contact(publicKey.string()))
}
 
fun setDraft(pk: PublicKey, draft: String) = launch {
fun setDraft(pk: PublicKey, draft: String) = scope.launch {
contactRepository.setDraftMessage(pk.string(), draft)
}
}
 
domain/src/main/kotlin/feature/FileTransferManager.kt added: 45, removed: 44, total 1
@@ -17,7 +17,7 @@ import java.util.Date
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.random.Random
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch
@@ -46,6 +46,7 @@ private fun String.fingerprint() = take(FINGERPRINT_LEN)
 
@Singleton
class FileTransferManager @Inject constructor(
private val scope: CoroutineScope,
private val context: Context,
private val resolver: ContentResolver,
private val contactRepository: ContactRepository,
@@ -66,7 +67,7 @@ class FileTransferManager @Inject constructor(
 
fun reset() {
fileTransfers.clear()
GlobalScope.launch {
scope.launch {
fileTransferRepository.resetTransientData()
}
}
 
domain/src/main/kotlin/feature/FriendRequestManager.kt added: 45, removed: 44, total 1
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2020 aTox contributors
// SPDX-FileCopyrightText: 2019-2021 Robin Lindén
//
// SPDX-License-Identifier: GPL-3.0-only
 
@@ -6,7 +6,6 @@ package ltd.evilcorp.domain.feature
 
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import ltd.evilcorp.core.repository.ContactRepository
@@ -17,18 +16,19 @@ import ltd.evilcorp.domain.tox.PublicKey
import ltd.evilcorp.domain.tox.Tox
 
class FriendRequestManager @Inject constructor(
private val scope: CoroutineScope,
private val contactRepository: ContactRepository,
private val friendRequestRepository: FriendRequestRepository,
private val tox: Tox
) : CoroutineScope by GlobalScope {
) {
fun getAll(): Flow<List<FriendRequest>> = friendRequestRepository.getAll()
fun get(id: PublicKey): Flow<FriendRequest> = friendRequestRepository.get(id.string())
 
fun accept(friendRequest: FriendRequest) = launch {
fun accept(friendRequest: FriendRequest) = scope.launch {
tox.acceptFriendRequest(PublicKey(friendRequest.publicKey))
contactRepository.add(Contact(friendRequest.publicKey))
friendRequestRepository.delete(friendRequest)
}
 
fun reject(friendRequest: FriendRequest) = launch { friendRequestRepository.delete(friendRequest) }
fun reject(friendRequest: FriendRequest) = scope.launch { friendRequestRepository.delete(friendRequest) }
}
 
domain/src/main/kotlin/feature/UserManager.kt added: 45, removed: 44, total 1
@@ -6,7 +6,6 @@ package ltd.evilcorp.domain.feature
 
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import ltd.evilcorp.core.repository.UserRepository
import ltd.evilcorp.core.vo.User
@@ -15,18 +14,19 @@ import ltd.evilcorp.domain.tox.PublicKey
import ltd.evilcorp.domain.tox.Tox
 
class UserManager @Inject constructor(
private val scope: CoroutineScope,
private val userRepository: UserRepository,
private val tox: Tox
) : CoroutineScope by GlobalScope {
) {
fun get(publicKey: PublicKey) = userRepository.get(publicKey.string())
 
fun create(user: User) = launch {
fun create(user: User) = scope.launch {
userRepository.add(user)
tox.setName(user.name)
tox.setStatusMessage(user.statusMessage)
}
 
fun verifyExists(publicKey: PublicKey) = launch {
fun verifyExists(publicKey: PublicKey) = scope.launch {
if (!userRepository.exists(publicKey.string())) {
val name = tox.getName()
val statusMessage = tox.getStatusMessage()
@@ -35,17 +35,17 @@ class UserManager @Inject constructor(
}
}
 
fun setName(name: String) = launch {
fun setName(name: String) = scope.launch {
tox.setName(name)
userRepository.updateName(tox.publicKey.string(), name)
}
 
fun setStatusMessage(statusMessage: String) = launch {
fun setStatusMessage(statusMessage: String) = scope.launch {
tox.setStatusMessage(statusMessage)
userRepository.updateStatusMessage(tox.publicKey.string(), statusMessage)
}
 
fun setStatus(status: UserStatus) = launch {
fun setStatus(status: UserStatus) = scope.launch {
tox.setStatus(status)
userRepository.updateStatus(tox.publicKey.string(), status)
}
 
domain/src/main/kotlin/tox/Tox.kt added: 45, removed: 44, total 1
@@ -12,7 +12,6 @@ import javax.inject.Inject
import javax.inject.Singleton
import kotlin.random.Random
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
@@ -29,11 +28,12 @@ private const val TAG = "Tox"
 
@Singleton
class Tox @Inject constructor(
private val scope: CoroutineScope,
private val contactRepository: ContactRepository,
private val userRepository: UserRepository,
private val saveManager: SaveManager,
private val nodeRegistry: BootstrapNodeRegistry,
) : CoroutineScope by GlobalScope {
) {
val toxId: ToxID get() = tox.getToxId()
val publicKey: PublicKey by lazy { tox.getPublicKey() }
var nospam: Int
@@ -81,7 +81,7 @@ class Tox @Inject constructor(
this.password = password
started = true
 
fun loadContacts() = launch {
fun loadContacts() = scope.launch {
contactRepository.resetTransientData()
 
for ((publicKey, _) in tox.getContacts()) {
@@ -91,7 +91,7 @@ class Tox @Inject constructor(
}
}
 
fun iterateForeverAv() = launch {
fun iterateForeverAv() = scope.launch {
toxAvRunning = true
while (running) {
tox.iterateAv()
@@ -100,7 +100,7 @@ class Tox @Inject constructor(
toxAvRunning = false
}
 
fun iterateForever() = launch {
fun iterateForever() = scope.launch {
running = true
userRepository.updateConnection(publicKey.string(), ConnectionStatus.None)
while (running || toxAvRunning) {
@@ -124,7 +124,7 @@ class Tox @Inject constructor(
iterateForeverAv()
}
 
fun stop() = launch {
fun stop() = scope.launch {
running = false
while (started) delay(10)
save().join()
@@ -133,7 +133,7 @@ class Tox @Inject constructor(
}
 
private val saveMutex = Mutex()
private fun save() = launch {
private fun save() = scope.launch {
saveMutex.withLock {
val passkey = passkey
saveManager.save(