From 502d2632814b0eb74f2cb14c17ea5e0ea281f4bd Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Fri, 24 Jul 2020 14:37:14 -0700 Subject: [PATCH] chore: cleanup --- packages/core/application/index.android.ts | 1 - packages/core/bundle-entry-points.ts | 92 +++-- packages/core/css/css-tree-parser.ts | 3 +- packages/core/global-types.d.ts | 1 - packages/core/globals/index.ts | 3 +- packages/core/module-name-resolver/index.ts | 6 - packages/core/package.json | 3 +- packages/core/platform/index.android.ts | 8 +- packages/core/ui/core/bindable/index.ts | 5 +- .../ui/core/view/view-helper/index.ios.ts | 340 +++++++++--------- packages/core/ui/frame/activity.android.ts | 112 ++++-- packages/core/ui/frame/index.android.ts | 37 +- packages/core/ui/frame/index.ios.ts | 1 + packages/core/ui/page/index.ios.ts | 1 + packages/core/xml/index.ts | 7 +- .../webpack/android-app-components-loader.js | 4 +- 16 files changed, 348 insertions(+), 276 deletions(-) diff --git a/packages/core/application/index.android.ts b/packages/core/application/index.android.ts index 8400d3304..6fa1585d9 100644 --- a/packages/core/application/index.android.ts +++ b/packages/core/application/index.android.ts @@ -501,7 +501,6 @@ function ensureBroadCastReceiverClass() { declare namespace com { namespace tns { - @NativeClass class NativeScriptApplication extends android.app.Application { static getInstance(): NativeScriptApplication; } diff --git a/packages/core/bundle-entry-points.ts b/packages/core/bundle-entry-points.ts index 4a3e1d3fc..b286c6e68 100644 --- a/packages/core/bundle-entry-points.ts +++ b/packages/core/bundle-entry-points.ts @@ -1,49 +1,47 @@ -if (global.TNS_WEBPACK) { - const g = require('./globals'); - g.initGlobal(); +const g = require('./globals'); +g.initGlobal(); - // Register "dynamically" loaded module that need to be resolved by the - // XML/component builders. +// Register "dynamically" loaded module that need to be resolved by the +// XML/component builders. - global.registerModule('text/formatted-string', () => require('./text/formatted-string')); - global.registerModule('text/span', () => require('./text/span')); - global.registerModule('ui/text-base/formatted-string', () => require('./ui/text-base/formatted-string')); - global.registerModule('ui/text-base/span', () => require('./ui/text-base/span')); - global.registerModule('ui/action-bar', () => require('./ui/action-bar')); - global.registerModule('ui/activity-indicator', () => require('./ui/activity-indicator')); - global.registerModule('ui/bottom-navigation', () => require('./ui/bottom-navigation')); - global.registerModule('ui/button', () => require('./ui/button')); - global.registerModule('ui/content-view', () => require('./ui/content-view')); - global.registerModule('ui/date-picker', () => require('./ui/date-picker')); - global.registerModule('ui/frame', () => require('./ui/frame')); - global.registerModule('ui/html-view', () => require('./ui/html-view')); - global.registerModule('ui/image', () => require('./ui/image')); - global.registerModule('ui/label', () => require('./ui/label')); - global.registerModule('ui/layouts/absolute-layout', () => require('./ui/layouts/absolute-layout')); - global.registerModule('ui/layouts/dock-layout', () => require('./ui/layouts/dock-layout')); - global.registerModule('ui/layouts/grid-layout', () => require('./ui/layouts/grid-layout')); - global.registerModule('ui/layouts/stack-layout', () => require('./ui/layouts/stack-layout')); - global.registerModule('ui/layouts/flexbox-layout', () => require('./ui/layouts/flexbox-layout')); - global.registerModule('ui/layouts/wrap-layout', () => require('./ui/layouts/wrap-layout')); - global.registerModule('ui/list-picker', () => require('./ui/list-picker')); - global.registerModule('ui/page', () => require('./ui/page')); - global.registerModule('ui/placeholder', () => require('./ui/placeholder')); - global.registerModule('ui/progress', () => require('./ui/progress')); - global.registerModule('ui/proxy-view-container', () => require('./ui/proxy-view-container')); - global.registerModule('ui/repeater', () => require('./ui/repeater')); - global.registerModule('ui/scroll-view', () => require('./ui/scroll-view')); - global.registerModule('ui/search-bar', () => require('./ui/search-bar')); - global.registerModule('ui/segmented-bar', () => require('./ui/segmented-bar')); - global.registerModule('ui/slider', () => require('./ui/slider')); - global.registerModule('ui/switch', () => require('./ui/switch')); - global.registerModule('ui/tab-view', () => require('./ui/tab-view')); - global.registerModule('ui/tab-navigation-base/tab-strip', () => require('./ui/tab-navigation-base/tab-strip')); - global.registerModule('ui/tab-navigation-base/tab-strip-item', () => require('./ui/tab-navigation-base/tab-strip-item')); - global.registerModule('ui/tab-navigation-base/tab-content-item', () => require('./ui/tab-navigation-base/tab-content-item')); - global.registerModule('ui/tabs', () => require('./ui/tabs')); - global.registerModule('ui/web-view', () => require('./ui/web-view')); - global.registerModule('ui/text-field', () => require('./ui/text-field')); - global.registerModule('ui/text-view', () => require('./ui/text-view')); - global.registerModule('ui/time-picker', () => require('./ui/time-picker')); - global.registerModule('ui/list-view', () => require('./ui/list-view')); -} +global.registerModule('text/formatted-string', () => require('./text/formatted-string')); +global.registerModule('text/span', () => require('./text/span')); +global.registerModule('ui/text-base/formatted-string', () => require('./ui/text-base/formatted-string')); +global.registerModule('ui/text-base/span', () => require('./ui/text-base/span')); +global.registerModule('ui/action-bar', () => require('./ui/action-bar')); +global.registerModule('ui/activity-indicator', () => require('./ui/activity-indicator')); +global.registerModule('ui/bottom-navigation', () => require('./ui/bottom-navigation')); +global.registerModule('ui/button', () => require('./ui/button')); +global.registerModule('ui/content-view', () => require('./ui/content-view')); +global.registerModule('ui/date-picker', () => require('./ui/date-picker')); +global.registerModule('ui/frame', () => require('./ui/frame')); +global.registerModule('ui/html-view', () => require('./ui/html-view')); +global.registerModule('ui/image', () => require('./ui/image')); +global.registerModule('ui/label', () => require('./ui/label')); +global.registerModule('ui/layouts/absolute-layout', () => require('./ui/layouts/absolute-layout')); +global.registerModule('ui/layouts/dock-layout', () => require('./ui/layouts/dock-layout')); +global.registerModule('ui/layouts/grid-layout', () => require('./ui/layouts/grid-layout')); +global.registerModule('ui/layouts/stack-layout', () => require('./ui/layouts/stack-layout')); +global.registerModule('ui/layouts/flexbox-layout', () => require('./ui/layouts/flexbox-layout')); +global.registerModule('ui/layouts/wrap-layout', () => require('./ui/layouts/wrap-layout')); +global.registerModule('ui/list-picker', () => require('./ui/list-picker')); +global.registerModule('ui/page', () => require('./ui/page')); +global.registerModule('ui/placeholder', () => require('./ui/placeholder')); +global.registerModule('ui/progress', () => require('./ui/progress')); +global.registerModule('ui/proxy-view-container', () => require('./ui/proxy-view-container')); +global.registerModule('ui/repeater', () => require('./ui/repeater')); +global.registerModule('ui/scroll-view', () => require('./ui/scroll-view')); +global.registerModule('ui/search-bar', () => require('./ui/search-bar')); +global.registerModule('ui/segmented-bar', () => require('./ui/segmented-bar')); +global.registerModule('ui/slider', () => require('./ui/slider')); +global.registerModule('ui/switch', () => require('./ui/switch')); +global.registerModule('ui/tab-view', () => require('./ui/tab-view')); +global.registerModule('ui/tab-navigation-base/tab-strip', () => require('./ui/tab-navigation-base/tab-strip')); +global.registerModule('ui/tab-navigation-base/tab-strip-item', () => require('./ui/tab-navigation-base/tab-strip-item')); +global.registerModule('ui/tab-navigation-base/tab-content-item', () => require('./ui/tab-navigation-base/tab-content-item')); +global.registerModule('ui/tabs', () => require('./ui/tabs')); +global.registerModule('ui/web-view', () => require('./ui/web-view')); +global.registerModule('ui/text-field', () => require('./ui/text-field')); +global.registerModule('ui/text-view', () => require('./ui/text-view')); +global.registerModule('ui/time-picker', () => require('./ui/time-picker')); +global.registerModule('ui/list-view', () => require('./ui/list-view')); diff --git a/packages/core/css/css-tree-parser.ts b/packages/core/css/css-tree-parser.ts index 25b5661c1..e3c63bbf1 100644 --- a/packages/core/css/css-tree-parser.ts +++ b/packages/core/css/css-tree-parser.ts @@ -1,4 +1,5 @@ -import { parse } from 'css-tree'; +import { parse as parseType } from 'css-tree'; +const parse: typeof parseType = require('css-tree'); function mapSelectors(selector: string): string[] { if (!selector) { diff --git a/packages/core/global-types.d.ts b/packages/core/global-types.d.ts index 2379aadc7..d574728ca 100644 --- a/packages/core/global-types.d.ts +++ b/packages/core/global-types.d.ts @@ -90,7 +90,6 @@ declare namespace NodeJS { __onLiveSyncCore: (context?: { type: string; path: string }) => void; __onUncaughtError: (error: NativeScriptError) => void; __onDiscardedError: (error: NativeScriptError) => void; - TNS_WEBPACK?: boolean; __snapshot?: boolean; __requireOverride?: (name: string, dir: string) => any; } diff --git a/packages/core/globals/index.ts b/packages/core/globals/index.ts index 09511beab..480524d1e 100644 --- a/packages/core/globals/index.ts +++ b/packages/core/globals/index.ts @@ -1,4 +1,5 @@ -import * as tslib from 'tslib'; +import * as tslibType from 'tslib'; +const tslib: typeof tslibType = require('tslib'); import { isIOS, isAndroid } from '../platform'; type ModuleLoader = (name?: string) => any; diff --git a/packages/core/module-name-resolver/index.ts b/packages/core/module-name-resolver/index.ts index aaa4f3486..35ded3c87 100644 --- a/packages/core/module-name-resolver/index.ts +++ b/packages/core/module-name-resolver/index.ts @@ -36,12 +36,6 @@ export class ModuleNameResolver { let result: string = null; ext = ext ? '.' + ext : ''; - // Compatibility path for non-webpack workflow - // register modules from FS first - if (!global.TNS_WEBPACK) { - registerModulesFromFileSystem(path); - } - // This call will return a clean path without qualifiers path = stripQualifiers(path); diff --git a/packages/core/package.json b/packages/core/package.json index 35bfc66ab..9bacc10cf 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -26,7 +26,8 @@ "css-tree": "^1.0.0-alpha.39", "@nativescript/hook": "~1.0.0", "reduce-css-calc": "^2.1.7", - "tns-core-modules": "rc" + "tns-core-modules": "rc", + "tslib": "2.0.0" }, "nativescript": { "platforms": { diff --git a/packages/core/platform/index.android.ts b/packages/core/platform/index.android.ts index da03ae99c..265e6db4e 100644 --- a/packages/core/platform/index.android.ts +++ b/packages/core/platform/index.android.ts @@ -1,5 +1,5 @@ /* tslint:disable:class-name */ -import * as appModule from '../application'; +import { getNativeApplication, on, off, orientationChangedEvent } from '../application'; const MIN_TABLET_PIXELS = 600; @@ -19,15 +19,15 @@ class MainScreen { } private initMetrics(): void { - const nativeApp = appModule.getNativeApplication(); + const nativeApp = getNativeApplication(); nativeApp.getSystemService(android.content.Context.WINDOW_SERVICE).getDefaultDisplay().getRealMetrics(this._metrics); } private get metrics(): android.util.DisplayMetrics { if (!this._metrics) { // NOTE: This will be memory leak but we MainScreen is singleton - appModule.on('cssChanged', this.reinitMetrics, this); - appModule.on(appModule.orientationChangedEvent, this.reinitMetrics, this); + on('cssChanged', this.reinitMetrics, this); + on(orientationChangedEvent, this.reinitMetrics, this); this._metrics = new android.util.DisplayMetrics(); this.initMetrics(); diff --git a/packages/core/ui/core/bindable/index.ts b/packages/core/ui/core/bindable/index.ts index ec7d73b9b..dae3e4995 100644 --- a/packages/core/ui/core/bindable/index.ts +++ b/packages/core/ui/core/bindable/index.ts @@ -10,7 +10,8 @@ import { escapeRegexSymbols } from '../../../utils'; import { Trace } from '../../../trace'; import * as types from '../../../utils/types'; import * as bindableResources from './bindable-resources'; -import * as polymerExpressions from '../../../js-libs/polymer-expressions'; +const polymerExpressions = require('../../../js-libs/polymer-expressions'); +import { PolymerExpressions } from '../../../js-libs/polymer-expressions'; const contextKey = 'context'; // this regex is used to get parameters inside [] for example: @@ -373,7 +374,7 @@ export class Binding { private _getExpressionValue(expression: string, isBackConvert: boolean, changedModel: any): any { try { - let exp = polymerExpressions.PolymerExpressions.getExpression(expression); + let exp = PolymerExpressions.getExpression(expression); if (exp) { let context = (this.source && this.source.get && this.source.get()) || global; let model = {}; diff --git a/packages/core/ui/core/view/view-helper/index.ios.ts b/packages/core/ui/core/view/view-helper/index.ios.ts index b2696d48c..863defece 100644 --- a/packages/core/ui/core/view/view-helper/index.ios.ts +++ b/packages/core/ui/core/view/view-helper/index.ios.ts @@ -10,10 +10,169 @@ export * from './view-helper-common'; const majorVersion = iOSNativeHelper.MajorVersion; -export namespace IOSHelper { - export const traitCollectionColorAppearanceChangedEvent = 'traitCollectionColorAppearanceChanged'; +@NativeClass +class UILayoutViewController extends UIViewController { + public owner: WeakRef; - export function getParentWithViewController(view: View): View { + public static initWithOwner(owner: WeakRef): UILayoutViewController { + const controller = UILayoutViewController.new(); + controller.owner = owner; + + return controller; + } + + public viewDidLoad(): void { + super.viewDidLoad(); + + // Unify translucent and opaque bars layout + // this.edgesForExtendedLayout = UIRectEdgeBottom; + this.extendedLayoutIncludesOpaqueBars = true; + } + + public viewWillLayoutSubviews(): void { + super.viewWillLayoutSubviews(); + const owner = this.owner.get(); + if (owner) { + IOSHelper.updateConstraints(this, owner); + } + } + + public viewDidLayoutSubviews(): void { + super.viewDidLayoutSubviews(); + const owner = this.owner.get(); + if (owner) { + if (majorVersion >= 11) { + // Handle nested UILayoutViewController safe area application. + // Currently, UILayoutViewController can be nested only in a TabView. + // The TabView itself is handled by the OS, so we check the TabView's parent (usually a Page, but can be a Layout). + const tabViewItem = owner.parent; + const tabView = tabViewItem && tabViewItem.parent; + let parent = tabView && tabView.parent; + + // Handle Angular scenario where TabView is in a ProxyViewContainer + // It is possible to wrap components in ProxyViewContainers indefinitely + // Not using instanceof ProxyViewContainer to avoid circular dependency + // TODO: Try moving UILayoutViewController out of view module + while (parent && !parent.nativeViewProtected) { + parent = parent.parent; + } + + if (parent) { + const parentPageInsetsTop = parent.nativeViewProtected.safeAreaInsets.top; + const currentInsetsTop = this.view.safeAreaInsets.top; + const additionalInsetsTop = Math.max(parentPageInsetsTop - currentInsetsTop, 0); + + const parentPageInsetsBottom = parent.nativeViewProtected.safeAreaInsets.bottom; + const currentInsetsBottom = this.view.safeAreaInsets.bottom; + const additionalInsetsBottom = Math.max(parentPageInsetsBottom - currentInsetsBottom, 0); + + if (additionalInsetsTop > 0 || additionalInsetsBottom > 0) { + const additionalInsets = new UIEdgeInsets({ + top: additionalInsetsTop, + left: 0, + bottom: additionalInsetsBottom, + right: 0, + }); + this.additionalSafeAreaInsets = additionalInsets; + } + } + } + + IOSHelper.layoutView(this, owner); + } + } + + public viewWillAppear(animated: boolean): void { + super.viewWillAppear(animated); + const owner = this.owner.get(); + if (!owner) { + return; + } + + IOSHelper.updateAutoAdjustScrollInsets(this, owner); + + if (!owner.parent) { + owner.callLoaded(); + } + } + + public viewDidDisappear(animated: boolean): void { + super.viewDidDisappear(animated); + const owner = this.owner.get(); + if (owner && !owner.parent) { + owner.callUnloaded(); + } + } + + // Mind implementation for other controllers + public traitCollectionDidChange(previousTraitCollection: UITraitCollection): void { + super.traitCollectionDidChange(previousTraitCollection); + + if (majorVersion >= 13) { + const owner = this.owner.get(); + if (owner && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { + owner.notify({ + eventName: traitCollectionColorAppearanceChangedEvent, + object: owner, + }); + } + } + } +} + +@NativeClass +class UIAdaptivePresentationControllerDelegateImp extends NSObject implements UIAdaptivePresentationControllerDelegate { + public static ObjCProtocols = [UIAdaptivePresentationControllerDelegate]; + + private owner: WeakRef; + private closedCallback: Function; + + public static initWithOwnerAndCallback(owner: WeakRef, whenClosedCallback: Function): UIAdaptivePresentationControllerDelegateImp { + const instance = super.new(); + instance.owner = owner; + instance.closedCallback = whenClosedCallback; + + return instance; + } + + public presentationControllerDidDismiss(presentationController: UIPresentationController) { + const owner = this.owner.get(); + if (owner && typeof this.closedCallback === 'function') { + this.closedCallback(); + } + } +} + +@NativeClass +class UIPopoverPresentationControllerDelegateImp extends NSObject implements UIPopoverPresentationControllerDelegate { + public static ObjCProtocols = [UIPopoverPresentationControllerDelegate]; + + private owner: WeakRef; + private closedCallback: Function; + + public static initWithOwnerAndCallback(owner: WeakRef, whenClosedCallback: Function): UIPopoverPresentationControllerDelegateImp { + const instance = super.new(); + instance.owner = owner; + instance.closedCallback = whenClosedCallback; + + return instance; + } + + public popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) { + const owner = this.owner.get(); + if (owner && typeof this.closedCallback === 'function') { + this.closedCallback(); + } + } +} + +export class IOSHelper { + traitCollectionColorAppearanceChangedEvent = 'traitCollectionColorAppearanceChanged'; + static UILayoutViewController = UILayoutViewController; + static UIAdaptivePresentationControllerDelegateImp = UIAdaptivePresentationControllerDelegateImp; + static UIPopoverPresentationControllerDelegateImp = UIPopoverPresentationControllerDelegateImp; + + static getParentWithViewController(view: View): View { while (view && !view.viewController) { view = view.parent as View; } @@ -22,7 +181,7 @@ export namespace IOSHelper { return view; } - export function updateAutoAdjustScrollInsets(controller: UIViewController, owner: View): void { + static updateAutoAdjustScrollInsets(controller: UIViewController, owner: View): void { if (majorVersion <= 10) { owner._automaticallyAdjustsScrollViewInsets = false; // This API is deprecated, but has no alternative for <= iOS 10 @@ -33,14 +192,14 @@ export namespace IOSHelper { } } - export function updateConstraints(controller: UIViewController, owner: View): void { + static updateConstraints(controller: UIViewController, owner: View): void { if (majorVersion <= 10) { const layoutGuide = initLayoutGuide(controller); (controller.view).safeAreaLayoutGuide = layoutGuide; } } - function initLayoutGuide(controller: UIViewController) { + static initLayoutGuide(controller: UIViewController) { const rootView = controller.view; const layoutGuide = UILayoutGuide.alloc().init(); rootView.addLayoutGuide(layoutGuide); @@ -49,7 +208,7 @@ export namespace IOSHelper { return layoutGuide; } - export function layoutView(controller: UIViewController, owner: View): void { + static layoutView(controller: UIViewController, owner: View): void { let layoutGuide = controller.view.safeAreaLayoutGuide; if (!layoutGuide) { Trace.write(`safeAreaLayoutGuide during layout of ${owner}. Creating fallback constraints, but layout might be wrong.`, Trace.categories.Layout, Trace.messageType.error); @@ -80,7 +239,7 @@ export namespace IOSHelper { } } - export function getPositionFromFrame(frame: CGRect): { left; top; right; bottom } { + static getPositionFromFrame(frame: CGRect): { left; top; right; bottom } { const left = layout.round(layout.toDevicePixels(frame.origin.x)); const top = layout.round(layout.toDevicePixels(frame.origin.y)); const right = layout.round(layout.toDevicePixels(frame.origin.x + frame.size.width)); @@ -89,7 +248,7 @@ export namespace IOSHelper { return { left, right, top, bottom }; } - export function getFrameFromPosition(position: { left; top; right; bottom }, insets?: { left; top; right; bottom }): CGRect { + static getFrameFromPosition(position: { left; top; right; bottom }, insets?: { left; top; right; bottom }): CGRect { insets = insets || { left: 0, top: 0, right: 0, bottom: 0 }; const left = layout.toDeviceIndependentPixels(position.left + insets.left); @@ -100,7 +259,7 @@ export namespace IOSHelper { return CGRectMake(left, top, width, height); } - export function shrinkToSafeArea(view: View, frame: CGRect): CGRect { + static shrinkToSafeArea(view: View, frame: CGRect): CGRect { const insets = view.getSafeAreaInsets(); if (insets.left || insets.top) { const position = IOSHelper.getPositionFromFrame(frame); @@ -116,8 +275,8 @@ export namespace IOSHelper { return null; } - export function expandBeyondSafeArea(view: View, frame: CGRect): CGRect { - const availableSpace = getAvailableSpaceFromParent(view, frame); + static expandBeyondSafeArea(view: View, frame: CGRect): CGRect { + const availableSpace = IOSHelper.getAvailableSpaceFromParent(view, frame); const safeArea = availableSpace.safeArea; const fullscreen = availableSpace.fullscreen; const inWindow = availableSpace.inWindow; @@ -154,7 +313,7 @@ export namespace IOSHelper { return adjustedFrame; } - function getAvailableSpaceFromParent(view: View, frame: CGRect): { safeArea: CGRect; fullscreen: CGRect; inWindow: CGRect } { + static getAvailableSpaceFromParent(view: View, frame: CGRect): { safeArea: CGRect; fullscreen: CGRect; inWindow: CGRect } { if (!view) { return; } @@ -206,159 +365,4 @@ export namespace IOSHelper { inWindow: inWindow, }; } - - export class UILayoutViewController extends UIViewController { - public owner: WeakRef; - - public static initWithOwner(owner: WeakRef): UILayoutViewController { - const controller = UILayoutViewController.new(); - controller.owner = owner; - - return controller; - } - - public viewDidLoad(): void { - super.viewDidLoad(); - - // Unify translucent and opaque bars layout - // this.edgesForExtendedLayout = UIRectEdgeBottom; - this.extendedLayoutIncludesOpaqueBars = true; - } - - public viewWillLayoutSubviews(): void { - super.viewWillLayoutSubviews(); - const owner = this.owner.get(); - if (owner) { - updateConstraints(this, owner); - } - } - - public viewDidLayoutSubviews(): void { - super.viewDidLayoutSubviews(); - const owner = this.owner.get(); - if (owner) { - if (majorVersion >= 11) { - // Handle nested UILayoutViewController safe area application. - // Currently, UILayoutViewController can be nested only in a TabView. - // The TabView itself is handled by the OS, so we check the TabView's parent (usually a Page, but can be a Layout). - const tabViewItem = owner.parent; - const tabView = tabViewItem && tabViewItem.parent; - let parent = tabView && tabView.parent; - - // Handle Angular scenario where TabView is in a ProxyViewContainer - // It is possible to wrap components in ProxyViewContainers indefinitely - // Not using instanceof ProxyViewContainer to avoid circular dependency - // TODO: Try moving UILayoutViewController out of view module - while (parent && !parent.nativeViewProtected) { - parent = parent.parent; - } - - if (parent) { - const parentPageInsetsTop = parent.nativeViewProtected.safeAreaInsets.top; - const currentInsetsTop = this.view.safeAreaInsets.top; - const additionalInsetsTop = Math.max(parentPageInsetsTop - currentInsetsTop, 0); - - const parentPageInsetsBottom = parent.nativeViewProtected.safeAreaInsets.bottom; - const currentInsetsBottom = this.view.safeAreaInsets.bottom; - const additionalInsetsBottom = Math.max(parentPageInsetsBottom - currentInsetsBottom, 0); - - if (additionalInsetsTop > 0 || additionalInsetsBottom > 0) { - const additionalInsets = new UIEdgeInsets({ - top: additionalInsetsTop, - left: 0, - bottom: additionalInsetsBottom, - right: 0, - }); - this.additionalSafeAreaInsets = additionalInsets; - } - } - } - - layoutView(this, owner); - } - } - - public viewWillAppear(animated: boolean): void { - super.viewWillAppear(animated); - const owner = this.owner.get(); - if (!owner) { - return; - } - - updateAutoAdjustScrollInsets(this, owner); - - if (!owner.parent) { - owner.callLoaded(); - } - } - - public viewDidDisappear(animated: boolean): void { - super.viewDidDisappear(animated); - const owner = this.owner.get(); - if (owner && !owner.parent) { - owner.callUnloaded(); - } - } - - // Mind implementation for other controllers - public traitCollectionDidChange(previousTraitCollection: UITraitCollection): void { - super.traitCollectionDidChange(previousTraitCollection); - - if (majorVersion >= 13) { - const owner = this.owner.get(); - if (owner && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { - owner.notify({ - eventName: traitCollectionColorAppearanceChangedEvent, - object: owner, - }); - } - } - } - } - - @NativeClass - export class UIAdaptivePresentationControllerDelegateImp extends NSObject implements UIAdaptivePresentationControllerDelegate { - public static ObjCProtocols = [UIAdaptivePresentationControllerDelegate]; - - private owner: WeakRef; - private closedCallback: Function; - - public static initWithOwnerAndCallback(owner: WeakRef, whenClosedCallback: Function): UIAdaptivePresentationControllerDelegateImp { - const instance = super.new(); - instance.owner = owner; - instance.closedCallback = whenClosedCallback; - - return instance; - } - - public presentationControllerDidDismiss(presentationController: UIPresentationController) { - const owner = this.owner.get(); - if (owner && typeof this.closedCallback === 'function') { - this.closedCallback(); - } - } - } - - @NativeClass - export class UIPopoverPresentationControllerDelegateImp extends NSObject implements UIPopoverPresentationControllerDelegate { - public static ObjCProtocols = [UIPopoverPresentationControllerDelegate]; - - private owner: WeakRef; - private closedCallback: Function; - - public static initWithOwnerAndCallback(owner: WeakRef, whenClosedCallback: Function): UIPopoverPresentationControllerDelegateImp { - const instance = super.new(); - instance.owner = owner; - instance.closedCallback = whenClosedCallback; - - return instance; - } - - public popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) { - const owner = this.owner.get(); - if (owner && typeof this.closedCallback === 'function') { - this.closedCallback(); - } - } - } } diff --git a/packages/core/ui/frame/activity.android.ts b/packages/core/ui/frame/activity.android.ts index 7fd612039..dc2212892 100644 --- a/packages/core/ui/frame/activity.android.ts +++ b/packages/core/ui/frame/activity.android.ts @@ -8,16 +8,11 @@ if (global.__snapshot) { //@ts-ignore @JavaProxy('com.tns.NativeScriptActivity') -class NativeScriptActivity extends androidx.appcompat.app.AppCompatActivity { - private _callbacks: AndroidActivityCallbacks; - public isNativeScriptActivity; - constructor() { - super(); - +const NativeScriptActivity = (androidx.appcompat.app.AppCompatActivity).extend('com.tns.NativeScriptActivity', { + init() { return global.__native(this); - } - - public onCreate(savedInstanceState: android.os.Bundle): void { + }, + onCreate(savedInstanceState: android.os.Bundle): void { appModule.android.init(this.getApplication()); // Set isNativeScriptActivity in onCreate. @@ -28,41 +23,100 @@ class NativeScriptActivity extends androidx.appcompat.app.AppCompatActivity { } this._callbacks.onCreate(this, savedInstanceState, this.getIntent(), super.onCreate); - } + }, - public onNewIntent(intent: android.content.Intent): void { + onNewIntent(intent: android.content.Intent): void { this._callbacks.onNewIntent(this, intent, super.setIntent, super.onNewIntent); - } + }, - public onSaveInstanceState(outState: android.os.Bundle): void { + onSaveInstanceState(outState: android.os.Bundle): void { this._callbacks.onSaveInstanceState(this, outState, super.onSaveInstanceState); - } + }, - public onStart(): void { + onStart(): void { this._callbacks.onStart(this, super.onStart); - } + }, - public onStop(): void { + onStop(): void { this._callbacks.onStop(this, super.onStop); - } + }, - public onDestroy(): void { + onDestroy(): void { this._callbacks.onDestroy(this, super.onDestroy); - } + }, - public onPostResume(): void { + onPostResume(): void { this._callbacks.onPostResume(this, super.onPostResume); - } + }, - public onBackPressed(): void { + onBackPressed(): void { this._callbacks.onBackPressed(this, super.onBackPressed); - } + }, - public onRequestPermissionsResult(requestCode: number, permissions: Array, grantResults: Array): void { + onRequestPermissionsResult(requestCode: number, permissions: Array, grantResults: Array): void { this._callbacks.onRequestPermissionsResult(this, requestCode, permissions, grantResults, undefined /*TODO: Enable if needed*/); - } + }, - public onActivityResult(requestCode: number, resultCode: number, data: android.content.Intent): void { + onActivityResult(requestCode: number, resultCode: number, data: android.content.Intent): void { this._callbacks.onActivityResult(this, requestCode, resultCode, data, super.onActivityResult); - } -} + }, +}); + +// class NativeScriptActivity extends androidx.appcompat.app.AppCompatActivity { +// private _callbacks: AndroidActivityCallbacks; +// public isNativeScriptActivity; +// constructor() { +// super(); + +// return global.__native(this); +// } + +// public onCreate(savedInstanceState: android.os.Bundle): void { +// appModule.android.init(this.getApplication()); + +// // Set isNativeScriptActivity in onCreate. +// // The JS constructor might not be called because the activity is created from Android. +// this.isNativeScriptActivity = true; +// if (!this._callbacks) { +// setActivityCallbacks(this); +// } + +// this._callbacks.onCreate(this, savedInstanceState, this.getIntent(), super.onCreate); +// } + +// public onNewIntent(intent: android.content.Intent): void { +// this._callbacks.onNewIntent(this, intent, super.setIntent, super.onNewIntent); +// } + +// public onSaveInstanceState(outState: android.os.Bundle): void { +// this._callbacks.onSaveInstanceState(this, outState, super.onSaveInstanceState); +// } + +// public onStart(): void { +// this._callbacks.onStart(this, super.onStart); +// } + +// public onStop(): void { +// this._callbacks.onStop(this, super.onStop); +// } + +// public onDestroy(): void { +// this._callbacks.onDestroy(this, super.onDestroy); +// } + +// public onPostResume(): void { +// this._callbacks.onPostResume(this, super.onPostResume); +// } + +// public onBackPressed(): void { +// this._callbacks.onBackPressed(this, super.onBackPressed); +// } + +// public onRequestPermissionsResult(requestCode: number, permissions: Array, grantResults: Array): void { +// this._callbacks.onRequestPermissionsResult(this, requestCode, permissions, grantResults, undefined /*TODO: Enable if needed*/); +// } + +// public onActivityResult(requestCode: number, resultCode: number, data: android.content.Intent): void { +// this._callbacks.onActivityResult(this, requestCode, resultCode, data, super.onActivityResult); +// } +// } diff --git a/packages/core/ui/frame/index.android.ts b/packages/core/ui/frame/index.android.ts index 2ae914c37..45d1f6fb8 100644 --- a/packages/core/ui/frame/index.android.ts +++ b/packages/core/ui/frame/index.android.ts @@ -49,27 +49,44 @@ export let attachStateChangeListener: android.view.View.OnAttachStateChangeListe function getAttachListener(): android.view.View.OnAttachStateChangeListener { if (!attachStateChangeListener) { @Interfaces([android.view.View.OnAttachStateChangeListener]) - class AttachListener extends java.lang.Object implements android.view.View.OnAttachStateChangeListener { - constructor() { - super(); - + const AttachListener = java.lang.Object.extend('AttachListener', { + init() { return global.__native(this); - } - + }, onViewAttachedToWindow(view: android.view.View): void { const owner: View = view[ownerSymbol]; if (owner) { owner._onAttachedToWindow(); } - } - + }, onViewDetachedFromWindow(view: android.view.View): void { const owner: View = view[ownerSymbol]; if (owner) { owner._onDetachedFromWindow(); } - } - } + }, + }); + // class AttachListener extends java.lang.Object implements android.view.View.OnAttachStateChangeListener { + // constructor() { + // super(); + + // return global.__native(this); + // } + + // onViewAttachedToWindow(view: android.view.View): void { + // const owner: View = view[ownerSymbol]; + // if (owner) { + // owner._onAttachedToWindow(); + // } + // } + + // onViewDetachedFromWindow(view: android.view.View): void { + // const owner: View = view[ownerSymbol]; + // if (owner) { + // owner._onDetachedFromWindow(); + // } + // } + // } attachStateChangeListener = new AttachListener(); } diff --git a/packages/core/ui/frame/index.ios.ts b/packages/core/ui/frame/index.ios.ts index 8a758f126..fda9e4f3a 100644 --- a/packages/core/ui/frame/index.ios.ts +++ b/packages/core/ui/frame/index.ios.ts @@ -384,6 +384,7 @@ class UINavigationControllerAnimatedDelegate extends NSObject implements UINavig } } +@NativeClass class UINavigationControllerImpl extends UINavigationController { private _owner: WeakRef; diff --git a/packages/core/ui/page/index.ios.ts b/packages/core/ui/page/index.ios.ts index 5c4048ccf..30a22e51f 100644 --- a/packages/core/ui/page/index.ios.ts +++ b/packages/core/ui/page/index.ios.ts @@ -64,6 +64,7 @@ function isBackNavigationFrom(controller: UIViewControllerImpl, page: Page): boo return true; } +@NativeClass class UIViewControllerImpl extends UIViewController { private _owner: WeakRef; diff --git a/packages/core/xml/index.ts b/packages/core/xml/index.ts index 6dcba7e59..999991d31 100644 --- a/packages/core/xml/index.ts +++ b/packages/core/xml/index.ts @@ -2,7 +2,8 @@ // https://github.com/NativeScript/nativescript-dev-webpack/issues/932 import * as definition from '.'; -import * as easysax from '../js-libs/easysax'; +const easysax = require('../js-libs/easysax'); +import { EasySAXParser } from '../js-libs/easysax'; /** * Defines a position within string, in line and column form. @@ -423,14 +424,14 @@ function _HandleAmpEntities(found: string, decimalValue: string, hexValue: strin export class XmlParser implements definition.XmlParser { //TODO: Add option to configure whether the parser should report ignorable whitespace, i.e. document formatting whitespace. - private _parser: easysax.EasySAXParser; + private _parser: EasySAXParser; private _processNamespaces: boolean; private _namespaceStack: Array; constructor(onEvent: (event: definition.ParserEvent) => void, onError?: (error: Error, position: Position) => void, processNamespaces?: boolean) { this._processNamespaces = processNamespaces; - this._parser = new easysax.EasySAXParser(); + this._parser = new EasySAXParser(); const that = this; this._parser.on('startNode', function (elem, attr, uq, tagend, str, pos) { diff --git a/packages/webpack/android-app-components-loader.js b/packages/webpack/android-app-components-loader.js index c2867a637..3a80e72a1 100644 --- a/packages/webpack/android-app-components-loader.js +++ b/packages/webpack/android-app-components-loader.js @@ -6,8 +6,8 @@ module.exports = function (source, map) { const imports = modules.map(convertSlashesInPath) .map(m => `require("${m}");`).join("\n"); const augmentedSource = ` - let applicationCheckPlatform = require("@nativescript/core/application"); - if (applicationCheckPlatform.android && !global["__snapshot"]) { + const isAndroid = require("@nativescript/core").isAndroid; + if (isAndroid && !global["__snapshot"]) { ${imports} }