From 3f29ff1c6cbf20d47a0a1781251482cfec2d4a0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Tue, 24 Mar 2015 01:20:56 +0100 Subject: [PATCH 01/15] Fixed --- Source/FORMDataSource.m | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Source/FORMDataSource.m b/Source/FORMDataSource.m index b0e5b2a5..eb6aa67a 100755 --- a/Source/FORMDataSource.m +++ b/Source/FORMDataSource.m @@ -877,8 +877,10 @@ - (void)updateSectionPosition:(FORMSection *)section NSInteger newPosition = [currentSection.position integerValue] - 1; currentSection.position = @(newPosition); - HYPParsedRelationship *parsedSection = [currentSection.sectionID hyp_parseRelationship]; - if (parsedSection.toMany) { + HYPParsedRelationship *parsedSection = [section.sectionID hyp_parseRelationship]; + HYPParsedRelationship *parsedCurrentSection = [currentSection.sectionID hyp_parseRelationship]; + if (parsedSection.toMany && + [parsedSection.relationship isEqualToString:parsedCurrentSection.relationship]) { NSInteger newRelationshipIndex = [parsedSection.index integerValue] - 1; currentSection.sectionID = [currentSection.sectionID hyp_updateRelationshipIndex:newRelationshipIndex]; From 50c692f350ae8581e13a409e6f322cf4c7adec85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Tue, 24 Mar 2015 08:51:39 +0100 Subject: [PATCH 02/15] Revert "Fixed" This reverts commit 3f29ff1c6cbf20d47a0a1781251482cfec2d4a0c. --- Source/FORMDataSource.m | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Source/FORMDataSource.m b/Source/FORMDataSource.m index eb6aa67a..b0e5b2a5 100755 --- a/Source/FORMDataSource.m +++ b/Source/FORMDataSource.m @@ -877,10 +877,8 @@ - (void)updateSectionPosition:(FORMSection *)section NSInteger newPosition = [currentSection.position integerValue] - 1; currentSection.position = @(newPosition); - HYPParsedRelationship *parsedSection = [section.sectionID hyp_parseRelationship]; - HYPParsedRelationship *parsedCurrentSection = [currentSection.sectionID hyp_parseRelationship]; - if (parsedSection.toMany && - [parsedSection.relationship isEqualToString:parsedCurrentSection.relationship]) { + HYPParsedRelationship *parsedSection = [currentSection.sectionID hyp_parseRelationship]; + if (parsedSection.toMany) { NSInteger newRelationshipIndex = [parsedSection.index integerValue] - 1; currentSection.sectionID = [currentSection.sectionID hyp_updateRelationshipIndex:newRelationshipIndex]; From 0e915d9ca79f01fe44f3ff722f233df788f3b220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Tue, 24 Mar 2015 16:52:11 +0100 Subject: [PATCH 03/15] Improve test --- Tests/Tests/FORMDataSourceTests.m | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Tests/Tests/FORMDataSourceTests.m b/Tests/Tests/FORMDataSourceTests.m index 4ee07d6f..d6ec3d40 100644 --- a/Tests/Tests/FORMDataSourceTests.m +++ b/Tests/Tests/FORMDataSourceTests.m @@ -437,6 +437,11 @@ - (void)testRemovingMultipleDynamicSectionsAddedByInitialValues FORMField *removeField = [dataSource fieldWithID:@"companies[0].remove" includingHiddenFields:YES]; [dataSource fieldCell:nil updatedWithField:removeField]; + + XCTAssertTrue(dataSource.values.count == 2); + XCTAssertEqualObjects(dataSource.values[@"companies[0].name"], @"Google"); + XCTAssertEqualObjects(dataSource.values[@"companies[0].phone_number"], @"4555666"); + XCTAssertTrue(dataSource.removedValues.count == 2); XCTAssertEqualObjects(dataSource.removedValues[@"companies[0].name"], @"Facebook"); XCTAssertEqualObjects(dataSource.removedValues[@"companies[0].phone_number"], @"1222333"); From 93fd85325bc694d42bb855deb26254bf46fc24e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Tue, 24 Mar 2015 17:42:49 +0100 Subject: [PATCH 04/15] Update test --- Tests/Tests/FORMDataSourceTests.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/Tests/FORMDataSourceTests.m b/Tests/Tests/FORMDataSourceTests.m index d6ec3d40..86eb8367 100644 --- a/Tests/Tests/FORMDataSourceTests.m +++ b/Tests/Tests/FORMDataSourceTests.m @@ -448,6 +448,8 @@ - (void)testRemovingMultipleDynamicSectionsAddedByInitialValues removeField = [dataSource fieldWithID:@"companies[0].remove" includingHiddenFields:YES]; [dataSource fieldCell:nil updatedWithField:removeField]; + + XCTAssertTrue(dataSource.values.count == 0); XCTAssertTrue(dataSource.removedValues.count == 4); XCTAssertEqualObjects(dataSource.removedValues[@"companies[1].name"], @"Google"); XCTAssertEqualObjects(dataSource.removedValues[@"companies[1].phone_number"], @"4555666"); From 0948ab61dcfa77bb5bc9c6b357893965f9aa7a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Tue, 24 Mar 2015 18:14:01 +0100 Subject: [PATCH 05/15] Update test --- Tests/Tests/FORMDataSourceTests.m | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Tests/Tests/FORMDataSourceTests.m b/Tests/Tests/FORMDataSourceTests.m index 86eb8367..4735a443 100644 --- a/Tests/Tests/FORMDataSourceTests.m +++ b/Tests/Tests/FORMDataSourceTests.m @@ -466,22 +466,32 @@ - (void)testUpdatedSectionPositionWhenRemovingDynamicSections values:nil disabled:YES]; + FORMSection *section = [dataSource sectionWithID:@"companies"]; + XCTAssertEqualObjects(section.position, @0); + + section = [dataSource sectionWithID:@"personal-details-0"]; + XCTAssertEqualObjects(section.position, @1); + FORMField *addField = [dataSource fieldWithID:@"companies.add" includingHiddenFields:NO]; XCTAssertNotNil(addField); [dataSource fieldCell:nil updatedWithField:addField]; - FORMField *removeField = [dataSource fieldWithID:@"companies[0].remove" includingHiddenFields:NO]; - XCTAssertNotNil(removeField); - - FORMSection *section = [dataSource sectionWithID:@"companies[0]"]; + section = [dataSource sectionWithID:@"companies[0]"]; XCTAssertNotNil(section); XCTAssertEqualObjects(section.position, @1); + section = [dataSource sectionWithID:@"personal-details-0"]; + XCTAssertEqualObjects(section.position, @2); + XCTAssertNotNil(section); + + FORMField *removeField = [dataSource fieldWithID:@"companies[0].remove" includingHiddenFields:NO]; + XCTAssertNotNil(removeField); + [dataSource fieldCell:nil updatedWithField:removeField]; - section = [dataSource sectionWithID:@"personal-details-1"]; - XCTAssertEqualObjects(section.position, @2); + section = [dataSource sectionWithID:@"personal-details-0"]; + XCTAssertEqualObjects(section.position, @1); XCTAssertNotNil(section); } From 0cecb9354a913ed1da85b3430a21012a6e627bb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Tue, 24 Mar 2015 18:14:23 +0100 Subject: [PATCH 06/15] Initial fix --- Source/FORMDataSource.m | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/Source/FORMDataSource.m b/Source/FORMDataSource.m index b0e5b2a5..9a5d02ca 100755 --- a/Source/FORMDataSource.m +++ b/Source/FORMDataSource.m @@ -537,20 +537,12 @@ - (void)fieldCell:(UICollectionViewCell *)fieldCell updatedWithField:(FORMField [self.formsManager.values removeObjectForKey:removedKey]; } - NSMutableArray *updatedKeys = [NSMutableArray new]; - [self.values enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { - if ([key hasPrefix:section.sectionID]) { - [updatedKeys addObject:key]; - } - }]; - - for (NSString *updatedKey in updatedKeys) { - [self.formsManager.values removeObjectForKey:updatedKey]; - } - NSDictionary *attributesJSON = [self.values hyp_JSONNestedAttributes]; + [self.formsManager.values removeAllObjects]; + NSArray *elements = [attributesJSON objectForKey:parsed.relationship]; NSInteger relationshipIndex = 0; + for (NSDictionary *element in elements) { for (NSString *key in element) { NSString *relationshipKey = [NSString stringWithFormat:@"%@[%ld].%@", parsed.relationship, (long)relationshipIndex, key]; @@ -559,14 +551,14 @@ - (void)fieldCell:(UICollectionViewCell *)fieldCell updatedWithField:(FORMField relationshipIndex++; } + [self updateSectionPosition:section]; + FORMGroup *group = section.form; [group.sections removeObject:section]; if (indexPaths) { [self.collectionView deleteItemsAtIndexPaths:indexPaths]; } - - [self updateSectionPosition:section]; }]; } } @@ -874,12 +866,14 @@ - (void)updateSectionPosition:(FORMSection *)section { for (FORMSection *currentSection in section.form.sections) { if ([currentSection.position integerValue] > [section.position integerValue]) { - NSInteger newPosition = [currentSection.position integerValue] - 1; + NSInteger newPosition = [section.position integerValue] - 1; currentSection.position = @(newPosition); - HYPParsedRelationship *parsedSection = [currentSection.sectionID hyp_parseRelationship]; - if (parsedSection.toMany) { - NSInteger newRelationshipIndex = [parsedSection.index integerValue] - 1; + HYPParsedRelationship *parsedSection = [section.sectionID hyp_parseRelationship]; + HYPParsedRelationship *parsedCurrentSection = [currentSection.sectionID hyp_parseRelationship]; + if (parsedSection.toMany && + [parsedSection.relationship isEqualToString:parsedCurrentSection.relationship]) { + NSInteger newRelationshipIndex = [parsedSection.index integerValue]; currentSection.sectionID = [currentSection.sectionID hyp_updateRelationshipIndex:newRelationshipIndex]; for (FORMField *field in currentSection.fields) { From 2d5a5fd07b55c8782d605b0a50a45c2dbb801d5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Tue, 24 Mar 2015 19:42:32 +0100 Subject: [PATCH 07/15] Add more tests --- Source/FORMDataSource.m | 24 +---- Source/Models/FORMGroup.h | 1 + Source/Models/FORMGroup.m | 24 +++++ Source/Models/FORMSection.m | 1 + Tests/Tests.xcodeproj/project.pbxproj | 14 +-- Tests/Tests/FORMGroupTests.m | 125 ++++++++++++++++++++++++++ 6 files changed, 161 insertions(+), 28 deletions(-) create mode 100644 Tests/Tests/FORMGroupTests.m diff --git a/Source/FORMDataSource.m b/Source/FORMDataSource.m index 9a5d02ca..17ab3747 100755 --- a/Source/FORMDataSource.m +++ b/Source/FORMDataSource.m @@ -551,7 +551,7 @@ - (void)fieldCell:(UICollectionViewCell *)fieldCell updatedWithField:(FORMField relationshipIndex++; } - [self updateSectionPosition:section]; + [section.form updateSectionsUsingRemovedSection:section]; FORMGroup *group = section.form; [group.sections removeObject:section]; @@ -862,28 +862,6 @@ - (NSDictionary *)removedValues #pragma mark - Private methods -- (void)updateSectionPosition:(FORMSection *)section -{ - for (FORMSection *currentSection in section.form.sections) { - if ([currentSection.position integerValue] > [section.position integerValue]) { - NSInteger newPosition = [section.position integerValue] - 1; - currentSection.position = @(newPosition); - - HYPParsedRelationship *parsedSection = [section.sectionID hyp_parseRelationship]; - HYPParsedRelationship *parsedCurrentSection = [currentSection.sectionID hyp_parseRelationship]; - if (parsedSection.toMany && - [parsedSection.relationship isEqualToString:parsedCurrentSection.relationship]) { - NSInteger newRelationshipIndex = [parsedSection.index integerValue]; - currentSection.sectionID = [currentSection.sectionID hyp_updateRelationshipIndex:newRelationshipIndex]; - - for (FORMField *field in currentSection.fields) { - field.fieldID = [field.fieldID hyp_updateRelationshipIndex:newRelationshipIndex]; - } - } - } - } -} - - (NSDictionary *)updateValueKeys:(NSArray *)currentKeys { NSArray *keys = [currentKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; diff --git a/Source/Models/FORMGroup.h b/Source/Models/FORMGroup.h index b61cf8da..9f717f54 100755 --- a/Source/Models/FORMGroup.h +++ b/Source/Models/FORMGroup.h @@ -23,5 +23,6 @@ - (NSInteger)numberOfFields:(NSMutableDictionary *)deletedSections; - (void)removeSection:(FORMSection *)section; - (void)resetSectionPositions; +- (void)updateSectionsUsingRemovedSection:(FORMSection *)removedSection; @end diff --git a/Source/Models/FORMGroup.m b/Source/Models/FORMGroup.m index c70bdf04..7a3efdee 100755 --- a/Source/Models/FORMGroup.m +++ b/Source/Models/FORMGroup.m @@ -6,10 +6,12 @@ #import "FORMTarget.h" #import "FORMClassFactory.h" #import "FORMValidator.h" +#import "HYPParsedRelationship.h" #import "NSString+HYPFormula.h" #import "NSDictionary+ANDYSafeValue.h" #import "NSJSONSerialization+ANDYJSONFile.h" +#import "NSString+HYPRelationshipParser.h" @interface FORMGroup () @@ -148,4 +150,26 @@ - (void)resetSectionPositions }]; } +- (void)updateSectionsUsingRemovedSection:(FORMSection *)removedSection +{ + for (FORMSection *currentSection in removedSection.form.sections) { + if ([currentSection.position integerValue] > [removedSection.position integerValue]) { + NSInteger newPosition = [removedSection.position integerValue] - 1; + currentSection.position = @(newPosition); + + HYPParsedRelationship *parsedSection = [removedSection.sectionID hyp_parseRelationship]; + HYPParsedRelationship *parsedCurrentSection = [currentSection.sectionID hyp_parseRelationship]; + if (parsedSection.toMany && + [parsedSection.relationship isEqualToString:parsedCurrentSection.relationship]) { + NSInteger newRelationshipIndex = [parsedSection.index integerValue]; + currentSection.sectionID = [currentSection.sectionID hyp_updateRelationshipIndex:newRelationshipIndex]; + + for (FORMField *field in currentSection.fields) { + field.fieldID = [field.fieldID hyp_updateRelationshipIndex:newRelationshipIndex]; + } + } + } + } +} + @end diff --git a/Source/Models/FORMSection.m b/Source/Models/FORMSection.m index df5b78a5..8a52d134 100755 --- a/Source/Models/FORMSection.m +++ b/Source/Models/FORMSection.m @@ -59,6 +59,7 @@ - (instancetype)initWithDictionary:(NSDictionary *)dictionary field.sectionSeparator = YES; field.position = @(fields.count); field.section = self; + field.fieldID = [NSString stringWithFormat:@"separator-%@", _sectionID]; [fields addObject:field]; } diff --git a/Tests/Tests.xcodeproj/project.pbxproj b/Tests/Tests.xcodeproj/project.pbxproj index 809ad262..314cce3a 100755 --- a/Tests/Tests.xcodeproj/project.pbxproj +++ b/Tests/Tests.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 1440AF0D1AC1D76500F15D15 /* FORMGroupTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1440AF0C1AC1D76500F15D15 /* FORMGroupTests.m */; }; 1449AE741AA07E0700BCC40E /* FORMField+Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1449AE731AA07E0700BCC40E /* FORMField+Tests.m */; }; 144A28BB1A99B2CF004A9086 /* FORMBaseFieldCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 144A28521A99B2CF004A9086 /* FORMBaseFieldCell.m */; }; 144A28BC1A99B2CF004A9086 /* FORMButtonFieldCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 144A28551A99B2CF004A9086 /* FORMButtonFieldCell.m */; }; @@ -82,6 +83,7 @@ /* Begin PBXFileReference section */ 0F976971F56B077A5DA2E58E /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = ""; }; + 1440AF0C1AC1D76500F15D15 /* FORMGroupTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FORMGroupTests.m; sourceTree = ""; }; 1449AE721AA07E0700BCC40E /* FORMField+Tests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "FORMField+Tests.h"; sourceTree = ""; }; 1449AE731AA07E0700BCC40E /* FORMField+Tests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "FORMField+Tests.m"; sourceTree = ""; }; 144A28511A99B2CF004A9086 /* FORMBaseFieldCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FORMBaseFieldCell.h; sourceTree = ""; }; @@ -430,17 +432,18 @@ 14C4182A1A01919C00636FD6 /* Tests */ = { isa = PBXGroup; children = ( - 149C290E1A9A23DD00F88B91 /* Helpers */, - 149C29111A9A23DD00F88B91 /* FORMFieldValueTests.m */, + 149C29131A9A23DD00F88B91 /* FORMDataSourceTests.m */, + 149C29151A9A23DD00F88B91 /* FORMDataTests.m */, 149C29121A9A23DD00F88B91 /* FORMFieldTests.m */, D5DFAA1A1AA87A9B0096FBFB /* FORMFieldValidationTests.m */, - 149C29131A9A23DD00F88B91 /* FORMDataSourceTests.m */, + 149C29111A9A23DD00F88B91 /* FORMFieldValueTests.m */, + 1440AF0C1AC1D76500F15D15 /* FORMGroupTests.m */, + 149C29181A9A23DD00F88B91 /* FORMPopoverFieldCellTests.m */, 149C29141A9A23DD00F88B91 /* FORMSectionTests.m */, - 149C29151A9A23DD00F88B91 /* FORMDataTests.m */, 149C29161A9A23DD00F88B91 /* FORMTargetTests.m */, 149C29171A9A23DD00F88B91 /* FORMTests.m */, - 149C29181A9A23DD00F88B91 /* FORMPopoverFieldCellTests.m */, 149C29191A9A23DD00F88B91 /* FORMTextFieldCellTests.m */, + 149C290E1A9A23DD00F88B91 /* Helpers */, 149C291A1A9A23DD00F88B91 /* JSONs */, 14C4182B1A01919C00636FD6 /* Supporting Files */, ); @@ -649,6 +652,7 @@ 144A28D41A99B2CF004A9086 /* FORMFloatInputValidator.m in Sources */, 144A28C31A99B2CF004A9086 /* FORMTextFieldCell.m in Sources */, 144A28CF1A99B2CF004A9086 /* FORMLayout.m in Sources */, + 1440AF0D1AC1D76500F15D15 /* FORMGroupTests.m in Sources */, 144A28E21A99B2CF004A9086 /* DDMathEvaluator+FORM.m in Sources */, 149C29271A9A23DD00F88B91 /* FORMTests.m in Sources */, 144A28CE1A99B2CF004A9086 /* FORMDataSource.m in Sources */, diff --git a/Tests/Tests/FORMGroupTests.m b/Tests/Tests/FORMGroupTests.m new file mode 100644 index 00000000..17204b11 --- /dev/null +++ b/Tests/Tests/FORMGroupTests.m @@ -0,0 +1,125 @@ +@import UIKit; +@import XCTest; + +#import "FORMGroup.h" +#import "FORMSection.h" + +@interface FORMGroupTests : XCTestCase + +@end + +@implementation FORMGroupTests + +- (void)testUpdateSectionsUsingRemovedSectionA +{ + NSArray *sections = @[@{@"id":@"companies", + @"type":@"dynamic", + @"action_title":@"ADD COMPANY ✛"}, + @{@"id":@"companies[0]", + @"fields":@[ + @{@"id":@"companies[0].name", + @"type":@"text", + @"size":@{ + @"width":@50, + @"height":@1 + }}, + @{@"id":@"companies[0].remove", + @"type":@"remove", + @"size":@{ + @"width":@20, + @"height":@1 + }} + ]}, + @{@"id":@"companies[1]", + @"fields":@[ + @{@"id":@"companies[1].name", + @"type":@"text", + @"size":@{ + @"width":@50, + @"height":@1 + }}, + @{@"id":@"companies[1].remove", + @"type":@"remove", + @"size":@{ + @"width":@20, + @"height":@1 + }} + ]}, + @{@"id":@"personal-details-0"}]; + + FORMGroup *group = [[FORMGroup alloc] initWithDictionary:@{@"id":@"personal-details", + @"title":@"Personal Details", + @"sections":sections} + position:0 + disabled:NO + disabledFieldsIDs:nil]; + + XCTAssertEqual(group.sections.count, 4); + NSArray *sectionPositions = @[@0, @1, @2, @3]; + NSArray *comparedSectionPositions = [group.sections valueForKey:@"position"]; + XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); + + FORMSection *section = group.sections[2]; + XCTAssertEqual(section.sectionID, @"companies[1]"); + NSArray *fieldIDs = [section.fields valueForKey:@"fieldID"]; + NSArray *comparedFieldIDs = @[@"companies[1].name", @"companies[1].remove", @"separator-companies[1]"]; + XCTAssertEqualObjects(fieldIDs, comparedFieldIDs); + + section = group.sections[1]; + [group updateSectionsUsingRemovedSection:section]; + + sectionPositions = @[@0, @1, @1, @2]; + comparedSectionPositions = [group.sections valueForKey:@"position"]; + XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); + + section = group.sections[2]; + XCTAssertEqual(section.sectionID, @"companies[0]"); + fieldIDs = [section.fields valueForKey:@"fieldID"]; + comparedFieldIDs = @[@"companies[0].name", @"companies[0].remove", @"separator-companies[0]"]; + XCTAssertEqualObjects(fieldIDs, comparedFieldIDs); + +} + +- (void)testUpdateSectionsUsingRemovedSectionB +{ + NSArray *sections = @[@{@"id":@"companies", + @"type":@"dynamic", + @"action_title":@"ADD COMPANY ✛"}, + @{@"id":@"companies[0]", + @"fields":@[ + @{@"id":@"companies[0].name", + @"type":@"text", + @"size":@{ + @"width":@50, + @"height":@1 + }}, + @{@"id":@"companies[0].remove", + @"type":@"remove", + @"size":@{ + @"width":@20, + @"height":@1 + }} + ]}, + @{@"id":@"personal-details-0"}]; + + FORMGroup *group = [[FORMGroup alloc] initWithDictionary:@{@"id":@"personal-details", + @"title":@"Personal Details", + @"sections":sections} + position:0 + disabled:NO + disabledFieldsIDs:nil]; + + XCTAssertEqual(group.sections.count, 3); + NSArray *sectionPositions = @[@0, @1, @2]; + NSArray *comparedSectionPositions = [group.sections valueForKey:@"position"]; + XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); + + FORMSection *section = group.sections[1]; + [group updateSectionsUsingRemovedSection:section]; + + sectionPositions = @[@0, @1, @1]; + comparedSectionPositions = [group.sections valueForKey:@"position"]; + XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); +} + +@end From 9fb07ace4da5f37f54ed54f1f1a58a29df73e445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Tue, 24 Mar 2015 20:03:30 +0100 Subject: [PATCH 08/15] Move removal logic to FORMData --- Source/FORMData.h | 3 +++ Source/FORMData.m | 49 +++++++++++++++++++++++++++++++++++++++++ Source/FORMDataSource.m | 40 ++------------------------------- 3 files changed, 54 insertions(+), 38 deletions(-) diff --git a/Source/FORMData.h b/Source/FORMData.h index 11fa0b2d..a781db84 100755 --- a/Source/FORMData.h +++ b/Source/FORMData.h @@ -39,6 +39,9 @@ - (void)fieldWithID:(NSString *)fieldID includingHiddenFields:(BOOL)includingHiddenFields completion:(void (^)(FORMField *field, NSIndexPath *indexPath))completion; +- (void)removeSection:(FORMSection *)removedSection + completion:(void (^)(NSArray *indexPaths))completion; + - (NSArray *)showTargets:(NSArray *)targets; - (NSArray *)hideTargets:(NSArray *)targets; - (NSArray *)updateTargets:(NSArray *)targets; diff --git a/Source/FORMData.m b/Source/FORMData.m index 79def295..7153375d 100755 --- a/Source/FORMData.m +++ b/Source/FORMData.m @@ -7,12 +7,15 @@ #import "FORMTarget.h" #import "DDMathParser.h" #import "FORMFieldValidation.h" +#import "HYPParsedRelationship.h" #import "NSString+HYPFormula.h" #import "NSDictionary+ANDYSafeValue.h" #import "NSString+HYPWordExtractor.h" #import "NSString+HYPContainsString.h" #import "DDMathEvaluator+FORM.h" +#import "NSString+HYPRelationshipParser.h" +#import "NSDictionary+HYPNestedAttributes.h" @interface FORMData () @@ -490,6 +493,52 @@ - (void)fieldWithID:(NSString *)fieldID includingHiddenFields:(BOOL)includingHid if (completion) completion(foundField, indexPath); } +- (void)removeSection:(FORMSection *)removedSection + completion:(void (^)(NSArray *indexPaths))completion +{ + NSMutableArray *removedKeys = [NSMutableArray new]; + [self.values enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { + if ([key hasPrefix:removedSection.sectionID]) { + [removedKeys addObject:key]; + } + }]; + + NSDictionary *removedAttributesJSON = [self.removedValues hyp_JSONNestedAttributes]; + HYPParsedRelationship *parsed = [removedSection.sectionID hyp_parseRelationship]; + NSArray *removedElements = [removedAttributesJSON objectForKey:parsed.relationship]; + NSInteger index = removedElements.count; + for (NSString *removedKey in removedKeys) { + NSString *newRemovedKey = [removedKey hyp_updateRelationshipIndex:index]; + [self.removedValues setValue:self.values[removedKey] forKey:newRemovedKey]; + [self.values removeObjectForKey:removedKey]; + } + + NSDictionary *attributesJSON = [self.values hyp_JSONNestedAttributes]; + [self.values removeAllObjects]; + + NSArray *elements = [attributesJSON objectForKey:parsed.relationship]; + NSInteger relationshipIndex = 0; + + for (NSDictionary *element in elements) { + for (NSString *key in element) { + NSString *relationshipKey = [NSString stringWithFormat:@"%@[%ld].%@", parsed.relationship, (long)relationshipIndex, key]; + self.values[relationshipKey] = element[key]; + } + relationshipIndex++; + } + + [removedSection.form updateSectionsUsingRemovedSection:removedSection]; + + FORMGroup *group = removedSection.form; + [group.sections removeObject:removedSection]; + + NSArray *removedIndexPaths = @[]; + + if (completion) { + completion(removedIndexPaths); + } +} + - (FORMField *)hiddenFieldWithFieldID:(NSString *)fieldID { NSArray *hiddenFields = [self.hiddenFieldsAndFieldIDsDictionary allValues]; diff --git a/Source/FORMDataSource.m b/Source/FORMDataSource.m index 17ab3747..f256167e 100755 --- a/Source/FORMDataSource.m +++ b/Source/FORMDataSource.m @@ -518,44 +518,8 @@ - (void)fieldCell:(UICollectionViewCell *)fieldCell updatedWithField:(FORMField HYPParsedRelationship *parsed = [field.fieldID hyp_parseRelationship]; parsed.attribute = nil; NSString *sectionID = [parsed key]; - [self.formsManager sectionWithID:sectionID completion:^(FORMSection *section, NSArray *indexPaths) { - - NSMutableArray *removedKeys = [NSMutableArray new]; - [self.values enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { - if ([key hasPrefix:section.sectionID]) { - [removedKeys addObject:key]; - } - }]; - - NSDictionary *removedAttributesJSON = [self.removedValues hyp_JSONNestedAttributes]; - HYPParsedRelationship *parsed = [section.sectionID hyp_parseRelationship]; - NSArray *removedElements = [removedAttributesJSON objectForKey:parsed.relationship]; - NSInteger index = removedElements.count; - for (NSString *removedKey in removedKeys) { - NSString *newRemovedKey = [removedKey hyp_updateRelationshipIndex:index]; - [self.formsManager.removedValues setValue:self.values[removedKey] forKey:newRemovedKey]; - [self.formsManager.values removeObjectForKey:removedKey]; - } - - NSDictionary *attributesJSON = [self.values hyp_JSONNestedAttributes]; - [self.formsManager.values removeAllObjects]; - - NSArray *elements = [attributesJSON objectForKey:parsed.relationship]; - NSInteger relationshipIndex = 0; - - for (NSDictionary *element in elements) { - for (NSString *key in element) { - NSString *relationshipKey = [NSString stringWithFormat:@"%@[%ld].%@", parsed.relationship, (long)relationshipIndex, key]; - self.formsManager.values[relationshipKey] = element[key]; - } - relationshipIndex++; - } - - [section.form updateSectionsUsingRemovedSection:section]; - - FORMGroup *group = section.form; - [group.sections removeObject:section]; - + FORMSection *section = [self.formsManager sectionWithID:sectionID]; + [self.formsManager removeSection:section completion:^(NSArray *indexPaths) { if (indexPaths) { [self.collectionView deleteItemsAtIndexPaths:indexPaths]; } From 8a70e670207a0ebbf6cc2410303f71555bcf4587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Tue, 24 Mar 2015 21:54:09 +0100 Subject: [PATCH 09/15] Add more tests --- Source/FORMData.h | 3 +- Source/FORMData.m | 26 ++++--- Source/FORMDataSource.m | 5 +- Source/Models/FORMGroup.h | 1 - Source/Models/FORMGroup.m | 22 ------ Tests/Tests/FORMDataSourceTests.m | 4 +- Tests/Tests/FORMDataTests.m | 88 +++++++++++++++++++++++ Tests/Tests/FORMGroupTests.m | 112 ------------------------------ 8 files changed, 112 insertions(+), 149 deletions(-) diff --git a/Source/FORMData.h b/Source/FORMData.h index a781db84..75414577 100755 --- a/Source/FORMData.h +++ b/Source/FORMData.h @@ -39,8 +39,7 @@ - (void)fieldWithID:(NSString *)fieldID includingHiddenFields:(BOOL)includingHiddenFields completion:(void (^)(FORMField *field, NSIndexPath *indexPath))completion; -- (void)removeSection:(FORMSection *)removedSection - completion:(void (^)(NSArray *indexPaths))completion; +- (void)removeSection:(FORMSection *)removedSection; - (NSArray *)showTargets:(NSArray *)targets; - (NSArray *)hideTargets:(NSArray *)targets; diff --git a/Source/FORMData.m b/Source/FORMData.m index 7153375d..cc01bd04 100755 --- a/Source/FORMData.m +++ b/Source/FORMData.m @@ -494,7 +494,6 @@ - (void)fieldWithID:(NSString *)fieldID includingHiddenFields:(BOOL)includingHid } - (void)removeSection:(FORMSection *)removedSection - completion:(void (^)(NSArray *indexPaths))completion { NSMutableArray *removedKeys = [NSMutableArray new]; [self.values enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { @@ -527,16 +526,27 @@ - (void)removeSection:(FORMSection *)removedSection relationshipIndex++; } - [removedSection.form updateSectionsUsingRemovedSection:removedSection]; + for (FORMSection *currentSection in removedSection.form.sections) { + if ([currentSection.position integerValue] > [removedSection.position integerValue]) { + NSInteger newPosition = [removedSection.position integerValue] - 1; + currentSection.position = @(newPosition); - FORMGroup *group = removedSection.form; - [group.sections removeObject:removedSection]; - - NSArray *removedIndexPaths = @[]; + HYPParsedRelationship *parsedSection = [removedSection.sectionID hyp_parseRelationship]; + HYPParsedRelationship *parsedCurrentSection = [currentSection.sectionID hyp_parseRelationship]; + if (parsedSection.toMany && + [parsedSection.relationship isEqualToString:parsedCurrentSection.relationship]) { + NSInteger newRelationshipIndex = [parsedSection.index integerValue]; + currentSection.sectionID = [currentSection.sectionID hyp_updateRelationshipIndex:newRelationshipIndex]; - if (completion) { - completion(removedIndexPaths); + for (FORMField *field in currentSection.fields) { + field.fieldID = [field.fieldID hyp_updateRelationshipIndex:newRelationshipIndex]; + } + } + } } + + FORMGroup *group = removedSection.form; + [group.sections removeObject:removedSection]; } - (FORMField *)hiddenFieldWithFieldID:(NSString *)fieldID diff --git a/Source/FORMDataSource.m b/Source/FORMDataSource.m index f256167e..88388c38 100755 --- a/Source/FORMDataSource.m +++ b/Source/FORMDataSource.m @@ -518,8 +518,9 @@ - (void)fieldCell:(UICollectionViewCell *)fieldCell updatedWithField:(FORMField HYPParsedRelationship *parsed = [field.fieldID hyp_parseRelationship]; parsed.attribute = nil; NSString *sectionID = [parsed key]; - FORMSection *section = [self.formsManager sectionWithID:sectionID]; - [self.formsManager removeSection:section completion:^(NSArray *indexPaths) { + [self.formsManager sectionWithID:sectionID completion:^(FORMSection *section, NSArray *indexPaths) { + [self.formsManager removeSection:section]; + if (indexPaths) { [self.collectionView deleteItemsAtIndexPaths:indexPaths]; } diff --git a/Source/Models/FORMGroup.h b/Source/Models/FORMGroup.h index 9f717f54..b61cf8da 100755 --- a/Source/Models/FORMGroup.h +++ b/Source/Models/FORMGroup.h @@ -23,6 +23,5 @@ - (NSInteger)numberOfFields:(NSMutableDictionary *)deletedSections; - (void)removeSection:(FORMSection *)section; - (void)resetSectionPositions; -- (void)updateSectionsUsingRemovedSection:(FORMSection *)removedSection; @end diff --git a/Source/Models/FORMGroup.m b/Source/Models/FORMGroup.m index 7a3efdee..c70ed9f6 100755 --- a/Source/Models/FORMGroup.m +++ b/Source/Models/FORMGroup.m @@ -150,26 +150,4 @@ - (void)resetSectionPositions }]; } -- (void)updateSectionsUsingRemovedSection:(FORMSection *)removedSection -{ - for (FORMSection *currentSection in removedSection.form.sections) { - if ([currentSection.position integerValue] > [removedSection.position integerValue]) { - NSInteger newPosition = [removedSection.position integerValue] - 1; - currentSection.position = @(newPosition); - - HYPParsedRelationship *parsedSection = [removedSection.sectionID hyp_parseRelationship]; - HYPParsedRelationship *parsedCurrentSection = [currentSection.sectionID hyp_parseRelationship]; - if (parsedSection.toMany && - [parsedSection.relationship isEqualToString:parsedCurrentSection.relationship]) { - NSInteger newRelationshipIndex = [parsedSection.index integerValue]; - currentSection.sectionID = [currentSection.sectionID hyp_updateRelationshipIndex:newRelationshipIndex]; - - for (FORMField *field in currentSection.fields) { - field.fieldID = [field.fieldID hyp_updateRelationshipIndex:newRelationshipIndex]; - } - } - } - } -} - @end diff --git a/Tests/Tests/FORMDataSourceTests.m b/Tests/Tests/FORMDataSourceTests.m index 4735a443..4d47226d 100644 --- a/Tests/Tests/FORMDataSourceTests.m +++ b/Tests/Tests/FORMDataSourceTests.m @@ -513,7 +513,7 @@ - (void)testUpdatedFieldPositionWhenHidingAndShowingField [fieldPositions addObject:field.position]; } - NSArray *expectedInitialPositions = @[@0,@1,@2,@3,@4,@5]; + NSArray *expectedInitialPositions = @[@0, @1, @2, @3, @4, @5]; XCTAssertEqualObjects(fieldPositions, expectedInitialPositions); [dataSource hideTargets:@[[FORMTarget hideFieldTargetWithID:@"section-0-field-2"]]]; @@ -523,7 +523,7 @@ - (void)testUpdatedFieldPositionWhenHidingAndShowingField [fieldPositions addObject:field.position]; } - NSArray *expectedUpdatedPositions = @[@0,@1,@2,@3,@4]; + NSArray *expectedUpdatedPositions = @[@0, @1, @2, @3, @4]; XCTAssertEqualObjects(fieldPositions, expectedUpdatedPositions); [dataSource showTargets:@[[FORMTarget showFieldTargetWithID:@"section-0-field-2"]]]; diff --git a/Tests/Tests/FORMDataTests.m b/Tests/Tests/FORMDataTests.m index 39b9cc80..d9a4eba0 100755 --- a/Tests/Tests/FORMDataTests.m +++ b/Tests/Tests/FORMDataTests.m @@ -369,4 +369,92 @@ - (void)testDynamicWithInitialValues XCTAssertNil(field.value); } +- (void)testRemoveDynamicSectionA +{ + NSArray *JSON = [NSJSONSerialization JSONObjectWithContentsOfFile:@"dynamic.json" + inBundle:[NSBundle bundleForClass:[self class]]]; + + FORMData *formData = [[FORMData alloc] initWithJSON:JSON + initialValues:@{@"companies[0].name" : @"Facebook", + @"companies[0].phone_number" : @"1222333"} + disabledFieldIDs:nil + disabled:NO]; + + XCTAssertEqual(formData.values.count, 2); + XCTAssertEqual(formData.removedValues.count, 0); + XCTAssertEqualObjects(formData.values[@"companies[0].name"], @"Facebook"); + XCTAssertEqualObjects(formData.values[@"companies[0].phone_number"], @"1222333"); + + FORMSection *section = [formData sectionWithID:@"companies[0]"]; + [formData removeSection:section]; + XCTAssertEqual(formData.values.count, 0); + XCTAssertEqual(formData.removedValues.count, 2); + XCTAssertEqualObjects(formData.removedValues[@"companies[0].name"], @"Facebook"); + XCTAssertEqualObjects(formData.removedValues[@"companies[0].phone_number"], @"1222333"); +} + +- (void)testRemoveDynamicSectionB +{ + NSArray *JSON = [NSJSONSerialization JSONObjectWithContentsOfFile:@"dynamic.json" + inBundle:[NSBundle bundleForClass:[self class]]]; + + FORMData *formData = [[FORMData alloc] initWithJSON:JSON + initialValues:@{@"companies[0].name" : @"Facebook", + @"companies[0].phone_number" : @"1222333", + @"companies[1].name" : @"Apple", + @"companies[1].phone_number" : @"4444555"} + disabledFieldIDs:nil + disabled:NO]; + + FORMGroup *group = formData.forms[0]; + XCTAssertEqual(group.sections.count, 6); + NSArray *sectionPositions = @[@0, @1, @2, @3, @4, @5]; + NSArray *comparedSectionPositions = [group.sections valueForKey:@"position"]; + XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); + + FORMSection *section = group.sections[1]; + XCTAssertEqualObjects(section.sectionID, @"companies[0]"); + NSArray *fieldIDs = [section.fields valueForKey:@"fieldID"]; + NSArray *comparedFieldIDs = @[@"companies[0].name", @"companies[0].phone_number", @"companies[0].remove"]; + XCTAssertEqualObjects(fieldIDs, comparedFieldIDs); + + [formData removeSection:section]; + + XCTAssertEqual(group.sections.count, 5); + sectionPositions = @[@0, @1, @2, @3, @4]; + comparedSectionPositions = [group.sections valueForKey:@"position"]; + XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); + + section = group.sections[1]; + XCTAssertEqualObjects(section.sectionID, @"companies[0]"); + fieldIDs = [section.fields valueForKey:@"fieldID"]; + comparedFieldIDs = @[@"companies[0].name", @"companies[0].phone_number", @"companies[0].remove"]; + XCTAssertEqualObjects(fieldIDs, comparedFieldIDs); +} + +- (void)testRemoveDynamicSectionC +{ + NSArray *JSON = [NSJSONSerialization JSONObjectWithContentsOfFile:@"dynamic.json" + inBundle:[NSBundle bundleForClass:[self class]]]; + + FORMData *formData = [[FORMData alloc] initWithJSON:JSON + initialValues:@{@"companies[0].name" : @"Facebook", + @"companies[0].phone_number" : @"1222333"} + disabledFieldIDs:nil + disabled:NO]; + + FORMGroup *group = formData.forms[0]; + XCTAssertEqual(group.sections.count, 3); + NSArray *sectionPositions = @[@0, @1, @2]; + NSArray *comparedSectionPositions = [group.sections valueForKey:@"position"]; + XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); + + FORMSection *section = group.sections[1]; + [formData removeSection:section]; + + sectionPositions = @[@0, @1, @1]; + comparedSectionPositions = [group.sections valueForKey:@"position"]; + XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); +} + @end diff --git a/Tests/Tests/FORMGroupTests.m b/Tests/Tests/FORMGroupTests.m index 17204b11..7e63c6c3 100644 --- a/Tests/Tests/FORMGroupTests.m +++ b/Tests/Tests/FORMGroupTests.m @@ -10,116 +10,4 @@ @interface FORMGroupTests : XCTestCase @implementation FORMGroupTests -- (void)testUpdateSectionsUsingRemovedSectionA -{ - NSArray *sections = @[@{@"id":@"companies", - @"type":@"dynamic", - @"action_title":@"ADD COMPANY ✛"}, - @{@"id":@"companies[0]", - @"fields":@[ - @{@"id":@"companies[0].name", - @"type":@"text", - @"size":@{ - @"width":@50, - @"height":@1 - }}, - @{@"id":@"companies[0].remove", - @"type":@"remove", - @"size":@{ - @"width":@20, - @"height":@1 - }} - ]}, - @{@"id":@"companies[1]", - @"fields":@[ - @{@"id":@"companies[1].name", - @"type":@"text", - @"size":@{ - @"width":@50, - @"height":@1 - }}, - @{@"id":@"companies[1].remove", - @"type":@"remove", - @"size":@{ - @"width":@20, - @"height":@1 - }} - ]}, - @{@"id":@"personal-details-0"}]; - - FORMGroup *group = [[FORMGroup alloc] initWithDictionary:@{@"id":@"personal-details", - @"title":@"Personal Details", - @"sections":sections} - position:0 - disabled:NO - disabledFieldsIDs:nil]; - - XCTAssertEqual(group.sections.count, 4); - NSArray *sectionPositions = @[@0, @1, @2, @3]; - NSArray *comparedSectionPositions = [group.sections valueForKey:@"position"]; - XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); - - FORMSection *section = group.sections[2]; - XCTAssertEqual(section.sectionID, @"companies[1]"); - NSArray *fieldIDs = [section.fields valueForKey:@"fieldID"]; - NSArray *comparedFieldIDs = @[@"companies[1].name", @"companies[1].remove", @"separator-companies[1]"]; - XCTAssertEqualObjects(fieldIDs, comparedFieldIDs); - - section = group.sections[1]; - [group updateSectionsUsingRemovedSection:section]; - - sectionPositions = @[@0, @1, @1, @2]; - comparedSectionPositions = [group.sections valueForKey:@"position"]; - XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); - - section = group.sections[2]; - XCTAssertEqual(section.sectionID, @"companies[0]"); - fieldIDs = [section.fields valueForKey:@"fieldID"]; - comparedFieldIDs = @[@"companies[0].name", @"companies[0].remove", @"separator-companies[0]"]; - XCTAssertEqualObjects(fieldIDs, comparedFieldIDs); - -} - -- (void)testUpdateSectionsUsingRemovedSectionB -{ - NSArray *sections = @[@{@"id":@"companies", - @"type":@"dynamic", - @"action_title":@"ADD COMPANY ✛"}, - @{@"id":@"companies[0]", - @"fields":@[ - @{@"id":@"companies[0].name", - @"type":@"text", - @"size":@{ - @"width":@50, - @"height":@1 - }}, - @{@"id":@"companies[0].remove", - @"type":@"remove", - @"size":@{ - @"width":@20, - @"height":@1 - }} - ]}, - @{@"id":@"personal-details-0"}]; - - FORMGroup *group = [[FORMGroup alloc] initWithDictionary:@{@"id":@"personal-details", - @"title":@"Personal Details", - @"sections":sections} - position:0 - disabled:NO - disabledFieldsIDs:nil]; - - XCTAssertEqual(group.sections.count, 3); - NSArray *sectionPositions = @[@0, @1, @2]; - NSArray *comparedSectionPositions = [group.sections valueForKey:@"position"]; - XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); - - FORMSection *section = group.sections[1]; - [group updateSectionsUsingRemovedSection:section]; - - sectionPositions = @[@0, @1, @1]; - comparedSectionPositions = [group.sections valueForKey:@"position"]; - XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); -} - @end From f6c7076b12796a2d899244a46ce06117d98468e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Wed, 25 Mar 2015 07:57:08 +0100 Subject: [PATCH 10/15] Improve removeSection --- Source/FORMData.m | 40 ++++++++++++++++++++----------------- Tests/Tests/FORMDataTests.m | 11 +++++++--- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/Source/FORMData.m b/Source/FORMData.m index cc01bd04..d4604743 100755 --- a/Source/FORMData.m +++ b/Source/FORMData.m @@ -526,27 +526,31 @@ - (void)removeSection:(FORMSection *)removedSection relationshipIndex++; } - for (FORMSection *currentSection in removedSection.form.sections) { - if ([currentSection.position integerValue] > [removedSection.position integerValue]) { - NSInteger newPosition = [removedSection.position integerValue] - 1; - currentSection.position = @(newPosition); - - HYPParsedRelationship *parsedSection = [removedSection.sectionID hyp_parseRelationship]; - HYPParsedRelationship *parsedCurrentSection = [currentSection.sectionID hyp_parseRelationship]; - if (parsedSection.toMany && - [parsedSection.relationship isEqualToString:parsedCurrentSection.relationship]) { - NSInteger newRelationshipIndex = [parsedSection.index integerValue]; - currentSection.sectionID = [currentSection.sectionID hyp_updateRelationshipIndex:newRelationshipIndex]; - - for (FORMField *field in currentSection.fields) { - field.fieldID = [field.fieldID hyp_updateRelationshipIndex:newRelationshipIndex]; - } + NSString *sectionID = removedSection.sectionID; + FORMGroup *form = removedSection.form; + [form.sections removeObject:removedSection]; + relationshipIndex = 0; + + NSInteger position = 0; + for (FORMSection *currentSection in form.sections) { + currentSection.position = @(position); + position++; + + HYPParsedRelationship *parsedSection = [sectionID hyp_parseRelationship]; + HYPParsedRelationship *parsedCurrentSection = [currentSection.sectionID hyp_parseRelationship]; + if (parsedSection.toMany && + [parsedSection.relationship isEqualToString:parsedCurrentSection.relationship]) { + NSInteger newRelationshipIndex = relationshipIndex; + currentSection.sectionID = [currentSection.sectionID hyp_updateRelationshipIndex:newRelationshipIndex]; + + for (FORMField *field in currentSection.fields) { + field.fieldID = [field.fieldID hyp_updateRelationshipIndex:newRelationshipIndex]; } + relationshipIndex++; + } else { + relationshipIndex = 0; } } - - FORMGroup *group = removedSection.form; - [group.sections removeObject:removedSection]; } - (FORMField *)hiddenFieldWithFieldID:(NSString *)fieldID diff --git a/Tests/Tests/FORMDataTests.m b/Tests/Tests/FORMDataTests.m index d9a4eba0..96b2983c 100755 --- a/Tests/Tests/FORMDataTests.m +++ b/Tests/Tests/FORMDataTests.m @@ -444,17 +444,22 @@ - (void)testRemoveDynamicSectionC disabled:NO]; FORMGroup *group = formData.forms[0]; - XCTAssertEqual(group.sections.count, 3); - NSArray *sectionPositions = @[@0, @1, @2]; + XCTAssertEqual(group.sections.count, 5); + NSArray *sectionPositions = @[@0, @1, @2, @3, @4]; NSArray *comparedSectionPositions = [group.sections valueForKey:@"position"]; XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); FORMSection *section = group.sections[1]; + XCTAssertEqualObjects(section.sectionID, @"companies[0]"); + [formData removeSection:section]; - sectionPositions = @[@0, @1, @1]; + sectionPositions = @[@0, @1, @2, @3]; comparedSectionPositions = [group.sections valueForKey:@"position"]; XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); + + section = group.sections[1]; + XCTAssertEqualObjects(section.sectionID, @"personal-details-0"); } @end From ead0085694c003882f992228b743103817dda242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Wed, 25 Mar 2015 08:27:32 +0100 Subject: [PATCH 11/15] Improve tests --- Tests/Tests/FORMDataSourceTests.m | 25 ++++++++++--- Tests/Tests/FORMDataTests.m | 62 +++++++++++++++++++++++++++---- 2 files changed, 75 insertions(+), 12 deletions(-) diff --git a/Tests/Tests/FORMDataSourceTests.m b/Tests/Tests/FORMDataSourceTests.m index 4d47226d..8418d3ce 100644 --- a/Tests/Tests/FORMDataSourceTests.m +++ b/Tests/Tests/FORMDataSourceTests.m @@ -289,7 +289,8 @@ - (void)testDynamicWithInitialValues FORMDataSource *dataSource = [[FORMDataSource alloc] initWithJSON:JSON collectionView:nil layout:nil - values:@{@"companies[0].name" : @"Facebook", + values:@{@"email":@"hi@there.com", + @"companies[0].name" : @"Facebook", @"companies[0].phone_number" : @"1222333", @"companies[1].name" : @"Google", @"companies[1].phone_number" : @"4555666"} @@ -312,6 +313,9 @@ - (void)testDynamicWithInitialValues field = [dataSource fieldWithID:@"companies[1].phone_number" includingHiddenFields:NO]; XCTAssertEqualObjects(field.value, @"4555666"); + + field = [dataSource fieldWithID:@"email" includingHiddenFields:NO]; + XCTAssertEqualObjects(field.value, @"hi@there.com"); } - (void)testAddingMultipleDynamicSections @@ -350,7 +354,7 @@ - (void)testAddingMultipleDynamicSections [dataSource fieldCell:nil updatedWithField:field]; - [dataSource fieldWithID:@"contacts[0].name" includingHiddenFields:NO completion:^(FORMField *field, NSIndexPath *indexPath) { + [dataSource fieldWithID:@"contacts[0].first_name" includingHiddenFields:NO completion:^(FORMField *field, NSIndexPath *indexPath) { fieldIndexPath = indexPath; }]; @@ -365,7 +369,8 @@ - (void)testRemovingSectionsAddedByInitialValuesA FORMDataSource *dataSource = [[FORMDataSource alloc] initWithJSON:JSON collectionView:nil layout:nil - values:@{@"companies[0].name" : @"Facebook", + values:@{@"email" : @"hi@there.com", + @"companies[0].name" : @"Facebook", @"companies[0].phone_number" : @"1222333", @"companies[1].name" : @"Google", @"companies[1].phone_number" : @"4555666", @@ -376,18 +381,21 @@ - (void)testRemovingSectionsAddedByInitialValuesA } disabled:NO]; - XCTAssertEqual(dataSource.values.count, 8); + XCTAssertEqual(dataSource.values.count, 9); XCTAssertEqual(dataSource.removedValues.count, 0); XCTAssertEqualObjects(dataSource.values[@"companies[0].name"], @"Facebook"); XCTAssertEqualObjects(dataSource.values[@"companies[0].phone_number"], @"1222333"); FORMField *removeField = [dataSource fieldWithID:@"companies[0].remove" includingHiddenFields:YES]; [dataSource fieldCell:nil updatedWithField:removeField]; + + XCTAssertEqual(dataSource.values.count, 7); XCTAssertEqual(dataSource.removedValues.count, 2); XCTAssertEqualObjects(dataSource.removedValues[@"companies[0].name"], @"Facebook"); XCTAssertEqualObjects(dataSource.removedValues[@"companies[0].phone_number"], @"1222333"); XCTAssertEqualObjects(dataSource.values[@"companies[0].name"], @"Google"); XCTAssertEqualObjects(dataSource.values[@"companies[0].phone_number"], @"4555666"); + XCTAssertEqualObjects(dataSource.values[@"email"], @"hi@there.com"); } - (void)testRemovingSectionsAddedByInitialValuesB @@ -398,7 +406,8 @@ - (void)testRemovingSectionsAddedByInitialValuesB FORMDataSource *dataSource = [[FORMDataSource alloc] initWithJSON:JSON collectionView:nil layout:nil - values:@{@"companies[0].name" : @"Facebook", + values:@{@"email" : @"hi@there.com", + @"companies[0].name" : @"Facebook", @"companies[0].phone_number" : @"1222333", @"companies[1].name" : @"Google", @"companies[1].phone_number" : @"4555666", @@ -409,15 +418,21 @@ - (void)testRemovingSectionsAddedByInitialValuesB } disabled:NO]; + XCTAssertEqual(dataSource.values.count, 9); + XCTAssertEqual(dataSource.removedValues.count, 0); XCTAssertEqualObjects(dataSource.values[@"companies[2].name"], @"Apple"); XCTAssertEqualObjects(dataSource.values[@"companies[2].phone_number"], @"7888999"); + FORMField *removeField = [dataSource fieldWithID:@"companies[1].remove" includingHiddenFields:YES]; [dataSource fieldCell:nil updatedWithField:removeField]; + + XCTAssertEqual(dataSource.values.count, 7); XCTAssertTrue(dataSource.removedValues.count == 2); XCTAssertEqualObjects(dataSource.removedValues[@"companies[0].name"], @"Google"); XCTAssertEqualObjects(dataSource.removedValues[@"companies[0].phone_number"], @"4555666"); XCTAssertEqualObjects(dataSource.values[@"companies[2].name"], @"Microsoft"); XCTAssertEqualObjects(dataSource.values[@"companies[2].phone_number"], @"11223344"); + XCTAssertEqualObjects(dataSource.values[@"email"], @"hi@there.com"); } - (void)testRemovingMultipleDynamicSectionsAddedByInitialValues diff --git a/Tests/Tests/FORMDataTests.m b/Tests/Tests/FORMDataTests.m index 96b2983c..07888754 100755 --- a/Tests/Tests/FORMDataTests.m +++ b/Tests/Tests/FORMDataTests.m @@ -346,7 +346,8 @@ - (void)testDynamicWithInitialValues inBundle:[NSBundle bundleForClass:[self class]]]; FORMData *data = [[FORMData alloc] initWithJSON:JSON - initialValues:@{@"companies[0].name" : @"Facebook", + initialValues:@{@"email" : @"hi@there.com", + @"companies[0].name" : @"Facebook", @"companies[0].phone_number" : @"1222333", @"companies[1].name" : @"Google"} disabledFieldIDs:nil @@ -367,33 +368,41 @@ - (void)testDynamicWithInitialValues field = [data fieldWithID:@"companies[1].phone_number" includingHiddenFields:NO]; XCTAssertNotNil(field); XCTAssertNil(field.value); + + field = [data fieldWithID:@"email" includingHiddenFields:NO]; + XCTAssertNotNil(field); + XCTAssertEqualObjects(field.value, @"hi@there.com"); } -- (void)testRemoveDynamicSectionA +- (void)testRemovedValuesWhenRemovingDynamicSection { NSArray *JSON = [NSJSONSerialization JSONObjectWithContentsOfFile:@"dynamic.json" inBundle:[NSBundle bundleForClass:[self class]]]; FORMData *formData = [[FORMData alloc] initWithJSON:JSON - initialValues:@{@"companies[0].name" : @"Facebook", + initialValues:@{@"email" : @"hi@there.com", + @"companies[0].name" : @"Facebook", @"companies[0].phone_number" : @"1222333"} disabledFieldIDs:nil disabled:NO]; - XCTAssertEqual(formData.values.count, 2); + XCTAssertEqual(formData.values.count, 3); XCTAssertEqual(formData.removedValues.count, 0); XCTAssertEqualObjects(formData.values[@"companies[0].name"], @"Facebook"); XCTAssertEqualObjects(formData.values[@"companies[0].phone_number"], @"1222333"); + XCTAssertEqualObjects(formData.values[@"email"], @"hi@there.com"); FORMSection *section = [formData sectionWithID:@"companies[0]"]; [formData removeSection:section]; - XCTAssertEqual(formData.values.count, 0); + + XCTAssertEqual(formData.values.count, 1); XCTAssertEqual(formData.removedValues.count, 2); + XCTAssertEqualObjects(formData.values[@"email"], @"hi@there.com"); XCTAssertEqualObjects(formData.removedValues[@"companies[0].name"], @"Facebook"); XCTAssertEqualObjects(formData.removedValues[@"companies[0].phone_number"], @"1222333"); } -- (void)testRemoveDynamicSectionB +- (void)testRemoveDynamicSectionA { NSArray *JSON = [NSJSONSerialization JSONObjectWithContentsOfFile:@"dynamic.json" inBundle:[NSBundle bundleForClass:[self class]]]; @@ -432,7 +441,7 @@ - (void)testRemoveDynamicSectionB XCTAssertEqualObjects(fieldIDs, comparedFieldIDs); } -- (void)testRemoveDynamicSectionC +- (void)testRemoveDynamicSectionB { NSArray *JSON = [NSJSONSerialization JSONObjectWithContentsOfFile:@"dynamic.json" inBundle:[NSBundle bundleForClass:[self class]]]; @@ -462,4 +471,43 @@ - (void)testRemoveDynamicSectionC XCTAssertEqualObjects(section.sectionID, @"personal-details-0"); } +- (void)testRemoveDynamicSectionC +{ + NSArray *JSON = [NSJSONSerialization JSONObjectWithContentsOfFile:@"dynamic.json" + inBundle:[NSBundle bundleForClass:[self class]]]; + + FORMData *formData = [[FORMData alloc] initWithJSON:JSON + initialValues:@{@"companies[0].name" : @"Facebook", + @"companies[0].phone_number" : @"1222333", + @"contacts[0].first_name" : @"Apple", + @"contacts[0].last_name" : @"Computers"} + disabledFieldIDs:nil + disabled:NO]; + + FORMGroup *group = formData.forms[0]; + XCTAssertEqual(group.sections.count, 6); + NSArray *sectionPositions = @[@0, @1, @2, @3, @4, @5]; + NSArray *comparedSectionPositions = [group.sections valueForKey:@"position"]; + XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); + + FORMSection *section = group.sections[1]; + XCTAssertEqualObjects(section.sectionID, @"companies[0]"); + NSArray *fieldIDs = [section.fields valueForKey:@"fieldID"]; + NSArray *comparedFieldIDs = @[@"companies[0].name", @"companies[0].phone_number", @"companies[0].remove"]; + XCTAssertEqualObjects(fieldIDs, comparedFieldIDs); + + [formData removeSection:section]; + + XCTAssertEqual(group.sections.count, 5); + sectionPositions = @[@0, @1, @2, @3, @4]; + comparedSectionPositions = [group.sections valueForKey:@"position"]; + XCTAssertEqualObjects(sectionPositions, comparedSectionPositions); + + section = group.sections[4]; + XCTAssertEqualObjects(section.sectionID, @"contacts[0]"); + fieldIDs = [section.fields valueForKey:@"fieldID"]; + comparedFieldIDs = @[@"contacts[0].first_name", @"contacts[0].last_name", @"contacts[0].remove"]; + XCTAssertEqualObjects(fieldIDs, comparedFieldIDs); +} + @end From b8b0bd0671b070ca4ee482a8310dba79641d05ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Wed, 25 Mar 2015 08:27:39 +0100 Subject: [PATCH 12/15] Improve dynamic json --- Tests/Tests/JSONs/dynamic.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/Tests/JSONs/dynamic.json b/Tests/Tests/JSONs/dynamic.json index d9f3e7f8..90dabf8b 100644 --- a/Tests/Tests/JSONs/dynamic.json +++ b/Tests/Tests/JSONs/dynamic.json @@ -114,8 +114,8 @@ "id":"contacts", "fields":[ { - "id":"contacts[:index].name", - "title":"Contact name", + "id":"contacts[:index].first_name", + "title":"First name", "type":"name", "validations":{ "required":true, @@ -127,9 +127,9 @@ } }, { - "id":"contacts[:index].phone_number", - "title":"Phone number", - "type":"number", + "id":"contacts[:index].last_name", + "title":"Last name", + "type":"name", "size":{ "width":30, "height":1 From db966115cc0917c80e41d78c85a4cc3cc5fde7ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Wed, 25 Mar 2015 08:27:51 +0100 Subject: [PATCH 13/15] Keep non-relationships values --- Source/FORMData.m | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Source/FORMData.m b/Source/FORMData.m index d4604743..d87c9c6a 100755 --- a/Source/FORMData.m +++ b/Source/FORMData.m @@ -513,7 +513,15 @@ - (void)removeSection:(FORMSection *)removedSection } NSDictionary *attributesJSON = [self.values hyp_JSONNestedAttributes]; - [self.values removeAllObjects]; + + NSMutableArray *removedRelationshipKeys = [NSMutableArray new]; + [[self.values copy] enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { + if ([key containsString:@"]."]) { + [removedRelationshipKeys addObject:key]; + } + }]; + + [self.values removeObjectsForKeys:removedRelationshipKeys]; NSArray *elements = [attributesJSON objectForKey:parsed.relationship]; NSInteger relationshipIndex = 0; @@ -547,8 +555,6 @@ - (void)removeSection:(FORMSection *)removedSection field.fieldID = [field.fieldID hyp_updateRelationshipIndex:newRelationshipIndex]; } relationshipIndex++; - } else { - relationshipIndex = 0; } } } From edfad6ece673583331bb94789b28ad46b989d66e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Wed, 25 Mar 2015 08:28:40 +0100 Subject: [PATCH 14/15] Use hyp_containsString --- Source/FORMData.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/FORMData.m b/Source/FORMData.m index d87c9c6a..dace3705 100755 --- a/Source/FORMData.m +++ b/Source/FORMData.m @@ -516,7 +516,7 @@ - (void)removeSection:(FORMSection *)removedSection NSMutableArray *removedRelationshipKeys = [NSMutableArray new]; [[self.values copy] enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { - if ([key containsString:@"]."]) { + if ([key hyp_containsString:@"]."]) { [removedRelationshipKeys addObject:key]; } }]; From 76fd3f1c9315b887e4c8162aa42fc9c08ca82a25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Wed, 25 Mar 2015 08:36:06 +0100 Subject: [PATCH 15/15] Refactoring --- Source/FORMData.m | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/FORMData.m b/Source/FORMData.m index dace3705..600398b8 100755 --- a/Source/FORMData.m +++ b/Source/FORMData.m @@ -495,19 +495,19 @@ - (void)fieldWithID:(NSString *)fieldID includingHiddenFields:(BOOL)includingHid - (void)removeSection:(FORMSection *)removedSection { + NSDictionary *removedAttributesJSON = [self.removedValues hyp_JSONNestedAttributes]; + HYPParsedRelationship *parsed = [removedSection.sectionID hyp_parseRelationship]; + NSArray *removedElements = [removedAttributesJSON objectForKey:parsed.relationship]; + NSInteger removedElementsCount = removedElements.count; + NSMutableArray *removedKeys = [NSMutableArray new]; [self.values enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { if ([key hasPrefix:removedSection.sectionID]) { [removedKeys addObject:key]; } }]; - - NSDictionary *removedAttributesJSON = [self.removedValues hyp_JSONNestedAttributes]; - HYPParsedRelationship *parsed = [removedSection.sectionID hyp_parseRelationship]; - NSArray *removedElements = [removedAttributesJSON objectForKey:parsed.relationship]; - NSInteger index = removedElements.count; for (NSString *removedKey in removedKeys) { - NSString *newRemovedKey = [removedKey hyp_updateRelationshipIndex:index]; + NSString *newRemovedKey = [removedKey hyp_updateRelationshipIndex:removedElementsCount]; [self.removedValues setValue:self.values[removedKey] forKey:newRemovedKey]; [self.values removeObjectForKey:removedKey]; }