This repository was archived by the owner on Sep 14, 2023. It is now read-only.
forked from flutter-team-archive/plugins
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFWFObjectHostApi.m
More file actions
125 lines (110 loc) · 5.21 KB
/
Copy pathFWFObjectHostApi.m
File metadata and controls
125 lines (110 loc) · 5.21 KB
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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "FWFObjectHostApi.h"
#import "FWFDataConverters.h"
@interface FWFObjectFlutterApiImpl ()
// BinaryMessenger and InstanceManager must be weak to prevent a circular reference
// with the objects it stores.
@property(nonatomic, weak) FWFInstanceManager *instanceManager;
@end
@implementation FWFObjectFlutterApiImpl
- (instancetype)initWithBinaryMessenger:(id<FlutterBinaryMessenger>)binaryMessenger
instanceManager:(FWFInstanceManager *)instanceManager {
self = [self initWithBinaryMessenger:binaryMessenger];
if (self) {
_instanceManager = instanceManager;
}
return self;
}
- (long)identifierForObject:(NSObject *)instance {
return [self.instanceManager identifierWithStrongReferenceForInstance:instance];
}
- (void)observeValueForObject:(NSObject *)instance
keyPath:(NSString *)keyPath
object:(NSObject *)object
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
completion:(void (^)(NSError *_Nullable))completion {
NSMutableArray<FWFNSKeyValueChangeKeyEnumData *> *changeKeys = [NSMutableArray array];
NSMutableArray<id> *changeValues = [NSMutableArray array];
[change enumerateKeysAndObjectsUsingBlock:^(NSKeyValueChangeKey key, id value, BOOL *stop) {
[changeKeys addObject:FWFNSKeyValueChangeKeyEnumDataFromNSKeyValueChangeKey(key)];
[changeValues addObject:value];
}];
NSNumber *objectIdentifier =
@([self.instanceManager identifierWithStrongReferenceForInstance:object]);
[self observeValueForObjectWithIdentifier:@([self identifierForObject:instance])
keyPath:keyPath
objectIdentifier:objectIdentifier
changeKeys:changeKeys
changeValues:changeValues
completion:completion];
}
@end
@implementation FWFObject
- (instancetype)initWithBinaryMessenger:(id<FlutterBinaryMessenger>)binaryMessenger
instanceManager:(FWFInstanceManager *)instanceManager {
self = [self init];
if (self) {
_objectApi = [[FWFObjectFlutterApiImpl alloc] initWithBinaryMessenger:binaryMessenger
instanceManager:instanceManager];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
context:(void *)context {
[self.objectApi observeValueForObject:self
keyPath:keyPath
object:object
change:change
completion:^(NSError *error) {
NSAssert(!error, @"%@", error);
}];
}
@end
@interface FWFObjectHostApiImpl ()
// BinaryMessenger and InstanceManager must be weak to prevent a circular reference
// with the objects it stores.
@property(nonatomic, weak) FWFInstanceManager *instanceManager;
@end
@implementation FWFObjectHostApiImpl
- (instancetype)initWithInstanceManager:(FWFInstanceManager *)instanceManager {
self = [self init];
if (self) {
_instanceManager = instanceManager;
}
return self;
}
- (NSObject *)objectForIdentifier:(NSNumber *)identifier {
return (NSObject *)[self.instanceManager instanceForIdentifier:identifier.longValue];
}
- (void)addObserverForObjectWithIdentifier:(nonnull NSNumber *)identifier
observerIdentifier:(nonnull NSNumber *)observer
keyPath:(nonnull NSString *)keyPath
options:
(nonnull NSArray<FWFNSKeyValueObservingOptionsEnumData *> *)
options
error:(FlutterError *_Nullable *_Nonnull)error {
NSKeyValueObservingOptions optionsInt = 0;
for (FWFNSKeyValueObservingOptionsEnumData *data in options) {
optionsInt |= FWFNSKeyValueObservingOptionsFromEnumData(data);
}
[[self objectForIdentifier:identifier] addObserver:[self objectForIdentifier:observer]
forKeyPath:keyPath
options:optionsInt
context:nil];
}
- (void)removeObserverForObjectWithIdentifier:(nonnull NSNumber *)identifier
observerIdentifier:(nonnull NSNumber *)observer
keyPath:(nonnull NSString *)keyPath
error:(FlutterError *_Nullable *_Nonnull)error {
[[self objectForIdentifier:identifier] removeObserver:[self objectForIdentifier:observer]
forKeyPath:keyPath];
}
- (void)disposeObjectWithIdentifier:(nonnull NSNumber *)identifier
error:(FlutterError *_Nullable *_Nonnull)error {
[self.instanceManager removeInstanceWithIdentifier:identifier.longValue];
}
@end