App框架 RAC+MVVM+Objection(路由)

网友投稿 708 2022-10-19

App框架 RAC+MVVM+Objection(路由)

App框架 RAC+MVVM+Objection(路由)

RACMVVMObjection

App框架 RAC+MVVM+Objection(路由)

1.项目初始化配置

+(void)load{ [JSObjection setDefaultInjector:[JSObjection createInjector:[[AppModule alloc]init]]];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.services = [self yh_getObjection:@protocol(YHViewModelServicesProtocol)]; self.navigationControllerStack = [[YHNavigationControllerStack alloc] initWithServices:self.services]; [self.services resetRootTo:Router_Weather]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES;}

2.路由配置

(1)注入类

- (void)configure{ [self bindBlock:^id(JSObjectionInjector *context) { return [[YHViewModelServicesImpl alloc] init]; } toProtocol:@protocol(YHViewModelServicesProtocol) inScope:JSObjectionScopeSingleton]; [self bindProvider:[[YHViewModelProvider alloc] initWithClass:YHWeatherViewModel.self] toClass:[YHWeatherViewModel class] inScope:JSObjectionScopeNormal]; [self bindClass:[YHWeatherViewController class] toClass:[YHWeatherViewModel class] named:Router_Weather]; [self bindProvider:[[YHViewModelProvider alloc] initWithClass:YHWeatherDetailViewModel.self] toClass:[YHWeatherDetailViewModel class]]; [self bindClass:[YHWeatherDetailViewController class] toClass:[YHWeatherDetailViewModel class] named:Router_WeatherDetail];}

(2)viewModel初始化及传值,需要遵守JSObjectionProvider协议

- (id)provide:(JSObjectionInjector *)context arguments:(NSArray *)arguments{ SEL selector = NSSelectorFromString(@"initWithServices:params:"); if ([_classObject instancesRespondToSelector:selector]) { id service = [context getObject:@protocol(YHViewModelServicesProtocol)]; return ((id (*)(id,SEL,id,id))objc_msgSend)([_classObject alloc],selector,service,arguments.firstObject); } return nil;}

(3)vc初始化,取出之前注入的viewModel把它赋值到vc中,方便监听拿值

- (YHBaseViewController *)viewControllerForViewModel:(NSString *)route params:(NSDictionary *)params{ if (!params) { params = @{}; } YHBaseViewModel *baseViewModel = [self yh_getObjection:[NSClassFromString(route) class] argumentList:@[params]]; //从路由中取出VC YHBaseViewController *viewController = (YHBaseViewController *)[self yh_getObjection:[baseViewModel class] name:route]; SEL selector = NSSelectorFromString(@"initWithViewModel:"); if ([[viewController class] instancesRespondToSelector:selector]) { Class VC = [viewController class]; NSParameterAssert([VC isSubclassOfClass:[YHBaseViewController class]]); NSParameterAssert([VC instancesRespondToSelector:@selector(initWithViewModel:)]); viewController = ((id (*)(id,SEL,id))objc_msgSend)([VC alloc],selector,baseViewModel); return viewController; } return nil;}

3.路由跳转

typedef void (^VoidBlock)(void);@protocol YHNavigationProtocol /** * push 到新的页面VC * * @param route * @param params * @param animated */- (void)pushViewTo:(NSString *)route params:(NSDictionary *)params animated:(BOOL)animated;/** * 返回到上层VC * * @param animated */- (void)popViewTo:(BOOL)animated;/** * 返回到根控制器VC * * @param animated */- (void)popToRootViewTo:(BOOL)animated;/** * 展示从下到上pop一个视图(模态)VC * * @param route * @param params * @param animated * @param completion */- (void)presentViewTo:(NSString *)route params:(NSDictionary *)params animated:(BOOL)animated completion:(VoidBlock)completion;/** * 关闭一个模态视图VC * * @param animated * @param completion */- (void)dismissViewTo:(BOOL)animated completion:(VoidBlock)completion;/** * keywindow的根控制器VC * * @param route * @param animated */- (void)resetRootTo:(NSString *)route;

跳转实例:

- (RACCommand *)didSelectCommand{ @weakify(self); return [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSIndexPath *indexPatht) { @strongify(self); [self.services pushViewTo:Router_WeatherDetail params:@{@"detailModel":self.dataSource[indexPatht.section][indexPatht.row]} animated:YES]; return [RACSignal empty]; }];}

4.网络层

+ (RACSignal *)excuteWithParams:(id)params formModelClass:(Class)modelClass{ YHBasicServices *basicServices = [[YHBasicServices alloc] init]; return [[RACSignal createSignal:^RACDisposable *(id subscriber) { [[YHWorkingManager shareManager] sendGETDataWithPath:@"http://baidu.com" withParamters:params success:^(NSURLSessionDataTask *task, id responseObject) { [MBProgressHUD hideHUD]; NSError *error; basicServices.model = [[modelClass alloc] initWithDictionary:responseObject error:&error]; [subscriber sendNext:basicServices.model]; [subscriber sendCompleted]; } failure:^(NSURLSessionDataTask *task, NSError *error) { [subscriber sendError:error]; }]; return [RACDisposable disposableWithBlock:^{ }]; }] setNameWithFormat:@"API - Signal - %@",NSStringFromClass(modelClass)] ;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基于ssm的高校校友信息管理系统设计与实现-计算机毕业设计
下一篇:基于ssm的奥博羽毛球俱乐部管理系统-计算机毕业设计
相关文章

 发表评论

暂时没有评论,来抢沙发吧~