11import { QLabel , NodeWidget , QMovie , QSize } from "@nodegui/nodegui" ;
22import { TextProps , setTextProps } from "../Text/RNText" ;
33import { RNWidget } from "../config" ;
4- import { throwUnsupported } from "../../utils/helpers" ;
4+ import { throwUnsupported , isValidUrl } from "../../utils/helpers" ;
5+ import phin from "phin" ;
56
67export interface AnimatedImageProps extends TextProps {
78 src ?: string ;
9+ buffer ?: Buffer ;
810}
911
1012const setAnimatedImageProps = (
@@ -13,15 +15,22 @@ const setAnimatedImageProps = (
1315 oldProps : AnimatedImageProps
1416) => {
1517 const setter : AnimatedImageProps = {
16- set src ( imageUrl : string ) {
17- if ( ! imageUrl ) {
18+ set src ( imageUrlOrPath : string ) {
19+ if ( ! imageUrlOrPath ) {
1820 return ;
1921 }
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 ) {
2030 const movie = new QMovie ( ) ;
21- movie . setFileName ( imageUrl ) ;
31+ movie . loadFromData ( imageBuffer ) ;
2232 widget . setMovie ( movie ) ;
23- const size = widget . size ( ) ;
24- movie . setScaledSize ( size ) ;
33+ widget . movie ( ) ?. start ( ) ;
2534 }
2635 } ;
2736 Object . assign ( setter , newProps ) ;
@@ -53,3 +62,15 @@ export class RNAnimatedImage extends QLabel implements RNWidget {
5362 movie ?. setScaledSize ( size ) ;
5463 }
5564}
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