1
1
import { QLabel , NodeWidget , QMovie , QSize } from "@nodegui/nodegui" ;
2
2
import { TextProps , setTextProps } from "../Text/RNText" ;
3
3
import { RNWidget } from "../config" ;
4
- import { throwUnsupported } from "../../utils/helpers" ;
4
+ import { throwUnsupported , isValidUrl } from "../../utils/helpers" ;
5
+ import phin from "phin" ;
5
6
6
7
export interface AnimatedImageProps extends TextProps {
7
8
src ?: string ;
9
+ buffer ?: Buffer ;
8
10
}
9
11
10
12
const setAnimatedImageProps = (
@@ -13,15 +15,22 @@ const setAnimatedImageProps = (
13
15
oldProps : AnimatedImageProps
14
16
) => {
15
17
const setter : AnimatedImageProps = {
16
- set src ( imageUrl : string ) {
17
- if ( ! imageUrl ) {
18
+ set src ( imageUrlOrPath : string ) {
19
+ if ( ! imageUrlOrPath ) {
18
20
return ;
19
21
}
22
+ getLoadedQMovie ( imageUrlOrPath )
23
+ . then ( movie => {
24
+ widget . setMovie ( movie ) ;
25
+ widget . movie ( ) ?. start ( ) ;
26
+ } )
27
+ . catch ( console . warn ) ;
28
+ } ,
29
+ set buffer ( imageBuffer : Buffer ) {
20
30
const movie = new QMovie ( ) ;
21
- movie . setFileName ( imageUrl ) ;
31
+ movie . loadFromData ( imageBuffer ) ;
22
32
widget . setMovie ( movie ) ;
23
- const size = widget . size ( ) ;
24
- movie . setScaledSize ( size ) ;
33
+ widget . movie ( ) ?. start ( ) ;
25
34
}
26
35
} ;
27
36
Object . assign ( setter , newProps ) ;
@@ -53,3 +62,15 @@ export class RNAnimatedImage extends QLabel implements RNWidget {
53
62
movie ?. setScaledSize ( size ) ;
54
63
}
55
64
}
65
+
66
+ async function getLoadedQMovie ( imageUrlOrPath : string ) : Promise < QMovie > {
67
+ const movie = new QMovie ( ) ;
68
+ if ( isValidUrl ( imageUrlOrPath ) ) {
69
+ const res = await phin ( imageUrlOrPath ) ;
70
+ const imageBuffer = Buffer . from ( res . body ) ;
71
+ movie . loadFromData ( imageBuffer ) ;
72
+ } else {
73
+ movie . setFileName ( imageUrlOrPath ) ;
74
+ }
75
+ return movie ;
76
+ }
0 commit comments