Skip to content

Commit

Permalink
add mediator
Browse files Browse the repository at this point in the history
  • Loading branch information
lovemo committed Apr 15, 2016
1 parent 26d92e6 commit 0b02065
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 3 deletions.
Binary file modified SUIMVVMDemo/.DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions SUIMVVMDemo/SUIMVVMDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
34870A4B1CBA4B1200EDEF26 /* ThirdView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 34870A391CBA4B1200EDEF26 /* ThirdView.xib */; };
34870A4C1CBA4B1200EDEF26 /* ThirdViewManger.m in Sources */ = {isa = PBXBuildFile; fileRef = 34870A3C1CBA4B1200EDEF26 /* ThirdViewManger.m */; };
34870A4D1CBA4B1200EDEF26 /* ThirdViewModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 34870A3F1CBA4B1200EDEF26 /* ThirdViewModel.m */; };
3491C7291CC07922008A9312 /* SMKAbstractMediator.m in Sources */ = {isa = PBXBuildFile; fileRef = 3491C7281CC07922008A9312 /* SMKAbstractMediator.m */; };
34F688BC1CB7B09E00518803 /* NSObject+SMKProperties.m in Sources */ = {isa = PBXBuildFile; fileRef = 34F688A51CB7B09E00518803 /* NSObject+SMKProperties.m */; };
34F688BD1CB7B09E00518803 /* NSObject+SMKRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 34F688A71CB7B09E00518803 /* NSObject+SMKRequest.m */; };
34F688BE1CB7B09E00518803 /* SMKAction.m in Sources */ = {isa = PBXBuildFile; fileRef = 34F688A91CB7B09E00518803 /* SMKAction.m */; };
Expand Down Expand Up @@ -67,6 +68,8 @@
34870A3C1CBA4B1200EDEF26 /* ThirdViewManger.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ThirdViewManger.m; sourceTree = "<group>"; };
34870A3E1CBA4B1200EDEF26 /* ThirdViewModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ThirdViewModel.h; sourceTree = "<group>"; };
34870A3F1CBA4B1200EDEF26 /* ThirdViewModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ThirdViewModel.m; sourceTree = "<group>"; };
3491C7271CC07922008A9312 /* SMKAbstractMediator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SMKAbstractMediator.h; sourceTree = "<group>"; };
3491C7281CC07922008A9312 /* SMKAbstractMediator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SMKAbstractMediator.m; sourceTree = "<group>"; };
34F688A41CB7B09E00518803 /* NSObject+SMKProperties.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+SMKProperties.h"; sourceTree = "<group>"; };
34F688A51CB7B09E00518803 /* NSObject+SMKProperties.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+SMKProperties.m"; sourceTree = "<group>"; };
34F688A61CB7B09E00518803 /* NSObject+SMKRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+SMKRequest.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -283,6 +286,8 @@
34F688B81CB7B09E00518803 /* UIView+SMKConfigure.m */,
34F688B91CB7B09E00518803 /* UIView+SMKEvents.h */,
34F688BA1CB7B09E00518803 /* UIView+SMKEvents.m */,
3491C7271CC07922008A9312 /* SMKAbstractMediator.h */,
3491C7281CC07922008A9312 /* SMKAbstractMediator.m */,
);
path = SUIMVVMKit;
sourceTree = "<group>";
Expand Down Expand Up @@ -662,6 +667,7 @@
34F688BF1CB7B09E00518803 /* SMKStore.m in Sources */,
34F72F861C8BF6A90033ED03 /* AppDelegate.m in Sources */,
34870A4C1CBA4B1200EDEF26 /* ThirdViewManger.m in Sources */,
3491C7291CC07922008A9312 /* SMKAbstractMediator.m in Sources */,
34870A451CBA4B1200EDEF26 /* FirstTableViewModel.m in Sources */,
34870A431CBA4B1200EDEF26 /* FirstCell.m in Sources */,
34870A4A1CBA4B1200EDEF26 /* ThirdView.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "ThirdView.h"
#import "UIView+SMKEvents.h"
#import "UIView+SMKConfigure.h"
#import "SMKAbstractMediator.h"

@interface ThirdVC ()

Expand Down Expand Up @@ -62,6 +63,21 @@ - (void)viewDidLoad {

// viewManger ----> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ - (IBAction)jumpOtherVC:(UIButton *)sender {
//}

- (void)smk_configureViewWithViewModel:(id<SMKViewModelProtocol>)viewModel {

[viewModel smk_viewModelWithModelBlcok:^(id model) {

ThirdModel *thirdModel = (ThirdModel *)model;
self.testLabel.text = thirdModel.title;
}];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "FirstVC.h"
#import "UIView+SMKEvents.h"
#import "UIView+SMKConfigure.h"
#import "SMKAbstractMediator.h"

@interface ThirdViewManger ()<SMKViewProtocol>

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,10 @@ - (ViewMangerInfosBlock)smk_viewModelWithViewMangerBlockOfInfos:(NSDictionary *)
};
}

#pragma mark 中介者传值
- (void)smk_notice {
[self.smk_mediator noticeViewMangerWithInfos:self.smk_viewModelInfos];
}


@end
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "SMKViewProtocol.h"
#import "SMKViewModelProtocol.h"
#import "SMKViewMangerProtocol.h"
#import "SMKAbstractMediator.h"

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// SMKAbstractMediator.h
// SUIMVVMDemo
//
// Created by yuantao on 16/4/15.
// Copyright © 2016年 lovemo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SMKViewModelProtocol.h"
#import "SMKViewMangerProtocol.h"

@interface SMKAbstractMediator : NSObject

/**
* viewModel
*/
@property (nonatomic, strong) NSObject<SMKViewModelProtocol> *viewModel;

/**
* viewManger
*/
@property (nonatomic, strong) NSObject<SMKViewMangerProtocol> *viewManger;

/**
* 初始化
*/
- (instancetype)initWithViewModel:(id<SMKViewModelProtocol>)viewModel viewManger:(id<SMKViewMangerProtocol>)viewManger;

+ (instancetype)mediatorWithViewModel:(id<SMKViewModelProtocol>)viewModel viewManger:(id<SMKViewMangerProtocol>)viewManger;

/**
* 将infos通知viewModel
*/
- (void)noticeViewModelWithInfos:(NSDictionary *)infos;

/**
* 将infos通知viewMnager
*/
- (void)noticeViewMangerWithInfos:(NSDictionary *)infos;

@end
Original file line number Diff line number Diff line change
@@ -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<SMKViewModelProtocol>)viewModel viewManger:(id<SMKViewMangerProtocol>)viewManger {
if (self = [super init]) {
self.viewModel = (NSObject<SMKViewModelProtocol> *)viewModel;
self.viewManger = (NSObject<SMKViewMangerProtocol> *)viewManger;
}
return self;
}

+ (instancetype)mediatorWithViewModel:(id<SMKViewModelProtocol>)viewModel viewManger:(id<SMKViewMangerProtocol>)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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#import <UIKit/UIKit.h>


typedef void (^ViewEventsBlock)( );

/**
Expand All @@ -17,11 +18,13 @@ typedef void (^ViewEventsBlock)( );
typedef void (^ViewModelInfosBlock)( );



@protocol SMKViewMangerProtocol <NSObject>

@optional


- (void)smk_notice;

/**
* 设置Controller的子视图的管理者为self
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import <Foundation/Foundation.h>


/**
* 请求成功block
*/
Expand All @@ -30,10 +31,13 @@ typedef void (^progressBlock)(NSProgress * progress);
typedef void (^ViewMangerInfosBlock)( );



@protocol SMKViewModelProtocol <NSObject>

@optional


- (void)smk_notice;

/**
* 返回指定viewModel的所引用的控制器
*/
Expand Down

0 comments on commit 0b02065

Please sign in to comment.