@@ -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,38 +47,77 @@ 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+ @discardableResult
53+ func authenticate( credentials: Credentials ) -> StoresManager {
5354 state = AuthenticatedState ( credentials: credentials)
54- sessionManager. credentials = credentials
55+ sessionManager. defaultCredentials = credentials
56+
57+ return self
5558 }
5659
60+ /// Synchronizes all of the Session's Entities.
61+ ///
62+ @discardableResult
63+ func synchronizeEntities( onCompletion: ( ( Error ? ) -> Void ) ? = nil ) -> StoresManager {
64+ synchronizeAccount ( onCompletion: onCompletion)
65+
66+ return self
67+ }
5768
5869 /// Switches the state to a Deauthenticated one.
5970 ///
60- func deauthenticate( ) {
71+ @discardableResult
72+ func deauthenticate( ) -> StoresManager {
6173 state = DeauthenticatedState ( )
6274 sessionManager. reset ( )
75+
76+ return self
6377 }
6478}
6579
6680
67- // MARK: - StoresManager Private Methods
81+ // MARK: - Private Methods
6882//
6983private extension StoresManager {
7084
71- /// Switches over to the AuthenticatedState whenever needed / possible! .
85+ /// Loads the Default Account into the current Session, if possible.
7286 ///
73- func authenticateIfPossible ( ) {
74- guard let credentials = sessionManager. credentials else {
87+ func restoreSessionAccountIfPossible ( ) {
88+ guard let accountID = sessionManager. defaultAccountID else {
7589 return
7690 }
7791
78- state = AuthenticatedState ( credentials: credentials)
92+ restoreSessionAccount ( with: accountID)
93+ }
94+
95+ /// Loads the specified accountID into the Session, if possible.
96+ ///
97+ func restoreSessionAccount( with accountID: Int ) {
98+ let action = AccountAction . loadAccount ( userID: accountID) { [ weak self] account in
99+ guard let `self` = self , let account = account else {
100+ return
101+ }
102+
103+ self . sessionManager. defaultAccount = account
104+ }
105+
106+ dispatch ( action)
107+ }
108+
109+ /// Synchronizes the WordPress.com Account, associated with the current credentials.
110+ ///
111+ func synchronizeAccount( onCompletion: ( ( Error ? ) -> Void ) ? ) {
112+ let action = AccountAction . synchronizeAccount { [ weak self] ( account, error) in
113+ if let `self` = self , let account = account, self . isAuthenticated {
114+ self . sessionManager. defaultAccount = account
115+ }
116+
117+ onCompletion ? ( error)
118+ }
119+
120+ dispatch ( action)
79121 }
80122}
81123
0 commit comments