Skip to content

Commit 9315df9

Browse files
authored
Merge pull request #854 from Adamant-im/trello.com/c/dEf1yVQr
[trello.com/c/dEf1yVQr] Improve overall code and its logic
2 parents a9eb7bc + fdd98e9 commit 9315df9

21 files changed

+213
-141
lines changed

Adamant/Modules/Account/AccountViewController/AccountViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ final class AccountViewController: FormViewController {
981981
}
982982

983983
Task { @MainActor in
984-
accountService.update(shouldUpdateUIBalance: true, updateOnlyADM: false, updateOnlyVisible: true)
984+
accountService.update(resetBalanceAndUpdate: true, updateOnlyADM: false, updateOnlyVisible: true)
985985
}
986986
refreshControl.endRefreshing()
987987
}
@@ -1057,7 +1057,7 @@ extension AccountViewController: PagingViewControllerDataSource, PagingViewContr
10571057

10581058
nonisolated func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem {
10591059
MainActor.assertIsolated()
1060-
1060+
10611061
return DispatchQueue.onMainThreadSyncSafe {
10621062
return viewModel.state.wallets[safe: index] ?? AccountWalletCellState.default
10631063
}

Adamant/Modules/Settings/VisibleWallets/VisibleWalletsViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ final class VisibleWalletsViewController: KeyboardObservingViewController {
147147
}
148148

149149
@objc private func updateBalances() {
150-
accountService.update(shouldUpdateUIBalance: true, updateOnlyADM: false, updateOnlyVisible: false)
150+
accountService.update(resetBalanceAndUpdate: true, updateOnlyADM: false, updateOnlyVisible: false)
151151
refreshControl.endRefreshing()
152152
}
153153

Adamant/Modules/Wallets/Adamant/AdmWalletService.swift

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, WalletStaticCoreProt
126126
var hasEnabledNodePublisher: AnyObservable<Bool> {
127127
apiService.hasEnabledNodePublisher
128128
}
129+
130+
@MainActor
131+
var hasAllowedNodePublisher: AnyObservable<Bool> {
132+
apiService.hasAllowedNodePublisher
133+
}
129134

130135
private(set) lazy var coinStorage: CoinStorageService = AdamantCoinStorageService(
131136
coinId: tokenUniqueID,
@@ -163,22 +168,34 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, WalletStaticCoreProt
163168
.store(in: &subscriptions)
164169

165170
NotificationCenter.default
166-
.notifications(named: .AdamantAccountService.walletUpdated, object: nil)
167-
.sink { [weak self] _ in
168-
self?.update()
171+
.publisher(for: .AdamantAccountService.isBalanceExpired)
172+
.compactMap { $0.object as? Bool }
173+
.sink { [weak self] isExpired in
174+
self?.resetBalanceAndUpdate()
169175
}
170176
.store(in: &subscriptions)
171177
}
172178

173-
func updateWithRefreshUIBalance(){
179+
func resetBalanceAndUpdate(){
174180
Task {
175-
admWallet?.isBalanceInitialized = false
176181
await walletUpdateSender.send()
177-
update()
182+
await update({ [weak self] in
183+
guard let self = self, let isBalanceExpired = self.accountService?.isBalanceExpired else {
184+
return
185+
}
186+
self.admWallet?.isBalanceInitialized = !isBalanceExpired
187+
})
178188
}
179189
}
180190

181191
func update() {
192+
Task{
193+
await update(nil)
194+
}
195+
}
196+
197+
@MainActor
198+
func update(_ completion: (() -> Void)?) async {
182199
guard let accountService = accountService, let account = accountService.account else {
183200
admWallet = nil
184201
return
@@ -197,7 +214,12 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, WalletStaticCoreProt
197214
isRaised = false
198215
}
199216

200-
admWallet?.isBalanceInitialized = !accountService.isBalanceExpired
217+
let isBalanceInitialized = admWallet?.isBalanceInitialized
218+
if isBalanceInitialized == false, !accountService.isBalanceExpired {
219+
admWallet?.isBalanceInitialized = !accountService.isBalanceExpired
220+
}
221+
222+
completion?()
201223

202224
if wallet != nil {
203225
Task { @MainActor in
@@ -206,7 +228,10 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, WalletStaticCoreProt
206228
}
207229

208230
if isRaised {
209-
Task { @MainActor in vibroService.applyVibration(.success) }
231+
Task {
232+
@MainActor in
233+
vibroService.applyVibration(.success)
234+
}
210235
}
211236
}
212237

Adamant/Modules/Wallets/Bitcoin/BtcWalletService.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ final class BtcWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @unc
206206
var hasEnabledNodePublisher: AnyObservable<Bool> {
207207
btcApiService.hasEnabledNodePublisher
208208
}
209+
210+
@MainActor
211+
var hasAllowedNodePublisher: AnyObservable<Bool> {
212+
btcApiService.hasAllowedNodePublisher
213+
}
209214

210215
private(set) lazy var coinStorage: CoinStorageService = AdamantCoinStorageService(
211216
coinId: tokenUniqueID,
@@ -264,28 +269,32 @@ final class BtcWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @unc
264269
.store(in: &subscriptions)
265270
}
266271

267-
func addTransactionObserver() {
272+
private func addTransactionObserver() {
268273
coinStorage.transactionsPublisher
269274
.sink { [weak self] transactions in
270275
self?.transactions = transactions
271276
}
272277
.store(in: &subscriptions)
273278
}
274-
279+
275280
func update() {
276281
Task {
277282
await update()
278283
}
279284
}
280285

281-
func updateWithRefreshUIBalance(){
286+
func resetBalanceAndUpdate() {
282287
Task {
283-
await update(updateWithRefreshUIBalance: true)
288+
if let wallet = btcWallet {
289+
wallet.isBalanceInitialized = false
290+
await walletUpdateSender.send()
291+
await update()
292+
}
284293
}
285294
}
286295

287296
@MainActor
288-
func update(updateWithRefreshUIBalance: Bool = false) async {
297+
func update() async {
289298
guard let wallet = btcWallet else {
290299
return
291300
}
@@ -295,15 +304,8 @@ final class BtcWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @unc
295304
return
296305

297306
case .upToDate:
298-
break
307+
setState(.updating)
299308
}
300-
301-
if updateWithRefreshUIBalance {
302-
wallet.isBalanceInitialized = false
303-
walletUpdateSender.send()
304-
}
305-
306-
setState(.updating)
307309

308310
if let balance = try? await getBalance() {
309311
if wallet.balance < balance, wallet.isBalanceInitialized {

Adamant/Modules/Wallets/Dash/DashWalletService.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ final class DashWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
115115
var hasEnabledNodePublisher: AnyObservable<Bool> {
116116
dashApiService.hasEnabledNodePublisher
117117
}
118+
119+
@MainActor
120+
var hasAllowedNodePublisher: AnyObservable<Bool> {
121+
dashApiService.hasAllowedNodePublisher
122+
}
118123

119124
// MARK: - Notifications
120125
let serviceEnabledChanged = Notification.Name("adamant.dashWallet.enabledChanged")
@@ -226,32 +231,29 @@ final class DashWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
226231
}
227232
}
228233

229-
func updateWithRefreshUIBalance(){
234+
func resetBalanceAndUpdate() {
230235
Task {
231-
await update(updateWithRefreshUIBalance: true)
236+
if let wallet = dashWallet {
237+
wallet.isBalanceInitialized = false
238+
await walletUpdateSender.send()
239+
await update()
240+
}
232241
}
233242
}
234-
243+
235244
@MainActor
236-
func update(updateWithRefreshUIBalance: Bool = false) async {
245+
func update() async {
237246
guard let wallet = dashWallet else {
238247
return
239248
}
240-
249+
241250
switch state {
242251
case .notInitiated, .updating, .initiationFailed:
243252
return
244253

245254
case .upToDate:
246-
break
247-
}
248-
249-
if updateWithRefreshUIBalance{
250-
wallet.isBalanceInitialized = false
251-
walletUpdateSender.send()
255+
setState(.updating)
252256
}
253-
254-
setState(.updating)
255257

256258
if let balance = try? await getBalance() {
257259
if wallet.balance < balance, wallet.isBalanceInitialized {

Adamant/Modules/Wallets/Doge/DogeWalletService.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ final class DogeWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
174174
var hasEnabledNodePublisher: AnyObservable<Bool> {
175175
dogeApiService.hasEnabledNodePublisher
176176
}
177+
178+
@MainActor
179+
var hasAllowedNodePublisher: AnyObservable<Bool> {
180+
dogeApiService.hasAllowedNodePublisher
181+
}
177182

178183
private(set) lazy var coinStorage: CoinStorageService = AdamantCoinStorageService(
179184
coinId: tokenUniqueID,
@@ -245,33 +250,30 @@ final class DogeWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
245250
await update()
246251
}
247252
}
248-
249-
func updateWithRefreshUIBalance(){
253+
254+
func resetBalanceAndUpdate() {
250255
Task {
251-
await update(updateWithRefreshUIBalance: true)
256+
if let wallet = dogeWallet {
257+
wallet.isBalanceInitialized = false
258+
await walletUpdateSender.send()
259+
await update()
260+
}
252261
}
253262
}
254263

255264
@MainActor
256-
func update(updateWithRefreshUIBalance: Bool = false) async {
265+
func update() async {
257266
guard let wallet = dogeWallet else {
258267
return
259268
}
260-
269+
261270
switch state {
262271
case .notInitiated, .updating, .initiationFailed:
263272
return
264273

265274
case .upToDate:
266-
break
267-
}
268-
269-
if updateWithRefreshUIBalance {
270-
wallet.isBalanceInitialized = false
271-
walletUpdateSender.send()
275+
setState(.updating)
272276
}
273-
274-
setState(.updating)
275277

276278
if let balance = try? await getBalance() {
277279
if wallet.balance < balance, wallet.isBalanceInitialized {

Adamant/Modules/Wallets/ERC20/ERC20WalletService.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ final class ERC20WalletService: WalletCoreProtocol, ERC20GasAlgorithmComputable,
204204
var hasEnabledNodePublisher: AnyObservable<Bool> {
205205
erc20ApiService.hasEnabledNodePublisher
206206
}
207+
208+
@MainActor
209+
var hasAllowedNodePublisher: AnyObservable<Bool> {
210+
erc20ApiService.hasAllowedNodePublisher
211+
}
207212

208213
private(set) lazy var coinStorage: CoinStorageService = AdamantCoinStorageService(
209214
coinId: tokenUniqueID,
@@ -260,33 +265,30 @@ final class ERC20WalletService: WalletCoreProtocol, ERC20GasAlgorithmComputable,
260265
await update()
261266
}
262267
}
263-
264-
func updateWithRefreshUIBalance() {
268+
269+
func resetBalanceAndUpdate() {
265270
Task {
266-
await update(updateWithRefreshUIBalance: true)
271+
if let wallet = ethWallet {
272+
wallet.isBalanceInitialized = false
273+
await walletUpdateSender.send()
274+
await update()
275+
}
267276
}
268277
}
269278

270279
@MainActor
271-
func update(updateWithRefreshUIBalance: Bool = false) async {
280+
func update() async {
272281
guard let wallet = ethWallet else {
273282
return
274-
}
275-
283+
}
284+
276285
switch state {
277286
case .notInitiated, .updating, .initiationFailed:
278287
return
279288

280289
case .upToDate:
281-
break
282-
}
283-
284-
if updateWithRefreshUIBalance {
285-
wallet.isBalanceInitialized = false
286-
walletUpdateSender.send()
290+
setState(.updating)
287291
}
288-
289-
setState(.updating)
290292

291293
if let balance = try? await getBalance(forAddress: wallet.ethAddress) {
292294
if wallet.balance < balance, wallet.isBalanceInitialized {

Adamant/Modules/Wallets/Ethereum/EthWalletService.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ final class EthWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, ERC2
205205
var hasEnabledNodePublisher: AnyObservable<Bool> {
206206
ethApiService.hasEnabledNodePublisher
207207
}
208+
209+
@MainActor
210+
var hasAllowedNodePublisher: AnyObservable<Bool> {
211+
ethApiService.hasAllowedNodePublisher
212+
}
208213

209214
private(set) lazy var coinStorage: CoinStorageService = AdamantCoinStorageService(
210215
coinId: tokenUniqueID,
@@ -291,33 +296,30 @@ final class EthWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, ERC2
291296
await update()
292297
}
293298
}
294-
295-
func updateWithRefreshUIBalance(){
299+
300+
func resetBalanceAndUpdate() {
296301
Task {
297-
await update(updateWithRefreshUIBalance: true)
302+
if let wallet = ethWallet {
303+
wallet.isBalanceInitialized = false
304+
await walletUpdateSender.send()
305+
await update()
306+
}
298307
}
299308
}
300309

301310
@MainActor
302-
func update(updateWithRefreshUIBalance: Bool = false) async {
311+
func update() async {
303312
guard let wallet = await getWallet() else {
304313
return
305314
}
306-
315+
307316
switch state {
308317
case .notInitiated, .updating, .initiationFailed:
309318
return
310319

311320
case .upToDate:
312-
break
313-
}
314-
315-
if updateWithRefreshUIBalance {
316-
wallet.isBalanceInitialized = false
317-
walletUpdateSender.send()
321+
setState(.updating)
318322
}
319-
320-
setState(.updating)
321323

322324
if let balance = try? await getBalance(forAddress: wallet.ethAddress) {
323325
if wallet.balance < balance, wallet.isBalanceInitialized {

0 commit comments

Comments
 (0)