update coroutine scopes

pull/547/head
andrekir 2022-12-24 00:20:54 -03:00
rodzic d42dbb7b04
commit 4ea17e36bc
2 zmienionych plików z 15 dodań i 16 usunięć

Wyświetl plik

@ -17,6 +17,7 @@ import androidx.activity.result.IntentSenderRequest
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.geeksville.mesh.android.GeeksvilleApplication import com.geeksville.mesh.android.GeeksvilleApplication
import com.geeksville.mesh.android.Logging import com.geeksville.mesh.android.Logging
import com.geeksville.mesh.MainActivity import com.geeksville.mesh.MainActivity
@ -34,8 +35,6 @@ import com.geeksville.mesh.util.anonymize
import com.geeksville.mesh.util.exceptionReporter import com.geeksville.mesh.util.exceptionReporter
import com.hoho.android.usbserial.driver.UsbSerialDriver import com.hoho.android.usbserial.driver.UsbSerialDriver
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
@ -277,7 +276,7 @@ class BTScanModel @Inject constructor(
// Start Network Service Discovery (find TCP devices) // Start Network Service Discovery (find TCP devices)
networkDiscovery = nsdRepository.networkDiscoveryFlow() networkDiscovery = nsdRepository.networkDiscoveryFlow()
.onEach { addDevice(TCPDeviceListEntry(it)) } .onEach { addDevice(TCPDeviceListEntry(it)) }
.launchIn(CoroutineScope(Dispatchers.Main)) .launchIn(viewModelScope)
if (hasBluetoothPermission) { if (hasBluetoothPermission) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S && hasCompanionDeviceApi) if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S && hasCompanionDeviceApi)

Wyświetl plik

@ -32,7 +32,11 @@ import com.google.protobuf.ByteString
import com.google.protobuf.InvalidProtocolBufferException import com.google.protobuf.InvalidProtocolBufferException
import dagger.Lazy import dagger.Lazy
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.* import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@ -174,7 +178,8 @@ class MeshService : Service(), Logging {
location.altitude.toInt(), location.altitude.toInt(),
(location.time / 1000).toInt(), (location.time / 1000).toInt(),
) )
}.launchIn(CoroutineScope(Dispatchers.Default)) }
.launchIn(serviceScope)
} }
} }
@ -247,17 +252,12 @@ class MeshService : Service(), Logging {
// We in turn need to use the radiointerface service // We in turn need to use the radiointerface service
radioInterfaceService.connect() radioInterfaceService.connect()
} }
serviceScope.handledLaunch { radioInterfaceService.connectionState.onEach(::onRadioConnectionState)
radioInterfaceService.connectionState.collect(::onRadioConnectionState) .launchIn(serviceScope)
} radioInterfaceService.receivedData.onEach(::onReceiveFromRadio)
serviceScope.handledLaunch { .launchIn(serviceScope)
radioInterfaceService.receivedData.collect(::onReceiveFromRadio) localConfigRepository.localConfigFlow.onEach { localConfig = it }
} .launchIn(serviceScope)
serviceScope.handledLaunch {
localConfigRepository.localConfigFlow.collect { config ->
localConfig = config
}
}
// the rest of our init will happen once we are in radioConnection.onServiceConnected // the rest of our init will happen once we are in radioConnection.onServiceConnected
} }