FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #4202 from NativeScript/app-displayed-event · NativeScript/NativeScript@d9d1f81 · GitHub

Commit d9d1f81

Browse files
Merge pull request #4202 from NativeScript/app-displayed-event
App "displayed" event
2 parents 2079201 + 610794f commit d9d1f81

4 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎tns-core-modules/application/application-common.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { UnhandledErrorEventData, iOSApplication, AndroidApplication, CssChanged
2121

2222
export const launchEvent = "launch";
2323
export const suspendEvent = "suspend";
24+
export const displayedEvent = "displayed";
2425
export const resumeEvent = "resume";
2526
export const exitEvent = "exit";
2627
export const lowMemoryEvent = "lowMemory";
@@ -45,6 +46,7 @@ export let ios = undefined;
4546
export const on: typeof events.on = events.on.bind(events);
4647
export const off: typeof events.off = events.off.bind(events);
4748
export const notify: typeof events.notify = events.notify.bind(events);
49+
export const hasListeners: typeof events.hasListeners = events.hasListeners.bind(events);
4850

4951
let app: iOSApplication | AndroidApplication;
5052
export function setApplication(instance: iOSApplication | AndroidApplication): void {

‎tns-core-modules/application/application.android.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
} from ".";
55

66
import {
7-
notify, lowMemoryEvent, orientationChangedEvent, suspendEvent, resumeEvent,
7+
notify, hasListeners, lowMemoryEvent, orientationChangedEvent, suspendEvent, resumeEvent, displayedEvent,
88
setApplication, livesync, Observable
99
} from "./application-common";
1010

@@ -181,7 +181,19 @@ function initLifecycleCallbacks() {
181181
androidApp.startActivity = activity;
182182
}
183183

184-
androidApp.notify(<AndroidActivityBundleEventData>{ eventName: ActivityCreated, object: androidApp, activity: activity, bundle: savedInstanceState });
184+
androidApp.notify(<AndroidActivityBundleEventData>{ eventName: ActivityCreated, object: androidApp, activity, bundle: savedInstanceState });
185+
186+
if (hasListeners(displayedEvent)) {
187+
let rootView = activity.findViewById((<any>android).R.id.content);
188+
let onGlobalLayoutListener = new android.view.ViewTreeObserver.OnGlobalLayoutListener({
189+
onGlobalLayout() {
190+
notify({ eventName: displayedEvent, object: androidApp, activity });
191+
let viewTreeObserver = rootView.getViewTreeObserver();
192+
viewTreeObserver.removeOnGlobalLayoutListener(onGlobalLayoutListener);
193+
}
194+
});
195+
rootView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
196+
}
185197
},
186198

187199
onActivityDestroyed: function (activity: android.app.Activity) {

‎tns-core-modules/application/application.d.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import { NavigationEntry, View, Observable, EventData } from "../ui/frame";
1111
*/
1212
export var launchEvent: string;
1313

14+
/**
15+
* String value used when hooking to displayed event.
16+
*/
17+
export var displayedEvent: string;
18+
1419
/**
1520
* String value used when hooking to uncaughtError event.
1621
*/
@@ -189,6 +194,13 @@ export function hasListeners(eventName: string): boolean;
189194
*/
190195
export function on(event: "launch", callback: (args: LaunchEventData) => void, thisArg?: any);
191196

197+
/**
198+
* This event is raised after the application has performed most of its startup actions.
199+
* Its intent is to be suitable for measuring app startup times.
200+
* @experimental
201+
*/
202+
export function on(event: "displayed", callback: (args: EventData) => void, thisArg?: any);
203+
192204
/**
193205
* This event is raised when the Application is suspended.
194206
*/

‎tns-core-modules/application/application.ios.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import {
44
notify, launchEvent, resumeEvent, suspendEvent, exitEvent, lowMemoryEvent,
5-
orientationChangedEvent, setApplication, livesync
5+
orientationChangedEvent, setApplication, livesync, displayedEvent
66
} from "./application-common";
77

88
// First reexport so that app module is initialized.
@@ -16,6 +16,8 @@ class Responder extends UIResponder {
1616
//
1717
}
1818

19+
let displayedOnce = false;
20+
1921
class Window extends UIWindow {
2022
public content;
2123

@@ -138,7 +140,13 @@ class IOSApplication implements IOSApplicationDefinition {
138140
}
139141

140142
private didBecomeActive(notification: NSNotification) {
141-
notify(<ApplicationEventData>{ eventName: resumeEvent, object: this, ios: utils.ios.getter(UIApplication, UIApplication.sharedApplication) });
143+
let ios = utils.ios.getter(UIApplication, UIApplication.sharedApplication);
144+
let object = this;
145+
notify(<ApplicationEventData>{ eventName: resumeEvent, object, ios });
146+
if (!displayedOnce) {
147+
notify(<ApplicationEventData>{ eventName: displayedEvent, object, ios });
148+
displayedOnce = true;
149+
}
142150
}
143151

144152
private didEnterBackground(notification: NSNotification) {

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL