srctree

Robin Linden parent 3f89c08c d5c96d71
Clean up coroutine creation in atox module

inlinesplit
atox/src/main/kotlin/AutoAway.kt added: 38, removed: 30, total 8
@@ -9,7 +9,7 @@ import java.util.Timer
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.concurrent.schedule
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import ltd.evilcorp.atox.settings.Settings
import ltd.evilcorp.core.vo.UserStatus
@@ -20,6 +20,7 @@ private const val TAG = "AutoAway"
 
@Singleton
class AutoAway @Inject constructor(
private val scope: CoroutineScope,
private val settings: Settings,
private val userManager: UserManager,
private val tox: Tox
@@ -32,7 +33,7 @@ class AutoAway @Inject constructor(
 
Log.i(TAG, "In background, scheduling away")
awayTimer.schedule(settings.autoAwaySeconds * 1_000) {
GlobalScope.launch {
scope.launch {
if (tox.getStatus() != UserStatus.None) return@launch
Log.i(TAG, "Setting away")
userManager.setStatus(UserStatus.Away)
 
atox/src/main/kotlin/di/AppModule.kt added: 38, removed: 30, total 8
@@ -6,6 +6,8 @@ package ltd.evilcorp.atox.di
 
import dagger.Module
import dagger.Provides
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import ltd.evilcorp.atox.tox.BootstrapNodeRegistryImpl
import ltd.evilcorp.domain.tox.BootstrapNodeRegistry
 
@@ -13,4 +15,7 @@ import ltd.evilcorp.domain.tox.BootstrapNodeRegistry
class AppModule {
@Provides
fun provideBootstrapNodeRegistry(nodeRegistry: BootstrapNodeRegistryImpl): BootstrapNodeRegistry = nodeRegistry
 
@Provides
fun provideCoroutineScope(): CoroutineScope = CoroutineScope(Dispatchers.Default)
}
 
atox/src/main/kotlin/tox/BootstrapNodeRegistryImpl.kt added: 38, removed: 30, total 8
@@ -10,8 +10,8 @@ import java.io.File
import java.nio.charset.StandardCharsets
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import ltd.evilcorp.atox.R
import ltd.evilcorp.atox.settings.BootstrapNodeSource
@@ -22,6 +22,7 @@ import ltd.evilcorp.domain.tox.BootstrapNodeRegistry
 
@Singleton
class BootstrapNodeRegistryImpl @Inject constructor(
private val scope: CoroutineScope,
private val context: Context,
private val parser: BootstrapNodeJsonParser,
private val settings: Settings,
@@ -33,7 +34,7 @@ class BootstrapNodeRegistryImpl @Inject constructor(
}
 
override fun reset() {
GlobalScope.launch(Dispatchers.IO) {
scope.launch(Dispatchers.IO) {
val str = if (settings.bootstrapNodeSource == BootstrapNodeSource.BuiltIn) {
context.resources.openRawResource(R.raw.nodes).use {
val bytes = ByteArray(it.available())
 
atox/src/main/kotlin/ui/call/CallViewModel.kt added: 38, removed: 30, total 8
@@ -9,7 +9,6 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import ltd.evilcorp.atox.ui.NotificationHelper
@@ -19,10 +18,11 @@ import ltd.evilcorp.domain.feature.ContactManager
import ltd.evilcorp.domain.tox.PublicKey
 
class CallViewModel @Inject constructor(
private val scope: CoroutineScope,
private val callManager: CallManager,
private val notificationHelper: NotificationHelper,
private val contactManager: ContactManager,
) : ViewModel(), CoroutineScope by GlobalScope {
) : ViewModel() {
private var publicKey = PublicKey("")
 
val contact: LiveData<Contact> by lazy {
@@ -35,10 +35,10 @@ class CallViewModel @Inject constructor(
 
fun startCall() {
callManager.startCall(publicKey)
launch { notificationHelper.showOngoingCallNotification(contactManager.get(publicKey).first()) }
scope.launch { notificationHelper.showOngoingCallNotification(contactManager.get(publicKey).first()) }
}
 
fun endCall() = launch {
fun endCall() = scope.launch {
callManager.endCall(publicKey)
notificationHelper.dismissCallNotification(publicKey)
}
 
atox/src/main/kotlin/ui/chat/ChatViewModel.kt added: 38, removed: 30, total 8
@@ -17,7 +17,6 @@ import java.io.FileInputStream
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.take
@@ -53,8 +52,9 @@ class ChatViewModel @Inject constructor(
private val fileTransferManager: FileTransferManager,
private val notificationHelper: NotificationHelper,
private val resolver: ContentResolver,
private val context: Context
) : ViewModel(), CoroutineScope by GlobalScope {
private val context: Context,
private val scope: CoroutineScope,
) : ViewModel() {
private var publicKey = PublicKey("")
private var sentTyping = false
 
@@ -82,7 +82,7 @@ class ChatViewModel @Inject constructor(
 
fun send(message: String, type: MessageType) = chatManager.sendMessage(publicKey, message, type)
 
fun clearHistory() = launch {
fun clearHistory() = scope.launch {
chatManager.clearHistory(publicKey)
fileTransferManager.deleteAll(publicKey)
}
@@ -108,26 +108,26 @@ class ChatViewModel @Inject constructor(
}
}
 
fun acceptFt(id: Int) = launch {
fun acceptFt(id: Int) = scope.launch {
fileTransferManager.accept(id)
}
 
fun rejectFt(id: Int) = launch {
fun rejectFt(id: Int) = scope.launch {
fileTransferManager.reject(id)
}
 
fun createFt(file: Uri) = launch {
fun createFt(file: Uri) = scope.launch {
fileTransferManager.create(publicKey, file)
}
 
fun delete(msg: Message) = launch {
fun delete(msg: Message) = scope.launch {
if (msg.type == MessageType.FileTransfer) {
fileTransferManager.delete(msg.correlationId)
}
chatManager.deleteMessage(msg.id)
}
 
fun exportFt(id: Int, dest: Uri) = launch {
fun exportFt(id: Int, dest: Uri) = scope.launch {
fileTransferManager.get(id).take(1).collect { ft ->
launch(Dispatchers.IO) {
try {
 
atox/src/main/kotlin/ui/contactlist/ContactListViewModel.kt added: 38, removed: 30, total 8
@@ -16,7 +16,6 @@ import java.io.FileOutputStream
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import ltd.evilcorp.atox.R
@@ -37,6 +36,7 @@ import ltd.evilcorp.domain.tox.ToxSaveStatus
import ltd.evilcorp.domain.tox.testToxSave
 
class ContactListViewModel @Inject constructor(
private val scope: CoroutineScope,
private val context: Context,
private val resolver: ContentResolver,
private val chatManager: ChatManager,
@@ -46,7 +46,7 @@ class ContactListViewModel @Inject constructor(
private val tox: Tox,
private val toxStarter: ToxStarter,
userManager: UserManager
) : ViewModel(), CoroutineScope by GlobalScope {
) : ViewModel() {
val publicKey by lazy { tox.publicKey }
 
val user: LiveData<User> by lazy { userManager.get(publicKey).asLiveData() }
@@ -62,12 +62,12 @@ class ContactListViewModel @Inject constructor(
fun deleteContact(publicKey: PublicKey) {
contactManager.delete(publicKey)
chatManager.clearHistory(publicKey)
launch {
scope.launch {
fileTransferManager.deleteAll(publicKey)
}
}
 
fun saveToxBackupTo(uri: Uri) = launch(Dispatchers.IO) {
fun saveToxBackupTo(uri: Uri) = scope.launch(Dispatchers.IO) {
// Export the save.
resolver.openFileDescriptor(uri, "w")!!.use { fd ->
FileOutputStream(fd.fileDescriptor).use { out ->
 
atox/src/main/kotlin/ui/settings/SettingsFragment.kt added: 38, removed: 30, total 8
@@ -22,8 +22,8 @@ import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import java.lang.NumberFormatException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import ltd.evilcorp.atox.BuildConfig
@@ -46,6 +46,7 @@ private fun Spinner.onItemSelectedListener(callback: (Int) -> Unit) {
 
class SettingsFragment : BaseFragment<FragmentSettingsBinding>(FragmentSettingsBinding::inflate) {
private val vm: SettingsViewModel by viewModels { vmFactory }
private val scope = CoroutineScope(Dispatchers.Default)
private val blockBackCallback = object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
Toast.makeText(requireContext(), getString(R.string.warn_proxy_broken), Toast.LENGTH_LONG).show()
@@ -59,7 +60,7 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding>(FragmentSettingsB
}
 
private val importNodesLauncher = registerForActivityResult(ActivityResultContracts.OpenDocument()) { uri ->
GlobalScope.launch {
scope.launch {
if (uri != null && vm.validateNodeJson(uri)) {
if (vm.importNodeJson(uri)) {
vm.setBootstrapNodeSource(BootstrapNodeSource.UserProvided)