@@ -43,11 +43,17 @@ public class StatefulViewController: UIViewController {
4343 /// All states other than `Content` imply that there is a placeholder view shown.
4444 public var currentState : StatefulViewControllerState {
4545 switch stateMachine. currentState {
46- case . None: return . Content
47- case . View( let viewKey) : return StatefulViewControllerState ( rawValue: viewKey) !
46+ case . None: return . Content
47+ case . View( let viewKey) : return StatefulViewControllerState ( rawValue: viewKey) !
4848 }
4949 }
5050
51+ public var lastState : StatefulViewControllerState {
52+ switch stateMachine. currentState {
53+ case . None: return . Content
54+ case . View( let viewKey) : return StatefulViewControllerState ( rawValue: viewKey) !
55+ }
56+ }
5157
5258 // MARK: Views
5359
@@ -71,8 +77,11 @@ public class StatefulViewController: UIViewController {
7177
7278 override public func viewWillAppear( animated: Bool ) {
7379 super. viewWillAppear ( animated)
74-
75- transitionViewStates ( animated: false )
80+
81+ // Make sure to stay in the correct state when transitioning
82+ let isLoading = ( currentState == . Loading)
83+ let error : NSError ? = ( currentState == . Error) ? NSError ( ) : nil
84+ transitionViewStates ( loading: isLoading, error: error, animated: false )
7685 }
7786
7887
@@ -83,8 +92,8 @@ public class StatefulViewController: UIViewController {
8392 ///
8493 /// :param: animated true if the switch to the placeholder view should be animated, false otherwise
8594 ///
86- public func startLoading( animated: Bool = false ) {
87- transitionViewStates ( loading: true , animated: animated)
95+ public func startLoading( animated: Bool = false , completion : ( ( ) -> ( ) ) ? = nil ) {
96+ transitionViewStates ( loading: true , animated: animated, completion : completion )
8897 }
8998
9099 /// Ends the controller's loading state.
@@ -94,8 +103,8 @@ public class StatefulViewController: UIViewController {
94103 /// :param: animated true if the switch to the placeholder view should be animated, false otherwise
95104 /// :param: error An error that might have occured whilst loading
96105 ///
97- public func endLoading( animated: Bool = true , error: NSError ? = nil ) {
98- transitionViewStates ( loading: false , animated: animated, error: error)
106+ public func endLoading( animated: Bool = true , error: NSError ? = nil , completion : ( ( ) -> ( ) ) ? = nil ) {
107+ transitionViewStates ( loading: false , animated: animated, error: error, completion : completion )
99108 }
100109
101110
@@ -108,29 +117,27 @@ public class StatefulViewController: UIViewController {
108117 /// :param: error An error that might have occured whilst loading
109118 /// :param: animated true if the switch to the placeholder view should be animated, false otherwise
110119 ///
111- public func transitionViewStates( loading: Bool = false , error: NSError ? = nil , animated: Bool = true ) {
112- dispatch_async ( dispatch_get_main_queue ( ) ) {
113- let hasContent = ( self as? StatefulViewControllerDelegate ) ? . hasContent ( ) ?? true
114-
115- // Update view for content (i.e. hide all placeholder views)
116- if hasContent {
117- if let e = error {
118- // show unobstrusive error
119- ( self as? StatefulViewControllerDelegate ) ? . handleErrorWhenContentAvailable ? ( e)
120- }
121- self . stateMachine. transitionToState ( . None, animated: animated)
122- return
123- }
124-
125- // Update view for placeholder
126- var newState : StatefulViewControllerState = . Empty
127- if loading {
128- newState = . Loading
129- } else if let e = error {
130- newState = . Error
131- }
132- self . stateMachine. transitionToState ( . View( newState. rawValue) , animated: animated)
133- }
120+ public func transitionViewStates( loading: Bool = false , error: NSError ? = nil , animated: Bool = true , completion: ( ( ) -> ( ) ) ? = nil ) {
121+ let hasContent = ( self as? StatefulViewControllerDelegate ) ? . hasContent ( ) ?? true
122+
123+ // Update view for content (i.e. hide all placeholder views)
124+ if hasContent {
125+ if let e = error {
126+ // show unobstrusive error
127+ ( self as? StatefulViewControllerDelegate ) ? . handleErrorWhenContentAvailable ? ( e)
128+ }
129+ self . stateMachine. transitionToState ( . None, animated: animated, completion: completion)
130+ return
131+ }
132+
133+ // Update view for placeholder
134+ var newState : StatefulViewControllerState = . Empty
135+ if loading {
136+ newState = . Loading
137+ } else if let e = error {
138+ newState = . Error
139+ }
140+ self . stateMachine. transitionToState ( . View( newState. rawValue) , animated: animated, completion: completion)
134141 }
135142
136143
0 commit comments