-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBindWays.m
230 lines (182 loc) · 5.95 KB
/
BindWays.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// Created by Pantelis Kalogiros on 26/11/15
//
#import "BindWays.h"
#import <objc/runtime.h>
@interface BindWayStorage : NSObject
@property void *CTX;
+ (id) getInstance;
@end
@implementation BindWayStorage
{
NSMutableDictionary *bindict_;
}
+ (id)getInstance
{
static BindWayStorage *instance = nil;
static dispatch_once_t once;
dispatch_once (&once, ^{
instance = [[self alloc] init];
});
return (instance);
}
- (BindWayStorage *)init
{
static void *CTX = &CTX;
self.CTX = CTX;
bindict_ = [NSMutableDictionary dictionary];
return (self);
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)source
change:(NSDictionary<NSString *,id> *)change
context:(void *)context
{
NSMutableDictionary *target_dict = [bindict_ objectForKey:mem_address (source)];
if (!target_dict) return ;
NSPointerArray *events_arr = [target_dict objectForKey:keyPath];
if (!events_arr) return ;
UInt16 l = (UInt16)events_arr.count;
for (int i = 0; i < l; i += 2)
{
id target = [events_arr pointerAtIndex:i];
NSString *key = [events_arr pointerAtIndex:i + 1];
if ([target valueForKey:key] != [source valueForKey:keyPath])
[target setValue:[source valueForKey:keyPath] forKey:key];
}
}
- (void)setUpFor:(id) source
and:(id) target
withKey:(NSString *)key_source
andKey:(NSString *)key_target
{
NSString *address = mem_address (source);
if (!key_target) key_target = key_source;
NSMutableDictionary *events_dict = [bindict_ objectForKey:address];
if (!events_dict)
{
events_dict = [NSMutableDictionary dictionary];
[bindict_ setObject:events_dict forKey:address];
[self attachToDealloc:source];
}
NSPointerArray *events_arr = [events_dict objectForKey:key_source];
if (!events_arr)
{
events_arr = [[NSPointerArray alloc] initWithOptions:NSPointerFunctionsWeakMemory];
[events_dict setObject:events_arr forKey:key_source];
}
else
clean_array_from_nil (events_arr);
[events_arr addPointer:(__bridge void * _Nullable)(target)];
[events_arr addPointer:(__bridge void * _Nullable)(key_target)];
}
- (void)attachToDealloc:(id)obj
{
Class class = [obj class];
SEL dealloc_sel = NSSelectorFromString (@"dealloc");
Method dealloc_meth = class_getInstanceMethod (class, dealloc_sel);
IMP dealloc_imp = method_getImplementation (dealloc_meth);
IMP dealloc_imp_swizz = imp_implementationWithBlock (^(void *el) {
@autoreleasepool
{
[self unbindAll:(__bridge id)el];
((void (*)(void *, SEL))dealloc_imp) (el, dealloc_sel);
}
});
class_replaceMethod(class,
dealloc_sel,
dealloc_imp_swizz,
method_getTypeEncoding (dealloc_meth));
}
- (void)unbindAll:(id)obj
{
NSString *address = mem_address (obj);
NSDictionary *dict = [bindict_ objectForKey:address];
for (NSString * key in dict)
[obj removeObserver:self forKeyPath:key context:self.CTX];
[bindict_ removeObjectForKey:address];
}
- (void)unbindAll:(id)obj ofProperty:(NSString *)property
{
NSString *address = mem_address (obj);
NSMutableDictionary *dict = [bindict_ objectForKey:address];
[dict removeObjectForKey:property];
[obj removeObserver:self forKeyPath:property context:self.CTX];
}
static inline NSString * mem_address (id o)
{
return ([NSString stringWithFormat:@"%p", o]);
}
static inline void clean_array_from_nil (NSPointerArray *arr)
{
int l = (int)arr.count - 2;
if (l < 1) return ;
for (;l > 0;l -= 2)
{
if ([arr pointerAtIndex:l] == nil)
{
[arr removePointerAtIndex:l];
[arr removePointerAtIndex:l];
}
}
}
@end
@implementation OneWay
+ (void)bind:(__weak NSString *)property of:(__weak id)source to:(__weak id)parent
{
[OneWay bind:property of:source to:nil of:parent];
}
+ (void)bind:(__weak NSString *)prop of:(__weak id)source to:(__weak NSString *)property of:(__weak id)target
{
BindWayStorage *storage = [BindWayStorage getInstance];
if (!property) property = prop;
[source addObserver:storage
forKeyPath:prop
options:NSKeyValueObservingOptionNew
context:storage.CTX];
[storage setUpFor:source and:target withKey:prop andKey:property];
}
+ (void)unbindAll:(__weak id)source
{
BindWayStorage *storage = [BindWayStorage getInstance];
[storage unbindAll:source];
}
+ (void)unbindAll:(__weak id)source ofProperty:(NSString *)property
{
BindWayStorage *storage = [BindWayStorage getInstance];
[storage unbindAll:source ofProperty:property];
}
@end
@implementation TwoWay
+ (void)bind:(__weak NSString *)property of:(__weak id)source to:(__weak id)parent
{
[TwoWay bind:property of:source to:nil of:parent];
}
+ (void)bind:(__weak NSString *)prop of:(__weak id)source to:(__weak NSString *)property of:(__weak id)target
{
BindWayStorage *storage = [BindWayStorage getInstance];
if (!property) property = prop;
[source addObserver:storage
forKeyPath:prop
options:NSKeyValueObservingOptionNew
context:storage.CTX];
[storage setUpFor:source and:target withKey:prop andKey:property];
[target addObserver:storage
forKeyPath:property
options:NSKeyValueObservingOptionNew
context:storage.CTX];
[storage setUpFor:target and:source withKey:property andKey:prop];
}
// @todo finish the unbind methods for 2 way
+ (void)unbindAll:(__weak id)source
{
}
// unbind all listeners between 2 objects
+ (void)unbindAll:(__weak id)source and:(__weak id)target
{
}
// unbind all listeners between 2 objects with specified property
+ (void)unbindAll:(__weak id)source ofProperty:(NSString *)property
{
}
@end