Akane 0.30.0-beta.1

Akane 0.30.0-beta.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2018年2月
SwiftSwift版本4.0
SPM支持SPM

pjechris 维护。



Akane 0.30.0-beta.1

  • 作者:
  • pjechris, akane和xebia

Akane

CocoaPods Carthage compatible Build Status

Akane是一个iOS框架,它通过采用\MVVM设计模式,帮助你构建更好的原生应用。

主要目标
😅 安全性:尽可能多地减少不良编码实践
🔧 面向功能:添加和维护功能既容易又安全
🔠 面向组件:您想添加到应用中的每个视觉功能都是一个组件(智能或无智能的)
✂️ 细粒度<\strong>关注点分离,即
👯 合并冲突更少
😎 更好地理解您的代码

智能组件由以下组成

  • ComponentViewController
  • ComponentViewModel
  • ComponentDisplayable

(新增长度0.19!) 无智能的组件仅由一个Displayable组成,没有相关的ViewModel

为什么选择Akane,或MVVM与iOS MVC的比较

iOS开发者往往将所有代码写入一个独特的专用ViewController类中。虽然这在几年前可能是可以接受的,但今天的应用代码库越来越大。维护一个单一、庞大、ViewController文件是危险的操作,通常会导致不可预测的副作用。

Akane使您可以将代码拆分成小的组件,这些组件由多个类组成。

模型

模型包含包含应用业务类的层次。

歌曲、电影、书籍:所有这些结构都属于这一层。它们不应包含任何对UIKit或Akane组件的引用。

struct User {
  enum Title: String {
    case sir
    case master
  }

  let username: String
  let title: Title
}

无智能组件

无智能组件绑定到特定状态,但仍然可以用原始数据更新。

它由Displayable协议表示。

class UserView: UIView, Displayable {
   @IBOutlet var title: UILabel!

   func bindings(_ observer: ViewObserver, props user: User) {
     self.title = UserFullNameConverter().convert(user)
   }
}

智能组件

智能组件代表具有定义它渲染状态的组件

  • 状态使用ComponentViewModel表示。
  • UI使用ComponentDisplayable表示。

ViewModel

ViewModel是实现了所有业务逻辑的地方。

请,保持不可知论:ViewModel中不应包含对任何视图或ViewController的引用。另外,优先考虑模式组合而不是继承:将代码分成多个ViewModel,每个ViewModel处理一个业务案例,然后创建另一个ViewModel来聚合所有这些逻辑。

import Akane

class UserViewModel : ComponentViewModel {
  let user: Observable<User>?
  var disconnect: Command! = nil

  init(user: User) {
    self.user = Observable(user)
    self.disconnect = RelayCommand() { [unowned self] in
      self.user.next(nil)
    }
  }

  func isConnected() -> Bool {
    return self.user != nil
  }
}

ComponentDisplayable

ViewModel和其ComponentDisplayable之间的数据流是双向的

  • View <- ViewModel(通过绑定)
  • View -> ViewModel(通过命令):例如,发送消息或订购商品。
import Akane

class LoggedUserView : UIView, ComponentDisplayable {
  @IBOutlet var userView: UserView!
  @IBOutlet var buttonDisconnect: UIButton!

  func bindings(observer: ViewObserver, viewModel: UserViewModel) {
    // New in 0.19! Bind a author with a dumb view
    observer.observe(viewModel.author).bind(to: self.authorView)

    // bind 'disconnect' command with 'buttonDisconnect'
    observer.observe(viewModel.disconnect)
            .bind(to: self.buttonDisconnect)
  }
}

ViewController

通过遵循ComponentController协议,ViewController建立ComponentViewModelComponentDisplayable之间的联系。

只需将您的ComponentViewModel传递给ViewController以便将其绑定到其视图。

application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {  

  let rootViewController = self.window.rootViewController as! ComponentViewController
  let user = User(username: "Bruce", title: .master)

  rootViewController.viewModel = UserViewModel(user: user)

  return true
}

如果需要,您甚至可以定义自己的自定义ViewController。

extension LoggedUserView {
  static func componentControllerClass() -> AnyComponentController.Type {
    return LoggedUserViewController.self
  }
}

class LoggedUserViewController : UIViewController, ComponentController {
  func viewDidLoad() {
    super.viewDidLoad()
    print("User component view loaded")
  }
}

集合

Akane支持在UITableViewsUICollectionViews中显示对象集合。
请阅读Collections.md文档了解更多信息。

安装

Akane支持通过CocoaPods和Carthage进行安装。

CocoaPods

pod 'Akane'

Akane基于Bond进行绑定管理。如果希望使用自己的库(如RxSwift),则只能使用Akane核心。

pod 'Akane/Core'

Carthage

github "akane/Akane"添加到您的Cartfile中。
为了使用Akane绑定和Akane集合,您还需要追加github "ReactiveKit/Bond"

团结就是力量

Akane单独使用效果良好,但结合我们的其他工具效果更佳。

  • Gaikan,Swift中的声明式视图样式。灵感来自CSS模块。
  • Magellan,解耦视图与导航逻辑的路由解决方案。

贡献

该项目最初由Xebia IT Architects开发,并已开源。我们致力于继续工作和投资于Akane。

我们鼓励社区通过打开tickes和/或pull requests来为项目做出贡献。

许可证

Akane是根据MIT许可证发布的。请参阅LICENSE文件以获取详细信息。