Skip to content

Commit 2f83390

Browse files
committed
StoresManager: Restoring Session's Account
1 parent b1d0bdb commit 2f83390

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

WooCommerce/Classes/Yosemite/DeauthenticatedState.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class DeauthenticatedState: StoresManagerState {
1313
AppDelegate.shared.displayAuthenticator()
1414
}
1515

16-
1716
/// NO-OP: During deauth method, we're not running any actions.
1817
///
1918
func onAction(_ action: Action) { }

WooCommerce/Classes/Yosemite/StoresManager.swift

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class StoresManager {
1717

1818
/// Active StoresManager State.
1919
///
20-
private var state: StoresManagerState = DeauthenticatedState() {
20+
private var state: StoresManagerState {
2121
didSet {
2222
state.didEnter()
2323
}
@@ -30,11 +30,14 @@ class StoresManager {
3030
}
3131

3232

33+
3334
/// Designated Initializer
3435
///
3536
init(sessionManager: SessionManager) {
3637
self.sessionManager = sessionManager
37-
authenticateIfPossible()
38+
self.state = AuthenticatedState(sessionManager: sessionManager) ?? DeauthenticatedState()
39+
40+
restoreSessionAccountIfPossible()
3841
}
3942

4043

@@ -44,16 +47,13 @@ class StoresManager {
4447
state.onAction(action)
4548
}
4649

47-
4850
/// Switches the internal state to Authenticated.
4951
///
50-
func authenticate(username: String, authToken: String) {
51-
let credentials = Credentials(username: username, authToken: authToken)
52-
52+
func authenticate(credentials: Credentials, onCompletion: ((Error?) -> Void)? = nil) {
5353
state = AuthenticatedState(credentials: credentials)
54-
sessionManager.credentials = credentials
55-
}
54+
sessionManager.defaultCredentials = credentials
5655

56+
}
5757

5858
/// Switches the state to a Deauthenticated one.
5959
///
@@ -64,18 +64,33 @@ class StoresManager {
6464
}
6565

6666

67-
// MARK: - StoresManager Private Methods
67+
// MARK: - Private Methods
6868
//
6969
private extension StoresManager {
7070

71-
/// Switches over to the AuthenticatedState whenever needed / possible!.
71+
/// Loads the Default Account into the current Session, if possible.
7272
///
73-
func authenticateIfPossible() {
74-
guard let credentials = sessionManager.credentials else {
73+
func restoreSessionAccountIfPossible() {
74+
guard let accountID = sessionManager.defaultAccountID else {
7575
return
7676
}
7777

78-
state = AuthenticatedState(credentials: credentials)
78+
restoreSessionAccount(with: accountID)
79+
}
80+
81+
/// Loads the specified accountID into the Session, if possible.
82+
///
83+
func restoreSessionAccount(with accountID: Int) {
84+
let action = AccountAction.loadAccount(userID: accountID) { [weak self] account in
85+
guard let `self` = self, let account = account else {
86+
return
87+
}
88+
89+
self.sessionManager.defaultAccount = account
90+
}
91+
92+
dispatch(action)
93+
}
7994
}
8095
}
8196

0 commit comments

Comments
 (0)