一、说明
本文档的主要目的是介绍如何使用MVVM框架库: appframeworkkit。 appframeworkkit库是应用层架构库,主要解决视图和模型高度解耦、业务逻辑可以轻松测试的框架。同时提供了对tableview常用视图的抽象,可以基于此框架方便、快捷地开发功能,同时具有高度可扩展和可维护性
关于appframeworkkit的原理与设计,请看此链接app架构之应用层架构
二、安装
使用Cocoapods安装: pod 'AppFrameworkKit', '~>0.0.1'
目录结构
安装完成后,在Pod目录下可以找到AppFrameworkKit文件夹,为方便业务使用和交流,采用源码开放式目录结构,如下:
|____AppFrameworkKit.h (头文件汇总)
|____BaseViewKit
| |____AFKCoordinatorProtocol.h (UIView的Coordinator Protocol)
|____Category
| |____UIResponder+AFKRouter.h (UIResponder Category)
| |____UIResponder+AFKRouter.m
| |____UIView+AFKContext.h (Context Category)
| |____UIView+AFKContext.m
|____Define
| |____AFKDefine.h (宏定义)
|____SectionTableView
| |____AFKSectionTableViewCoordinator.h (Section TableView Coordinator 基础类,需要子类继承它)
| |____AFKSectionTableViewCoordinator.m
| |____AFKSectionTableViewModelProtocol.h (SectionTableView ViewModel Protocol)
|____TableView
| |____AFKTableViewCellCoordinatorProtocol.h (TableView Cell的 Coordinator Protocol)
| |____AFKTableViewCellManager.h (TableView Cell的管理类,用于注册cell类型与获取cell)
| |____AFKTableViewCellManager.m
| |____AFKTableViewCellViewModelProtocol.h (TableView Cell ViewModel的 Coordinator Protocol)
| |____AFKTableViewCoordinator.h (TableView Coordinator 基础类,需要子类继承它)
| |____AFKTableViewCoordinator.m
| |____AFKTableViewModelProtocol.h (TableView ViewModel Protocol)
|____Tools(工具类,用于处理不同ViewController间的数据传递)
| |____AFKObserverCenter.h
| |____AFKObserverCenter.m
| |____NotificationCenter
| | |____AFKMessageCenter.h
| | |____AFKMessageCenter.m
| |____ProtocolDispatch
| | |____AFKProtocolDispatcher.h
| | |____AFKProtocolDispatcher.m
| | |____AFKProtocolDispatcherCenter.h
| | |____AFKProtocolDispatcherCenter.m
模板导入
Pod完成后,将在本地Xcode自动生成文件模板,文件模板的路径为 ~/Library/Developer/Xcode/Templates/File Templates,有兴趣的同学可以一起来研究并完善它。
三、使用步骤
UIView类型
模板创建步骤
模板介绍
从最后一步可以看到,一共生成了3个类,分别是
1.IMSRecentChatView.h和IMSRecentChatView.m
该类继承自UIView,业务布局操作在此类中进行。注意:如果使用了Model进行属性封装,必须确保该Model不与ViewModel共享。
2.IMSRecentChatViewModel.IMSRecentChatViewModel.m
该类继承自NSObject,业务在fetchData方法中进行数据获取和处理操作。对于不需要外部输入的简单View,可以直接删除该ViewModel类。
3.IMSRecentChatCoordinator.h和IMSRecentChatCoordinator.m
该类实现协议AFKCoordinatorProtocol,作用是绑定IMSRecentChatView与IMSRecentChatViewModel。在bindData方法中进行绑定即可,对于简单View则无需绑定。
包含tableView的UIViewController(不含Section)
模板选择
模板介绍
注意:生成的tableView文件夹是绿色的,需要先对其删除引用,再拖入。(模板创建如果控制成group,无法在本地生成目录结果,因此此处需要手动处理一下。)
从最后一步可以看到,该模板包含ViewController,Context,TableView TableViewCell,接下来对其分别进行介绍。
1.IMSRecentViewController.h和 IMSRecentViewController.m
该类继承ViewController,主要负责模块安装,context初始化,以及事件响应拦截。
2.IMSRecentViewControllerContext.h和 IMSRecentViewControllerContext.m
该类实现协议AFKContextProtocol,主要用于保存上下文信息,如ViewController。对于简单类型的tableView,用不到可以删除它。
3.IMSRecentTableViewModel.h 和 IMSRecentTableViewModel.m
该类实现协议AFKTableViewModelProtocol, 业务在此类的fetchData进行数据获取以及处理操作。
4.IMSRecentTableViewCoordinator.h 和 IMSRecentTableViewCoordinator.m
重点:该类继承AFKTableViewCoordinator,业务需在此类进行注册Cell类型,绑定数据操作。此类必须实现父类方法initWithViewModel。
5.IMSChatCellView.h和 IMSChatCellView.m
该类继承UITableViewCell,业务在此类进行布局操作。注意:如果对属性使用了Model封装,务必保证该Model不与ViewModel共用。
6.IMSChatCellViewModel.h 和 IMSChatCellViewModel.m
该类实现协议AFKTableViewCellViewModelProtocol,业务在此类进行数据处理操作。需要实现identifier与cellHeight
7.IMSRecentChatCoordinator.h和IMSRecentChatCoordinator.m
该类实现协议AFKTableViewCellCoordinatorProtocol,作用是绑定 IMSChatCellViewModel 与 IMSChatCellView 。在bindData方法中进行绑定即可,若简单View则无需绑定。
包含TableView的UIStoryboardController(含Section)
模板选择
模板介绍
此模板结构与不含Section的模板类似,多了注册HeaderView与FooterView。在此不做过多介绍。
其他模版
AFK Controller Context: 用于单独创建上述提到的Context类。
AFK TableViewCell:用于单独创建TableViewCell类。
AFK TableViewFooter:用于单独创建tableview section中的footerView类。
AFK TableViewHeader:用于单独创建tableview section中的headerView类。