Skip to content

Commit 0e3bc69

Browse files
authored
Merge pull request #759 from Adamant-im/trello.com/c/YsjIF3DY
[trello.com/c/YsjIF3DY] (Secret wallets) Handle logout/login case
2 parents 7ce5fe3 + 7f2db6f commit 0e3bc69

File tree

6 files changed

+39
-15
lines changed

6 files changed

+39
-15
lines changed

Adamant/Modules/Account/AccountViewController/AccountViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ final class AccountViewController: FormViewController {
178178
accountHeaderView.delegate = self
179179

180180
secretWalletsViewModel.$state
181-
.map { $0.currentActiveIndex }
182181
.removeDuplicates()
182+
.map { $0.currentActiveIndex }
183183
.receive(on: DispatchQueue.main)
184184
.sink { [weak self] index in
185185
guard let self = self else { return }

Adamant/Modules/SecretWallets/SecretWalletsState.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ struct SecretWalletsState: Equatable {
1717
return wallets[currentActiveIndex]
1818
}
1919

20-
static let `default` = Self(wallets: [], currentActiveIndex: -1)
20+
static let `default` = Self(
21+
wallets: [WalletItem(
22+
name: String.localized(
23+
"SecretWallets.Menu.Regular",
24+
comment: "Secret wallet menu: regular wallet"
25+
)
26+
)],
27+
currentActiveIndex: 0
28+
)
2129
}
2230

2331
struct WalletItem: Equatable, Identifiable {

Adamant/Modules/SecretWallets/SecretWalletsViewModel.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Copyright © 2025 Adamant. All rights reserved.
77
//
88

9+
import Foundation
910
import CommonKit
1011
import Combine
1112

@@ -22,11 +23,6 @@ final class SecretWalletsViewModel: ObservableObject {
2223
setup()
2324
}
2425

25-
private func setup() {
26-
self.state.currentActiveIndex = 0
27-
self.state.wallets.append(WalletItem(name: String.localized("SecretWallets.Menu.Regular", comment: "Secret wallet menu: regular wallet")))
28-
}
29-
3026
func pickWallet(at index: Int) {
3127
if index == 0 {
3228
secretWalletsManager.activateDefaultWallet()
@@ -46,3 +42,18 @@ final class SecretWalletsViewModel: ObservableObject {
4642
self.state.currentActiveIndex = index
4743
}
4844
}
45+
46+
private extension SecretWalletsViewModel {
47+
func setup() {
48+
NotificationCenter.default.notifications(named: .AdamantAccountService.userLoggedOut)
49+
.sink { [weak self] _ in await self?.removeAllSecretWallets() }
50+
.store(in: &subscriptions)
51+
}
52+
53+
func removeAllSecretWallets() {
54+
secretWalletsManager.removeAllSecretWallets()
55+
secretWalletsManager.activateDefaultWallet()
56+
state.currentActiveIndex = 0
57+
state.wallets.removeLast(state.wallets.count - 1)
58+
}
59+
}

Adamant/ServiceProtocols/SecretWalletsManagerProtocol.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ protocol SecretWalletsManagerProtocol {
1717
func getSecretWallets() -> [WalletStoreServiceProtocol]
1818
func activateSecretWallet(at index: Int)
1919
func activateDefaultWallet()
20+
func removeAllSecretWallets()
2021
}
2122

2223
protocol SecretWalletsManagerStateProtocol {

Adamant/Services/AdamantAccountService.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,14 @@ extension AdamantAccountService {
313313

314314
_ = await initWallets()
315315

316+
let userInfo = [AdamantUserInfoKey.AccountService.loggedAccountAddress: account.address]
317+
318+
NotificationCenter.default.post(
319+
name: Notification.Name.AdamantAccountService.userLoggedIn,
320+
object: self,
321+
userInfo: userInfo
322+
)
323+
316324
return .success(account: account, alert: nil)
317325
}
318326

@@ -384,14 +392,6 @@ extension AdamantAccountService {
384392
self.keypair = keypair
385393
markBalanceAsFresh()
386394

387-
let userInfo = [AdamantUserInfoKey.AccountService.loggedAccountAddress: account.address]
388-
389-
NotificationCenter.default.post(
390-
name: Notification.Name.AdamantAccountService.userLoggedIn,
391-
object: self,
392-
userInfo: userInfo
393-
)
394-
395395
self.state = .loggedIn
396396
return account
397397
} catch let error as ApiServiceError {

Adamant/Services/SecretWalletsManager.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,8 @@ final class AdamantSecretWalletsManager: SecretWalletsManagerProtocol {
6666
state.currentWallet = state.regularWallet
6767
statePublisher.send(state)
6868
}
69+
70+
func removeAllSecretWallets() {
71+
state.secretWallets.removeAll()
72+
}
6973
}

0 commit comments

Comments
 (0)