SCIntent 0.5.0

SCIntent 0.5.0

FlowSc 维护。



 
依赖
RxSwift~> 5.0.1
RxCocoa~> 5.0.1
 

SCIntent 0.5.0

  • FlowSc

SCIntent

CI Status Version License Platform

概念

  • 简单 iOS 单向架构
  • 模型 - 视图 - 意图
  • 在 ViewController 中分离绑定函数和渲染函数 - 绑定用于视图到意图,渲染用于视图中的模型(由意图转换)
  • stateObserver 控制意图中所有相关模型的状态。

示例

要运行示例项目,首先要克隆仓库,然后从 Example 目录运行 pod install

要求

  • iOS 11.0+
  • Xcode 11.0+
  • Swift 5.0+

安装

SCIntent 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod 'SCIntent'

使用说明

ModelState

import SCIntent

enum CounterState: ModelState {
    case valueChanged(Int)
    case mininum
    case maximum
}

Intent

import SCIntent
import RxSwift
import RxCocoa

struct CounterIntent: Intent { 

    typealias State = CounterState
    
    var disposeBag: DisposeBag = DisposeBag()
    var stateObserver: PublishRelay<CounterState> = PublishRelay<CounterState>()
    
    func valueDown(_ current: Int) {
        if current == 0 {
            stateObserver.accept(.mininum)
        } else {
            stateObserver.accept(.valueChanged(current - 1))
        }
    }
    
    func valueUp(_ current: Int) {
        if current == 10 {
            stateObserver.accept(.maximum)
        } else {
            stateObserver.accept(.valueChanged(current + 1))
        }
    }
}

视图控制器

import SCIntent
import RxSwift
import RxCocoa

class ViewController: IntentViewController<CounterIntent> { 
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.intent = CounterIntent()
    }
    
    override func bind(_ intent: CounterIntent) { // Bind View Controller Action and Intent's Business Logic
        downBtn.rx.tap.bind {
            intent.valueDown(self.currentValue)
        }.disposed(by: disposeBag)
        upBtn.rx.tap.bind {
            intent.valueUp(self.currentValue)
        }.disposed(by: disposeBag)
    }
    
    override func renderBy(_ state: CounterState) { // Do Presentation Logic by Model State
        
        switch state {
        case .valueChanged(let val):
            setValue(val)
        case .mininum:
            showAlert(msg: "Counter cannot show under 0")
        case .maximum:
            showAlert(msg: "Conuter cannot show over 10")
        }
    }
    
    func setValue(_ val: Int) {
        self.currentValue = val
        self.counterLb.text = "\(val)"
    }
    
    func showAlert(msg: String) {
        
        let alertVc = UIAlertController(title: nil, message: msg, preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        
        alertVc.addAction(action)
        
        self.present(alertVc, animated: true)
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

作者

FlowSc,[email protected]

许可

SCIntent 在 MIT 许可下提供。有关更多信息,请参阅 LICENSE 文件。