Skip to content

Commit

Permalink
add viewManger -->info<-- viewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
lovemo committed Apr 10, 2016
1 parent b9b9a02 commit e3ead60
Show file tree
Hide file tree
Showing 30 changed files with 430 additions and 41 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified SUIMVVMDemo/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,33 @@ - (void)viewDidLoad {
[super viewDidLoad];
self.title = @"MVVM Example";

// 将thirdView的事件处理者代理给thirdViewManger (代理方式)
[self.thirdView smk_viewWithViewManger:self.thirdViewManger];
self.thirdView.viewEventsBlock = [self.thirdViewManger smk_viewMangerWithEventBlockOfView:self.thirdView];

// self.thirdView.viewEventsBlock (block方式)
self.thirdView.viewEventsBlock = [self.thirdViewManger smk_viewMangerWithViewEventBlockOfInfos:@{@"view" : self.thirdView}];

// viewManger ----> <----- viewModel 之间通过代理方式交互
self.thirdViewManger.viewMangerDelegate = self.viewModel;
self.viewModel.viewModelDelegate = self.thirdViewManger;

// viewManger ----> <----- viewModel 之间通过block方式交互
self.thirdViewManger.viewMangerInfosBlock = [self.viewModel smk_viewModelWithViewMangerBlockOfInfos:@{@"info" : @"viewManger"}];
}

- (IBAction)clickBtnAction:(UIButton *)sender {

__weak typeof(self) weakSelf = self;
// __weak typeof(self) weakSelf = self;

[self.viewModel smk_viewModelWithProgress:nil success:^(id responseObject) {
[weakSelf.thirdViewManger smk_viewMangerWithModel:^NSDictionary *{
return @{@"model" : [weakSelf.viewModel getRandomData:responseObject]};
}];
} failure:nil];
// [self.viewModel smk_viewModelWithProgress:nil success:^(id responseObject) {

// thirdView 通过viewModel传递的model来配置view
[self.thirdView smk_configureViewWithViewModel:self.viewModel];

// [weakSelf.thirdViewManger smk_viewMangerWithModel:^NSDictionary *{
// return @{@"model" : [weakSelf.viewModel getRandomData:responseObject]};
// }];
// } failure:nil];

}

Expand Down
22 changes: 16 additions & 6 deletions SUIMVVMDemo/SUIMVVMDemo/Classes/Src/thirdExample/View/ThirdView.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "ThirdModel.h"
#import "UIView+SMKEvents.h"
#import "UIView+SMKConfigure.h"
#import "ThirdViewModel.h"

@interface ThirdView()
@property (weak, nonatomic) IBOutlet UILabel *testLabel;
Expand All @@ -28,18 +29,27 @@ - (IBAction)testBtnClick:(UIButton *)sender {
self.viewEventsBlock(@"btnClick");
}
}

// 按钮事件
- (IBAction)jumpOtherVC:(UIButton *)sender {
// 传递事件
if (self.delegate && [self.delegate respondsToSelector:@selector(smk_view:withEvents:)]) {
[self.delegate smk_view:self withEvents:@{@"jump": @"vc"}];
if (self.viewDelegate && [self.viewDelegate respondsToSelector:@selector(smk_view:withEvents:)]) {
[self.viewDelegate smk_view:self withEvents:@{@"jump": @"vc"}];
}
}

// 根据模型数据配置View
- (void)smk_configureViewWithModel:(id)model {
ThirdModel *thirdModel = (ThirdModel *)model;
self.testLabel.text = thirdModel.title;
//// 根据模型数据配置View
//- (void)smk_configureViewWithModel:(id)model {
// ThirdModel *thirdModel = (ThirdModel *)model;
// self.testLabel.text = thirdModel.title;
//}

- (void)smk_configureViewWithViewModel:(id<SMKViewModelProtocol>)viewModel {
[viewModel smk_viewModelWithModelBlcok:^(id model) {

ThirdModel *thirdModel = (ThirdModel *)model;
self.testLabel.text = thirdModel.title;
}];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<rect key="frame" x="20" y="24" width="201" height="71"/>
<color key="backgroundColor" red="1" green="0.76364387256469834" blue="0.76711821004815572" alpha="1" colorSpace="calibratedRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<state key="normal" title="点我显示改变后模型数据"/>
<state key="normal" title="点我监听事件改变"/>
<connections>
<action selector="testBtnClick:" destination="iN0-l3-epB" eventType="touchUpInside" id="y1L-A3-Buv"/>
</connections>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

#import <Foundation/Foundation.h>

@interface ThirdViewManger : NSObject<SMKViewMangerProtocol, SMKViewProtocol>
@interface ThirdViewManger : NSObject<SMKViewMangerProtocol, SMKViewProtocol, SMKViewModelProtocol>



@end
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@interface ThirdViewManger ()<SMKViewProtocol>

@property (nonatomic, strong) NSDictionary *dict;
//@property (nonatomic, strong) NSDictionary *dict;

@end

Expand All @@ -30,17 +30,32 @@ - (void)smk_view:(__kindof UIView *)view withEvents:(NSDictionary *)events {

}

- (ViewEventsBlock)smk_viewMangerWithEventBlockOfView:(__kindof UIView *)view {
- (ViewEventsBlock)smk_viewMangerWithViewEventBlockOfInfos:(NSDictionary *)infos {

return ^(NSString *info){
[view smk_configureViewWithModel:self.dict[@"model"]];

if (self.viewMangerInfosBlock) {
self.viewMangerInfosBlock();
}

if (self.viewMangerDelegate && [self.viewMangerDelegate respondsToSelector:@selector(smk_viewManger:withInfos:)]) {
[self.viewMangerDelegate smk_viewManger:self withInfos: @{@"info" : @"哈哈,你好ViewModel,我是viewManger,我被点击了"}];
}

// NSLog(@"%@",info);
// [view smk_configureViewWithModel:self.dict[@"model"]];
};
}

// 得到模型数据
- (void)smk_viewMangerWithModel:(NSDictionary *(^)( ))dictBlock {
if (dictBlock) {
self.dict = dictBlock();
}
- (void)smk_viewModel:(id)viewModel withInfos:(NSDictionary *)infos {
NSLog(@"%@",infos);
}

//// 得到模型数据
//- (void)smk_viewMangerWithModel:(NSDictionary *(^)( ))dictBlock {
// if (dictBlock) {
// self.dict = dictBlock();
// }
//}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#import "SUIMVVMKit.h"

@interface ThirdViewModel : NSObject<SMKViewModelProtocol>
@interface ThirdViewModel : NSObject<SMKViewModelProtocol, SMKViewMangerProtocol>

- (id)getRandomData:(NSArray *)array;
//- (id)getRandomData:(NSArray *)array;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,29 @@ - (id)getRandomData:(NSArray *)array {
u_int32_t index = arc4random_uniform((u_int32_t)10);
return array[index];
}

- (void)smk_viewModelWithModelBlcok:(void (^)(id))modelBlock {
[self smk_viewModelWithProgress:nil success:^(id responseObject) {
if (modelBlock) {

if (self.viewModelDelegate && [self.viewModelDelegate respondsToSelector:@selector(smk_viewModel:withInfos:)]) {
[self.viewModelDelegate smk_viewModel:self withInfos:@{@"info" : @"呵呵, 你好, 我是ViewModel,我加载数据成功了"}];
}

modelBlock([self getRandomData:responseObject]);
}
} failure:nil];

}

- (void)smk_viewManger:(id)viewManger withInfos:(NSDictionary *)infos {
NSLog(@"%@",infos);
}

- (ViewMangerInfosBlock)smk_viewModelWithViewMangerBlockOfInfos:(NSDictionary *)infos {
return ^{
NSLog(@"hello");
};
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,61 @@
//

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

NS_ASSUME_NONNULL_BEGIN

/**
* ViewModelBlock
*/
typedef _Nonnull id (^ViewModelBlock)( );
/**
* ViewMangerInfosBlock
*/
typedef void (^ViewMangerInfosBlock)( );
/**
* ViewModelInfosBlock
*/
typedef void (^ViewModelInfosBlock)( );




@interface NSObject (SMKProperties)

/**
* viewModelBlock
*/
@property (nonatomic, copy, nonnull) ViewModelBlock viewModelBlock;

/**
* 获取一个对象的所有属性
*/
- (nullable NSDictionary *)smk_allProperties;

/**
* viewMangerDelegate
*/
@property (nullable, nonatomic, weak) id<SMKViewMangerProtocol> viewMangerDelegate;

/**
* ViewMangerInfosBlock
*/
@property (nonatomic, copy) ViewMangerInfosBlock viewMangerInfosBlock;

/**
* viewModelDelegate
*/
@property (nullable, nonatomic, weak) id<SMKViewModelProtocol> viewModelDelegate;

/**
* ViewModelInfosBlock
*/
@property (nonatomic, copy) ViewModelInfosBlock viewModelInfosBlock;


@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,46 @@

@implementation NSObject (SMKProperties)

- (id<SMKViewModelProtocol>)viewModelDelegate {
return objc_getAssociatedObject(self, _cmd);
}

- (void)setViewModelDelegate:(id<SMKViewModelProtocol>)viewModelDelegate {
objc_setAssociatedObject(self, @selector(viewModelDelegate), viewModelDelegate, OBJC_ASSOCIATION_ASSIGN);
}

- (id<SMKViewMangerProtocol>)viewMangerDelegate {
return objc_getAssociatedObject(self, _cmd);
}

- (void)setViewMangerDelegate:(id<SMKViewMangerProtocol>)viewMangerDelegate {
objc_setAssociatedObject(self, @selector(viewMangerDelegate), viewMangerDelegate, OBJC_ASSOCIATION_ASSIGN);
}

- (ViewMangerInfosBlock)viewMangerInfosBlock {
return objc_getAssociatedObject(self, @selector(viewMangerInfosBlock));
}

- (void)setViewMangerInfosBlock:(ViewMangerInfosBlock)viewMangerInfosBlock {
objc_setAssociatedObject(self, @selector(viewMangerInfosBlock), viewMangerInfosBlock, OBJC_ASSOCIATION_COPY);
}

- (ViewModelInfosBlock)viewModelInfosBlock {
return objc_getAssociatedObject(self, @selector(viewModelInfosBlock));
}

- (void)setViewModelInfosBlock:(ViewModelInfosBlock)viewModelInfosBlock {
objc_setAssociatedObject(self, @selector(viewModelInfosBlock), viewModelInfosBlock, OBJC_ASSOCIATION_COPY);
}

- (ViewModelBlock)viewModelBlock {
return objc_getAssociatedObject(self, @selector(viewModelBlock));
}

- (void)setViewModelBlock:(ViewModelBlock)viewModelBlock {
objc_setAssociatedObject(self, @selector(viewModelBlock), viewModelBlock, OBJC_ASSOCIATION_COPY);
}

- (nullable NSDictionary *)smk_allProperties
{
unsigned int count = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,32 @@ typedef enum : NSUInteger {
* scheme (eg: http, https, ftp)
*/
@property (nonatomic, copy, nonnull) NSString *smk_scheme;

/**
* host
*/
@property (nonatomic, copy, nonnull) NSString *smk_host;

/**
* path
*/
@property (nonatomic, copy, nonnull) NSString *smk_path;

/**
* method
*/
@property (nonatomic, assign) SMKRequestMethod smk_method;

/**
* url (如果设置了url,则不需要在设置scheme,host,path 属性)
*/
@property (nonatomic, copy, nonnull) NSString *smk_url;

/**
* parameters
*/
@property (nonatomic, retain, nonnull) id smk_params;

/**
* fileConfig
*/
Expand Down
11 changes: 11 additions & 0 deletions SUIMVVMDemo/SUIMVVMDemo/SUIMVVMKit/SUIMVVMKit/SMKAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
* 请求成功block
*/
typedef void (^successBlock)(id responseObject);

/**
* 请求失败block
*/
typedef void (^failureBlock) (NSError *error);

/**
* 请求响应block
*/
typedef void (^responseBlock)(id dataObj, NSError *error);

/**
* 监听进度响应block
*/
Expand All @@ -35,14 +38,17 @@ typedef void (^progressBlock)(NSProgress * progress);
* 请求超时时间
*/
@property (nonatomic, assign) NSTimeInterval timeoutInterval;

/**
reachable
*/
@property (readonly, nonatomic, assign, getter = isReachable) BOOL reachable;

/**
reachableViaWWAN
*/
@property (readonly, nonatomic, assign, getter = isReachableViaWWAN) BOOL reachableViaWWAN;

/**
reachableViaWiFi
*/
Expand Down Expand Up @@ -90,6 +96,8 @@ typedef void (^progressBlock)(NSProgress * progress);
@end




/**
* 用来封装上文件数据的模型类
*/
Expand All @@ -99,14 +107,17 @@ typedef void (^progressBlock)(NSProgress * progress);
* 文件数据
*/
@property (nonatomic, strong) NSData *fileData;

/**
* 服务器接收参数名
*/
@property (nonatomic, copy) NSString *name;

/**
* 文件名
*/
@property (nonatomic, copy) NSString *fileName;

/**
* 文件类型
*/
Expand Down
Loading

0 comments on commit e3ead60

Please sign in to comment.