import { Http , knownFolders } from '@nativescript/core' ; import { GifCommon , headersProperty , srcProperty } from './gif.common' ; declare var pl ; export class Gif extends GifCommon { private _drawable : any ; private _headers : any ; private _src : string ; constructor ( ) { super ( ) ; this . _headers = null ; this . _src = null ; } public createNativeView ( ) { this . nativeView = new pl . droidsonroids . gif . GifImageView ( this . _context ) ; return this . nativeView ; } /**
     * Stop playing the .gif
     */ public stop ( ) { if ( this . _drawable ) { this . _drawable . stop ( ) ; } } /**
     * Start playing the .gif
     */ public start ( ) { if ( this . _drawable ) { this . _drawable . start ( ) ; } } returns public isPlaying ( ) { if ( this . _drawable ) { const isPlaying = this . _drawable . isRunning ( ) ; return isPlaying ; } else { return false ; } } returns public getFrameCount ( ) { let frames ; if ( this . _drawable ) { frames = this . _drawable . getNumberOfFrames ( ) ; } return frames ; } public reset ( ) { this . _drawable . reset ( ) ; } public getDuration ( ) { if ( this . _drawable ) { const duration = this . _drawable . getDuration ( ) ; return duration ; } else { return 0 ; } } public setSpeed ( factor ) { if ( this . _drawable ) { this . _drawable . setSpeed ( factor ) ; } } public recycle ( ) { if ( this . _drawable ) { this . _drawable . recycle ( ) ; } } [ headersProperty . setNative ] ( value ) { this . _setHeader ( value ? value : null ) ; } [ srcProperty . setNative ] ( value ) { this . _setSrcProperty ( value ) ; } private _setSrcProperty ( value ) { if ( value ) { value = value . trim ( ) ; let isUrl = false ; if ( value . indexOf ( '://' ) !== - 1 ) { if ( value . indexOf ( 'res://' ) === - 1 ) { isUrl = true ; } } this . _src = value ; if ( ! isUrl ) { const currentPath = knownFolders . currentApp ( ) . path ; if ( value [ 1 ] === '/' && ( value [ 0 ] === '.' || value [ 0 ] === '~' ) ) { value = value . substr ( 2 ) ; } if ( value [ 0 ] !== '/' ) { value = currentPath + '/' + value ; } this . _drawable = new pl . droidsonroids . gif . GifDrawable ( value ) ; this . nativeView . setImageDrawable ( this . _drawable ) ; } else { const requestOptions = { url : value , method : 'GET' } ; if ( this . _headers !== null ) { requestOptions . headers = this . _headers ; } Http . request ( requestOptions ) . then ( ( r ) => { if ( r . statusCode === 200 ) { this . _drawable = new pl . droidsonroids . gif . GifDrawable ( r . content . raw . toByteArray ( ) ) ; this . nativeView . setImageDrawable ( this . _drawable ) ; } else { console . log ( 'error getting image: ' + r . statusCode ) ; } } , ( err ) => { console . log ( err ) ; } ) ; } } else { console . log ( 'No src property set for the Gif.' ) ; } } private _setHeader ( headers ) { if ( headers ) { this . _headers = headers ; if ( this . _src && this . _src . length > 0 ) { this . _setSrcProperty ( this . _src ) ; } } else { this . _headers = null ; } } }