Attach @MainActor where needed

Some methods were updating UI from non-main thread.
This commit is contained in:
Davide De Rosa 2022-07-15 17:21:40 +02:00
parent f33380b4e2
commit 58e375ec41
9 changed files with 24 additions and 7 deletions

View File

@ -27,6 +27,7 @@ import Foundation
import Intents import Intents
import PassepartoutLibrary import PassepartoutLibrary
@MainActor
extension IntentDispatcher { extension IntentDispatcher {
private enum IntentError: Error { private enum IntentError: Error {
case notProvider(UUID) case notProvider(UUID)

View File

@ -93,9 +93,11 @@ extension IntentsManager: INUIEditVoiceShortcutViewControllerDelegate {
// so damn it, reload manually after a delay // so damn it, reload manually after a delay
Task { Task {
await Task.maybeWait(forMilliseconds: Constants.Delays.xxxReloadEditedShortcut) await Task.maybeWait(forMilliseconds: Constants.Delays.xxxReloadEditedShortcut)
await MainActor.run {
reloadShortcuts() reloadShortcuts()
} }
} }
}
func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didDeleteVoiceShortcutWithIdentifier deletedVoiceShortcutIdentifier: UUID) { func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didDeleteVoiceShortcutWithIdentifier deletedVoiceShortcutIdentifier: UUID) {
shortcuts.removeValue(forKey: deletedVoiceShortcutIdentifier) shortcuts.removeValue(forKey: deletedVoiceShortcutIdentifier)

View File

@ -27,6 +27,8 @@ import Foundation
import PassepartoutLibrary import PassepartoutLibrary
extension AddProviderView { extension AddProviderView {
@MainActor
class ViewModel: ObservableObject { class ViewModel: ObservableObject {
enum PendingOperation { enum PendingOperation {
case index case index

View File

@ -27,6 +27,8 @@ import Foundation
import PassepartoutLibrary import PassepartoutLibrary
extension CoreContext { extension CoreContext {
@MainActor
static let shared = CoreContext(store: UserDefaultsStore(defaults: .standard)) static let shared = CoreContext(store: UserDefaultsStore(defaults: .standard))
} }

View File

@ -60,6 +60,7 @@ class CoreContext {
private var cancellables: Set<AnyCancellable> = [] private var cancellables: Set<AnyCancellable> = []
@MainActor
init(store: KeyValueStore) { init(store: KeyValueStore) {
self.store = store self.store = store

View File

@ -288,11 +288,13 @@ extension DefaultProfileManager {
currentProfile.isLoading = true currentProfile.isLoading = true
Task { Task {
try await makeProfileReady(profile) try await makeProfileReady(profile)
await MainActor.run {
currentProfile.value = profile currentProfile.value = profile
currentProfile.isLoading = false currentProfile.isLoading = false
} }
} }
} }
}
} }
extension DefaultProfileManager { extension DefaultProfileManager {
@ -334,7 +336,7 @@ extension DefaultProfileManager {
didUpdateProfiles.send() didUpdateProfiles.send()
// IMPORTANT: defer task to avoid recursive saves // IMPORTANT: defer task to avoid recursive saves (is non-main thread an issue?)
// FIXME: Core Data, not sure about this workaround // FIXME: Core Data, not sure about this workaround
Task { Task {
fixDuplicateNames(in: newHeaders) fixDuplicateNames(in: newHeaders)

View File

@ -53,10 +53,12 @@ public class SSIDReader: NSObject, ObservableObject, CLLocationManagerDelegate {
private func notifyCurrentSSID() { private func notifyCurrentSSID() {
Task { Task {
let currentSSID = await Utils.currentWifiSSID() ?? "" let currentSSID = await Utils.currentWifiSSID() ?? ""
await MainActor.run {
publisher.send(currentSSID) publisher.send(currentSSID)
cancellables.removeAll() cancellables.removeAll()
} }
} }
}
public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus { switch manager.authorizationStatus {

View File

@ -45,6 +45,7 @@ public class MockVPNManagerStrategy: VPNManagerStrategy {
public func reinstate(configuration: VPNConfiguration) { public func reinstate(configuration: VPNConfiguration) {
} }
@MainActor
public func connect(configuration: VPNConfiguration) { public func connect(configuration: VPNConfiguration) {
guard currentState?.vpnStatus != .connected else { guard currentState?.vpnStatus != .connected else {
return return
@ -58,6 +59,7 @@ public class MockVPNManagerStrategy: VPNManagerStrategy {
} }
} }
@MainActor
public func disconnect() { public func disconnect() {
stopCountingData() stopCountingData()
guard currentState?.vpnStatus != .disconnected else { guard currentState?.vpnStatus != .disconnected else {
@ -92,6 +94,7 @@ public class MockVPNManagerStrategy: VPNManagerStrategy {
dataCountTimer = nil dataCountTimer = nil
} }
@MainActor
public func removeConfigurations() { public func removeConfigurations() {
disconnect() disconnect()
} }

View File

@ -68,6 +68,7 @@ public class TunnelKitVPNManagerStrategy: VPNManagerStrategy {
private var currentBundleIdentifier: String? private var currentBundleIdentifier: String?
@MainActor
public init(appGroup: String, tunnelBundleIdentifier: @escaping (VPNProtocolType) -> String, dataCountInterval: TimeInterval = 3.0) { public init(appGroup: String, tunnelBundleIdentifier: @escaping (VPNProtocolType) -> String, dataCountInterval: TimeInterval = 3.0) {
self.appGroup = appGroup self.appGroup = appGroup
self.tunnelBundleIdentifier = tunnelBundleIdentifier self.tunnelBundleIdentifier = tunnelBundleIdentifier
@ -107,6 +108,7 @@ public class TunnelKitVPNManagerStrategy: VPNManagerStrategy {
// use this to drop redundant NE notifications // use this to drop redundant NE notifications
vpnState vpnState
.removeDuplicates() .removeDuplicates()
.receive(on: DispatchQueue.main)
.sink { .sink {
self.currentState?.isEnabled = $0.isEnabled self.currentState?.isEnabled = $0.isEnabled
self.currentState?.vpnStatus = $0.vpnStatus self.currentState?.vpnStatus = $0.vpnStatus