import pdfviewer = com . github . barteksc . pdfviewer ; import * as fs from 'tns-core-modules/file-system' ; import * as http from 'tns-core-modules/http' ; import { PDFViewCommon , srcProperty } from './pdf-view.common' ; export class PDFView extends PDFViewCommon { private promise : Promise < void > ; private tempFolder = fs . knownFolders . temp ( ) . getFolder ( 'PDFViewer.temp/' ) ; private onLoadHandler = ( ( ) => { const pdfViewRef = new WeakRef ( this ) ; return new pdfviewer . listener . OnLoadCompleteListener ( { loadComplete : numPages => { PDFViewCommon . notifyOfEvent ( PDFViewCommon . loadEvent , pdfViewRef ) ; } , } ) ; } ) ( ) ; public get android ( ) { return this . nativeView as pdfviewer . PDFView ; } public set android ( value ) { this . nativeView = value ; } public createNativeView ( ) { return new pdfviewer . PDFView ( this . _context , void 0 ) ; } public [ srcProperty . setNative ] ( value ) { this . loadPDF ( value ) ; } public loadPDF ( src ) { if ( ! src || ! this . android ) { return ; } this . promise = void 0 ; if ( src . indexOf ( '://' ) === - 1 ) { src = 'file://' + src ; } else if ( src . indexOf ( 'file://' ) !== 0 ) { this . cacheThenLoad ( src ) ; return ; } const uri = android . net . Uri . parse ( src ) ; this . android . fromUri ( uri ) . onLoad ( this . onLoadHandler ) . load ( ) ; } private cacheThenLoad ( url ) { this . tempFolder . clear ( ) . then ( ( ) => { const promise = this . promise = http . getFile ( url , `${ this . tempFolder . path }/${ Date . now ( ) }.pdf` ) . then ( file => { if ( this . promise === promise ) { this . loadPDF ( file . path ) ; } } ) . catch ( error => { console . error ( error ) ; } ) ; } ) ; } }