Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
foundation flutter apis
  • Loading branch information
bparrishMines committed Jun 7, 2022
commit adf9875e9eb84db126da1b4e9223d1cc8f8afc41
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ Iterable<NSKeyValueObservingOptionsEnumData>
});
}

extension _NSKeyValueChangeKeyEnumDataConverter on NSKeyValueChangeKeyEnumData {
NSKeyValueChangeKey toNSKeyValueChangeKey() {
return NSKeyValueChangeKey.values.firstWhere(
(NSKeyValueChangeKey element) => element.name == value.name,
);
}
}

/// Handles initialization of Flutter APIs for the Foundation library.
// TODO(bparrishMines): Add NSObjectFlutterApiImpl once the callback methods
// are added.
Expand Down Expand Up @@ -132,3 +140,41 @@ class NSObjectHostApiImpl extends NSObjectHostApi {
instanceManager == other.instanceManager;
}
}

/// Flutter api implementation for [NSObject].
class NSObjectFlutterApiImpl extends NSObjectFlutterApi {
/// Constructs a [NSObjectFlutterApiImpl].
NSObjectFlutterApiImpl({InstanceManager? instanceManager}) {
this.instanceManager = instanceManager ?? NSObject.globalInstanceManager;
}

/// Maintains instances stored to communicate with native language objects.
late final InstanceManager instanceManager;

NSObject _getObject(int identifier) {
return instanceManager.getInstanceWithWeakReference(identifier)!;
}

@override
void observeValue(
int identifier,
String keyPath,
int objectIdentifier,
List<NSKeyValueChangeKeyEnumData?> changeKeys,
List<Object?> changeValues,
) {
final void Function(String, NSObject, Map<NSKeyValueChangeKey, Object?>)?
function = _getObject(identifier).observeValue;
function?.call(
keyPath,
instanceManager.getInstanceWithWeakReference(objectIdentifier)!
as NSObject,
Map<NSKeyValueChangeKey, Object?>.fromIterables(
changeKeys.map<NSKeyValueChangeKey>(
(NSKeyValueChangeKeyEnumData? data) {
return data!.toNSKeyValueChangeKey();
},
), changeValues),
);
}
}