From 0b020652d8d6bdba46a19cf8761d2adedde6f925 Mon Sep 17 00:00:00 2001 From: lovemo <928627819@qq.com> Date: Fri, 15 Apr 2016 13:26:10 +0800 Subject: [PATCH] add mediator --- SUIMVVMDemo/.DS_Store | Bin 6148 -> 6148 bytes .../SUIMVVMDemo.xcodeproj/project.pbxproj | 6 +++ .../Src/TwoExample/Controller/ThirdVC.m | 16 +++++++ .../Classes/Src/TwoExample/View/ThirdView.m | 2 +- .../TwoExample/ViewManger/ThirdViewManger.m | 6 +++ .../Src/TwoExample/ViewModel/ThirdViewModel.m | 6 +++ .../SUIMVVMKit/NSObject+SMKProperties.h | 15 +++++++ .../SUIMVVMKit/NSObject+SMKProperties.m | 30 +++++++++++++ .../SUIMVVMKit/SMKAbstractMediator.h | 42 ++++++++++++++++++ .../SUIMVVMKit/SMKAbstractMediator.m | 34 ++++++++++++++ .../SUIMVVMKit/SMKViewMangerProtocol.h | 5 ++- .../SUIMVVMKit/SMKViewModelProtocol.h | 6 ++- 12 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKAbstractMediator.h create mode 100644 SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKAbstractMediator.m diff --git a/SUIMVVMDemo/.DS_Store b/SUIMVVMDemo/.DS_Store index 9aa41c99c76b9fa50bf32df3db535d1f53ae1003..c252b6de130b45872fc9ff30d265b52d18832403 100644 GIT binary patch delta 76 zcmZoMXffEZh?SXPW87p#Ha))Fd>5CL(voBbhRdd(lqWxAwPuFU29y2Sc$pa info <----- viewModel 之间通过block方式交互 self.thirdViewManger.viewMangerInfosBlock = [self.viewModel smk_viewModelWithViewMangerBlockOfInfos:@{@"info" : @"viewManger"}]; + + // 中介者传值 + SMKAbstractMediator *mediator = [SMKAbstractMediator mediatorWithViewModel:self.viewModel viewManger:self.thirdViewManger]; + + self.thirdViewManger.smk_mediator = mediator; + self.viewModel.smk_mediator = mediator; + + self.thirdViewManger.smk_viewMangerInfos = @{@"xxxxxx" : @"22222222"}; + [self.thirdViewManger smk_notice]; + NSLog(@"viewManger------>viewModel==%@", self.viewModel.smk_viewModelInfos); + + self.viewModel.smk_viewModelInfos = @{@"oooooo" : @"888888888"}; + [self.viewModel smk_notice]; + NSLog(@"viewModel=====>viewManger==%@", self.thirdViewManger.smk_viewMangerInfos); + } - (IBAction)clickBtnAction:(UIButton *)sender { diff --git a/SUIMVVMDemo/SUIMVVMDemo/Classes/Src/TwoExample/View/ThirdView.m b/SUIMVVMDemo/SUIMVVMDemo/Classes/Src/TwoExample/View/ThirdView.m index a7f98cf..b054d37 100644 --- a/SUIMVVMDemo/SUIMVVMDemo/Classes/Src/TwoExample/View/ThirdView.m +++ b/SUIMVVMDemo/SUIMVVMDemo/Classes/Src/TwoExample/View/ThirdView.m @@ -45,8 +45,8 @@ - (IBAction)jumpOtherVC:(UIButton *)sender { //} - (void)smk_configureViewWithViewModel:(id)viewModel { + [viewModel smk_viewModelWithModelBlcok:^(id model) { - ThirdModel *thirdModel = (ThirdModel *)model; self.testLabel.text = thirdModel.title; }]; diff --git a/SUIMVVMDemo/SUIMVVMDemo/Classes/Src/TwoExample/ViewManger/ThirdViewManger.m b/SUIMVVMDemo/SUIMVVMDemo/Classes/Src/TwoExample/ViewManger/ThirdViewManger.m index b723a0e..a2da431 100644 --- a/SUIMVVMDemo/SUIMVVMDemo/Classes/Src/TwoExample/ViewManger/ThirdViewManger.m +++ b/SUIMVVMDemo/SUIMVVMDemo/Classes/Src/TwoExample/ViewManger/ThirdViewManger.m @@ -10,6 +10,7 @@ #import "FirstVC.h" #import "UIView+SMKEvents.h" #import "UIView+SMKConfigure.h" +#import "SMKAbstractMediator.h" @interface ThirdViewManger () @@ -53,6 +54,11 @@ - (void)smk_viewModel:(id)viewModel withInfos:(NSDictionary *)infos { NSLog(@"%@",infos); } +#pragma mark 中介者传值 +- (void)smk_notice { + [self.smk_mediator noticeViewModelWithInfos:self.smk_viewMangerInfos]; +} + //// 得到模型数据 //- (void)smk_viewMangerWithModel:(NSDictionary *(^)( ))dictBlock { // if (dictBlock) { diff --git a/SUIMVVMDemo/SUIMVVMDemo/Classes/Src/TwoExample/ViewModel/ThirdViewModel.m b/SUIMVVMDemo/SUIMVVMDemo/Classes/Src/TwoExample/ViewModel/ThirdViewModel.m index c0ac2ce..20ad224 100644 --- a/SUIMVVMDemo/SUIMVVMDemo/Classes/Src/TwoExample/ViewModel/ThirdViewModel.m +++ b/SUIMVVMDemo/SUIMVVMDemo/Classes/Src/TwoExample/ViewModel/ThirdViewModel.m @@ -63,4 +63,10 @@ - (ViewMangerInfosBlock)smk_viewModelWithViewMangerBlockOfInfos:(NSDictionary *) }; } +#pragma mark 中介者传值 +- (void)smk_notice { + [self.smk_mediator noticeViewMangerWithInfos:self.smk_viewModelInfos]; +} + + @end diff --git a/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/NSObject+SMKProperties.h b/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/NSObject+SMKProperties.h index 90ff7bd..d2592fd 100755 --- a/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/NSObject+SMKProperties.h +++ b/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/NSObject+SMKProperties.h @@ -10,6 +10,7 @@ #import "SMKViewProtocol.h" #import "SMKViewModelProtocol.h" #import "SMKViewMangerProtocol.h" +#import "SMKAbstractMediator.h" NS_ASSUME_NONNULL_BEGIN @@ -61,6 +62,20 @@ typedef void (^ViewModelInfosBlock)( ); */ @property (nonatomic, copy) ViewModelInfosBlock viewModelInfosBlock; +/** + * mediator + */ +@property (nonatomic, strong) SMKAbstractMediator *smk_mediator; + +/** + * smk_viewMangerInfos + */ +@property (nonatomic, copy) NSDictionary *smk_viewMangerInfos; + +/** + * smk_viewModelInfos + */ +@property (nonatomic, copy) NSDictionary *smk_viewModelInfos; @end diff --git a/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/NSObject+SMKProperties.m b/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/NSObject+SMKProperties.m index 813ca33..d022f2a 100755 --- a/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/NSObject+SMKProperties.m +++ b/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/NSObject+SMKProperties.m @@ -51,6 +51,36 @@ - (void)setViewModelBlock:(ViewModelBlock)viewModelBlock { objc_setAssociatedObject(self, @selector(viewModelBlock), viewModelBlock, OBJC_ASSOCIATION_COPY); } +/** + * mediator + */ +- (void)setSmk_mediator:(SMKAbstractMediator *)smk_mediator { + objc_setAssociatedObject(self, @selector(smk_mediator), smk_mediator, OBJC_ASSOCIATION_RETAIN); +} +- (SMKAbstractMediator *)smk_mediator { + return objc_getAssociatedObject(self, @selector(smk_mediator)); +} + +/** + * smk_viewMangerInfos + */ +- (void)setSmk_viewMangerInfos:(NSDictionary *)smk_viewMangerInfos { + objc_setAssociatedObject(self, @selector(smk_viewMangerInfos), smk_viewMangerInfos, OBJC_ASSOCIATION_COPY); +} +- (NSDictionary *)smk_viewMangerInfos { + return objc_getAssociatedObject(self, @selector(smk_viewMangerInfos)); +} + +/** + * smk_viewModelInfos + */ +- (void)setSmk_viewModelInfos:(NSDictionary *)smk_viewModelInfos { + objc_setAssociatedObject(self, @selector(smk_viewModelInfos), smk_viewModelInfos, OBJC_ASSOCIATION_COPY); +} +- (NSDictionary *)smk_viewModelInfos { + return objc_getAssociatedObject(self, @selector(smk_viewModelInfos)); +} + - (nullable NSDictionary *)smk_allProperties { unsigned int count = 0; diff --git a/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKAbstractMediator.h b/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKAbstractMediator.h new file mode 100644 index 0000000..cca4895 --- /dev/null +++ b/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKAbstractMediator.h @@ -0,0 +1,42 @@ +// +// SMKAbstractMediator.h +// SUIMVVMDemo +// +// Created by yuantao on 16/4/15. +// Copyright © 2016年 lovemo. All rights reserved. +// + +#import +#import "SMKViewModelProtocol.h" +#import "SMKViewMangerProtocol.h" + +@interface SMKAbstractMediator : NSObject + +/** + * viewModel + */ +@property (nonatomic, strong) NSObject *viewModel; + +/** + * viewManger + */ +@property (nonatomic, strong) NSObject *viewManger; + +/** + * 初始化 + */ +- (instancetype)initWithViewModel:(id)viewModel viewManger:(id)viewManger; + ++ (instancetype)mediatorWithViewModel:(id)viewModel viewManger:(id)viewManger; + +/** + * 将infos通知viewModel + */ +- (void)noticeViewModelWithInfos:(NSDictionary *)infos; + +/** + * 将infos通知viewMnager + */ +- (void)noticeViewMangerWithInfos:(NSDictionary *)infos; + +@end diff --git a/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKAbstractMediator.m b/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKAbstractMediator.m new file mode 100644 index 0000000..b268a26 --- /dev/null +++ b/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKAbstractMediator.m @@ -0,0 +1,34 @@ +// +// SMKAbstractMediator.m +// SUIMVVMDemo +// +// Created by yuantao on 16/4/15. +// Copyright © 2016年 lovemo. All rights reserved. +// + +#import "SMKAbstractMediator.h" +#import "NSObject+SMKRequest.h" + +@implementation SMKAbstractMediator + +- (instancetype)initWithViewModel:(id)viewModel viewManger:(id)viewManger { + if (self = [super init]) { + self.viewModel = (NSObject *)viewModel; + self.viewManger = (NSObject *)viewManger; + } + return self; +} + ++ (instancetype)mediatorWithViewModel:(id)viewModel viewManger:(id)viewManger { + return [[self alloc]initWithViewModel:viewModel viewManger:viewManger]; +} + +- (void)noticeViewModelWithInfos:(NSDictionary *)infos { + self.viewModel.smk_viewModelInfos = infos; +} + +- (void)noticeViewMangerWithInfos:(NSDictionary *)infos { + self.viewManger.smk_viewMangerInfos = infos; +} + +@end diff --git a/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKViewMangerProtocol.h b/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKViewMangerProtocol.h index 706e669..e911aae 100644 --- a/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKViewMangerProtocol.h +++ b/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKViewMangerProtocol.h @@ -9,6 +9,7 @@ #import + typedef void (^ViewEventsBlock)( ); /** @@ -17,11 +18,13 @@ typedef void (^ViewEventsBlock)( ); typedef void (^ViewModelInfosBlock)( ); - @protocol SMKViewMangerProtocol @optional + +- (void)smk_notice; + /** * 设置Controller的子视图的管理者为self * diff --git a/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKViewModelProtocol.h b/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKViewModelProtocol.h index 3036040..8566f84 100644 --- a/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKViewModelProtocol.h +++ b/SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKViewModelProtocol.h @@ -8,6 +8,7 @@ #import + /** * 请求成功block */ @@ -30,10 +31,13 @@ typedef void (^progressBlock)(NSProgress * progress); typedef void (^ViewMangerInfosBlock)( ); - @protocol SMKViewModelProtocol @optional + + +- (void)smk_notice; + /** * 返回指定viewModel的所引用的控制器 */