Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.
2 changes: 2 additions & 0 deletions Source/FORMData.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
- (void)fieldWithID:(NSString *)fieldID includingHiddenFields:(BOOL)includingHiddenFields
completion:(void (^)(FORMField *field, NSIndexPath *indexPath))completion;

- (void)removeSection:(FORMSection *)removedSection;

- (NSArray *)showTargets:(NSArray *)targets;
- (NSArray *)hideTargets:(NSArray *)targets;
- (NSArray *)updateTargets:(NSArray *)targets;
Expand Down
69 changes: 69 additions & 0 deletions Source/FORMData.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()

Expand Down Expand Up @@ -490,6 +493,72 @@ - (void)fieldWithID:(NSString *)fieldID includingHiddenFields:(BOOL)includingHid
if (completion) completion(foundField, indexPath);
}

- (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];
}
}];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline 😱

for (NSString *removedKey in removedKeys) {
NSString *newRemovedKey = [removedKey hyp_updateRelationshipIndex:removedElementsCount];
[self.removedValues setValue:self.values[removedKey] forKey:newRemovedKey];
[self.values removeObjectForKey:removedKey];
}

NSDictionary *attributesJSON = [self.values hyp_JSONNestedAttributes];

NSMutableArray *removedRelationshipKeys = [NSMutableArray new];
[[self.values copy] enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if ([key hyp_containsString:@"]."]) {
[removedRelationshipKeys addObject:key];
}
}];

[self.values removeObjectsForKeys:removedRelationshipKeys];

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++;
}

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++;
}
}
}

- (FORMField *)hiddenFieldWithFieldID:(NSString *)fieldID
{
NSArray *hiddenFields = [self.hiddenFieldsAndFieldIDsDictionary allValues];
Expand Down
65 changes: 1 addition & 64 deletions Source/FORMDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -519,54 +519,11 @@ - (void)fieldCell:(UICollectionViewCell *)fieldCell updatedWithField:(FORMField
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];
}

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];
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++;
}

FORMGroup *group = section.form;
[group.sections removeObject:section];
[self.formsManager removeSection:section];

if (indexPaths) {
[self.collectionView deleteItemsAtIndexPaths:indexPaths];
}

[self updateSectionPosition:section];
}];
}
}
Expand Down Expand Up @@ -870,26 +827,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 = [currentSection.position integerValue] - 1;
currentSection.position = @(newPosition);

HYPParsedRelationship *parsedSection = [currentSection.sectionID hyp_parseRelationship];
if (parsedSection.toMany) {
NSInteger newRelationshipIndex = [parsedSection.index integerValue] - 1;
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:)];
Expand Down
2 changes: 2 additions & 0 deletions Source/Models/FORMGroup.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()

Expand Down
1 change: 1 addition & 0 deletions Source/Models/FORMSection.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ - (instancetype)initWithDictionary:(NSDictionary *)dictionary
field.sectionSeparator = YES;
field.position = @(fields.count);
field.section = self;
field.fieldID = [NSString stringWithFormat:@"separator-%@", _sectionID];
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separators didn't have a field ID, they where showing up as nil

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yah, I think I saw the description method exploding when running it on a separator.

[fields addObject:field];
}

Expand Down
14 changes: 9 additions & 5 deletions Tests/Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
1440AF0C1AC1D76500F15D15 /* FORMGroupTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FORMGroupTests.m; sourceTree = "<group>"; };
1449AE721AA07E0700BCC40E /* FORMField+Tests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "FORMField+Tests.h"; sourceTree = "<group>"; };
1449AE731AA07E0700BCC40E /* FORMField+Tests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "FORMField+Tests.m"; sourceTree = "<group>"; };
144A28511A99B2CF004A9086 /* FORMBaseFieldCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FORMBaseFieldCell.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
);
Expand Down Expand Up @@ -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 */,
Expand Down
Loading