UCForm
一个各个类职责明确, 方便工程Form架构规范的一个小组件.
Example
To run the example project, clone the repo, and run pod install from the Example directory first.
Requirements
swift version > 5.0
Installation
UCForm is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'UCForm'Feature
- 支持最核心的ViewModel协议以及默认实现, ViewModel基类
- 支持cocoapods
- 基础核心能力TableView Demo完成 链接
- 基础核心能力CollectionView Demo完成
- 单元测试完成
- 支持
RxSwift - 支持
Combine - 完善CI
开发思想
利用面向协议的思想尽量规范化Form的各个对象职责单一, 让各个部分的可维护性以及可扩展性变得更高. 由于一些语言层面的限制, 一些类我无法摆脱NSObject的继承束缚, 以及无法为UITableViewModel这样的Objc做一个默认的协议扩展这还是比较遗憾的.
Form架构规范
一个Form VC由这几部分组成: VC, DataSource, ViewModel, Model, CellModel, Cell组成.
VC: 将她变成一个单纯的数据交换场所, 以及UI控件的一个承载者.
DataSource: 数据的请求工厂, 在这里使json -> model, 同时分页, 请求状态都在这里维护.
ViewModel: 数据的承载着, DataSource请求完毕后, 我们在这里完成数据的model -> cellModel这个过程.
Model: 最纯粹的一个json静态映射, 不作任何逻辑.
Cell: 单纯的UI展示, 展示CellModel提供的逻辑.
CellModel: 提供cell的数据和逻辑, 他可以控制并缓存cell的状态, Cell的逻辑都交给他来处理.
核心协议和对象
-
FormViewModelable提供了viewModel改编数据时的安全api.func section(at sectionIndex: Int) -> [RowType]? func replaceSection(at sectionIndex: Int, section: [RowType]) func appendSection(section: [RowType]) func insertSection(at sectionIndex: Int, section: [RowType]) func removeSection(at sectionIndex: Int) func removeAllSection() func row(at indexPath: IndexPath) -> RowType? func addRow(at sectionIndex: Int, row: RowType) func addRow(at sectionIndex: Int, rows: [RowType]) func insertRow(at indexPath: IndexPath, row: RowType) func insertRow(at indexPath: IndexPath, rows: [RowType]) func removeRow(at indexPath: IndexPath) func replaceRow(at indexPath: IndexPath, row: RowType) subscript<M>(indexPath: IndexPath) -> M? { get set } func cellModel<M>(at indexPath: IndexPath) -> M? -
FormUpdatablecell需要实现并且遵守这个协议 -
Row一个包裹, 里面包裹了CellModel和Cell -
CollectionViewModel和TableViewModel由于objc协议无法扩展默认实现, 所以这里只能继承.
Author
Link913, [email protected]
License
UCForm is available under the MIT license. See the LICENSE file for more info.