Latest 0.0.1
Homepage https://github.com/cemolcay/ProtocolOrientedViewModel
License MIT
Platforms ios 8.0, osx 10.10, tvos 9.0, requires ARC
Authors

Create universal view’s with different layouts for each platform and share the view logic between them.

Requirements

  • Swift 3.0+
  • iOS 8.0+
  • tvOS 9.0+
  • macOS 10.10+

Install

use_frameworks!
pod 'ProtocolOrientedViewModel'

Usage

This is most useful for universal ios/tvos/mac projects.
Create Layouts for your view. For example, portrait and landscape.
Create xib’s for each layout of each platform or just create them programmaitaclly.

enum HomeScreenLayout: Layout {
  case portrait
  case landscape

  // Define the type of view
  typealias ViewType = HomeScreenView
  var view: HomeScreenView? {
    // return from xib or create and return them here for each layout.
  }
}

Create your view and its delegate with actions.

protocol HomeScreenViewDelegate: class {
  func homeScreenViewDidPressSomething(_ homeScreenView: HomeScreenView)
  ...
}

class HomeScreenView: POView { // POView is a typealias of NSView and UIView for sharing view.
  weak var delegate: HomeScreenViewDelegate?
  @IBOutlet var someLabel: UILabel?

  @IBAction func somethingDidPress() {
    delegate?.homeScreenViewDidPressSomething(self)
  }

  ...
}

And create your view model

class HomeScreenViewModel: ViewModel, HomeScreenViewDelegate {

  // MARK: ViewModel
  // define the type of view
  typealias ViewType = HomeScerenView
  var view: HomeScrenView? { didSet{ view?.delegate = self }}

  // Make your updates for your view.
  typealias LayoutType = ExampleLayout
  func render(layout: ExampleLayout?) {
    view?.someLabel?.text = dataSource.someValue
    view?...

    if let layout = layout, case .portrait = layout {
      // Do layout specific stuff.
    }
  }

  // MARK: HomeScreenViewDelegate
  func homeScreenViewDidPressSomething(_ homeScreenView: HomeScreenView) {
    dataSource.someValue = "new value"
    render(layout: nil) 
  }

  ...
}

Call updateLayoutFor:InView: function on the view model to update layout of your view.
Also see the example project for universal usage with multiple layouts with xibs in multiple platforms.

Latest podspec

{
    "name": "ProtocolOrientedViewModel",
    "version": "0.0.1",
    "summary": "Create universal view's with different layouts for each platform and share the view logic between them.",
    "description": "ProtocolOrientedViewModeln===nnCreate universal view's with different layouts for each platform and share the view logic between them.nnRequirementsn----nn- Swift 3.0+n- iOS 8.0+n- tvOS 9.0+n- macOS 10.10+nnInstalln----nn```npod 'ProtocolOrientedViewModel'n```nnUsagen----nnThis is most useful for universal ios/tvos/mac projects.nCreate `Layout`s for your view. For example, portrait and landscape.nCreate xib's for each layout of each platform or just create them programmaitaclly.nn```nenum HomeScreenLayout: Layout {ncase portraitncase landscapenn// Define the type of viewntypealias View = HomeScreenViewnvar view: HomeScreenView? {nreturn from xib or create and return them here for each layout.n}n}n```nnCreate your view and its delegate with actions.nn```nprotocol HomeScreenViewDelegate: class {nfunc homeScreenViewDidPressSomething(_ homeScreenView: HomeScreenView)n...n}nnclass HomeScreenView: POView { // POView is a typealias of NSView and UIView for sharing view.nweak var delegate: HomeScreenV[email protected] var someLabel: [email protected] func somethingDidPress() {ndelegate?.homeScreenViewDidPressSomething(self)n}nn...n}n```nnAnd create your view modelnn```nclass HomeScreenViewModel: ViewModel, HomeScreenViewDelegate {nn// MARK: ViewModeln// define the type of viewntypealias View = HomeScerenViewnvar view: HomeScrenView?nn// Make your updates for your view.nfunc render() {nview?.someLabel?.text = dataSource.someValuenview?...n}nn// MARK: HomeScreenViewDelegatenfunc homeScreenViewDidPressSomething(_ homeScreenView: HomeScreenView) {ndataSource.someValue = "new value"nrender()n}nn...n}n```nnCall `updateLayoutFor:InView:` function on the view model to update layout of your view.nAlso see the example project for universal usage with multiple layouts with xibs in multiple platforms.",
    "homepage": "https://github.com/cemolcay/ProtocolOrientedViewModel",
    "license": "MIT",
    "authors": {
        "cemolcay": "[email protected]"
    },
    "social_media_url": "http://twitter.com/cemolcay",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0"
    },
    "source": {
        "git": "https://github.com/cemolcay/ProtocolOrientedViewModel.git",
        "tag": "0.0.1"
    },
    "source_files": "Source/*.swift",
    "requires_arc": true,
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This