UCForm
一个各组件职责明确,便于工程 Form 架构规范的小组件.
示例
要运行示例项目,请先克隆仓库,然后从 Example 目录运行 pod install
。
需求
swift 版本
> 5.0
安装
UCForm 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 即可
pod 'UCForm'
特性
- 支持最核心的 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?
-
FormUpdatable
cell需要实现并且遵守这个协议 -
Row
一个包裹,里面包裹了CellModel
和Cell
-
CollectionViewModel
和TableViewModel
由于objc
协议无法扩展默认实现,所以这里只能继承。
作者
Link913,[email protected]
许可证
UCForm 可在MIT许可证下使用。更多信息请参阅LICENSE文件。