Skip to content
Permalink
Browse files

implement mvvm with EazyReact

  • Loading branch information...
HarrisonXi committed Aug 15, 2018
1 parent 9a8d478 commit 0feb1cbc35467fb1e75f3c10199d5987ec2cb573
Showing with 26 additions and 26 deletions.
  1. +5 −5 MvvmDemo/ViewController.m
  2. +3 −3 MvvmDemo/ViewModel.h
  3. +18 −18 MvvmDemo/ViewModel.m
@@ -22,12 +22,12 @@ - (void)viewDidLoad {

self.viewModel = [ViewModel new];
// bind input signals
RAC(self.viewModel, username) = self.usernameTextField.rac_textSignal;
RAC(self.viewModel, password) = self.passwordTextField.rac_textSignal;
EZR_PATH(self.viewModel, username) = EZR_PATH(self.usernameTextField, text);
EZR_PATH(self.viewModel, password) = EZR_PATH(self.passwordTextField, text);
// bind output signals
RAC(self.usernameTextField, backgroundColor) = ConvertInputStateToColor(RACObserve(self.viewModel, usernameInputState));
RAC(self.passwordTextField, backgroundColor) = ConvertInputStateToColor(RACObserve(self.viewModel, passwordInputState));
RAC(self.loginButton, enabled) = RACObserve(self.viewModel, loginEnabled);
EZR_PATH(self.usernameTextField, backgroundColor) = ConvertInputStateToColor(EZR_PATH(self.viewModel, usernameInputState));
EZR_PATH(self.passwordTextField, backgroundColor) = ConvertInputStateToColor(EZR_PATH(self.viewModel, passwordInputState));
EZR_PATH(self.loginButton, enabled) = EZR_PATH(self.viewModel, loginEnabled);
}

@end
@@ -8,19 +8,19 @@

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <ReactiveObjC/ReactiveObjC.h>
#import <EasyReact/EasyReact.h>

typedef enum : NSUInteger {
InputStateEmpty,
InputStateValid,
InputStateInvalid
} InputState;

#define ConvertInputStateToColor(signal) [InputStateToColorConverter convert:signal]
#define ConvertInputStateToColor(node) [InputStateToColorConverter convert:node]

@interface InputStateToColorConverter : NSObject

+ (RACSignal *)convert:(RACSignal *)signal;
+ (EZRMutableNode *)convert:(EZRNode *)node;

@end

@@ -14,9 +14,9 @@

@implementation InputStateToColorConverter

+ (RACSignal *)convert:(RACSignal *)signal
+ (EZRMutableNode *)convert:(EZRNode *)node
{
return [signal map:^id(NSNumber *inputStateNumber) {
return [node map:^id _Nullable(NSNumber * _Nullable inputStateNumber) {
InputState inputState = [inputStateNumber unsignedIntegerValue];
switch (inputState) {
case InputStateValid:
@@ -26,7 +26,7 @@ + (RACSignal *)convert:(RACSignal *)signal
default:
return WhiteBgColor;
}
}];
}].mutablify;
}

@end
@@ -44,7 +44,7 @@ @implementation ViewModel
- (instancetype)init
{
if (self = [super init]) {
RAC(self, usernameInputState) = [RACObserve(self, username) map:^id(NSString *username) {
EZR_PATH(self, usernameInputState) = [EZR_PATH(self, username) map:^id _Nullable(NSString * _Nullable username) {
if ([username length] >= 4 && [username length] <= 16) {
return @(InputStateValid);
} else {
@@ -54,9 +54,9 @@ - (instancetype)init
return @(InputStateInvalid);
}
}
}];
}].mutablify;

RAC(self, passwordInputState) = [RACObserve(self, password) map:^id(NSString *password) {
EZR_PATH(self, passwordInputState) = [EZR_PATH(self, password) map:^id _Nullable(NSString * _Nullable password) {
if ([password length] >= 8 && [password length] <= 16) {
return @(InputStateValid);
} else {
@@ -66,19 +66,19 @@ - (instancetype)init
return @(InputStateInvalid);
}
}
}];
}].mutablify;

RAC(self, loginEnabled) = [RACSignal combineLatest:@[RACObserve(self, usernameInputState),
RACObserve(self, passwordInputState)]
reduce:^id(NSNumber *usernameInputStateValue,
NSNumber *passwordInputStateValue){
if ([usernameInputStateValue unsignedIntegerValue] == InputStateValid &&
[passwordInputStateValue unsignedIntegerValue] == InputStateValid) {
return @(YES);
} else {
return @(NO);
}
}];
EZR_PATH(self, loginEnabled) = [EZRCombine(EZR_PATH(self, usernameInputState),
EZR_PATH(self, passwordInputState))
mapEach:^id _Nonnull(NSNumber * _Nonnull usernameInputStateValue,
NSNumber * _Nonnull passwordInputStateValue) {
if ([usernameInputStateValue unsignedIntegerValue] == InputStateValid &&
[passwordInputStateValue unsignedIntegerValue] == InputStateValid) {
return @(YES);
} else {
return @(NO);
}
}].mutablify;
}
return self;
}

0 comments on commit 0feb1cb

Please sign in to comment.
You can’t perform that action at this time.