-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathlottie-splashscreen.ts
85 lines (76 loc) · 3.25 KB
/
lottie-splashscreen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { exec } from 'cordova';
type LottieEvent = 'lottieAnimationStart' | 'lottieAnimationEnd' | 'lottieAnimationCancel' | 'lottieAnimationRepeat';
const lottieEvents: LottieEvent[] = ['lottieAnimationStart', 'lottieAnimationEnd', 'lottieAnimationRepeat', 'lottieAnimationCancel'];
class LottieSplashScreen {
static animationEnded: boolean;
/**
* Hides the active Lottie animation programmatically.
* In case that no animation is playing, the Promise is rejected.
*/
static hide() {
this.animationEnded = true;
return new Promise<string>((resolve, reject) => {
exec(resolve, reject, 'LottieSplashScreen', 'hide', []);
});
}
/**
* Shows a Lottie animation programmatically. If an animation is already playing, the Promise will be rejected.
* If a value is omitted, the configured default will be used instead.
*
* @param location - Optional. The animation that should be shown. Can be file path or URL.
* @param remote - Optional. The boolean flag whether the animation should be loaded from remote.
* @param width - Optional. The desired animation width.
* @param height - Optional. The desired animation height.
*/
static show(location?: string, remote?: boolean, width?: number, height?: number) {
this.animationEnded = false;
return new Promise<string>((resolve, reject) => {
exec(resolve, reject, 'LottieSplashScreen', 'show', [location, remote, width, height]);
});
}
/**
* Listens to custom lottie events that are dispatched from the native side.
*
* @param event - The desired event to listen to. If a falsy value, such as null or "", is passed, the method will listen to all Lottie events.
* @param callback - The callback function.
*/
static on(event: LottieEvent | undefined, callback: (ev: Event) => void) {
if (event) {
document.addEventListener(event, callback);
} else {
for (const lottieEvent of lottieEvents) {
document.addEventListener(lottieEvent, callback);
}
}
}
/**
* Listens to a custom lottie event once and resolves the Promise once the event has been called.
*
* @param event - The desired event to listen to.
*/
static once(event: LottieEvent) {
return new Promise((resolve) => {
document.addEventListener(event, resolve);
});
}
}
LottieSplashScreen.on('lottieAnimationStart', () => (LottieSplashScreen.animationEnded = false));
LottieSplashScreen.on('lottieAnimationCancel', () => (LottieSplashScreen.animationEnded = true));
LottieSplashScreen.on('lottieAnimationEnd', () => (LottieSplashScreen.animationEnded = true));
document.addEventListener('deviceready', () => {
exec(
(data: string | boolean) => {
const payload = Boolean(data);
if (!LottieSplashScreen.animationEnded && payload) {
document.dispatchEvent(new Event('lottieAnimationEnd'));
}
LottieSplashScreen.animationEnded = payload;
},
// tslint:disable-next-line: no-console
console.error,
'LottieSplashScreen',
'initialAnimationEnded',
[],
);
});
export = LottieSplashScreen;