forked from escoz/QuickDialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QBindingEvaluator.m
161 lines (131 loc) · 6 KB
/
QBindingEvaluator.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// 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 "QBindingEvaluator.h"
#import "QuickDialog.h"
@interface QBindingEvaluator ()
+ (BOOL)stringIsEmpty:(NSString *)aString;
@end
@implementation QBindingEvaluator {
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)])
return;
NSString *string = [object bind];
[self bindObject:object toData:data withString:string];
}
- (void)bindObject:(id)object toData:(id)data withString:string {
if ([QBindingEvaluator stringIsEmpty:string])
return;
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]];
if ([propName isEqualToString:@"iterate"] && [object isKindOfClass:[QSection class]]) {
[self bindSection:(QSection *)object toCollection:[@"self" isEqualToString:valueName] ? data : [data valueForKeyPath:valueName]];
} else if ([propName isEqualToString:@"iterate"] && [object isKindOfClass:[QRootElement class]]) {
NSArray *itemsToIterate = [valueName isEqualToString:@"self"]? data : [data valueForKeyPath:valueName];
[self bindRootElement:(QRootElement *) object toCollection:itemsToIterate];
} else if ([propName isEqualToString:@"iterateproperties"] && [object isKindOfClass:[QSection class]]) {
[self bindSection:(QSection *)object toProperties:[data valueForKeyPath:valueName]];
} else if ([valueName isEqualToString:@"self"]) {
[QRootBuilder trySetProperty:propName onObject:object withValue:data localized:NO];
} else {
[QRootBuilder trySetProperty:propName onObject:object withValue:[data valueForKeyPath:valueName] localized:NO];
}
}
}
+ (BOOL)stringIsEmpty:(NSString *) aString {
if (aString == nil || ([aString length] == 0)) {
return YES;
}
aString = [aString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString length] == 0) {
return YES;
}
return NO;
}
- (void)bindSection:(QSection *)section toCollection:(NSArray *)items {
[section.elements removeAllObjects];
for (id item in section.beforeTemplateElements){
QElement *element = [_builder buildElementWithObject:item];
[section addElement:element];
[element bindToObject:item];
}
for (id item in items){
QElement *element = [_builder buildElementWithObject:section.elementTemplate];
[section addElement:element];
[element bindToObject:item];
}
for (id item in section.afterTemplateElements){
QElement *element = [_builder buildElementWithObject:item];
[section addElement:element];
[element bindToObject:item];
}
}
- (void)bindRootElement:(QRootElement *)element toCollection:(NSArray *)items {
[element.sections removeAllObjects];
for (id item in items){
QSection *section = [_builder buildSectionWithObject:element.sectionTemplate];
[element addSection:section];
[section bindToObject:item];
}
if (element.sections.count==0 && element.emptyMessage !=nil){
QSection *section = [[QSection alloc] init];
[section addElement:[[QTextElement alloc] initWithText:element.emptyMessage]];
[element addSection:section];
}
}
- (void)bindSection:(QSection *)section toProperties:(NSDictionary *)object {
if ([object isKindOfClass:[NSNull class]]) {
return;
}
[section.elements removeAllObjects];
for (id item in [object allKeys]){
QElement *element = [_builder buildElementWithObject:section.elementTemplate];
[section addElement:element];
[element bindToObject:[NSDictionary dictionaryWithObjectsAndKeys:item, @"key", [object valueForKey:item], @"value", nil]];
}
}
- (void)fetchValueFromObject:(QElement *)element toData:(id)data {
if (element.bind == nil || ([element.bind length] == 0)) {
return;
}
for (NSString *each in [element.bind componentsSeparatedByString:@","])
{
NSArray *bindingParams = [each componentsSeparatedByString:@":"];
NSString *propName = [((NSString *) [bindingParams objectAtIndex:0]) stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *valueName = [((NSString *) [bindingParams objectAtIndex:1]) stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (![propName isEqualToString:@"iterate"] && ![valueName isEqualToString:@"self"]) {
@try {
id value = [element valueForKeyPath:propName];
if (propName!= nil && value!=nil)
[data setValue:value forKeyPath:valueName];
else if (valueName != nil && value==nil)
[data setNilValueForKey:valueName];
}
@catch (NSException *exception) {
NSLog(@"Couldn't set property %@ on object %@", valueName, data);
}
}
}
}
@end