From f3cd3d3375fbb42b9387e6b5654a3af9123a4417 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Fri, 3 Sep 2021 22:41:49 -0300 Subject: [PATCH] fix(modal): persist modal through configuration changes (#9533) --- packages/core/ui/button/index.ios.ts | 11 ++++++++++- packages/core/ui/core/view/index.android.ts | 9 ++++++++- packages/core/ui/frame/index.android.ts | 18 ++++++++++++------ packages/core/ui/frame/index.ios.ts | 8 ++++---- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/packages/core/ui/button/index.ios.ts b/packages/core/ui/button/index.ios.ts index 4b0828c10..1e0cac65d 100644 --- a/packages/core/ui/button/index.ios.ts +++ b/packages/core/ui/button/index.ios.ts @@ -1,10 +1,11 @@ import { ControlStateChangeListener } from '../core/control-state-change'; import { ButtonBase } from './button-common'; import { View, PseudoClassHandler } from '../core/view'; -import { borderTopWidthProperty, borderRightWidthProperty, borderBottomWidthProperty, borderLeftWidthProperty, paddingLeftProperty, paddingTopProperty, paddingRightProperty, paddingBottomProperty } from '../styling/style-properties'; +import { backgroundColorProperty, borderTopWidthProperty, borderRightWidthProperty, borderBottomWidthProperty, borderLeftWidthProperty, paddingLeftProperty, paddingTopProperty, paddingRightProperty, paddingBottomProperty } from '../styling/style-properties'; import { textAlignmentProperty, whiteSpaceProperty } from '../text-base'; import { layout } from '../../utils'; import { CoreTypes } from '../../core-types'; +import { Color } from '../../color'; export * from './button-common'; @@ -56,6 +57,14 @@ export class Button extends ButtonBase { } } + [backgroundColorProperty.getDefault](): UIColor { + return this.nativeViewProtected.backgroundColor; + } + + [backgroundColorProperty.setNative](value: UIColor | Color) { + this.nativeViewProtected.backgroundColor = value instanceof Color ? value.ios : value; + } + [borderTopWidthProperty.getDefault](): CoreTypes.LengthType { return { value: this.nativeViewProtected.contentEdgeInsets.top, diff --git a/packages/core/ui/core/view/index.android.ts b/packages/core/ui/core/view/index.android.ts index e7a2910d5..504594687 100644 --- a/packages/core/ui/core/view/index.android.ts +++ b/packages/core/ui/core/view/index.android.ts @@ -161,6 +161,7 @@ function initializeDialogFragment() { private _cancelable: boolean; private _shownCallback: () => void; private _dismissCallback: () => void; + private activity: WeakRef; constructor() { super(); @@ -215,6 +216,7 @@ function initializeDialogFragment() { public onCreateView(inflater: android.view.LayoutInflater, container: android.view.ViewGroup, savedInstanceState: android.os.Bundle): android.view.View { const owner = this.owner; + this.activity = new WeakRef(this.getActivity()); owner._setupAsRootView(this.getActivity()); owner._isAddedToNativeVisualTree = true; @@ -252,7 +254,8 @@ function initializeDialogFragment() { public onDismiss(dialog: android.content.DialogInterface): void { super.onDismiss(dialog); const manager = this.getFragmentManager(); - if (manager) { + const activity = this.activity?.get(); + if (manager && !activity?.isChangingConfigurations()) { removeModal(this.owner._domId); this._dismissCallback(); } @@ -266,6 +269,10 @@ function initializeDialogFragment() { public onDestroy(): void { super.onDestroy(); const owner = this.owner; + const activity = this.activity?.get(); + if (!activity?.isChangingConfigurations()) { + this.activity = null; + } if (owner) { // Android calls onDestroy before onDismiss. diff --git a/packages/core/ui/frame/index.android.ts b/packages/core/ui/frame/index.android.ts index 2bf817071..a9ce04d29 100644 --- a/packages/core/ui/frame/index.android.ts +++ b/packages/core/ui/frame/index.android.ts @@ -1189,12 +1189,18 @@ class ActivityCallbacksImplementation implements AndroidActivityCallbacks { rootView._tearDownUI(true); } - const exitArgs = { - eventName: application.exitEvent, - object: application.android, - android: activity, - }; - application.notify(exitArgs); + // this may happen when the user changes the system theme + // In such case, isFinishing() is false (and isChangingConfigurations is true), and the app will start again (onCreate) with a savedInstanceState + // as a result, launchEvent will never be called + // possible alternative: always fire launchEvent and exitEvent, but pass extra flags to make it clear what kind of launch/destroy is happening + if (activity.isFinishing()) { + const exitArgs = { + eventName: application.exitEvent, + object: application.android, + android: activity, + }; + application.notify(exitArgs); + } } finally { superFunc.call(activity); } diff --git a/packages/core/ui/frame/index.ios.ts b/packages/core/ui/frame/index.ios.ts index 732b318d8..b429dc24e 100644 --- a/packages/core/ui/frame/index.ios.ts +++ b/packages/core/ui/frame/index.ios.ts @@ -404,13 +404,13 @@ class UINavigationControllerImpl extends UINavigationController { } get owner(): Frame { - return this._owner.get(); + return this._owner.get?.(); } @profile public viewWillAppear(animated: boolean): void { super.viewWillAppear(animated); - const owner = this._owner.get(); + const owner = this._owner.get?.(); if (owner && !owner.isLoaded && !owner.parent) { owner.callLoaded(); } @@ -419,7 +419,7 @@ class UINavigationControllerImpl extends UINavigationController { @profile public viewDidDisappear(animated: boolean): void { super.viewDidDisappear(animated); - const owner = this._owner?.get(); + const owner = this._owner?.get?.(); if (owner && owner.isLoaded && !owner.parent && !this.presentedViewController) { owner.callUnloaded(); owner._tearDownUI(true); @@ -543,7 +543,7 @@ class UINavigationControllerImpl extends UINavigationController { super.traitCollectionDidChange(previousTraitCollection); if (majorVersion >= 13) { - const owner = this._owner.get(); + const owner = this._owner.get?.(); if (owner && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection && this.traitCollection.hasDifferentColorAppearanceComparedToTraitCollection(previousTraitCollection)) { owner.notify({ eventName: IOSHelper.traitCollectionColorAppearanceChangedEvent,