Skip to content

Commit

Permalink
Merge pull request 3lvis#421 from jeffleeismyhero/feature/counter-field
Browse files Browse the repository at this point in the history
Add Counter Type field with controls
  • Loading branch information
3lvis committed Jun 15, 2015
2 parents f343c08 + 494c47e commit eed1f04
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 6 deletions.
Binary file added Assets/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion Source/Cells/Text/FORMTextFieldCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ - (NSString *)rawTextForField:(FORMField *)field {

if (field.value) {
switch (field.type) {
case FORMFieldTypeNumber: {
case FORMFieldTypeNumber:
case FORMFieldTypeCount: {
if ([field.value isKindOfClass:[NSNumber class]]) {
NSNumber *value = field.value;
rawText = [value stringValue];
Expand Down
2 changes: 2 additions & 0 deletions Source/Cells/Views/FORMTextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef NS_ENUM(NSInteger, FORMTextFieldType) {
FORMTextFieldTypePassword,
FORMTextFieldTypeSelect,
FORMTextFieldTypeDate,
FORMTextFieldTypeCount,
FORMTextFieldTypeUnknown
};

Expand All @@ -28,6 +29,7 @@ typedef NS_ENUM(NSInteger, FORMTextFieldInputType) {
FORMTextFieldInputTypeAddress,
FORMTextFieldInputTypeEmail,
FORMTextFieldInputTypePassword,
FORMTextFieldInputTypeCount,
FORMTextFieldInputTypeUnknown
};

Expand Down
64 changes: 63 additions & 1 deletion Source/Cells/Views/FORMTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

static const CGFloat FORMTextFieldClearButtonWidth = 30.0f;
static const CGFloat FORMTextFieldClearButtonHeight = 20.0f;
static const CGFloat FORMTextFieldMinusButtonWidth = 30.0f;
static const CGFloat FORMTextFieldMinusButtonHeight = 20.0f;
static const CGFloat FORMTextFieldPlusButtonWidth = 30.0f;
static const CGFloat FORMTextFieldPlusButtonHeight = 20.0f;

static UIColor *activeBackgroundColor;
static UIColor *activeBorderColor;
Expand Down Expand Up @@ -68,7 +72,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
clearButton.frame = CGRectMake(0.0f, 0.0f, FORMTextFieldClearButtonWidth, FORMTextFieldClearButtonHeight);
self.rightView = clearButton;
self.rightViewMode = UITextFieldViewModeWhileEditing;

return self;
}

Expand Down Expand Up @@ -142,6 +146,9 @@ - (void)setTypeString:(NSString *)typeString {
type = FORMTextFieldTypeDefault;
} else if ([typeString isEqualToString:@"password"]) {
type = FORMTextFieldTypePassword;
} else if ([typeString isEqualToString:@"count"]) {
type = FORMTextFieldTypeCount;
[self setCountButtons];
} else if (!typeString.length) {
type = FORMTextFieldTypeDefault;
} else {
Expand Down Expand Up @@ -173,6 +180,8 @@ - (void)setInputTypeString:(NSString *)inputTypeString {
inputType = FORMTextFieldInputTypeDefault;
} else if ([inputTypeString isEqualToString:@"password"]) {
inputType = FORMTextFieldInputTypePassword;
} else if ([inputTypeString isEqualToString:@"count"]) {
inputType = FORMTextFieldInputTypeCount;
} else if (!inputTypeString.length) {
inputType = FORMTextFieldInputTypeDefault;
} else {
Expand Down Expand Up @@ -287,6 +296,30 @@ - (void)clearButtonAction {
}
}

- (void)minusButtonAction {
NSNumber *number = [NSNumber numberWithInt:[self.rawText integerValue] - 1];
if ([number integerValue] < 0) {
self.rawText = @"0";
} else {
self.rawText = [number stringValue];
}

if ([self.textFieldDelegate respondsToSelector:@selector(textFormField:didUpdateWithText:)]) {
[self.textFieldDelegate textFormField:self
didUpdateWithText:self.rawText];
}
}

- (void)plusButtonAction {
NSNumber *number = [NSNumber numberWithInt:[self.rawText integerValue] + 1];
self.rawText = [number stringValue];

if ([self.textFieldDelegate respondsToSelector:@selector(textFormField:didUpdateWithText:)]) {
[self.textFieldDelegate textFormField:self
didUpdateWithText:self.rawText];
}
}

#pragma mark - Appearance

- (void)setActive:(BOOL)active {
Expand Down Expand Up @@ -331,6 +364,35 @@ - (void)setValid:(BOOL)valid {
}
}

- (void)setCountButtons {
NSString *bundlePath = [[[NSBundle bundleForClass:self.class] resourcePath] stringByAppendingPathComponent:@"Form.bundle"];
NSBundle *bundle = [NSBundle bundleWithPath: bundlePath];

UITraitCollection *trait = [UITraitCollection traitCollectionWithDisplayScale:2.0];

UIButton *minusButton = [UIButton buttonWithType:UIButtonTypeCustom];
[minusButton setImage:[UIImage imageNamed:@"minus"
inBundle:bundle
compatibleWithTraitCollection:trait] forState:UIControlStateNormal];
[minusButton addTarget:self action:@selector(minusButtonAction) forControlEvents:UIControlEventTouchUpInside];
minusButton.frame = CGRectMake(0.0f, 0.0f, FORMTextFieldMinusButtonWidth, FORMTextFieldMinusButtonHeight);

UIButton *plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
[plusButton setImage:[UIImage imageNamed:@"plus"
inBundle:bundle
compatibleWithTraitCollection:trait] forState:UIControlStateNormal];
[plusButton addTarget:self action:@selector(plusButtonAction) forControlEvents:UIControlEventTouchUpInside];
plusButton.frame = CGRectMake(0.0f, 0.0f, FORMTextFieldPlusButtonWidth, FORMTextFieldPlusButtonHeight);

self.leftView = minusButton;
self.leftViewMode = UITextFieldViewModeAlways;

self.rightView = plusButton;
self.rightViewMode = UITextFieldViewModeAlways;

self.textAlignment = NSTextAlignmentCenter;
}

- (void)setCustomFont:(UIFont *)font {
self.font = font;
}
Expand Down
8 changes: 8 additions & 0 deletions Source/Cells/Views/FORMTextFieldTypeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ - (void)setUpType:(FORMTextFieldInputType)type forTextField:(UITextField *)textF
case FORMTextFieldInputTypeAddress : [self setupAddressTextField:textField]; break;
case FORMTextFieldInputTypeEmail : [self setupEmailTextField:textField]; break;
case FORMTextFieldInputTypePassword : [self setupPasswordTextField:textField]; break;
case FORMTextFieldInputTypeCount : [self setupCountTextField:textField]; break;

case FORMTextFieldInputTypeUnknown:
abort();
Expand Down Expand Up @@ -77,4 +78,11 @@ - (void)setupPasswordTextField:(UITextField *)textField {
textField.secureTextEntry = YES;
}

- (void)setupCountTextField:(UITextField *)textField {
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.secureTextEntry = NO;
}

@end
4 changes: 2 additions & 2 deletions Source/FORMData.m
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ - (NSMutableDictionary *)valuesForFormula:(FORMField *)field {
}
}
} else {
if (field.type == FORMFieldTypeNumber || field.type == FORMFieldTypeFloat) {
if (field.type == FORMFieldTypeNumber || field.type == FORMFieldTypeFloat || field.type == FORMFieldTypeCount) {
[values addEntriesFromDictionary:@{fieldID : @"0"}];
} else {
[values addEntriesFromDictionary:@{fieldID : @""}];
Expand Down Expand Up @@ -877,7 +877,7 @@ - (NSArray *)updateTargets:(NSArray *)targets {
for (NSString *fieldID in fieldIDs) {

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

FORMField *targetField = [self fieldWithID:fieldID includingHiddenFields:YES];
Expand Down
1 change: 1 addition & 0 deletions Source/FORMDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
case FORMFieldTypeText:
case FORMFieldTypeFloat:
case FORMFieldTypeNumber:
case FORMFieldTypeCount:
identifier = FORMTextFieldCellIdentifier;
break;

Expand Down
1 change: 1 addition & 0 deletions Source/Models/FORMField.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef NS_ENUM(NSInteger, FORMFieldType) {
FORMFieldTypeFloat,
FORMFieldTypeNumber,
FORMFieldTypeButton,
FORMFieldTypeCount,
FORMFieldTypeCustom
};

Expand Down
6 changes: 5 additions & 1 deletion Source/Models/FORMField.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ - (void)setValue:(id)fieldValue {

switch (self.type) {
case FORMFieldTypeNumber:
case FORMFieldTypeFloat: {
case FORMFieldTypeFloat:
case FORMFieldTypeCount: {
if (![fieldValue isKindOfClass:[NSString class]]) {
resultValue = [fieldValue stringValue];
}
Expand Down Expand Up @@ -157,6 +158,7 @@ - (id)rawFieldValue {
}
return @([self.value floatValue]);
case FORMFieldTypeNumber:
case FORMFieldTypeCount:
return @([self.value integerValue]);

case FORMFieldTypeText:
Expand Down Expand Up @@ -234,6 +236,8 @@ - (FORMFieldType)typeFromTypeString:(NSString *)typeString {
return FORMFieldTypeFloat;
} else if ([typeString isEqualToString:@"number"]) {
return FORMFieldTypeNumber;
} else if ([typeString isEqualToString:@"count"]) {
return FORMFieldTypeCount;
} else if ([typeString isEqualToString:@"button"]) {
return FORMFieldTypeButton;
} else {
Expand Down
14 changes: 13 additions & 1 deletion Tests/Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
44713C611AE8E9DC00AA97DA /* simple-number-field.json in Resources */ = {isa = PBXBuildFile; fileRef = 44713C601AE8E9DC00AA97DA /* simple-number-field.json */; };
44932E771AE79BBD00A159BE /* simple-section.json in Resources */ = {isa = PBXBuildFile; fileRef = 44932E761AE79BBD00A159BE /* simple-section.json */; };
4496E4961AE7913400F731C9 /* simple-text-field.json in Resources */ = {isa = PBXBuildFile; fileRef = 4496E4951AE7913400F731C9 /* simple-text-field.json */; };
7A7DC4A11B1E18810056E846 /* counter-field.json in Resources */ = {isa = PBXBuildFile; fileRef = 7A7DC4A01B1E18810056E846 /* counter-field.json */; };
7A7DC4A41B1E197A0056E846 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 7A7DC4A21B1E197A0056E846 /* [email protected] */; };
7A7DC4A51B1E197A0056E846 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 7A7DC4A31B1E197A0056E846 /* [email protected] */; };
8EC9C8158643C507F883EEC4 /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4E09ABAE07DB25A266537B76 /* Pods.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
BD2405701AE6B38D00592E19 /* default-values-in-template.json in Resources */ = {isa = PBXBuildFile; fileRef = BD24056F1AE6B38D00592E19 /* default-values-in-template.json */; };
BD5845751AC4074600596C62 /* FORMTooltipView.m in Sources */ = {isa = PBXBuildFile; fileRef = BD5845741AC4074600596C62 /* FORMTooltipView.m */; };
Expand Down Expand Up @@ -166,7 +169,7 @@
144A28981A99B2CF004A9086 /* FORMClassFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FORMClassFactory.h; sourceTree = "<group>"; };
144A28991A99B2CF004A9086 /* FORMClassFactory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FORMClassFactory.m; sourceTree = "<group>"; };
144A289A1A99B2CF004A9086 /* FORMField.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FORMField.h; sourceTree = "<group>"; };
144A289B1A99B2CF004A9086 /* FORMField.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = FORMField.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
144A289B1A99B2CF004A9086 /* FORMField.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = FORMField.m; sourceTree = "<group>"; };
144A289C1A99B2CF004A9086 /* FORMFieldValidation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FORMFieldValidation.h; sourceTree = "<group>"; };
144A289D1A99B2CF004A9086 /* FORMFieldValidation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FORMFieldValidation.m; sourceTree = "<group>"; };
144A289E1A99B2CF004A9086 /* FORMFieldValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FORMFieldValue.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -231,6 +234,9 @@
4496E4951AE7913400F731C9 /* simple-text-field.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "simple-text-field.json"; sourceTree = "<group>"; };
4E09ABAE07DB25A266537B76 /* Pods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods.framework; sourceTree = BUILT_PRODUCTS_DIR; };
653AC6BBDF66E205BA44BA17 /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = "<group>"; };
7A7DC4A01B1E18810056E846 /* counter-field.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "counter-field.json"; sourceTree = "<group>"; };
7A7DC4A21B1E197A0056E846 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
7A7DC4A31B1E197A0056E846 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
BD24056F1AE6B38D00592E19 /* default-values-in-template.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "default-values-in-template.json"; sourceTree = "<group>"; };
BD5845731AC4074600596C62 /* FORMTooltipView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FORMTooltipView.h; sourceTree = "<group>"; };
BD5845741AC4074600596C62 /* FORMTooltipView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FORMTooltipView.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -446,6 +452,7 @@
44713C601AE8E9DC00AA97DA /* simple-number-field.json */,
44932E761AE79BBD00A159BE /* simple-section.json */,
4496E4951AE7913400F731C9 /* simple-text-field.json */,
7A7DC4A01B1E18810056E846 /* counter-field.json */,
);
path = JSONs;
sourceTree = "<group>";
Expand Down Expand Up @@ -551,6 +558,8 @@
14F5949E1AE0618300772756 /* Assets */ = {
isa = PBXGroup;
children = (
7A7DC4A21B1E197A0056E846 /* [email protected] */,
7A7DC4A31B1E197A0056E846 /* [email protected] */,
14F5949F1AE0618300772756 /* [email protected] */,
14F594A01AE0618300772756 /* [email protected] */,
14F594A11AE0618300772756 /* [email protected] */,
Expand Down Expand Up @@ -647,11 +656,13 @@
149C292C1A9A23DD00F88B91 /* multiple-show-hide-field-targets.json in Resources */,
14B564731A06D87B00342CDA /* .travis.yml in Resources */,
BD92FBDA1AE61FF000D45BD4 /* dynamic-formulas.json in Resources */,
7A7DC4A11B1E18810056E846 /* counter-field.json in Resources */,
149C292A1A9A23DD00F88B91 /* default-values.json in Resources */,
44713C611AE8E9DC00AA97DA /* simple-number-field.json in Resources */,
14C4183D1A019A1500636FD6 /* LICENSE.md in Resources */,
149C292B1A9A23DD00F88B91 /* field-validations.json in Resources */,
4496E4961AE7913400F731C9 /* simple-text-field.json in Resources */,
7A7DC4A41B1E197A0056E846 /* [email protected] in Resources */,
14F594A41AE0618300772756 /* [email protected] in Resources */,
1492C05A1A9F740900D80D91 /* dynamic.json in Resources */,
149C29331A9A251800F88B91 /* forms.json in Resources */,
Expand All @@ -662,6 +673,7 @@
144B80291AA882DB009C69DC /* section-field-position.json in Resources */,
44932E771AE79BBD00A159BE /* simple-section.json in Resources */,
149C292E1A9A23DD00F88B91 /* number-formula.json in Resources */,
7A7DC4A51B1E197A0056E846 /* [email protected] in Resources */,
14F594A51AE0618300772756 /* [email protected] in Resources */,
14F594A31AE0618300772756 /* [email protected] in Resources */,
14F594A61AE0618300772756 /* [email protected] in Resources */,
Expand Down
12 changes: 12 additions & 0 deletions Tests/Tests/FORMDataTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -800,4 +800,16 @@ - (void)testInitializatingAFieldWithAValueInTheJSON {
XCTAssertEqualObjects(formData.values[@"textie"], @"1");
}

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

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

XCTAssertEqualObjects(formData.values[@"count"], @"1");
}

@end
23 changes: 23 additions & 0 deletions Tests/Tests/JSONs/counter-field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"id": "group",
"title": "Group",
"sections": [
{
"id": "section",
"fields": [
{
"id": "count",
"title": "My Counter",
"type": "counter",
"value": "1",
"size": {
"width": 25,
"height": 1
}
}
]
}
]
}
]

0 comments on commit eed1f04

Please sign in to comment.