测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2018年2月 |
SwiftSwift版本 | 4.0 |
SPM支持SPM | ✗ |
由 pjechris 维护。
Akane是一个iOS框架,它通过采用\MVVM设计模式,帮助你构建更好的原生应用。
主要目标 | |
---|---|
安全性:尽可能多地减少不良编码实践 | |
面向功能:添加和维护功能既容易又安全 | |
面向组件:您想添加到应用中的每个视觉功能都是一个组件(智能或无智能的) | |
细粒度<\strong>关注点分离,即 | |
合并冲突更少 | |
更好地理解您的代码 |
智能组件由以下组成
ComponentViewController
ComponentViewModel
ComponentDisplayable
(新增长度0.19!) 无智能的组件仅由一个Displayable
组成,没有相关的ViewModel
。
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
表示。ComponentDisplayable
表示。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
}
}
ViewModel和其ComponentDisplayable之间的数据流是双向的
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)
}
}
通过遵循ComponentController
协议,ViewController建立ComponentViewModel
与ComponentDisplayable
之间的联系。
只需将您的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支持在UITableViews
和UICollectionViews
中显示对象集合。
请阅读Collections.md文档了解更多信息。
Akane支持通过CocoaPods和Carthage进行安装。
pod 'Akane'
Akane基于Bond进行绑定管理。如果希望使用自己的库(如RxSwift),则只能使用Akane核心。
pod 'Akane/Core'
将github "akane/Akane"
添加到您的Cartfile
中。
为了使用Akane绑定和Akane集合,您还需要追加github "ReactiveKit/Bond"
。
Akane单独使用效果良好,但结合我们的其他工具效果更佳。
该项目最初由Xebia IT Architects开发,并已开源。我们致力于继续工作和投资于Akane。
我们鼓励社区通过打开tickes和/或pull requests来为项目做出贡献。
Akane是根据MIT许可证发布的。请参阅LICENSE文件以获取详细信息。