Skip to content

Commit

Permalink
Drop support for Swift 3, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
davidask committed Mar 26, 2019
1 parent a74923b commit 8bc6adc
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 11 deletions.
Binary file removed Images/example_gif.gif
Binary file not shown.
Binary file added Images/lifecycle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/state_transition.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@

When creating rich stateful view controllers, a single view controller class is often tasked with managing the appearance of many other views, controls, and other user interface elements based on a state. That state, in turn, is often inferred from multiple properties that need to be synchronized to correctly represent a single state. Usually the end result is known as the *Massive View Controller* problem, often solved by deviating from the [MVC](https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html) pattern used and endorsed heavily by Apple. While other patterns, such as [MVVM](https://en.wikipedia.org/wiki/Model–view–viewmodel) or [MVP](https://en.wikipedia.org/wiki/Model–view–presenter), can solve your issues, going with the grain rather than against makes interacting with UIKit a whole lot easier. *This repository houses one dependency-free class, called `StateViewController`, which is tasked with solving this issue.*


## Overview
`StateViewController` is a container view controller that presents one or more view controllers for any given state that you define, such as `loading`, `list`, or `editing`. It manages the appearance cycles of each content view controller, making sure that the view lifecycle of the content view controllers are intact and in order, notifying you about state transitions and which content view controllers are about to appear or disappear from the view hierarchy. This allos you to compose multiple view controllers and re-use them throughout the app. The state view controller also provides extensive support for animating the transition between states.

## Typical usecase
The state view controller helps you manage child view controllers representing different states. In the example application included in this project the state view controller switches between two view controllers. Firstly, it displays and animates the transition of an activity indicator view controller while a network call is being performed. Once the network call is successfully completed it transitions into a state displaying a table view with the loaded content.
<p align="center">
<img src="./Images/state_transition.png" width="512" />
</p>

The state view controller helps you manage child view controllers representing different states. In the example application included in this project the state view controller switches between two view controllers. Firstly, it displays and animates the transition of an activity indicator view controller while a network call is being performed. Once the network call is successfully completed it transitions into a state displaying a table view with the loaded content.

<p>
<img src="./Images/example_gif.gif" width="300" />
<p align="center">
<img src="./Images/lifecycle.png" width="512" />
</p>

## Documentation
Expand Down
12 changes: 5 additions & 7 deletions Sources/StateViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ public var defaultStateTransitionCoordinator: StateViewControllerTransitionCoord
/// - Set `defaultStateTransitioningCoordinator`
/// - Override `stateTransitionCoordinator(for:)` in your `StateViewController` subclasses
/// - Conform view controllers contained in `StateViewController` to `StateViewControllerTransitioning`.
///
///
open class StateViewController<State: Equatable>: UIViewController {

/// Current state storage
Expand Down Expand Up @@ -153,7 +151,8 @@ open class StateViewController<State: Equatable>: UIViewController {
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

// Note that we're in an appearance transition
// When `viewWillAppear(animated:)` is called we do not yet connsider ourselves in an appearance transition
// internally because first we have to assert whether we are changing to an appearannce state.
isApplyingAppearanceState = false
isInAppearanceTransition = false

Expand All @@ -171,17 +170,16 @@ open class StateViewController<State: Equatable>: UIViewController {
}

// Prematurely remove view controllers that are being removed.
// They do not need to be visible what so ever.
// As we're not yet setting the `isInAppearanceTransition` to `true`, the appearance methods
// for each child view controller below will be forwarded correctly.
for child in viewControllersBeingRemoved {
removeContentViewController(child, animated: false)
}

// Note that we're not in an appearance transition
// Note that we're in an appearance transition
isInAppearanceTransition = true

// Forward begin appearance transitions to child view controllers
// Forward begin appearance transitions to child view controllers.
forwardBeginApperanceTransition(isAppearing: true, animated: animated)
}

Expand All @@ -203,7 +201,7 @@ open class StateViewController<State: Equatable>: UIViewController {
}
}

// End state transition if needed. Child view controllers may still be in a transition,
// End state transition if needed. Child view controllers may still be in a transition.
endStateTransitionIfNeeded(animated: animated)
}

Expand Down
2 changes: 2 additions & 0 deletions StateViewController.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.formbound.StateViewController;
PRODUCT_NAME = StateViewController;
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -342,6 +343,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.formbound.StateViewController;
PRODUCT_NAME = StateViewController;
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand Down

0 comments on commit 8bc6adc

Please sign in to comment.