Skip to content

Commit dbeeae0

Browse files
committed
feat: adjust dynamic properties - color
1 parent 8b0aefd commit dbeeae0

File tree

5 files changed

+79
-7
lines changed

5 files changed

+79
-7
lines changed

src/LottieHelper.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export declare class LottieHelper {
2+
public static keyPath(keys: string[]);
3+
public static getIntCallback(value);
4+
public static getFloatCallback(value);
5+
public static getPointFCallback(x: number, y: number); // float
6+
public static getSizeCallback(h: number, w: number); // float
7+
}

src/nativescript-lottie.android.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
**********************************************************************************/
77
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" />
88

9-
import { View } from 'tns-core-modules/ui/core/view';
10-
import { Property } from 'tns-core-modules/ui/core/properties';
9+
import { Color, View } from 'tns-core-modules/ui/core/view';
1110
import {
1211
LottieViewBase,
1312
srcProperty,
1413
loopProperty,
1514
autoPlayProperty,
16-
cacheStrategyProperty
15+
cacheStrategyProperty,
16+
Theme,
17+
themeProperty
1718
} from './nativescript-lottie.common';
1819

20+
const LottieProperty = com.airbnb.lottie.LottieProperty;
21+
const LottieHelper = com.nativescript_lottie.LottieHelper;
22+
1923
declare var com: any;
2024

2125
export class LottieView extends LottieViewBase {
@@ -94,6 +98,25 @@ export class LottieView extends LottieViewBase {
9498
}
9599
}
96100

101+
// todo: add more dynamic properties
102+
public [themeProperty.setNative](value: Theme[]) {
103+
this.setTheme(value);
104+
}
105+
106+
public setTheme(value: Theme[]) {
107+
if (!this.nativeView) {
108+
return;
109+
}
110+
111+
if (value && value.length) {
112+
value.forEach(dynamicValue => {
113+
const callBack = LottieHelper.getIntCallback(new Color(dynamicValue.value).android);
114+
const keyPath = LottieHelper.keyPath(dynamicValue.keyPath);
115+
this.nativeView.addValueCallback(keyPath, LottieProperty.COLOR, callBack);
116+
});
117+
}
118+
}
119+
97120
public playAnimation(): void {
98121
if (this.nativeView) {
99122
this.nativeView.playAnimation();

src/nativescript-lottie.common.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,14 @@ export const cacheStrategyProperty = new Property<LottieViewBase, CacheStrategy>
4343
});
4444

4545
cacheStrategyProperty.register(LottieViewBase);
46+
47+
export interface Theme {
48+
keyPath: string[];
49+
value: string;
50+
}
51+
52+
export const themeProperty = new Property<LottieViewBase, Theme[]>({
53+
name: 'theme'
54+
});
55+
56+
themeProperty.register(LottieViewBase);

src/nativescript-lottie.ios.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@
66
**********************************************************************************/
77
/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" />
88

9-
import { layout } from 'tns-core-modules/ui/core/view';
10-
import { LottieViewBase, srcProperty, loopProperty, autoPlayProperty } from './nativescript-lottie.common';
9+
import { Color, layout } from 'tns-core-modules/ui/core/view';
10+
import {
11+
LottieViewBase,
12+
srcProperty,
13+
loopProperty,
14+
autoPlayProperty,
15+
themeProperty,
16+
Theme
17+
} from './nativescript-lottie.common';
1118
import { screen } from 'tns-core-modules/platform';
19+
1220
declare var LOTAnimationView: any;
21+
declare var LOTKeypath: any;
22+
declare var LOTColorValueCallback: any;
1323

1424
export class LottieView extends LottieViewBase {
1525
private _contentMode: any;
@@ -88,6 +98,27 @@ export class LottieView extends LottieViewBase {
8898
}
8999
}
90100

101+
// todo: add more dynamic properties
102+
public [themeProperty.setNative](value: Theme[]) {
103+
this.setTheme(value);
104+
}
105+
106+
public setTheme(value: Theme[]) {
107+
if (!this._animationView) {
108+
setTimeout(() => this.setTheme(value), 50);
109+
return;
110+
}
111+
112+
if (value && value.length) {
113+
value.forEach(dynamicValue => {
114+
const callBack = LOTColorValueCallback.withCGColor(new Color(dynamicValue.value).ios.CGColor);
115+
dynamicValue.keyPath.push('Color');
116+
const keyPath = LOTKeypath.keypathWithString(dynamicValue.keyPath.join('.'));
117+
this._animationView.setValueDelegateForKeypath(callBack, keyPath);
118+
});
119+
}
120+
}
121+
91122
public onLoaded() {
92123
super.onLoaded(); // ensure 'loaded' event fires
93124
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
dependencies {
22
def supportLibrariesVersion = '27.1.1'
33
compile "com.android.support:support-v4:${supportLibrariesVersion}"
4-
compile "com.android.support:appcompat-v7:{supportLibrariesVersion}"
5-
debugCompile "com.android.support:design:{supportLibrariesVersion}"
4+
compile "com.android.support:appcompat-v7:${supportLibrariesVersion}"
5+
debugCompile "com.android.support:design:${supportLibrariesVersion}"
66
compile 'com.airbnb.android:lottie:2.5.5'
77
}

0 commit comments

Comments
 (0)