Skip to content

Commit c1b8065

Browse files
authored
Merge pull request #834 from Adamant-im/trello.com/c/dEf1yVQr
[trello.com/c/dEf1yVQr] Allow sending the first message faster
2 parents 5c80ff8 + 8f9669a commit c1b8065

27 files changed

+369
-358
lines changed

Adamant.xcodeproj/project.pbxproj

-8
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,6 @@
204204
);
205205
target = E96D64DA2295CD4700CA5587 /* NotificationServiceExtension */;
206206
};
207-
D3A8618F2DA1C1AD0007B599 /* Exceptions for "NotificationServiceExtension" folder in "TransferNotificationContentExtension" target */ = {
208-
isa = PBXFileSystemSynchronizedBuildFileExceptionSet;
209-
membershipExceptions = (
210-
WalletImages/bitcoin_notificationContent.png,
211-
);
212-
target = E957E12C229B10F80019732A /* TransferNotificationContentExtension */;
213-
};
214207
D3A861992DA1C1B10007B599 /* Exceptions for "TransferNotificationContentExtension" folder in "TransferNotificationContentExtension" target */ = {
215208
isa = PBXFileSystemSynchronizedBuildFileExceptionSet;
216209
membershipExceptions = (
@@ -258,7 +251,6 @@
258251
isa = PBXFileSystemSynchronizedRootGroup;
259252
exceptions = (
260253
D3A8618E2DA1C1AD0007B599 /* Exceptions for "NotificationServiceExtension" folder in "NotificationServiceExtension" target */,
261-
D3A8618F2DA1C1AD0007B599 /* Exceptions for "NotificationServiceExtension" folder in "TransferNotificationContentExtension" target */,
262254
);
263255
path = NotificationServiceExtension;
264256
sourceTree = "<group>";

Adamant/Modules/Account/AccountViewController/AccountViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ final class AccountViewController: FormViewController {
981981
}
982982

983983
Task { @MainActor in
984-
accountService.updateWithRefreshUI()
984+
accountService.update(shouldUpdateUIBalance: true, updateOnlyADM: false, updateOnlyVisible: true)
985985
}
986986
refreshControl.endRefreshing()
987987
}

Adamant/Modules/Settings/VisibleWallets/VisibleWalletsViewController.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ final class VisibleWalletsViewController: KeyboardObservingViewController {
103103

104104
private func addObservers() {
105105
for (index, wallet) in wallets.enumerated() {
106-
NotificationCenter.default.publisher(for: wallet.walletUpdatedNotification, object: wallet)
107-
.removeDuplicates()
106+
wallet.walletUpdatePublisher
108107
.receive(on: DispatchQueue.main)
109108
.debounce(for: .milliseconds(500), scheduler: DispatchQueue.main)
110109
.sink { [weak self] _ in
@@ -146,7 +145,7 @@ final class VisibleWalletsViewController: KeyboardObservingViewController {
146145
}
147146

148147
@objc private func updateBalances() {
149-
NotificationCenter.default.post(name: .AdamantAccountService.forceUpdateAllBalances, object: nil)
148+
accountService.update(shouldUpdateUIBalance: true, updateOnlyADM: false, updateOnlyVisible: false)
150149
refreshControl.endRefreshing()
151150
}
152151

Adamant/Modules/Wallets/Adamant/AdmWalletService.swift

+28-16
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, WalletStaticCoreProt
5555
var qqPrefix: String {
5656
return Self.qqPrefix
5757
}
58+
59+
var balanceCheckInterval: Int? {
60+
Self.balanceCheckInterval
61+
}
62+
63+
var balanceValidInterval: Int? {
64+
Self.balanceValidInterval
65+
}
66+
67+
var balanceCheckIntervalNewAccount: Int? {
68+
Self.coinInfo?.balanceCheckIntervalNewAccount
69+
}
5870

5971
var nodeGroups: [NodeGroup] {
6072
[.adm]
@@ -72,7 +84,7 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, WalletStaticCoreProt
7284
var vibroService: VibroService!
7385

7486
// MARK: - Notifications
75-
let walletUpdatedNotification = Notification.Name("adamant.admWallet.updated")
87+
let adamantAccount = Notification.Name("adamant.admWallet.walletUpdated")
7688
let serviceEnabledChanged = Notification.Name("adamant.admWallet.enabledChanged")
7789
let transactionFeeUpdated = Notification.Name("adamant.admWallet.feeUpdated")
7890
let serviceStateChanged = Notification.Name("adamant.admWallet.stateChanged")
@@ -130,7 +142,7 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, WalletStaticCoreProt
130142
// MARK: - Logic
131143
override init() {
132144
super.init()
133-
145+
134146
// Notifications
135147
addObservers()
136148
}
@@ -149,6 +161,13 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, WalletStaticCoreProt
149161
self?.admWallet = nil
150162
}
151163
.store(in: &subscriptions)
164+
165+
NotificationCenter.default
166+
.notifications(named: .AdamantAccountService.walletUpdated, object: nil)
167+
.sink { [weak self] _ in
168+
self?.update()
169+
}
170+
.store(in: &subscriptions)
152171
}
153172

154173
func updateWithRefreshUIBalance(){
@@ -180,8 +199,10 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, WalletStaticCoreProt
180199

181200
admWallet?.isBalanceInitialized = !accountService.isBalanceExpired
182201

183-
if let wallet = wallet {
184-
postUpdateNotification(with: wallet)
202+
if wallet != nil {
203+
Task { @MainActor in
204+
walletUpdateSender.send()
205+
}
185206
}
186207

187208
if isRaised {
@@ -199,17 +220,6 @@ final class AdmWalletService: NSObject, WalletCoreProtocol, WalletStaticCoreProt
199220
addressRegex.perfectMatch(with: address) ? .valid : .invalid(description: nil)
200221
}
201222

202-
private func postUpdateNotification(with wallet: WalletAccount) {
203-
NotificationCenter.default.post(
204-
name: walletUpdatedNotification,
205-
object: self,
206-
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
207-
)
208-
Task { @MainActor in
209-
walletUpdateSender.send()
210-
}
211-
}
212-
213223
func getWalletAddress(byAdamantAddress address: String) async throws -> String {
214224
return address
215225
}
@@ -248,7 +258,9 @@ extension AdmWalletService: NSFetchedResultsControllerDelegate {
248258

249259
if newCount != wallet.notifications {
250260
wallet.notifications = newCount
251-
postUpdateNotification(with: wallet)
261+
Task { @MainActor in
262+
walletUpdateSender.send()
263+
}
252264
}
253265
}
254266
}

Adamant/Modules/Wallets/Adamant/AdmWalletViewController.swift

+12-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import CommonKit
1010
import Eureka
1111
import SafariServices
1212
import UIKit
13+
import Combine
1314

1415
extension String.adamant.wallets {
1516
static var adamant: String {
@@ -87,7 +88,9 @@ final class AdmWalletViewController: WalletViewControllerBase {
8788

8889
// MARK: - Props & Deps
8990
var hideFreeTokensRow = false
90-
91+
92+
var cancellables: Set<AnyCancellable> = []
93+
9194
// MARK: - Lifecycle
9295

9396
override func viewDidLoad() {
@@ -198,12 +201,14 @@ final class AdmWalletViewController: WalletViewControllerBase {
198201

199202
// Notifications
200203
if let service = service {
201-
NotificationCenter.default.addObserver(
202-
forName: service.core.walletUpdatedNotification,
203-
object: service.core,
204-
queue: OperationQueue.main,
205-
using: { [weak self] _ in MainActor.assumeIsolatedSafe { self?.updateRows() } }
206-
)
204+
service.core.walletUpdatePublisher
205+
.receive(on: DispatchQueue.main)
206+
.sink { _ in
207+
MainActor.assumeIsolatedSafe {
208+
self.updateRows()
209+
}
210+
}
211+
.store(in: &cancellables)
207212
}
208213

209214
NotificationCenter.default.addObserver(

Adamant/Modules/Wallets/Bitcoin/BtcWalletService.swift

+10-21
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ final class BtcWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @unc
101101
var qqPrefix: String {
102102
return Self.qqPrefix
103103
}
104+
105+
var balanceCheckInterval: Int? {
106+
Self.balanceCheckInterval
107+
}
108+
109+
var balanceValidInterval: Int? {
110+
Self.balanceValidInterval
111+
}
104112

105113
var isSupportIncreaseFee: Bool {
106114
return true
@@ -149,7 +157,6 @@ final class BtcWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @unc
149157
private let walletPath = "m/44'/0'/21'/0/0"
150158

151159
// MARK: - Notifications
152-
let walletUpdatedNotification = Notification.Name("adamant.btcWallet.walletUpdated")
153160
let serviceEnabledChanged = Notification.Name("adamant.btcWallet.enabledChanged")
154161
let serviceStateChanged = Notification.Name("adamant.btcWallet.stateChanged")
155162
let transactionFeeUpdated = Notification.Name("adamant.btcWallet.feeUpdated")
@@ -309,12 +316,6 @@ final class BtcWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @unc
309316

310317
walletUpdateSender.send()
311318

312-
NotificationCenter.default.post(
313-
name: walletUpdatedNotification,
314-
object: self,
315-
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
316-
)
317-
318319
setState(.upToDate)
319320

320321
if let rate = try? await getFeeRate() {
@@ -386,16 +387,10 @@ final class BtcWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @unc
386387
wallet.isBalanceInitialized = true
387388

388389
balanceInvalidationSubscription = Task { [weak self] in
389-
try await Task.sleep(interval: Self.balanceLifetime, pauseInBackground: true)
390-
guard let self else { return }
390+
guard let self = self else { return }
391+
try await Task.sleep(interval: Double(self.balanceValidInterval ?? Self.balanceCheckIntervalDefault) / 1000, pauseInBackground: true)
391392
wallet.isBalanceInitialized = false
392393

393-
NotificationCenter.default.post(
394-
name: walletUpdatedNotification,
395-
object: self,
396-
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
397-
)
398-
399394
await walletUpdateSender.send()
400395
}.eraseToAnyCancellable()
401396
}
@@ -477,12 +472,6 @@ extension BtcWalletService {
477472
self.btcWallet = eWallet
478473
let kvsAddressModel = makeKVSAddressModel(wallet: eWallet)
479474

480-
NotificationCenter.default.post(
481-
name: walletUpdatedNotification,
482-
object: self,
483-
userInfo: [AdamantUserInfoKey.WalletService.wallet: eWallet]
484-
)
485-
486475
await walletUpdateSender.send()
487476

488477
if !self.enabled {

Adamant/Modules/Wallets/Dash/DashWalletService.swift

+10-21
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ final class DashWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
4949
var qqPrefix: String {
5050
return Self.qqPrefix
5151
}
52+
53+
var balanceCheckInterval: Int? {
54+
Self.balanceCheckInterval
55+
}
56+
57+
var balanceValidInterval: Int? {
58+
Self.balanceValidInterval
59+
}
5260

5361
var nodeGroups: [NodeGroup] {
5462
[.dash]
@@ -109,7 +117,6 @@ final class DashWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
109117
}
110118

111119
// MARK: - Notifications
112-
let walletUpdatedNotification = Notification.Name("adamant.dashWallet.walletUpdated")
113120
let serviceEnabledChanged = Notification.Name("adamant.dashWallet.enabledChanged")
114121
let serviceStateChanged = Notification.Name("adamant.dashWallet.stateChanged")
115122
let transactionFeeUpdated = Notification.Name("adamant.dashWallet.feeUpdated")
@@ -257,12 +264,6 @@ final class DashWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
257264

258265
walletUpdateSender.send()
259266

260-
NotificationCenter.default.post(
261-
name: walletUpdatedNotification,
262-
object: self,
263-
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
264-
)
265-
266267
setState(.upToDate)
267268
}
268269

@@ -281,16 +282,10 @@ final class DashWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
281282
wallet.isBalanceInitialized = true
282283

283284
balanceInvalidationSubscription = Task { [weak self] in
284-
try await Task.sleep(interval: Self.balanceLifetime, pauseInBackground: true)
285-
guard let self else { return }
285+
guard let self = self else { return }
286+
try await Task.sleep(interval: Double(self.balanceValidInterval ?? Self.balanceCheckIntervalDefault) / 1000, pauseInBackground: true)
286287
wallet.isBalanceInitialized = false
287288

288-
NotificationCenter.default.post(
289-
name: walletUpdatedNotification,
290-
object: self,
291-
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
292-
)
293-
294289
await self.walletUpdateSender.send()
295290
}.eraseToAnyCancellable()
296291
}
@@ -331,12 +326,6 @@ extension DashWalletService {
331326
self.dashWallet = eWallet
332327
let kvsAddressModel = makeKVSAddressModel(wallet: eWallet)
333328

334-
NotificationCenter.default.post(
335-
name: walletUpdatedNotification,
336-
object: self,
337-
userInfo: [AdamantUserInfoKey.WalletService.wallet: eWallet]
338-
)
339-
340329
self.walletUpdateSender.send()
341330

342331
if !self.enabled {

Adamant/Modules/Wallets/Doge/DogeWalletService.swift

+10-21
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ final class DogeWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
106106
var qqPrefix: String {
107107
return Self.qqPrefix
108108
}
109+
110+
var balanceCheckInterval: Int? {
111+
Self.balanceCheckInterval
112+
}
113+
114+
var balanceValidInterval: Int? {
115+
Self.balanceValidInterval
116+
}
109117

110118
var nodeGroups: [NodeGroup] {
111119
[.doge]
@@ -116,7 +124,6 @@ final class DogeWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
116124
@Atomic private(set) var isWarningGasPrice = false
117125

118126
// MARK: - Notifications
119-
let walletUpdatedNotification = Notification.Name("adamant.dogeWallet.walletUpdated")
120127
let serviceEnabledChanged = Notification.Name("adamant.dogeWallet.enabledChanged")
121128
let serviceStateChanged = Notification.Name("adamant.dogeWallet.stateChanged")
122129
let transactionFeeUpdated = Notification.Name("adamant.dogeWallet.feeUpdated")
@@ -276,12 +283,6 @@ final class DogeWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
276283
}
277284

278285
walletUpdateSender.send()
279-
280-
NotificationCenter.default.post(
281-
name: walletUpdatedNotification,
282-
object: self,
283-
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
284-
)
285286

286287
setState(.upToDate)
287288
}
@@ -301,16 +302,10 @@ final class DogeWalletService: WalletCoreProtocol, WalletStaticCoreProtocol, @un
301302
wallet.isBalanceInitialized = true
302303

303304
balanceInvalidationSubscription = Task { [weak self] in
304-
try await Task.sleep(interval: Self.balanceLifetime, pauseInBackground: true)
305-
guard let self else { return }
305+
guard let self = self else { return }
306+
try await Task.sleep(interval: Double(self.balanceValidInterval ?? Self.balanceCheckIntervalDefault) / 1000, pauseInBackground: true)
306307
wallet.isBalanceInitialized = false
307308

308-
NotificationCenter.default.post(
309-
name: walletUpdatedNotification,
310-
object: self,
311-
userInfo: [AdamantUserInfoKey.WalletService.wallet: wallet]
312-
)
313-
314309
await self.walletUpdateSender.send()
315310
}.eraseToAnyCancellable()
316311
}
@@ -349,12 +344,6 @@ extension DogeWalletService {
349344
self.dogeWallet = eWallet
350345
let kvsAddressModel = makeKVSAddressModel(wallet: eWallet)
351346

352-
NotificationCenter.default.post(
353-
name: walletUpdatedNotification,
354-
object: self,
355-
userInfo: [AdamantUserInfoKey.WalletService.wallet: eWallet]
356-
)
357-
358347
await walletUpdateSender.send()
359348

360349
if !self.enabled {

0 commit comments

Comments
 (0)