Skip to content

Instantly share code, notes, and snippets.

@zmcartor
Created August 19, 2021 03:16
Show Gist options
  • Select an option

  • Save zmcartor/817b08571e184a35b43af89f101b8850 to your computer and use it in GitHub Desktop.

Select an option

Save zmcartor/817b08571e184a35b43af89f101b8850 to your computer and use it in GitHub Desktop.

Revisions

  1. zmcartor created this gist Aug 19, 2021.
    67 changes: 67 additions & 0 deletions mvvmc.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    import UIKit

    struct FrontScreenViewmodel {
    var hello = "there"
    }

    // Builds the ViewControllers, and injects the ViewModels
    class Factory {

    let viewModelFactory = ViewModelFactory()
    let mainStack: UINavigationController

    func createFrontScreen(withStack: UINavigationController? = nil) -> UIViewController {
    // create a new Coordinator, pass in our factory
    let coor = FrontScreenCoordinator(withStack: withStack ?? mainStack, factory: self)
    let viewmodel = viewModelFactory.FrontScreenViewmodel()
    return FrontScreenViewController(withViewModel: viewmodel, coordinator: coor)
    }

    init(withStack: UINavigationController) {
    mainStack = withStack
    }
    }

    // Handles Core Data, Networing, etc assembly of documentModel into ViewModel
    class ViewModelFactory {
    func FrontScreenViewmodel() -> FrontScreenViewmodel {
    // pass in other things like interactors, etc.
    return FrontScreenViewmodel()
    }
    }

    class FrontScreenCoordinator {

    // encapsulates the Routes this VC can trigger. Each VC has unique coordinator.
    func gotoNext() {}
    func goBack() {}
    func showStore(withId: Int) {
    let vc = factory.createFrontScreen()
    stack.pushViewController(vc, animated: true)
    }

    private let factory: Factory
    private let stack: UINavigationController

    init(withStack: UINavigationController, factory: Factory) {
    self.stack = withStack
    self.factory = factory
    }
    }

    class FrontScreenViewController: UIViewController {

    private let coordinator: FrontScreenCoordinator
    private let viewModel: FrontScreenViewmodel

    init(withViewModel: FrontScreenViewmodel, coordinator: FrontScreenCoordinator) {

    self.coordinator = coordinator
    self.viewModel = withViewModel
    super.init()
    }

    required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
    }
    }