美文网首页
RAC - 利用combineLatest 与 map 函数将输

RAC - 利用combineLatest 与 map 函数将输

作者: 俺不是大佬儿 | 来源:发表于2020-01-02 14:42 被阅读0次

RAC - 利用combineLatest 与 map 函数将输入信号合并判断登录按钮是否有效

应用场景:需要综合判断多个输入框输入内容是否全部满足条件,返回条件判断的结果
以登录场景为例,需要手机号 和 短信验证码都输入完成(输入框有内容)才能解锁登录按钮事件

左:登录按钮未解锁 右:登录按钮已解锁
#import "SMSLoginViewModel.h"
@interface SMSLoginViewModel ()

@property (strong, nonatomic, readwrite) RACSubject *getSMSCodeSub;
///登录参数合并信号
@property (strong, nonatomic) RACSignal *loginParamBigSignal;

@property (copy, nonatomic)NSString *phoneStr;
@property (copy, nonatomic)NSString *SMSCodeStr;

@end
@implementation SMSLoginViewModel

- (void)initialize {
    [super initialize];

    @weakify(self);
    [RACObserve(self, phoneStr) subscribeNext:^(id  _Nullable x) {
        NSLog(@"实时手机号输入:%@",x);
    }];
   
     //合并两个输入框的信号量,并使用map对信号量返回值进行映射使其返回bool值
    self.loginParamBigSignal = [[RACSignal combineLatest:@[RACObserve(self, phoneStr),RACObserve(self, SMSCodeStr)]] map:^id _Nullable(RACTuple * _Nullable value) {
        RACTupleUnpack(NSString *phone , NSString *SMSCode) = value;
        //@()将值变量装箱成对象
        return @([phone length]>0 && [SMSCode length]>0);
    }];
}

- (NSArray *)_createCellItems{
    LogoIconTabCellModel *logoIconItem = [[LogoIconTabCellModel alloc] initWithIcon:@""];
    
    LoginInputTabCellModel *phoneItem = [[LoginInputTabCellModel alloc] initWithIcon:@"phone_mark_icon" placeholder:@"请输入手机号" inputText:self.phoneStr secureTextEntry:NO];
    RACChannelTo(phoneItem, inputText) = RACChannelTo(self, phoneStr);
    
    LoginCustomTFTabCellModel *codeItem = [[LoginCustomTFTabCellModel alloc]initWithCustomUIType:2 icon:@"smsCode_mark_icon" placeholder:@"请输入验证码" inputText:self.SMSCodeStr];
    codeItem.getSMSCodeSub = self.getSMSCodeSub;
    RACChannelTo(codeItem, inputText) = RACChannelTo(self, SMSCodeStr);
    
    LoginSubmitTabCellModel *submitItem = [[LoginSubmitTabCellModel alloc] initWithLoginTypeTitle:@"账号密码登录" forgetPasdTitle:@"找回密码"];
    submitItem.operationSub = self.submitItemOperationSub;
   //监听 手机号、短信验证码输入是否满足条件,解锁登录按钮
    RAC(submitItem, loginParamValid) = self.loginParamBigSignal;
    
    ThirdLoginWayTabCellModel *thirdLoginItem = [[ThirdLoginWayTabCellModel alloc] init];

    return @[logoIconItem,phoneItem,codeItem,submitItem,thirdLoginItem];
}

后续将对 combineLatest 与 merge 函数进行详细讲解☺

相关文章

网友评论

      本文标题:RAC - 利用combineLatest 与 map 函数将输

      本文链接:https://www.haomeiwen.com/subject/wgqooctx.html