Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 15 additions & 25 deletions StatefulViewController/ViewStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ViewStateMachine {
}()

/// The view that should act as the superview for any added views
public let view: UIView
public weak var view: UIView?

/// The current display state of views
public fileprivate(set) var currentState: ViewStateMachineState = .none
Expand Down Expand Up @@ -130,31 +130,18 @@ public class ViewStateMachine {
public func transitionToState(_ state: ViewStateMachineState, animated: Bool = true, completion: (() -> ())? = nil) {
lastState = state

queue.async { [weak self] in
guard let strongSelf = self else { return }
if state == currentState {
return
}

if state == strongSelf.currentState {
return
}

// Suspend the queue, it will be resumed in the completion block
strongSelf.queue.suspend()
strongSelf.currentState = state

let c: () -> () = {
strongSelf.queue.resume()
completion?()
}

// Switch state and update the view
DispatchQueue.main.sync {
switch state {
case .none:
strongSelf.hideAllViews(animated: animated, completion: c)
case .view(let viewKey):
strongSelf.showView(forKey: viewKey, animated: animated, completion: c)
}
}
// Suspend the queue, it will be resumed in the completion block
currentState = state

switch state {
case .none:
hideAllViews(animated: animated, completion: completion)
case .view(let viewKey):
showView(forKey: viewKey, animated: animated, completion: completion)
}
}

Expand All @@ -163,6 +150,9 @@ public class ViewStateMachine {

fileprivate func showView(forKey state: String, animated: Bool, completion: (() -> ())? = nil) {
// Add the container view
guard let view = view else {
return
}
containerView.frame = view.bounds
view.addSubview(containerView)

Expand Down