Skip to content

Commit

Permalink
Major refactoring of root builder, moving the code to the new class
Browse files Browse the repository at this point in the history
  • Loading branch information
escoz committed Jan 9, 2012
1 parent 94eb139 commit 6ad33d2
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 145 deletions.
8 changes: 8 additions & 0 deletions QuickDialog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@
D8F180E813F0599A009B0CB1 /* jsonadvancedsample.json in Resources */ = {isa = PBXBuildFile; fileRef = D8F180E813F0599A009B0CB0 /* jsonadvancedsample.json */; };
D8F180E813F0599A009B0CB3 /* BindingEvaluator.m in Sources */ = {isa = PBXBuildFile; fileRef = D8F180E813F0599A009B0CB2 /* BindingEvaluator.m */; };
D8F180E813F0599A009B0CB5 /* BindingEvaluator.h in Headers */ = {isa = PBXBuildFile; fileRef = D8F180E813F0599A009B0CB4 /* BindingEvaluator.h */; };
D8F180E813F0599A009B0CB7 /* QRootBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = D8F180E813F0599A009B0CB6 /* QRootBuilder.m */; };
D8F180E813F0599A009B0CB9 /* QRootBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = D8F180E813F0599A009B0CB8 /* QRootBuilder.h */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -235,6 +237,8 @@
D8F180E813F0599A009B0CB0 /* jsonadvancedsample.json */ = {isa = PBXFileReference; lastKnownFileType = file.json; path = jsonadvancedsample.json; sourceTree = "<group>"; };
D8F180E813F0599A009B0CB2 /* BindingEvaluator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BindingEvaluator.m; path = quickdialog/BindingEvaluator.m; sourceTree = SOURCE_ROOT; };
D8F180E813F0599A009B0CB4 /* BindingEvaluator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BindingEvaluator.h; path = quickdialog/BindingEvaluator.h; sourceTree = SOURCE_ROOT; };
D8F180E813F0599A009B0CB6 /* QRootBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = QRootBuilder.m; path = quickdialog/QRootBuilder.m; sourceTree = SOURCE_ROOT; };
D8F180E813F0599A009B0CB8 /* QRootBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = QRootBuilder.h; path = quickdialog/QRootBuilder.h; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -374,6 +378,8 @@
D860356713E0534000CB1785 /* quickdialog */ = {
isa = PBXGroup;
children = (
D8F180E813F0599A009B0CB8 /* QRootBuilder.h */,
D8F180E813F0599A009B0CB6 /* QRootBuilder.m */,
D8F180E813F0599A009B0CB4 /* BindingEvaluator.h */,
D8F180E813F0599A009B0CB2 /* BindingEvaluator.m */,
D8F180E813F0599A009B0CA9 /* QBadgeLabel.h */,
Expand Down Expand Up @@ -494,6 +500,7 @@
D8F180E813F0599A009B0CAA /* QBadgeLabel.h in Headers */,
D8F180E813F0599A009B0CB5 /* BindingEvaluator.h in Headers */,
D871018614BB3D7A00156D9D /* QRootElement+Builder.h in Headers */,
D8F180E813F0599A009B0CB9 /* QRootBuilder.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -626,6 +633,7 @@
D8F180E813F0599A009B0CA8 /* QBadgeLabel.m in Sources */,
D8F180E813F0599A009B0CB3 /* BindingEvaluator.m in Sources */,
D871018714BB3D7A00156D9D /* QRootElement+Builder.m in Sources */,
D8F180E813F0599A009B0CB7 /* QRootBuilder.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
14 changes: 13 additions & 1 deletion quickdialog/BindingEvaluator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
//
// Created by escoz on 1/8/12.
// Copyright 2011 ESCOZ Inc - http://escoz.com
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//


#import <Foundation/Foundation.h>


Expand All @@ -13,4 +24,5 @@
- (void)bindSection:(QSection *)section toCollection:(NSArray *)items;

- (void)bindSection:(QSection *)section toProperties:(id)object;

@end
35 changes: 29 additions & 6 deletions quickdialog/BindingEvaluator.m
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
//
// Created by escoz on 1/8/12.
// Copyright 2011 ESCOZ Inc - http://escoz.com
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//

#import "BindingEvaluator.h"

@interface BindingEvaluator ()
+ (BOOL)stringIsEmpty:(NSString *)aString;

@end

@implementation BindingEvaluator
@implementation BindingEvaluator {
QRootBuilder *_builder;
}
- (id)init {
self = [super init];
if (self) {
_builder = [QRootBuilder new];
}

return self;
}

- (void)bindObject:(id)object toData:(id)data {
if (![object respondsToSelector:@selector(bind)])
Expand All @@ -20,6 +41,7 @@ - (void)bindObject:(id)object toData:(id)data {

for (NSString *each in [string componentsSeparatedByString:@","]) {
NSArray *bindingParams = [each componentsSeparatedByString:@":"];

NSString *propName = [((NSString *) [bindingParams objectAtIndex:0]) stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *valueName = [((NSString *) [bindingParams objectAtIndex:1]) stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

Expand All @@ -30,8 +52,7 @@ - (void)bindObject:(id)object toData:(id)data {
[self bindSection:(QSection *)object toProperties:[data objectForKey:valueName]];

} else if ([data objectForKey:valueName]!=nil) {
[object setValue:[data objectForKey:valueName] forKey:propName];

[QRootBuilder trySetProperty:propName onObject:object withValue:[data objectForKey:valueName]];
}
}
}
Expand All @@ -50,8 +71,9 @@ + (BOOL)stringIsEmpty:(NSString *) aString {

- (void)bindSection:(QSection *)section toCollection:(NSArray *)items {
[section.elements removeAllObjects];

for (id item in items){
QElement *element = [section.rootElement buildElementWithObject:section.template];
QElement *element = [_builder buildElementWithObject:section.template];
[section addElement:element];
[element bindToObject:item];
}
Expand All @@ -60,8 +82,9 @@ - (void)bindSection:(QSection *)section toCollection:(NSArray *)items {
- (void)bindSection:(QSection *)section toProperties:(NSDictionary *)object {
[section.elements removeAllObjects];
for (id item in [object allKeys]){
QElement *element = [section.rootElement buildElementWithObject:section.template];
QElement *element = [_builder buildElementWithObject:section.template];
[section addElement:element];

[element bindToObject:[NSDictionary dictionaryWithObjectsAndKeys:item, @"key", [object valueForKey:item], @"value", nil]];
}
}
Expand Down
13 changes: 12 additions & 1 deletion quickdialog/QBadgeLabel.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
//
// Created by escoz on 1/6/12.
// Copyright 2011 ESCOZ Inc - http://escoz.com
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//


#import <Foundation/Foundation.h>

Expand Down
14 changes: 12 additions & 2 deletions quickdialog/QBadgeLabel.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
//
// Created by escoz on 1/6/12.
// Copyright 2011 ESCOZ Inc - http://escoz.com
//
#import "QBadgeLabel.h"
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//


@implementation QBadgeLabel {
UIColor *_badgeColor;
Expand Down
6 changes: 3 additions & 3 deletions quickdialog/QLabelElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
@interface QLabelElement : QRootElement {

@protected
NSString *_value;
id _value;
UIImage *_image;
}

@property(nonatomic, strong) UIImage *image;
@property(nonatomic, strong) NSString *value;
@property(nonatomic, strong) id value;


- (QLabelElement *)initWithTitle:(NSString *)string Value:(NSString *)value;
- (QLabelElement *)initWithTitle:(NSString *)string Value:(id)value;
@end
4 changes: 2 additions & 2 deletions quickdialog/QLabelElement.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ @implementation QLabelElement
@synthesize image = _image;
@synthesize value = _value;

- (QLabelElement *)initWithTitle:(NSString *)title Value:(NSString *)value {
- (QLabelElement *)initWithTitle:(NSString *)title Value:(id)value {
self = [super init];
_title = title;
_value = value;
Expand All @@ -33,7 +33,7 @@ - (UITableViewCell *)getCellForTableView:(QuickDialogTableView *)tableView contr
cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.textLabel.text = _title;
cell.detailTextLabel.text = _value;
cell.detailTextLabel.text = [_value description];
cell.imageView.image = _image;
cell.accessoryType = self.sections!= nil || self.controllerAction!=nil ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
cell.selectionStyle = self.sections!= nil || self.controllerAction!=nil ? UITableViewCellSelectionStyleBlue: UITableViewCellSelectionStyleNone;
Expand Down
29 changes: 29 additions & 0 deletions quickdialog/QRootBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Copyright 2011 ESCOZ Inc - http://escoz.com
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//

#import <Foundation/Foundation.h>
#import "quickdialog.h"

@interface QRootBuilder : NSObject {

}

- (QRootElement *)buildSectionsWithObject:(id)obj;

+ (void)trySetProperty:(NSString *)propertyName onObject:(id)target withValue:(id)value;

- (QElement *)buildElementWithObject:(id)obj;


@end
132 changes: 132 additions & 0 deletions quickdialog/QRootBuilder.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//
// Copyright 2011 ESCOZ Inc - http://escoz.com
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//

NSDictionary * QRootElementJSONBuilderConversionDict;

@interface QRootBuilder ()
- (void)initializeMappings;

@end

@implementation QRootBuilder

+ (void)trySetProperty:(NSString *)propertyName onObject:(id)target withValue:(id)value {
if ([value isKindOfClass:[NSString class]] && [target respondsToSelector:NSSelectorFromString(propertyName)]) {
[target setValue:value forKey:propertyName];
if ([QRootElementJSONBuilderConversionDict objectForKey:propertyName]!=nil) {
[target setValue:[[QRootElementJSONBuilderConversionDict objectForKey:propertyName] objectForKey:value] forKey:propertyName];
}
} else if ([value isKindOfClass:[NSNumber class]]){
[target setValue:value forKey:propertyName];
} else if ([value isKindOfClass:[NSArray class]]) {
[target setValue:value forKey:propertyName];
} else if ([value isKindOfClass:[NSDictionary class]]){
[target setValue:value forKey:propertyName];
}
}

- (void)updateObject:(id)element withPropertiesFrom:(NSDictionary *)dict {
for (NSString *key in dict.allKeys){
if ([key isEqualToString:@"type"] || [key isEqualToString:@"sections"]|| [key isEqualToString:@"elements"])
continue;

id value = [dict valueForKey:key];
[QRootBuilder trySetProperty:key onObject:element withValue:value];
}
}

- (QElement *)buildElementWithObject:(id)obj {
QElement *element = [[NSClassFromString([obj valueForKey:[NSString stringWithFormat:@"type"]]) alloc] init];
if (element==nil)
return nil;
[self updateObject:element withPropertiesFrom:obj];
return element;
}

- (void)buildSectionWithObject:(id)obj forRoot:(QRootElement *)root {
QSection *sect = [[QSection alloc] init];
[self updateObject:sect withPropertiesFrom:obj];
[root addSection:sect];
for (id element in (NSArray *)[obj valueForKey:[NSString stringWithFormat:@"elements"]]){
[sect addElement:[self buildElementWithObject:element] ];
}
}

- (QRootElement *)buildSectionsWithObject:(id)obj {
if (QRootElementJSONBuilderConversionDict==nil)
[self initializeMappings];

QRootElement *root = [QRootElement new];
[self updateObject:root withPropertiesFrom:obj];
for (id section in (NSArray *)[obj valueForKey:[NSString stringWithFormat:@"sections"]]){
[self buildSectionWithObject:section forRoot:root];
}

return root;
}

- (void)initializeMappings {
QRootElementJSONBuilderConversionDict = [[NSDictionary alloc] initWithObjectsAndKeys:

[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:UITextAutocapitalizationTypeNone], @"None",
[NSNumber numberWithInt:UITextAutocapitalizationTypeWords], @"Words",
[NSNumber numberWithInt:UITextAutocapitalizationTypeSentences], @"Sentences",
[NSNumber numberWithInt:UITextAutocapitalizationTypeAllCharacters], @"AllCharacters",
nil], @"autocapitalizationType",

[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:UITextAutocorrectionTypeDefault], @"Default",
[NSNumber numberWithInt:UITextAutocorrectionTypeNo], @"No",
[NSNumber numberWithInt:UITextAutocorrectionTypeYes], @"Yes",
nil], @"autocorrectionType",

[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:UIKeyboardTypeDefault], @"Default",
[NSNumber numberWithInt:UIKeyboardTypeASCIICapable], @"ASCIICapable",
[NSNumber numberWithInt:UIKeyboardTypeNumbersAndPunctuation], @"NumbersAndPunctuation",
[NSNumber numberWithInt:UIKeyboardTypeURL], @"URL",
[NSNumber numberWithInt:UIKeyboardTypeNumberPad], @"NumberPad",
[NSNumber numberWithInt:UIKeyboardTypePhonePad], @"PhonePad",
[NSNumber numberWithInt:UIKeyboardTypeNamePhonePad], @"NamePhonePad",
[NSNumber numberWithInt:UIKeyboardTypeEmailAddress], @"EmailAddress",
[NSNumber numberWithInt:UIKeyboardTypeDecimalPad], @"DecimalPad",
[NSNumber numberWithInt:UIKeyboardTypeTwitter], @"Twitter",
[NSNumber numberWithInt:UIKeyboardTypeAlphabet], @"Alphabet",
nil], @"keyboardType",

[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:UIKeyboardAppearanceDefault], @"Default",
[NSNumber numberWithInt:UIKeyboardAppearanceAlert], @"Alert",
nil], @"keyboardAppearance",

[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:UIReturnKeyDefault], @"Default",
[NSNumber numberWithInt:UIReturnKeyGo], @"Go",
[NSNumber numberWithInt:UIReturnKeyGoogle], @"Google",
[NSNumber numberWithInt:UIReturnKeyJoin], @"Join",
[NSNumber numberWithInt:UIReturnKeyNext], @"Next",
[NSNumber numberWithInt:UIReturnKeyRoute], @"Route",
[NSNumber numberWithInt:UIReturnKeySearch], @"Search",
[NSNumber numberWithInt:UIReturnKeySend], @"Send",
[NSNumber numberWithInt:UIReturnKeyYahoo], @"Yahoo",
[NSNumber numberWithInt:UIReturnKeyDone], @"Done",
[NSNumber numberWithInt:UIReturnKeyEmergencyCall], @"EmergencyCall",
nil], @"returnKeyType",

nil];
}


@end
Loading

0 comments on commit 6ad33d2

Please sign in to comment.