-
Notifications
You must be signed in to change notification settings - Fork 61
3. Module Manager (模块管理)
What is Module? Module can include some layer in VIPER design Pattern which work interactively. Each of the modules is independent mutually, which is convenience to programming and debugging, focusing finish concrete function and operation.
Then it has an a detailed needs analysis of each module, completing the division of system functionality which described from different aspects like data structure and algorithm process.
The class XFRoutingLinkManager
manager the relationships between the modules.
获得当前模块名在共享模块中可以用到,共享模块在 Module Assembly 章节里会详细讲解。获得当前模块名只能在视图层、事件层、路由层才可以,但在事件层才是最常用的,例如:
#import "BDJPostPresenter.h"
#import "BDJPostInteractorPort.h"
// 定义访问真实相应层接口的宏
#define Interactor XFConvertInteractorToType(id<BDJPostInteractorPort>)
#define Routing XFConvertRoutingToType(id<BDJPostWireFramePort>)
@implementation BDJPostPresenter
#pragma mark - DoAction
- (void)didPostCellSelectedActionAtIndex:(NSInteger)index
{
// 查找帖子ID(使用宏XF_ModuleName获得当前模块名,用以区别帖子类型)
NSString *PostID = [Interactor fetchPostIDForIndex:index type:BDJ_Post_Str2Type(XF_ModuleName)];
// 切换到评论
[Routing transition2PostComment];
// 发送选择帖子ID事件
XF_SendEventForModule_(@"PostComment", BDJPostSelectedEvent, PostID)
}
@end
模块视图层Activity往往会包含一些子View,子View又会包含自己的子View,当这些多重View发生控件事件时,就可以委托给事件层Presenter,为了不让View不要写业务相关代码,框架提供一个分类UIView+XFLego
,无论有多少层子View都能获得事件层,使用如下:
#import "BDJPostPictureView.h"
#import "UIView+XFLego.h" // 导入分类头文件
#import "BDJPostEventHandlerPort.h" // 事件层接口
// 定义访问真实事件层接口的宏(注意:宏XFConvertPresenterToType是框架定义的,它会把self.eventHandler转成相应接口类型,这里是BDJPostEventHandlerPort)
#define EventHandler XFConvertPresenterToType(id<BDJPostEventHandlerPort>)
// 这个View相对于Activity的层级关系:BDJPostActivity->BDJPostCell->BDJPostPictureView,因此是Activity的子子View
@implementation BDJPostPictureView
- (void)didPictureViewClick
{
// 使用self.eventHandler就能获取事件层,so easy!
NSLog(@"%@",self.eventHandler);
[EventHandler didPictureViewClickActionWithExpressPiece:self.expressPiece];
}
@end
我们知道,在苹果MVC开发模式里,控制器绑定着视图生命周期,控制器可以添加子控制器,再父视图添加子视图。在乐高里也有相似的概念,当有两个页面都是VIPER模块,又互为父子关系,就要使用添加子模块,使框架内部能更好地管理模块,如:
@implementation BDJEssenceActivity
// 给精华页面添加多种类型帖子页面
- (void)addChildActivity
{
// 多个子模块
NSArray *modules = @[@"AllPost",@"VideoPost",@"VoicePost",@"PicturePost",@"WordsPost"];
NSUInteger count = modules.count;
for (int i = 0; i < count; i++) {
// 使用宏`XF_SubUInterface_`获得一个模块的视图
XFActivity *activity = XF_SubUInterface_(modules[i]);
// 调用系统提供的API添加子控制器
[self addChildViewController:activity];
}
}
@end
乐高框架有自己的模块编写方式,但也能与MVx模式旧项目代码融合在一起,同时支持在MVx控制器添加VIPER子模块,如:
// 导入头文件
#import "XFLegoVIPER.h"
@implementation BDJEssenceViewController
- (void)addChildViewController
{
NSArray *modules = @[@"AllPost",@"VideoPost",@"VoicePost",@"PicturePost",@"WordsPost"];
NSUInteger count = modules.count;
for (int i = 0; i < count; i++) {
// 使用宏`XF_SubUInterface_`或`XF_SubUInterface_URL`获得一个模块的视图
XFActivity *activity = XF_SubUInterface_(modules[i]);
[self addChildViewController:activity];
}
}
@end
注意: 例子中的代码来自一个通过乐高框架编写的完整项目:BDJProjectExample