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.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@
42ED55BB3985830447C7EC6F /* Pods_CocoaPods_Basic_ObjC.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 07A98696E9AE57AA5ED35615 /* Pods_CocoaPods_Basic_ObjC.framework */; };
43B5CBB8974CFFEFAD7535CE /* Pods_CocoaPods_Payment.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3841465E0BCB95DB9D521885 /* Pods_CocoaPods_Payment.framework */; };
739F1D308833B568F7E3EDCF /* Pods_CocoaPods_iPhone_Storyboard.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6283A96200ECA336FF064FA2 /* Pods_CocoaPods_iPhone_Storyboard.framework */; };
7A1D3E391DD78E2A0002C33C /* FORMSwitchFieldCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A1D3E381DD78E2A0002C33C /* FORMSwitchFieldCell.m */; };
7A1D3E3B1DD7A1570002C33C /* switch-field.json in Resources */ = {isa = PBXBuildFile; fileRef = 7A1D3E3A1DD7A1570002C33C /* switch-field.json */; };
7A41ADEF1DABE0EB0063DB62 /* FORMSegmentFieldCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A41ADEE1DABE0EB0063DB62 /* FORMSegmentFieldCell.m */; };
7A41ADF11DAC2FCD0063DB62 /* segment-field.json in Resources */ = {isa = PBXBuildFile; fileRef = 7A41ADF01DAC2FCD0063DB62 /* segment-field.json */; };
7A4ABAF31D7DBE7200DAE893 /* FORMSpacerFieldCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A4ABAF21D7DBE7200DAE893 /* FORMSpacerFieldCell.m */; };
Expand Down Expand Up @@ -396,6 +398,9 @@
71ED68BA8DBA9D8DEB2F1288 /* Pods-CocoaPods-LoginDemo.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CocoaPods-LoginDemo.debug.xcconfig"; path = "Pods/Target Support Files/Pods-CocoaPods-LoginDemo/Pods-CocoaPods-LoginDemo.debug.xcconfig"; sourceTree = "<group>"; };
729E65C530FAEB51178E6C77 /* Pods-CocoaPods-Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CocoaPods-Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-CocoaPods-Tests/Pods-CocoaPods-Tests.release.xcconfig"; sourceTree = "<group>"; };
7313DABFEEF45FCCCD39281E /* Pods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7A1D3E371DD78E2A0002C33C /* FORMSwitchFieldCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FORMSwitchFieldCell.h; path = Switch/FORMSwitchFieldCell.h; sourceTree = "<group>"; };
7A1D3E381DD78E2A0002C33C /* FORMSwitchFieldCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FORMSwitchFieldCell.m; path = Switch/FORMSwitchFieldCell.m; sourceTree = "<group>"; };
7A1D3E3A1DD7A1570002C33C /* switch-field.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "switch-field.json"; sourceTree = "<group>"; };
7A41ADED1DABE0EB0063DB62 /* FORMSegmentFieldCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FORMSegmentFieldCell.h; path = Switch/FORMSegmentFieldCell.h; sourceTree = "<group>"; };
7A41ADEE1DABE0EB0063DB62 /* FORMSegmentFieldCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FORMSegmentFieldCell.m; path = Switch/FORMSegmentFieldCell.m; sourceTree = "<group>"; };
7A41ADF01DAC2FCD0063DB62 /* segment-field.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "segment-field.json"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -581,6 +586,7 @@
140803641BEA50AB0098C288 /* Cells */ = {
isa = PBXGroup;
children = (
7A1D3E361DD78DF10002C33C /* Switch */,
7A41ADE91DABD2930063DB62 /* Segment */,
7AB346BD1D78C83100AAAD2E /* Spacer */,
140803651BEA50AB0098C288 /* Base */,
Expand Down Expand Up @@ -778,6 +784,7 @@
1408042B1BEA50C00098C288 /* simple-section.json */,
1408042C1BEA50C00098C288 /* simple-text-field.json */,
1408042D1BEA50C00098C288 /* styled-fields.json */,
7A1D3E3A1DD7A1570002C33C /* switch-field.json */,
);
path = JSONs;
sourceTree = "<group>";
Expand Down Expand Up @@ -1005,6 +1012,15 @@
name = Frameworks;
sourceTree = "<group>";
};
7A1D3E361DD78DF10002C33C /* Switch */ = {
isa = PBXGroup;
children = (
7A1D3E371DD78E2A0002C33C /* FORMSwitchFieldCell.h */,
7A1D3E381DD78E2A0002C33C /* FORMSwitchFieldCell.m */,
);
name = Switch;
sourceTree = "<group>";
};
7A41ADE91DABD2930063DB62 /* Segment */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1217,7 +1233,7 @@
attributes = {
CLASSPREFIX = "";
LastSwiftUpdateCheck = 0710;
LastUpgradeCheck = 0800;
LastUpgradeCheck = 0810;
ORGANIZATIONNAME = "";
TargetAttributes = {
1406BB791BEA520D00131623 = {
Expand Down Expand Up @@ -1367,6 +1383,7 @@
1408043F1BEA50C00098C288 /* collapsed-groups.json in Resources */,
7A41ADF11DAC2FCD0063DB62 /* segment-field.json in Resources */,
1408044E1BEA50C00098C288 /* simple-text-field.json in Resources */,
7A1D3E3B1DD7A1570002C33C /* switch-field.json in Resources */,
1408044A1BEA50C00098C288 /* postal_codes.json in Resources */,
1408044C1BEA50C00098C288 /* simple-number-field.json in Resources */,
140804401BEA50C00098C288 /* counter-field.json in Resources */,
Expand Down Expand Up @@ -1850,6 +1867,7 @@
140804001BEA50AB0098C288 /* DDMathEvaluator+FORM.m in Sources */,
140803E81BEA50AB0098C288 /* FORMData.m in Sources */,
140803DB1BEA50AB0098C288 /* FORMPopoverFieldCell.m in Sources */,
7A1D3E391DD78E2A0002C33C /* FORMSwitchFieldCell.m in Sources */,
140803F41BEA50AB0098C288 /* FORMNameInputValidator.m in Sources */,
1408043C1BEA50C00098C288 /* FORMValidatorTests.m in Sources */,
140804381BEA50C00098C288 /* FORMSectionTests.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Demo.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0800"
LastUpgradeVersion = "0810"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ DEPENDENCIES:

EXTERNAL SOURCES:
Form:
:path: "."
:path: .

SPEC CHECKSUMS:
Form: 21a16754109802adc59e764a287b6983949cc0c6
Expand Down
12 changes: 12 additions & 0 deletions Source/Cells/Switch/FORMSwitchFieldCell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@import UIKit;

#import "FORMBaseFieldCell.h"

static NSString * const FORMSwitchFieldCellIdentifier = @"FORMSwitchFieldCellIdentifier";

@interface FORMSwitchFieldCell : FORMBaseFieldCell

- (void)setTintColor:(UIColor *)tintColor UI_APPEARANCE_SELECTOR;
- (void)setBackgroundColor:(UIColor *)backgroundColor UI_APPEARANCE_SELECTOR;

@end
101 changes: 101 additions & 0 deletions Source/Cells/Switch/FORMSwitchFieldCell.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#import "FORMSwitchFieldCell.h"
@import Hex;
#import "FORMFieldValue.h"

static NSString * const FORMSwitchTintColorKey = @"tint_color";
static NSString * const FORMSwitchBackgroundColorKey = @"background_color";

static const CGFloat FORMSwitchFieldCellMarginTop = 36.0f;
static const CGFloat FORMSwitchFieldCellMarginBottom = 4.0f;

@interface FORMSwitchFieldCell ()

@property (nonatomic) UISwitch *switchControl;

@end

@implementation FORMSwitchFieldCell

- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (!self) return nil;

self.switchControl = [[UISwitch alloc] initWithFrame:frame];
[self.switchControl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
[self.contentView addSubview:self.switchControl];

return self;
}

#pragma mark - FORMBaseFormFieldCell

- (void)updateFieldWithDisabled:(BOOL)disabled {
self.switchControl.alpha = disabled ? 0.5f : 1.0f;
self.switchControl.enabled = disabled ? NO : YES;
}

- (void)updateWithField:(FORMField *)field {
[super updateWithField:field];

self.switchControl.enabled = !field.disabled;
self.disabled = field.disabled;
self.switchControl.on = [field.value boolValue];

if ([field.accessibilityLabel length] > 0) {
self.switchControl.accessibilityLabel = field.accessibilityLabel;
} else {
self.switchControl.accessibilityLabel = self.headingLabel.text;
}
}

#pragma mark - Layout

- (void)layoutSubviews {
[super layoutSubviews];

self.switchControl.frame = [self switchFrame];
}

- (CGRect)switchFrame {
CGFloat marginX = FORMTextFieldCellMarginX;
CGFloat marginTop = FORMSwitchFieldCellMarginTop;
CGFloat marginBotton = FORMSwitchFieldCellMarginBottom;

CGFloat width = CGRectGetWidth(self.frame) - (marginX * 2);
CGFloat height = CGRectGetHeight(self.frame) - marginTop - marginBotton;
CGRect frame = CGRectMake(marginX, marginTop, width, height);

return frame;
}

#pragma mark - Actions

- (void)switchAction:(id)sender {
self.field.value = [sender isOn] ? @YES : @NO;

if ([self.delegate respondsToSelector:@selector(fieldCell:updatedWithField:)]) {
[self.delegate fieldCell:self updatedWithField:self.field];
}
}

#pragma mark - Styling

- (void)setTintColor:(UIColor *)tintColor {
NSString *style = [self.field.styles valueForKey:FORMSwitchTintColorKey];
if ([style length] > 0) {
tintColor = [[UIColor alloc] initWithHex:style];
}

self.switchControl.onTintColor = tintColor;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
NSString *style = [self.field.styles valueForKey:FORMSwitchBackgroundColorKey];
if ([style length] > 0) {
backgroundColor = [[UIColor alloc] initWithHex:style];
}

self.switchControl.backgroundColor = backgroundColor;
}

@end
7 changes: 7 additions & 0 deletions Source/FORMDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "FORMButtonFieldCell.h"
#import "FORMSpacerFieldCell.h"
#import "FORMSegmentFieldCell.h"
#import "FORMSwitchFieldCell.h"
#import "FORMFieldValue.h"
#import "HYPParsedRelationship.h"

Expand Down Expand Up @@ -195,6 +196,12 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
forCellWithReuseIdentifier:identifier];
break;

case FORMFieldTypeSwitch:
identifier = [NSString stringWithFormat:@"%@-%@", FORMSwitchFieldCellIdentifier, field.fieldID];
[collectionView registerClass:[FORMSwitchFieldCell class]
forCellWithReuseIdentifier:identifier];
break;

case FORMFieldTypeCustom: abort();
}

Expand Down
4 changes: 4 additions & 0 deletions Source/FORMDefaultStyle.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "FORMTextFieldCell.h"
#import "FORMButtonFieldCell.h"
#import "FORMSegmentFieldCell.h"
#import "FORMSwitchFieldCell.h"
#import "FORMBaseFieldCell.h"

@import Hex;
Expand Down Expand Up @@ -106,6 +107,9 @@ + (void)applyStyle {
[[FORMSegmentFieldCell appearance] setBackgroundColor:[[UIColor alloc] initWithHex:@"FFFFFF"]];
[[FORMSegmentFieldCell appearance] setTintColor:[[UIColor alloc] initWithHex:@"3DAFEB"]];

[[FORMSwitchFieldCell appearance] setBackgroundColor:[[UIColor alloc] initWithHex:@"FFFFFF"]];
[[FORMSwitchFieldCell appearance] setTintColor:[[UIColor alloc] initWithHex:@"3DAFEB"]];

}

@end
1 change: 1 addition & 0 deletions Source/Models/FORMField.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef NS_ENUM(NSInteger, FORMFieldType) {
FORMFieldTypeSpacer,
FORMFieldTypeCount,
FORMFieldTypeSegment,
FORMFieldTypeSwitch,
FORMFieldTypeCustom
};

Expand Down
4 changes: 4 additions & 0 deletions Source/Models/FORMField.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ - (void)setValue:(id)fieldValue {
case FORMFieldTypeButton:
case FORMFieldTypeSpacer:
case FORMFieldTypeSegment:
case FORMFieldTypeSwitch:
case FORMFieldTypeCustom:
break;
}
Expand Down Expand Up @@ -182,6 +183,7 @@ - (id)rawFieldValue {
case FORMFieldTypeButton:
case FORMFieldTypeSpacer:
case FORMFieldTypeSegment:
case FORMFieldTypeSwitch:
case FORMFieldTypeCustom:
return nil;
}
Expand Down Expand Up @@ -257,6 +259,8 @@ - (FORMFieldType)typeFromTypeString:(NSString *)typeString {
return FORMFieldTypeSpacer;
} else if ([typeString isEqualToString:@"segment"]) {
return FORMFieldTypeSegment;
} else if ([typeString isEqualToString:@"switch"]) {
return FORMFieldTypeSwitch;
} else {
return FORMFieldTypeCustom;
}
Expand Down
9 changes: 9 additions & 0 deletions Tests/FORMDataSourceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,15 @@ - (void)testStyleFields {
XCTAssertEqualObjects([segmentField.styles objectForKey:@"font"], @"AvenirNext-DemiBold");
XCTAssertEqualObjects([segmentField.styles objectForKey:@"font_size"], @"16.0");

// Switch Field Styles
FORMField *switchField = [dataSource fieldWithID:@"styled_switch" includingHiddenFields:NO];
XCTAssertNotNil(switchField);

[dataSource fieldCell:nil updatedWithField:switchField];
XCTAssertNotNil(switchField.styles);
XCTAssertEqualObjects([switchField.styles objectForKey:@"tint_color"], @"#FF0000");
XCTAssertEqualObjects([switchField.styles objectForKey:@"background_color"], @"#FFFFFF");

// Text Field Styles
FORMField *textField = [dataSource fieldWithID:@"styled_text_field" includingHiddenFields:NO];
XCTAssertNotNil(textField);
Expand Down
20 changes: 20 additions & 0 deletions Tests/FORMDataTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,18 @@ - (void)testInitializatingASegmentFieldWithAValueInTheJSON {
XCTAssertEqualObjects(formData.values[@"segment"], @"part_1");
}

- (void)testInitializatingASwitchFieldWithAValueInTheJSON {
NSArray *JSON = [NSJSONSerialization JSONObjectWithContentsOfFile:@"switch-field.json"
inBundle:[NSBundle bundleForClass:[self class]]];

FORMData *formData = [[FORMData alloc] initWithJSON:JSON
initialValues:nil
disabledFieldIDs:nil
disabled:NO];

XCTAssertEqualObjects(formData.values[@"switch"], @YES);
}

- (void)testFieldStyles {
NSArray *JSON = [NSJSONSerialization JSONObjectWithContentsOfFile:@"styled-fields.json"
inBundle:[NSBundle bundleForClass:[self class]]];
Expand Down Expand Up @@ -862,6 +874,14 @@ - (void)testFieldStyles {
XCTAssertEqualObjects([field.styles objectForKey:@"font"], @"AvenirNext-DemiBold");
XCTAssertEqualObjects([field.styles objectForKey:@"font_size"], @"16.0");

// Switch Styles
field = [formData fieldWithID:@"styled_switch" includingHiddenFields:NO];
XCTAssertNotNil(field);
XCTAssertNotNil(field.styles);

XCTAssertEqualObjects([field.styles objectForKey:@"tint_color"], @"#FF0000");
XCTAssertEqualObjects([field.styles objectForKey:@"background_color"], @"#FFFFFF");

// Text Field Styles
field = [formData fieldWithID:@"styled_text_field" includingHiddenFields:NO];
XCTAssertNotNil(field);
Expand Down
23 changes: 23 additions & 0 deletions Tests/FORMFieldTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,29 @@ - (void)testInitWithDictionary {
XCTAssertTrue(field.disabled);
XCTAssertNil(field.validation);
XCTAssertFalse(field.hidden);

field = [[FORMField alloc] initWithDictionary:@{@"id": @"switch_control",
@"title": @"Switch Control",
@"accessibility_label": @"Accessibility label",
@"type": @"switch",
@"value": @YES,
@"size": @{@"width": @30,
@"height": @1}
}
position:1
disabled:NO
disabledFieldsIDs:@[@"switch_control"]];
XCTAssertNotNil(field);
XCTAssertEqualObjects(field.typeString, @"switch");
XCTAssertTrue(field.type == FORMFieldTypeSwitch);

XCTAssertEqualObjects(field.value, @YES);
XCTAssertEqualObjects(field.accessibilityLabel, @"Accessibility label");

XCTAssertTrue(CGSizeEqualToSize(field.size, CGSizeMake(30, 1)));
XCTAssertTrue(field.disabled);
XCTAssertNil(field.validation);
XCTAssertFalse(field.hidden);
}

- (void)testInputValidator {
Expand Down
13 changes: 13 additions & 0 deletions Tests/JSONs/styled-fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@
"width":40,
"height":1
},
},
{
"id":"styled_switch",
"title":"Styled Switch",
"type":"switch",
"styles":{
"background_color":"#FFFFFF",
"tint_color":"#FF0000"
},
"size":{
"width":40,
"height":1
},
}
]
}
Expand Down
Loading