rac+mvvm开发

1.IGListKit使用
2.最快让你上手ReactiveCocoa之基础篇
3.最快让你上手ReactiveCocoa之进阶篇
4.ReactiveCocoa单向绑定与双向绑定
5.基本开发模式示例:

image.png

rac+mvvm开发_第1张图片
image.png
// MVVM:框架思想

/*
    框架思想: 把业务逻辑划分更清楚,是代码更加好维护
    本质: 就是把一个类的东西抽离到另外一个类中
 
    MVC:
    MVC S(业务类:manager)
 
    MVVM : 2015
 
    VM:处理界面业务(显示,事件)
 
    VIPER: 2014
    V:
 
    E:模型
 
    I:交互(事件)
 
    P:展示()
 
    R:路由(跳转)
 
    VIPER:用于大公司 维护成本高 
 
 */

ViewController

#import "HomeViewModel.h"
@interface ViewController ()
@property (nonatomic, strong) HomeViewModel *homeVM;
@end

@implementation ViewController
- (HomeViewModel *)homeVM
{
    if (_homeVM == nil) {
        _homeVM = [[HomeViewModel alloc] init];
    }
    return _homeVM;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // 请求首页数据
    @weakify(self);
    [[self.homeVM.loadHomeDataCommand execute:nil] subscribeNext:^(id   responseObject) {
        @strongify(self);
        // 刷新表格
        [self.tableView reloadData];
    } error:^(NSError * _Nullable error) {
        NSLog(@"%@",error);
    }];
    // 绑定视图模型,对控件的设置
    [self.homeVM bindViewModel:self.tableView];
}
@end

HomeViewModel

#import "HomeViewModel.h"
#import "HttpManager.h"
#import "NetworkManager.h"
#import "HomeRecommendItem.h"
#import "MJExtension.h"
#import "HomeRecommendCell.h"
#import "HomeCellViewModel.h"
static NSString * const ID = @"home";

@interface HomeViewModel ()
@property (nonatomic, strong) NSArray *recommandCellVMs;
@end

@implementation HomeViewModel

- (RACCommand *)loadHomeDataCommand
{
    if (_loadHomeDataCommand == nil) {
        
        // 请求数据
        _loadHomeDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {

            return [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
                
                // 发送请求
                NSString *url = [NetworkManager urlWithHome];
                NSDictionary *param = [NetworkManager paramWithHome];
                
                [HttpManager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
                    
                    // 数据处理
                    // 获取推荐课程(栅栏广告)
                    NSDictionary *result = responseObject[@"result"];
                    
                    NSDictionary *recommendsDict = result[@"recommends"];
                    NSArray *recommends;
                    
                    // 字段转模型
                    recommends =  [HomeRecommendItem mj_objectArrayWithKeyValuesArray: recommendsDict[@"courses"]];
                    
                    // 模型转视图模型
                    // 创建Cell视图模型
                   _recommandCellVMs = [[recommends.rac_sequence map:^id _Nullable(id  _Nullable value) {
                        // 创建Cell视图模型
                        HomeCellViewModel *cellVM = [[HomeCellViewModel alloc] init];
                        cellVM.item = value;
                        return cellVM;
                        
                   }] array];
                    
                    
                    // 请求成功
                    [subscriber sendNext:recommends];
                    [subscriber sendCompleted];
                    
                    
                } failure:^(NSURLSessionDataTask *task, NSError *error) {
                    [subscriber sendError:error];
                }];
                
                return nil;
            }];
            
        }];
        
    }
    
    return _loadHomeDataCommand;
}


#pragma mark - UITableView数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.recommandCellVMs.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 视图模型
    HomeCellViewModel *cellVm = self.recommandCellVMs[indexPath.row];
    
    // 绑定视图模型
    [cellVm bindViewModel:cell];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

#pragma mark - 视图模型绑定
- (void)bindViewModel:(UIView *)bindView
{
    UITableView *tableView = (UITableView *)bindView;
    // 注册
    [tableView registerNib:[UINib nibWithNibName:@"XMGHomeRecommendCell" bundle:nil] forCellReuseIdentifier:ID];
    
    tableView.dataSource = self;
    tableView.delegate = self;
}

@end

HomeCellViewModel

#import "HomeCellViewModel.h"
#import "HomeRecommendCell.h"
#import "UIImageView+WebCache.h"
#import "HomeRecommendItem.h"

@implementation HomeCellViewModel

- (void)bindViewModel:(id)bindView
{
    HomeRecommendCell *cell = (HomeRecommendItem *)bindView;
    
    [cell.iconView sd_setImageWithURL:[NSURL URLWithString:_item.courseImage]];
    cell.nameView.text = _item.courseName;
    [cell.numView setTitle:_item.studentNum forState:UIControlStateNormal];
}

@end

你可能感兴趣的:(rac+mvvm开发)