Skip to content

Commit

Permalink
Merge pull request 3lvis#378 from hyperoslo/improve/targets
Browse files Browse the repository at this point in the history
Improve targets
  • Loading branch information
3lvis committed May 26, 2015
2 parents 5c5ccb2 + 4933905 commit 57e5d57
Show file tree
Hide file tree
Showing 15 changed files with 375 additions and 138 deletions.
73 changes: 29 additions & 44 deletions Source/FORMData.m
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,16 @@ - (void)generateFormsWithJSON:(id)JSON
}
}


for (FORMField *field in group.fields) {

if (field.hidden){
[hideTargets addObject:[FORMTarget hideFieldTargetWithID:field.fieldID]];
}

if ([initialValues andy_valueForKey:field.fieldID]) {
id initialValue = [initialValues andy_valueForKey:field.fieldID];
if (initialValue) {
if (field.type == FORMFieldTypeSelect) {
for (FORMFieldValue *value in field.values) {

BOOL isInitialValue = ([value identifierIsEqualTo:[initialValues andy_valueForKey:field.fieldID]]);
if (isInitialValue) {
if ([value identifierIsEqualTo:initialValue]) {
field.value = value;
}
}
Expand All @@ -226,15 +223,10 @@ - (void)generateFormsWithJSON:(id)JSON
}

for (FORMFieldValue *fieldValue in field.values) {

id initialValue = [initialValues andy_valueForKey:field.fieldID];

BOOL fieldHasInitialValue = (initialValue != nil);
if (fieldHasInitialValue) {

BOOL fieldValueMatchesInitialValue = ([fieldValue identifierIsEqualTo:initialValue]);
if (fieldValueMatchesInitialValue) {

if ([fieldValue identifierIsEqualTo:initialValue]) {
for (FORMTarget *target in fieldValue.targets) {
if ([self evaluateCondition:target.condition]) {
if (target.actionType == FORMTargetActionHide) {
Expand Down Expand Up @@ -281,15 +273,11 @@ - (void)generateFormsWithJSON:(id)JSON

for (FORMTarget *target in hideTargets) {
if ([self evaluateCondition:target.condition]) {

if (target.type == FORMTargetTypeField) {

FORMField *field = [self fieldWithID:target.targetID
includingHiddenFields:YES];
[self.hiddenFieldsAndFieldIDsDictionary addEntriesFromDictionary:@{target.targetID : field}];

} else if (target.type == FORMTargetTypeSection) {

FORMSection *section = [self sectionWithID:target.targetID];
[self.hiddenSections addEntriesFromDictionary:@{target.targetID : section}];
}
Expand All @@ -299,7 +287,6 @@ - (void)generateFormsWithJSON:(id)JSON
for (FORMTarget *target in hideTargets) {
if ([self evaluateCondition:target.condition]) {
if (target.type == FORMTargetTypeField) {

FORMField *field = [self fieldWithID:target.targetID
includingHiddenFields:NO];
if (field) {
Expand All @@ -308,9 +295,7 @@ - (void)generateFormsWithJSON:(id)JSON
inGroups:self.groups];
[section resetFieldPositions];
}

} else if (target.type == FORMTargetTypeSection) {

FORMSection *section = [self sectionWithID:target.targetID];
if (section) {
FORMGroup *group = section.group;
Expand Down Expand Up @@ -830,71 +815,71 @@ - (NSArray *)updateTargets:(NSArray *)targets {
NSMutableArray *updatedIndexPaths = [NSMutableArray new];

for (FORMTarget *target in targets) {

BOOL shouldContinue = (![self evaluateCondition:target.condition] ||
target.type == FORMTargetTypeSection ||
(self.hiddenFieldsAndFieldIDsDictionary)[target.targetID]);

if (shouldContinue) {
continue;
}


__block FORMField *field = nil;
[self fieldWithID:target.targetID
includingHiddenFields:YES
completion:^(FORMField *foundField, NSIndexPath *indexPath) {
if (foundField) {
field = foundField;
if (indexPath) {
[updatedIndexPaths addObject:indexPath];
}
}
}];

[self fieldWithID:target.targetID includingHiddenFields:YES completion:^(FORMField *foundField, NSIndexPath *indexPath) {
if (foundField) {
field = foundField;
if (indexPath) {
[updatedIndexPaths addObject:indexPath];
if (field) {
NSArray *properties = [target propertiesToUpdate];
for (NSString *propertyName in properties) {
id value = [target valueForKey:propertyName];

if (![propertyName isEqualToString:@"value"]) {
[field setValue:value forKey:propertyName];
}
}
}];

if (field) {
if (target.targetValue) {

if (target.value) {
if (field.type == FORMFieldTypeSelect) {
FORMFieldValue *selectedFieldValue = [field selectFieldValueWithValueID:target.targetValue];

FORMFieldValue *selectedFieldValue = [field selectFieldValueWithValueID:target.value];
if (selectedFieldValue) {
(self.values)[field.fieldID] = selectedFieldValue.valueID;
field.value = selectedFieldValue;
}

} else {
field.value = target.targetValue;
field.value = target.value;
(self.values)[field.fieldID] = field.value;
}

} else if (target.actionType == FORMTargetActionClear) {
field.value = nil;
(self.values)[field.fieldID] = [NSNull null];
} else if (field.formula) {
}

if (field.formula) {
NSArray *fieldIDs = [field.formula hyp_variables];
NSMutableDictionary *values = [NSMutableDictionary new];

for (NSString *fieldID in fieldIDs) {

id value = (self.values)[fieldID];
BOOL isNumericField = (field.type == FORMFieldTypeFloat || field.type == FORMFieldTypeNumber);
NSString *defaultEmptyValue = (isNumericField) ? @"0" : @"";

FORMField *targetField = [self fieldWithID:fieldID includingHiddenFields:YES];

if (targetField.type == FORMFieldTypeSelect) {

if ([targetField.value isKindOfClass:[FORMFieldValue class]]) {

FORMFieldValue *fieldValue = targetField.value;

if (fieldValue.value) {
[values addEntriesFromDictionary:@{fieldID : fieldValue.value}];
}
} else {
FORMFieldValue *foundFieldValue = nil;
for (FORMFieldValue *fieldValue in field.values) {
if ([fieldValue identifierIsEqualTo:field.value]) {
for (FORMFieldValue *fieldValue in targetField.values) {
if ([fieldValue identifierIsEqualTo:targetField.value]) {
foundFieldValue = fieldValue;
}
}
Expand Down
16 changes: 5 additions & 11 deletions Source/Models/FORMField.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@class FORMFieldValidation;

#import "FORMValidator.h"
#import "FORMFieldBase.h"

typedef NS_ENUM(NSInteger, FORMFieldType) {
FORMFieldTypeText = 0,
Expand All @@ -19,26 +20,19 @@ typedef NS_ENUM(NSInteger, FORMFieldType) {
FORMFieldTypeCustom
};

@interface FORMField : NSObject
@interface FORMField : FORMFieldBase

@property (nonatomic) NSString *fieldID;
@property (nonatomic) NSString *title;
@property (nonatomic) NSString *info;
@property (nonatomic) BOOL hidden;
@property (nonatomic) CGSize size;
@property (nonatomic) NSNumber *position;
@property (nonatomic) id value;
@property (nonatomic) NSString *typeString;
@property (nonatomic) NSString *inputTypeString;
@property (nonatomic) FORMFieldType type;
@property (nonatomic) NSArray *values;
@property (nonatomic, getter=isDisabled) BOOL disabled;
@property (nonatomic) BOOL initiallyDisabled;
@property (nonatomic) NSDate *minimumDate;
@property (nonatomic) NSDate *maximumDate;
@property (nonatomic) BOOL hidden;
@property (nonatomic) NSNumber *position;
@property (nonatomic) CGSize size;

@property (nonatomic) FORMFieldValidation *validation;
@property (nonatomic) NSString *formula;
@property (nonatomic) NSArray *targets;

@property (nonatomic) FORMSection *section;
Expand Down
96 changes: 40 additions & 56 deletions Source/Models/FORMField.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,27 @@ - (instancetype)initWithDictionary:(NSDictionary *)dictionary
position:(NSInteger)position
disabled:(BOOL)disabled
disabledFieldsIDs:(NSArray *)disabledFieldsIDs {
self = [super init];
self = [super initWithDictionary:dictionary];
if (!self) return nil;

NSString *remoteID = [dictionary andy_valueForKey:@"id"];

_valid = YES;
_fieldID = remoteID;
_validationResultType = FORMValidationResultTypeValid;
_title = [dictionary andy_valueForKey:@"title"];
_typeString = [dictionary andy_valueForKey:@"type"];
_hidden = [[dictionary andy_valueForKey:@"hidden"] boolValue];
_type = [self typeFromTypeString:self.typeString];
_inputTypeString = [dictionary andy_valueForKey:@"input_type"];
_info = [dictionary andy_valueForKey:@"info"];
_hidden = [[dictionary andy_valueForKey:@"hidden"] boolValue];

NSNumber *width = [dictionary andy_valueForKey:@"size.width"] ?: @100;
NSNumber *height = [dictionary andy_valueForKey:@"size.height"]?: @1;
_size = CGSizeMake([width floatValue], [height floatValue]);
_position = @(position);

NSDictionary *validations = [dictionary andy_valueForKey:@"validations"];
if (validations && [validations count] > 0) {
_validation = [[FORMFieldValidation alloc]
initWithDictionary:[dictionary andy_valueForKey:@"validations"]];
}
self.position = @(position);

_disabled = [[dictionary andy_valueForKey:@"disabled"] boolValue];
_initiallyDisabled = _disabled;
_formula = [dictionary andy_valueForKey:@"formula"];

ISO8601DateFormatter *dateFormatter = [ISO8601DateFormatter new];

NSString *maximumDateString = [dictionary andy_valueForKey:@"maximum_date"];
if (maximumDateString) {
_maximumDate = [dateFormatter dateFromString:maximumDateString];
}

NSString *minimumDateString = [dictionary andy_valueForKey:@"minimum_date"];
if (minimumDateString) {
_minimumDate = [dateFormatter dateFromString:minimumDateString];
}

NSMutableArray *targets = [NSMutableArray new];

Expand Down Expand Up @@ -90,14 +70,13 @@ - (instancetype)initWithDictionary:(NSDictionary *)dictionary

_values = values;

_value = [dictionary andy_valueForKey:@"value"];

BOOL isDateType = (_type == FORMFieldTypeDate ||
_type == FORMFieldTypeDateTime ||
_type == FORMFieldTypeTime);

if (_value && isDateType) {
_value = [dateFormatter dateFromString:_value];
if (self.value && isDateType) {
ISO8601DateFormatter *dateFormatter = [ISO8601DateFormatter new];
self.value = [dateFormatter dateFromString:self.value];
}

return self;
Expand All @@ -108,39 +87,44 @@ - (instancetype)initWithDictionary:(NSDictionary *)dictionary
- (void)setValue:(id)fieldValue {
id resultValue = fieldValue;

switch (self.type) {
case FORMFieldTypeNumber:
case FORMFieldTypeFloat: {
if (![fieldValue isKindOfClass:[NSString class]]) {
resultValue = [fieldValue stringValue];
}
} break;
if ([fieldValue isKindOfClass:[FORMFieldValue class]]) {
FORMFieldValue *value = (FORMFieldValue *)fieldValue;
resultValue = value.valueID;
} else {
switch (self.type) {
case FORMFieldTypeNumber:
case FORMFieldTypeFloat: {
if (![fieldValue isKindOfClass:[NSString class]]) {
resultValue = [fieldValue stringValue];
}
} break;

case FORMFieldTypeDateTime:
case FORMFieldTypeTime:
case FORMFieldTypeDate: {
if ([fieldValue isKindOfClass:[NSString class]]) {
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'Z"];
resultValue = [formatter dateFromString:fieldValue];
}
} break;

case FORMFieldTypeText:
case FORMFieldTypeSelect:
case FORMFieldTypeButton:
case FORMFieldTypeCustom:
break;
}

case FORMFieldTypeDateTime:
case FORMFieldTypeTime:
case FORMFieldTypeDate: {
if ([fieldValue isKindOfClass:[NSString class]]) {
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'Z"];
resultValue = [formatter dateFromString:fieldValue];
if ([resultValue isKindOfClass:[NSString class]]) {
NSString *value = (NSString *)resultValue;
if (!value.length) {
resultValue = nil;
}
} break;

case FORMFieldTypeText:
case FORMFieldTypeSelect:
case FORMFieldTypeButton:
case FORMFieldTypeCustom:
break;
}

if ([resultValue isKindOfClass:[NSString class]]) {
NSString *value = (NSString *)resultValue;
if (!value.length) {
resultValue = nil;
}
}

_value = resultValue;
[super setValue:resultValue];
}

#pragma mark - Getters
Expand Down Expand Up @@ -344,7 +328,7 @@ - (NSNumber *)sectionPosition {
}

- (NSString *)description {
return [NSString stringWithFormat:@"\n — Field: %@\n title: %@\n info: %@\n size: %@\n position: %@\n fieldValue: %@\n type: %@\n values: %@\n disabled: %@\n initiallyDisabled: %@\n minimumDate: %@\n maximumDate: %@\n validations: %@\n formula: %@\n valid: %@\n sectionSeparator: %@\n",
return [NSString stringWithFormat:@"\n — Field: %@\n title: %@\n info: %@\n size: %@\n position: %@\n value: %@\n type: %@\n values: %@\n disabled: %@\n initiallyDisabled: %@\n minimumDate: %@\n maximumDate: %@\n validations: %@\n formula: %@\n valid: %@\n sectionSeparator: %@\n",
self.fieldID, self.title, self.info, NSStringFromCGSize(self.size), self.position,
self.value, self.typeString, self.values, (self.disabled) ? @"YES" : @"NO", (self.initiallyDisabled) ? @"YES" : @"NO", self.minimumDate,
self.maximumDate, self.validation, self.formula, (self.valid) ? @"YES" : @"NO", (self.sectionSeparator) ? @"YES" : @"NO"];
Expand Down
19 changes: 19 additions & 0 deletions Source/Models/FORMFieldBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@import UIKit;
@import Foundation;

@class FORMFieldValidation;

@interface FORMFieldBase : NSObject

@property (nonatomic) NSString *title;
@property (nonatomic) NSString *info;
@property (nonatomic) NSDate *minimumDate;
@property (nonatomic) NSDate *maximumDate;
@property (nonatomic) id value;

@property (nonatomic) FORMFieldValidation *validation;
@property (nonatomic) NSString *formula;

- (instancetype)initWithDictionary:(NSDictionary *)dictionary;

@end
Loading

0 comments on commit 57e5d57

Please sign in to comment.