Code Monkey home page Code Monkey logo

protocolorientedviewmodel's Introduction

ProtocolOrientedViewModel

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.