小程序原生组件—提升你的小程序体验
708
2022-10-19
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
跳转实例:
- (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
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~