Skip to content

Commit

Permalink
Merge pull request #395 from Adamant-im/dev/trello.com/c/QSPhhLjV
Browse files Browse the repository at this point in the history
[trello.com/c/QSPhhLjV] fix: wallets balance freezes when pull refresh
  • Loading branch information
StanislavDevIOS authored Nov 28, 2023
2 parents 1f1415c + 45e4ffd commit 6541c95
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 81 deletions.
26 changes: 13 additions & 13 deletions Adamant.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@
"repositoryURL": "https://github.com/google/GoogleDataTransport.git",
"state": {
"branch": null,
"revision": "98a00258d4518b7521253a70b7f70bb76d2120fe",
"version": "9.2.4"
"revision": "aae45a320fd0d11811820335b1eabc8753902a40",
"version": "9.2.5"
}
},
{
"package": "GoogleUtilities",
"repositoryURL": "https://github.com/google/GoogleUtilities.git",
"state": {
"branch": null,
"revision": "4446686bc3714d49ce043d0f68318f42ed718cb6",
"version": "7.11.4"
"revision": "bc27fad73504f3d4af235de451f02ee22586ebd3",
"version": "7.12.1"
}
},
{
Expand Down Expand Up @@ -159,8 +159,8 @@
"repositoryURL": "https://github.com/firebase/leveldb.git",
"state": {
"branch": null,
"revision": "0706abcc6b0bd9cedfbb015ba840e4a780b5159b",
"version": "1.22.2"
"revision": "9d108e9112aa1d65ce508facf804674546116d9c",
"version": "1.22.3"
}
},
{
Expand All @@ -177,8 +177,8 @@
"repositoryURL": "https://github.com/MessageKit/MessageKit.git",
"state": {
"branch": null,
"revision": "14bfa7eb9f93267c3d7b8cdf58615bba27be672a",
"version": "4.1.1"
"revision": "1993e8e90d4e9a61b8d9bc8ceb733964ce943376",
"version": "4.2.0"
}
},
{
Expand Down Expand Up @@ -276,7 +276,7 @@
"repositoryURL": "https://github.com/socketio/socket.io-client-swift",
"state": {
"branch": "master",
"revision": "a1ed825835a2d8c2555938e96557ccc05e4bebf3",
"revision": "175da8b5156f6b132436f0676cc84c2f6a766b6e",
"version": null
}
},
Expand All @@ -285,17 +285,17 @@
"repositoryURL": "https://github.com/daltoniam/Starscream",
"state": {
"branch": null,
"revision": "df8d82047f6654d8e4b655d1b1525c64e1059d21",
"version": "4.0.4"
"revision": "ac6c0fc9da221873e01bd1a0d4818498a71eef33",
"version": "4.0.6"
}
},
{
"package": "SwiftProtobuf",
"repositoryURL": "https://github.com/apple/swift-protobuf.git",
"state": {
"branch": null,
"revision": "ce20dc083ee485524b802669890291c0d8090170",
"version": "1.22.1"
"revision": "07f7f26ded8df9645c072f220378879c4642e063",
"version": "1.25.1"
}
},
{
Expand Down
4 changes: 3 additions & 1 deletion Adamant/Modules/Wallets/Adamant/AdmWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@ final class AdmWalletService: NSObject, WalletService {
let isRaised: Bool

if let wallet = wallet as? AdmWallet {
wallet.isBalanceInitialized = true
isRaised = (wallet.balance < account.balance) && wallet.isBalanceInitialized
if wallet.balance != account.balance {
wallet.balance = account.balance
notify = true
} else if wallet.isBalanceInitialized {
notify = true
} else {
notify = false
}
wallet.isBalanceInitialized = true
} else {
let wallet = AdmWallet(address: account.address)
wallet.isBalanceInitialized = true
Expand Down
18 changes: 10 additions & 8 deletions Adamant/Modules/Wallets/Bitcoin/BtcWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ final class BtcWalletService: WalletService {
@Atomic private(set) var btcWallet: BtcWallet?
@Atomic private(set) var enabled = true
@Atomic public var network: Network
@Atomic private var initialBalanceCheck = false

static let jsonDecoder = JSONDecoder()

Expand Down Expand Up @@ -215,7 +214,6 @@ final class BtcWalletService: WalletService {
.receive(on: OperationQueue.main)
.sink { [weak self] _ in
self?.btcWallet = nil
self?.initialBalanceCheck = false
if let balanceObserver = self?.balanceObserver {
NotificationCenter.default.removeObserver(balanceObserver)
self?.balanceObserver = nil
Expand Down Expand Up @@ -255,22 +253,21 @@ final class BtcWalletService: WalletService {
setState(.updating)

if let balance = try? await getBalance() {
wallet.isBalanceInitialized = true
let notification: Notification.Name?

let isRaised = (wallet.balance < balance) && !initialBalanceCheck
let isRaised = (wallet.balance < balance) && wallet.isBalanceInitialized

if wallet.balance != balance {
wallet.balance = balance
notification = walletUpdatedNotification
initialBalanceCheck = false
} else if initialBalanceCheck {
initialBalanceCheck = false
} else if !wallet.isBalanceInitialized {
notification = walletUpdatedNotification
} else {
notification = nil
}

wallet.isBalanceInitialized = true

if isRaised {
vibroService.applyVibration(.success)
}
Expand Down Expand Up @@ -413,6 +410,12 @@ extension BtcWalletService: InitiatedWithPassphraseService {
let eWallet = try BtcWallet(privateKey: privateKey, addressConverter: addressConverter)
self.btcWallet = eWallet

NotificationCenter.default.post(
name: walletUpdatedNotification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: eWallet]
)

if !self.enabled {
self.enabled = true
NotificationCenter.default.post(name: self.serviceEnabledChanged, object: self)
Expand All @@ -429,7 +432,6 @@ extension BtcWalletService: InitiatedWithPassphraseService {
throw WalletServiceError.accountNotFound
}

service.initialBalanceCheck = true
service.setState(.upToDate, silent: true)
Task {
service.update()
Expand Down
18 changes: 10 additions & 8 deletions Adamant/Modules/Wallets/Dash/DashWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ final class DashWalletService: WalletService {
@Atomic private (set) var dashWallet: DashWallet?
@Atomic private (set) var enabled = true
@Atomic public var network: Network
@Atomic private var initialBalanceCheck = false

let defaultDispatchQueue = DispatchQueue(label: "im.adamant.dashWalletService", qos: .userInteractive, attributes: [.concurrent])

Expand Down Expand Up @@ -191,7 +190,6 @@ final class DashWalletService: WalletService {
.receive(on: OperationQueue.main)
.sink { [weak self] _ in
self?.dashWallet = nil
self?.initialBalanceCheck = false
if let balanceObserver = self?.balanceObserver {
NotificationCenter.default.removeObserver(balanceObserver)
self?.balanceObserver = nil
Expand Down Expand Up @@ -231,21 +229,20 @@ final class DashWalletService: WalletService {
setState(.updating)

if let balance = try? await getBalance() {
wallet.isBalanceInitialized = true
let notification: Notification.Name?
let isRaised = (wallet.balance < balance) && !initialBalanceCheck
let isRaised = (wallet.balance < balance) && wallet.isBalanceInitialized

if wallet.balance != balance {
wallet.balance = balance
notification = walletUpdatedNotification
initialBalanceCheck = false
} else if initialBalanceCheck {
initialBalanceCheck = false
} else if !wallet.isBalanceInitialized {
notification = walletUpdatedNotification
} else {
notification = nil
}

wallet.isBalanceInitialized = true

if isRaised {
vibroService.applyVibration(.success)
}
Expand Down Expand Up @@ -304,6 +301,12 @@ extension DashWalletService: InitiatedWithPassphraseService {

self.dashWallet = eWallet

NotificationCenter.default.post(
name: walletUpdatedNotification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: eWallet]
)

if !self.enabled {
self.enabled = true
NotificationCenter.default.post(name: self.serviceEnabledChanged, object: self)
Expand All @@ -319,7 +322,6 @@ extension DashWalletService: InitiatedWithPassphraseService {
}
}

service.initialBalanceCheck = true
service.setState(.upToDate, silent: true)
Task {
service.update()
Expand Down
18 changes: 10 additions & 8 deletions Adamant/Modules/Wallets/Doge/DogeWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ final class DogeWalletService: WalletService {
@Atomic private(set) var dogeWallet: DogeWallet?
@Atomic private(set) var enabled = true
@Atomic public var network: Network
@Atomic private var initialBalanceCheck = false

let defaultDispatchQueue = DispatchQueue(
label: "im.adamant.dogeWalletService",
Expand Down Expand Up @@ -189,7 +188,6 @@ final class DogeWalletService: WalletService {
.receive(on: OperationQueue.main)
.sink { [weak self] _ in
self?.dogeWallet = nil
self?.initialBalanceCheck = false
if let balanceObserver = self?.balanceObserver {
NotificationCenter.default.removeObserver(balanceObserver)
self?.balanceObserver = nil
Expand Down Expand Up @@ -229,21 +227,20 @@ final class DogeWalletService: WalletService {
setState(.updating)

if let balance = try? await getBalance() {
wallet.isBalanceInitialized = true
let notification: Notification.Name?
let isRaised = (wallet.balance < balance) && !initialBalanceCheck
let isRaised = (wallet.balance < balance) && wallet.isBalanceInitialized

if wallet.balance != balance {
wallet.balance = balance
notification = walletUpdatedNotification
initialBalanceCheck = false
} else if initialBalanceCheck {
initialBalanceCheck = false
} else if !wallet.isBalanceInitialized {
notification = walletUpdatedNotification
} else {
notification = nil
}

wallet.isBalanceInitialized = true

if isRaised {
vibroService.applyVibration(.success)
}
Expand Down Expand Up @@ -293,6 +290,12 @@ extension DogeWalletService: InitiatedWithPassphraseService {
let eWallet = try DogeWallet(privateKey: privateKey, addressConverter: addressConverter)
self.dogeWallet = eWallet

NotificationCenter.default.post(
name: walletUpdatedNotification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: eWallet]
)

if !self.enabled {
self.enabled = true
NotificationCenter.default.post(name: self.serviceEnabledChanged, object: self)
Expand All @@ -308,7 +311,6 @@ extension DogeWalletService: InitiatedWithPassphraseService {
}
}

service.initialBalanceCheck = true
service.setState(.upToDate, silent: true)

Task {
Expand Down
18 changes: 10 additions & 8 deletions Adamant/Modules/Wallets/ERC20/ERC20WalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ final class ERC20WalletService: WalletService {
let token: ERC20Token
@Atomic private(set) var enabled = true
@Atomic private var subscriptions = Set<AnyCancellable>()
@Atomic private var initialBalanceCheck = false

// MARK: - State
@Atomic private (set) var state: WalletServiceState = .notInitiated
Expand Down Expand Up @@ -199,7 +198,6 @@ final class ERC20WalletService: WalletService {
.receive(on: OperationQueue.main)
.sink { [weak self] _ in
self?.ethWallet = nil
self?.initialBalanceCheck = false
if let balanceObserver = self?.balanceObserver {
NotificationCenter.default.removeObserver(balanceObserver)
self?.balanceObserver = nil
Expand Down Expand Up @@ -239,21 +237,20 @@ final class ERC20WalletService: WalletService {
setState(.updating)

if let balance = try? await getBalance(forAddress: wallet.ethAddress) {
wallet.isBalanceInitialized = true
let notification: Notification.Name?
let isRaised = (wallet.balance < balance) && !initialBalanceCheck
let isRaised = (wallet.balance < balance) && wallet.isBalanceInitialized

if wallet.balance != balance {
wallet.balance = balance
notification = walletUpdatedNotification
initialBalanceCheck = false
} else if initialBalanceCheck {
initialBalanceCheck = false
} else if !wallet.isBalanceInitialized {
notification = walletUpdatedNotification
} else {
notification = nil
}

wallet.isBalanceInitialized = true

if isRaised {
vibroService.applyVibration(.success)
}
Expand Down Expand Up @@ -375,7 +372,12 @@ extension ERC20WalletService: InitiatedWithPassphraseService {
NotificationCenter.default.post(name: serviceEnabledChanged, object: self)
}

self.initialBalanceCheck = true
NotificationCenter.default.post(
name: walletUpdatedNotification,
object: self,
userInfo: [AdamantUserInfoKey.WalletService.wallet: eWallet]
)

self.setState(.upToDate, silent: true)
Task {
await update()
Expand Down
Loading

0 comments on commit 6541c95

Please sign in to comment.