From f5acb81f7c270e41cfd977c5234cb2e476b8011a Mon Sep 17 00:00:00 2001 From: Matthew <6657488+reez@users.noreply.github.com> Date: Mon, 21 Aug 2023 16:38:33 -0500 Subject: [PATCH] refactor: main actor --- .../View Model/ReceiveViewModel.swift | 1 + BDKSwiftExampleWallet/View Model/SendViewModel.swift | 2 ++ BDKSwiftExampleWallet/View Model/WalletViewModel.swift | 10 ++-------- .../BDKSwiftExampleWalletWalletViewModelTests.swift | 1 + 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/BDKSwiftExampleWallet/View Model/ReceiveViewModel.swift b/BDKSwiftExampleWallet/View Model/ReceiveViewModel.swift index c9bef50..bf1e05a 100644 --- a/BDKSwiftExampleWallet/View Model/ReceiveViewModel.swift +++ b/BDKSwiftExampleWallet/View Model/ReceiveViewModel.swift @@ -6,6 +6,7 @@ // import Foundation +import Observation @Observable class ReceiveViewModel { diff --git a/BDKSwiftExampleWallet/View Model/SendViewModel.swift b/BDKSwiftExampleWallet/View Model/SendViewModel.swift index 7bd4711..7e825ae 100644 --- a/BDKSwiftExampleWallet/View Model/SendViewModel.swift +++ b/BDKSwiftExampleWallet/View Model/SendViewModel.swift @@ -7,7 +7,9 @@ import Foundation import BitcoinDevKit +import Observation +@MainActor @Observable class SendViewModel { var balanceTotal: UInt64 = 0 diff --git a/BDKSwiftExampleWallet/View Model/WalletViewModel.swift b/BDKSwiftExampleWallet/View Model/WalletViewModel.swift index c9c23e9..4160fec 100644 --- a/BDKSwiftExampleWallet/View Model/WalletViewModel.swift +++ b/BDKSwiftExampleWallet/View Model/WalletViewModel.swift @@ -7,7 +7,9 @@ import Foundation import BitcoinDevKit +import Observation +@MainActor @Observable class WalletViewModel { var balanceTotal: UInt64 = 0 @@ -26,10 +28,8 @@ class WalletViewModel { func getPrices() async { do { let price = try await priceService.prices() - DispatchQueue.main.async { self.price = price.usd self.time = price.time - } } catch { print("getPrices error: \(error.localizedDescription)") } @@ -60,23 +60,17 @@ class WalletViewModel { } func sync() async { - DispatchQueue.main.async { self.walletSyncState = .syncing - } Task { do { try await BDKService.shared.sync() - DispatchQueue.main.async { self.walletSyncState = .synced self.lastSyncTime = Date() self.getBalance() self.getTransactions() self.valueInUSD() - } } catch { - DispatchQueue.main.async { self.walletSyncState = .error(error) - } } } } diff --git a/BDKSwiftExampleWalletTests/BDKSwiftExampleWalletWalletViewModelTests.swift b/BDKSwiftExampleWalletTests/BDKSwiftExampleWalletWalletViewModelTests.swift index c3dd3b6..38fb106 100644 --- a/BDKSwiftExampleWalletTests/BDKSwiftExampleWalletWalletViewModelTests.swift +++ b/BDKSwiftExampleWalletTests/BDKSwiftExampleWalletWalletViewModelTests.swift @@ -8,6 +8,7 @@ import XCTest @testable import BDKSwiftExampleWallet +@MainActor final class BDKSwiftExampleWalletWalletViewModelTests: XCTestCase { func testWalletViewModel() async {