feat(ios): addDelegateHandler to add App Delegate handlers (#10371)

This commit is contained in:
Igor Randjelovic
2023-09-02 05:37:31 +02:00
committed by GitHub
parent 8d25d251cd
commit a959a797df
3 changed files with 54 additions and 0 deletions

View File

@ -12,6 +12,13 @@ if (Application.ios) {
Application.ios.addNotificationObserver(UIApplicationDidFinishLaunchingNotification, (notification: NSNotification) => { Application.ios.addNotificationObserver(UIApplicationDidFinishLaunchingNotification, (notification: NSNotification) => {
console.log('UIApplicationDidFinishLaunchingNotification:', notification); console.log('UIApplicationDidFinishLaunchingNotification:', notification);
}); });
// Make sure we can add multiple handlers for the same event.
for (let i = 0; i < 10; i++) {
Application.ios.addDelegateHandler('applicationDidBecomeActive', (application: UIApplication) => {
console.log('applicationDidBecomeActive', i, application);
});
}
} }
// Common events for both Android and iOS. // Common events for both Android and iOS.

View File

@ -167,6 +167,14 @@ export class iOSApplication extends ApplicationCommon {
*/ */
set delegate(value: UIApplicationDelegate | unknown); set delegate(value: UIApplicationDelegate | unknown);
/**
* Adds a delegate handler for the specified delegate method name. This method does not replace an existing handler,
* but rather adds the new handler to the existing chain of handlers.
* @param methodName The name of the delegate method to add a handler for.
* @param handler A function that will be called when the specified delegate method is called.
*/
addDelegateHandler<T extends keyof UIApplicationDelegate>(methodName: T, handler: (typeof UIApplicationDelegate.prototype)[T]): void;
/** /**
* Adds an observer to the default notification center for the specified notification. * Adds an observer to the default notification center for the specified notification.
* For more information, please visit 'https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:' * For more information, please visit 'https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:'

View File

@ -74,6 +74,7 @@ class Responder extends UIResponder implements UIApplicationDelegate {
export class iOSApplication extends ApplicationCommon implements IiOSApplication { export class iOSApplication extends ApplicationCommon implements IiOSApplication {
private _delegate: UIApplicationDelegate; private _delegate: UIApplicationDelegate;
private _delegateHandlers = new Map<string, Array<Function>>();
private _window: UIWindow; private _window: UIWindow;
private _notificationObservers: NotificationObserver[] = []; private _notificationObservers: NotificationObserver[] = [];
private _rootView: View; private _rootView: View;
@ -255,6 +256,44 @@ export class iOSApplication extends ApplicationCommon implements IiOSApplication
} }
} }
addDelegateHandler<T extends keyof UIApplicationDelegate>(methodName: T, handler: (typeof UIApplicationDelegate.prototype)[T]): void {
// safe-guard against invalid handlers
if (typeof handler !== 'function') {
return;
}
// ensure we have a delegate
this.delegate ??= Responder as any;
const handlers = this._delegateHandlers.get(methodName) ?? [];
if (!this._delegateHandlers.has(methodName)) {
const originalHandler = this.delegate.prototype[methodName];
if (originalHandler) {
// if there is an original handler, we add it to the handlers array to be called first.
handlers.push(originalHandler as Function);
}
// replace the original method implementation with one that will call all handlers.
this.delegate.prototype[methodName] = function (...args: any[]) {
let res: any;
for (const handler of handlers) {
if (typeof handler !== 'function') {
continue;
}
res = handler.apply(this, args);
}
return res;
} as (typeof UIApplicationDelegate.prototype)[T];
// store the handlers
this._delegateHandlers.set(methodName, handlers);
}
handlers.push(handler);
}
getNativeApplication() { getNativeApplication() {
return this.nativeApp; return this.nativeApp;
} }