diff --git a/package.json b/package.json index 6306960de..3759ffe5e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "scripts": { "nx": "nx", "format": "nx format:write", - "setup": "npx rimraf -- hooks node_modules package-lock.json && npm i && nx run core:setup" + "setup": "npx rimraf -- hooks node_modules package-lock.json && npm i && ts-patch install && nx run core:setup" }, "private": true, "dependencies": { @@ -49,6 +49,7 @@ "terser-webpack-plugin": "~3.0.6", "ts-jest": "~26.1.1", "ts-node": "~8.10.2", + "ts-patch": "^1.2.5", "tslint": "~6.1.2", "typescript": "~3.9.0", "webpack": "~4.43.0", diff --git a/packages/core/__tests__/e2e/automated/app/ui/layouts/layout-helper.android.ts b/packages/core/__tests__/e2e/automated/app/ui/layouts/layout-helper.android.ts index 6b1e57fd1..0f73e2cf7 100644 --- a/packages/core/__tests__/e2e/automated/app/ui/layouts/layout-helper.android.ts +++ b/packages/core/__tests__/e2e/automated/app/ui/layouts/layout-helper.android.ts @@ -8,6 +8,7 @@ import * as def from "./layout-helper"; var DELTA = 0.1; +@NativeClass class NativeButton extends android.widget.Button { constructor(context: android.content.Context, public owner: def.MeasuredView) { super(context); @@ -28,6 +29,7 @@ class NativeButton extends android.widget.Button { } } +@NativeClass class NativeStackLayout extends org.nativescript.widgets.StackLayout { constructor(context: android.content.Context, public owner: def.MeasuredView) { super(context); @@ -48,6 +50,7 @@ class NativeStackLayout extends org.nativescript.widgets.StackLayout { } } +@NativeClass class NativeGridLayout extends org.nativescript.widgets.GridLayout { constructor(context: android.content.Context, public owner: def.MeasuredView) { super(context); diff --git a/packages/core/application/index.android.ts b/packages/core/application/index.android.ts index 13a900284..8400d3304 100644 --- a/packages/core/application/index.android.ts +++ b/packages/core/application/index.android.ts @@ -478,6 +478,7 @@ function ensureBroadCastReceiverClass() { return; } + @NativeClass class BroadcastReceiver extends android.content.BroadcastReceiver { private _onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void; @@ -500,6 +501,7 @@ function ensureBroadCastReceiverClass() { declare namespace com { namespace tns { + @NativeClass class NativeScriptApplication extends android.app.Application { static getInstance(): NativeScriptApplication; } diff --git a/packages/core/application/index.ios.ts b/packages/core/application/index.ios.ts index 2b7c86d4f..cb723fbbf 100644 --- a/packages/core/application/index.ios.ts +++ b/packages/core/application/index.ios.ts @@ -25,6 +25,7 @@ const majorVersion = iOSNativeHelper.MajorVersion; // NOTE: UIResponder with implementation of window - related to https://github.com/NativeScript/ios-runtime/issues/430 // TODO: Refactor the UIResponder to use Typescript extends when this issue is resolved: // https://github.com/NativeScript/ios-runtime/issues/1012 + const Responder = (UIResponder).extend( { get window() { @@ -39,6 +40,7 @@ const Responder = (UIResponder).extend( } ); +@NativeClass class NotificationObserver extends NSObject { private _onReceiveCallback: (notification: NSNotification) => void; @@ -61,6 +63,8 @@ class NotificationObserver extends NSObject { let displayedOnce = false; let displayedLinkTarget; let displayedLink; + +@NativeClass class CADisplayLinkTarget extends NSObject { onDisplayed(link: CADisplayLink) { link.invalidate(); diff --git a/packages/core/connectivity/index.android.ts b/packages/core/connectivity/index.android.ts index e1aadf5f4..bc400cc87 100644 --- a/packages/core/connectivity/index.android.ts +++ b/packages/core/connectivity/index.android.ts @@ -109,29 +109,31 @@ let networkCallback; let notifyCallback; export function startMonitoring(connectionTypeChangedCallback: (newConnectionType: number) => void): void { if (android.os.Build.VERSION.SDK_INT >= 28) { - const manager = getConnectivityManager() as any; + const manager = getConnectivityManager(); if (manager) { notifyCallback = () => { let newConnectionType = getConnectionType(); let zoneCallback = zonedCallback(connectionTypeChangedCallback); zoneCallback(newConnectionType); }; - const ConnectivityManager = (android as any).net.ConnectivityManager; + const ConnectivityManager = android.net.ConnectivityManager; if (!networkCallback) { - networkCallback = ConnectivityManager.NetworkCallback.extend({ - onAvailable(network) { + @NativeClass + class NetworkCallbackImpl extends ConnectivityManager.NetworkCallback{ + onAvailable(network: android.net.Network) { notifyCallback(); - }, - onCapabilitiesChanged(network, networkCapabilities) { + } + onCapabilitiesChanged(network: android.net.Network, networkCapabilities: android.net.NetworkCapabilities) { notifyCallback(); - }, + } onLost(network) { notifyCallback(); - }, + } onUnavailable() { notifyCallback(); - }, - }); + } + }; + networkCallback = NetworkCallbackImpl; } callback = new networkCallback(); manager.registerDefaultNetworkCallback(callback); diff --git a/packages/core/fps-meter/fps-native.ios.ts b/packages/core/fps-meter/fps-native.ios.ts index 46456ce35..0d37d7275 100644 --- a/packages/core/fps-meter/fps-native.ios.ts +++ b/packages/core/fps-meter/fps-native.ios.ts @@ -1,5 +1,6 @@ import * as definition from './fps-native'; +@NativeClass class FrameHandlerImpl extends NSObject { private _owner: WeakRef; diff --git a/packages/core/global-types.d.ts b/packages/core/global-types.d.ts index 409e53e30..eaeb47ba9 100644 --- a/packages/core/global-types.d.ts +++ b/packages/core/global-types.d.ts @@ -202,6 +202,18 @@ declare var exports: any; declare function Deprecated(target: Object, key?: string | symbol, value?: any): void; declare function Experimental(target: Object, key?: string | symbol, value?: any): void; +declare interface NativeClassOptions { + nativeClassName?: string; // for @JavaProxy and + protocols?: any[]; + interfaces?: any[]; +} + +/** + * Decorates class that extends a native class(iOS or Android) + */ +declare function NativeClass(constructor:T); +declare function NativeClass(options?:NativeClassOptions); + /** * Decorates class that implements native Java interfaces. * @param interfaces Implemented interfaces. diff --git a/packages/core/http/http-request/index.ios.ts b/packages/core/http/http-request/index.ios.ts index fbec23290..fd802de31 100644 --- a/packages/core/http/http-request/index.ios.ts +++ b/packages/core/http/http-request/index.ios.ts @@ -31,6 +31,7 @@ function parseJSON(source: string): any { return JSON.parse(src); } +@NativeClass class NSURLSessionTaskDelegateImpl extends NSObject implements NSURLSessionTaskDelegate { public static ObjCProtocols = [NSURLSessionTaskDelegate]; public URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler(session: NSURLSession, task: NSURLSessionTask, response: NSHTTPURLResponse, request: NSURLRequest, completionHandler: (p1: NSURLRequest) => void): void { diff --git a/packages/core/timer/index.ios.ts b/packages/core/timer/index.ios.ts index dc5e8454d..bc1fa4999 100644 --- a/packages/core/timer/index.ios.ts +++ b/packages/core/timer/index.ios.ts @@ -7,6 +7,7 @@ interface KeyValuePair { v: V; } +@NativeClass class TimerTargetImpl extends NSObject { private callback: Function; private disposed: boolean; diff --git a/packages/core/tsconfig.lib.json b/packages/core/tsconfig.lib.json index 0cae837a5..dff655588 100644 --- a/packages/core/tsconfig.lib.json +++ b/packages/core/tsconfig.lib.json @@ -3,7 +3,6 @@ "compilerOptions": { "noEmitOnError": true, "noEmitHelpers": true, - "target": "es5", "declaration": true, "noImplicitAny": false, "noImplicitUseStrict": true, @@ -12,11 +11,13 @@ "diagnostics": true, "sourceMap": true, "inlineSourceMap": false, - "module": "commonjs", "baseUrl": ".", "outDir": "../../dist/out-tsc", "rootDir": "./", - "types": ["node"] + "types": ["node"], + "plugins": [ + { "transform": "../../../packages/webpack/transformers/ns-transform-native-classes.ts", "type": "raw" } + ] }, "exclude": ["**/*.spec.ts", "dist", "__tests__"], "include": ["**/*.ts", "./references.d.ts"] diff --git a/packages/core/ui/action-bar/index.ios.ts b/packages/core/ui/action-bar/index.ios.ts index 13759269d..bc557be49 100644 --- a/packages/core/ui/action-bar/index.ios.ts +++ b/packages/core/ui/action-bar/index.ios.ts @@ -35,6 +35,7 @@ function loadActionIcon(item: ActionItemDefinition): any /* UIImage */ { return img; } +@NativeClass class TapBarItemHandlerImpl extends NSObject { private _owner: WeakRef; diff --git a/packages/core/ui/animation/index.ios.ts b/packages/core/ui/animation/index.ios.ts index fc9e602c5..c9080542f 100644 --- a/packages/core/ui/animation/index.ios.ts +++ b/packages/core/ui/animation/index.ios.ts @@ -29,6 +29,7 @@ class AnimationInfo { public delay: number; } +@NativeClass class AnimationDelegateImpl extends NSObject implements CAAnimationDelegate { public nextAnimation: Function; diff --git a/packages/core/ui/bottom-navigation/index.android.ts b/packages/core/ui/bottom-navigation/index.android.ts index a089a2c19..3f9a72311 100644 --- a/packages/core/ui/bottom-navigation/index.android.ts +++ b/packages/core/ui/bottom-navigation/index.android.ts @@ -54,6 +54,7 @@ function initializeNativeClasses() { return; } + @NativeClass class TabFragmentImplementation extends org.nativescript.widgets.FragmentBase { private owner: BottomNavigation; private index: number; @@ -143,6 +144,7 @@ function initializeNativeClasses() { } } + @NativeClass class BottomNavigationBarImplementation extends org.nativescript.widgets.BottomNavigationBar { constructor(context: android.content.Context, public owner: BottomNavigation) { super(context); diff --git a/packages/core/ui/bottom-navigation/index.ios.ts b/packages/core/ui/bottom-navigation/index.ios.ts index 93e0749fc..95caab676 100644 --- a/packages/core/ui/bottom-navigation/index.ios.ts +++ b/packages/core/ui/bottom-navigation/index.ios.ts @@ -94,6 +94,7 @@ class UITabBarControllerImpl extends UITabBarController { } } +@NativeClass class UITabBarControllerDelegateImpl extends NSObject implements UITabBarControllerDelegate { public static ObjCProtocols = [UITabBarControllerDelegate]; @@ -160,6 +161,7 @@ class UITabBarControllerDelegateImpl extends NSObject implements UITabBarControl } } +@NativeClass class UINavigationControllerDelegateImpl extends NSObject implements UINavigationControllerDelegate { public static ObjCProtocols = [UINavigationControllerDelegate]; diff --git a/packages/core/ui/button/index.ios.ts b/packages/core/ui/button/index.ios.ts index d48d92af2..7b86fee43 100644 --- a/packages/core/ui/button/index.ios.ts +++ b/packages/core/ui/button/index.ios.ts @@ -265,6 +265,7 @@ export class Button extends ButtonBase { } } +@NativeClass class TapHandlerImpl extends NSObject { private _owner: WeakRef