Skip to content

Instantly share code, notes, and snippets.

@markcleonard
Last active September 30, 2016 11:10
Show Gist options
  • Select an option

  • Save markcleonard/f1a19b27c00f8e1c82872705c1a06863 to your computer and use it in GitHub Desktop.

Select an option

Save markcleonard/f1a19b27c00f8e1c82872705c1a06863 to your computer and use it in GitHub Desktop.

Revisions

  1. markcleonard revised this gist Sep 30, 2016. 1 changed file with 18 additions and 9 deletions.
    27 changes: 18 additions & 9 deletions StoryboardCreatable.swift
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,9 @@
    import UIKit

    enum Storyboard: String {
    case Main
    case History
    case More
    case main = "Main"
    case history = "History"
    case more = "More"

    func storyboard() -> UIStoryboard {
    return UIStoryboard(name: rawValue, bundle: nil)
    @@ -14,9 +14,7 @@ enum Storyboard: String {
    protocol StoryboardCreatable {
    associatedtype ViewModel
    static var storyboard: Storyboard { get }
    static var storyboardIdentifier: String { get }
    var viewModel: ViewModel { get set }
    mutating func configureWithViewModel(viewModel: ViewModel)
    }

    extension StoryboardCreatable {
    @@ -47,19 +45,30 @@ extension UIStoryboard {
    }
    }

    // MARK: Implementation

    struct User { }
    struct User {
    let name: String
    }
    struct History { }

    struct HistoryViewModel {
    let user: User
    let history: History
    }

    //MARK: Implementation
    final class HistoryViewController: UIViewController, StoryboardCreatable {
    static var storyboard: Storyboard = .History
    static var storyboard: Storyboard = .history
    var viewModel: HistoryViewModel!

    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
    super.viewDidLoad()

    label.text = viewModel.user.name
    }
    }

    let viewModel = HistoryViewModel(user: User(), history: History())
    let viewModel = HistoryViewModel(user: User(name: "Mark"), history: History())
    let viewController = HistoryViewController.createFromStoryboard(viewModel: viewModel)
  2. markcleonard revised this gist Sep 29, 2016. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions StoryboardCreatable.swift
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ protocol StoryboardCreatable {

    extension StoryboardCreatable {
    static var storyboardIdentifier: String {
    return String(Self)
    return String(describing: Self.self)
    }
    mutating func configureWithViewModel(viewModel: ViewModel) {
    self.viewModel = viewModel
    @@ -31,15 +31,15 @@ extension StoryboardCreatable {
    extension StoryboardCreatable where Self: UIViewController {
    static func createFromStoryboard(viewModel: ViewModel) -> Self {
    var viewController: Self = UIStoryboard.createViewController()
    viewController.configureWithViewModel(viewModel)
    viewController.configureWithViewModel(viewModel: viewModel)
    return viewController
    }
    }

    extension UIStoryboard {
    static func createViewController<T: StoryboardCreatable>() -> T {
    let storyboard = UIStoryboard(name: T.storyboard.rawValue, bundle: nil)
    let createViewController = storyboard.instantiateViewControllerWithIdentifier(T.storyboardIdentifier)
    let createViewController = storyboard.instantiateViewController(withIdentifier: T.storyboardIdentifier)
    guard createViewController is T else {
    fatalError("Expected view controller with identifier \(T.storyboardIdentifier)")
    }
    @@ -59,5 +59,7 @@ struct HistoryViewModel {
    final class HistoryViewController: UIViewController, StoryboardCreatable {
    static var storyboard: Storyboard = .History
    var viewModel: HistoryViewModel!

    }

    let viewModel = HistoryViewModel(user: User(), history: History())
    let viewController = HistoryViewController.createFromStoryboard(viewModel: viewModel)
  3. markcleonard revised this gist Sep 29, 2016. 1 changed file with 0 additions and 9 deletions.
    9 changes: 0 additions & 9 deletions StoryboardCreatable.swift
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,3 @@
    import UIKit

    //
    // Storyboard.swift
    // PokerMate
    //
    // Created by Mark Leonard on 8/7/16.
    //
    //

    import UIKit

  4. markcleonard revised this gist Sep 29, 2016. 1 changed file with 43 additions and 29 deletions.
    72 changes: 43 additions & 29 deletions StoryboardCreatable.swift
    Original file line number Diff line number Diff line change
    @@ -1,58 +1,72 @@
    import UIKit

    // MARK: Logic
    protocol StoryboardCreatable {
    associatedtype Dependencies
    //
    // Storyboard.swift
    // PokerMate
    //
    // Created by Mark Leonard on 8/7/16.
    //
    //

    import UIKit

    enum Storyboard: String {
    case Main
    case History
    case More

    func storyboard() -> UIStoryboard {
    return UIStoryboard(name: rawValue, bundle: nil)
    }
    }

    protocol StoryboardCreatable {
    associatedtype ViewModel
    static var storyboard: Storyboard { get }
    static var storyboardIdentifier: String { get }
    static func createFromStoryboard(dependencies: Dependencies) -> Self
    func configure(dependencies: Dependencies)
    var viewModel: ViewModel { get set }
    mutating func configureWithViewModel(viewModel: ViewModel)
    }

    extension StoryboardCreatable {
    static var storyboardIdentifier: String {
    return String(Self)
    }
    mutating func configureWithViewModel(viewModel: ViewModel) {
    self.viewModel = viewModel
    }
    }

    extension StoryboardCreatable where Self: UIViewController {
    static func createFromStoryboard(dependencies: Dependencies) -> Self {
    let viewController: Self = UIStoryboard.createViewController()
    viewController.configure(dependencies: dependencies)
    static func createFromStoryboard(viewModel: ViewModel) -> Self {
    var viewController: Self = UIStoryboard.createViewController()
    viewController.configureWithViewModel(viewModel)
    return viewController
    }
    }

    extension UIStoryboard {
    static func createViewController<T: StoryboardCreatable>() -> T {
    let storyboard = UIStoryboard(name: T.storyboard.rawValue, bundle: nil)
    let createViewController = storyboard.instantiateViewController(withIdentifier: T.storyboardIdentifier)
    let createViewController = storyboard.instantiateViewControllerWithIdentifier(T.storyboardIdentifier)
    guard createViewController is T else {
    fatalError("Expected view controller with identifier \(T.storyboardIdentifier)")
    }
    return createViewController as! T
    }
    }

    //MARK: Model
    enum Storyboard: String {
    case Main = "Main"
    case History = "History"
    case More = "More"

    func storyboard() -> UIStoryboard {
    return UIStoryboard(name: rawValue, bundle: nil)
    }
    }

    struct User { }
    struct History { }
    struct HistoryViewModel {
    let user: User
    let history: History
    }

    //MARK: Implementation
    final class HistoryViewController: UIViewController, StoryboardCreatable {
    static var storyboard = Storyboard.History
    static var storyboardIdentifier = "HistoryViewController"
    typealias Dependencies = (user: User, history: History)
    func configure(dependencies: Dependencies) {
    history = dependencies.history
    user = dependencies.user
    }

    private var user: User!
    private var history: History!
    static var storyboard: Storyboard = .History
    var viewModel: HistoryViewModel!

    }
  5. markcleonard created this gist Sep 25, 2016.
    58 changes: 58 additions & 0 deletions StoryboardCreatable.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    import UIKit

    // MARK: Logic
    protocol StoryboardCreatable {
    associatedtype Dependencies

    static var storyboard: Storyboard { get }
    static var storyboardIdentifier: String { get }
    static func createFromStoryboard(dependencies: Dependencies) -> Self
    func configure(dependencies: Dependencies)
    }

    extension StoryboardCreatable where Self: UIViewController {
    static func createFromStoryboard(dependencies: Dependencies) -> Self {
    let viewController: Self = UIStoryboard.createViewController()
    viewController.configure(dependencies: dependencies)
    return viewController
    }
    }

    extension UIStoryboard {
    static func createViewController<T: StoryboardCreatable>() -> T {
    let storyboard = UIStoryboard(name: T.storyboard.rawValue, bundle: nil)
    let createViewController = storyboard.instantiateViewController(withIdentifier: T.storyboardIdentifier)
    guard createViewController is T else {
    fatalError("Expected view controller with identifier \(T.storyboardIdentifier)")
    }
    return createViewController as! T
    }
    }

    //MARK: Model
    enum Storyboard: String {
    case Main = "Main"
    case History = "History"
    case More = "More"

    func storyboard() -> UIStoryboard {
    return UIStoryboard(name: rawValue, bundle: nil)
    }
    }

    struct User { }
    struct History { }

    //MARK: Implementation
    final class HistoryViewController: UIViewController, StoryboardCreatable {
    static var storyboard = Storyboard.History
    static var storyboardIdentifier = "HistoryViewController"
    typealias Dependencies = (user: User, history: History)
    func configure(dependencies: Dependencies) {
    history = dependencies.history
    user = dependencies.user
    }

    private var user: User!
    private var history: History!
    }