import * as data_observable from "tns-core-modules/data/observable" ; import * as imageAssetModule from "tns-core-modules/image-asset" ; import { Options , ImagePickerMediaType } from "./imagepicker.common" ; import { View } from "tns-core-modules/ui/core/view/view" ; import * as utils from "tns-core-modules/utils/utils" ; export * from "./imagepicker.common" ; const defaultAssetCollectionSubtypes = NSArray . arrayWithArray ( < any > [ PHAssetCollectionSubtype . SmartAlbumRecentlyAdded , PHAssetCollectionSubtype . SmartAlbumUserLibrary , PHAssetCollectionSubtype . AlbumMyPhotoStream , PHAssetCollectionSubtype . SmartAlbumFavorites , PHAssetCollectionSubtype . SmartAlbumPanoramas , PHAssetCollectionSubtype . SmartAlbumBursts , PHAssetCollectionSubtype . AlbumCloudShared , PHAssetCollectionSubtype . SmartAlbumSelfPortraits , PHAssetCollectionSubtype . SmartAlbumScreenshots , PHAssetCollectionSubtype . SmartAlbumLivePhotos ] ) ; export class ImagePicker extends data_observable . Observable { _imagePickerController : QBImagePickerController ; _hostView : View ; get hostView ( ) { return this . _hostView ; } get hostController ( ) : UIViewController { let vc = this . hostView ? this . hostView . viewController : UIApplication . sharedApplication . keyWindow . rootViewController ; while ( vc . presentedViewController && vc . presentedViewController . viewLoaded && vc . presentedViewController . view . window ) { vc = vc . presentedViewController ; } return vc ; } constructor ( options = { } , hostView ) { super ( ) ; this . _hostView = hostView ; let imagePickerController = QBImagePickerController . alloc ( ) . init ( ) ; imagePickerController . assetCollectionSubtypes = defaultAssetCollectionSubtypes ; imagePickerController . mediaType = options . mediaType ? < QBImagePickerMediaType > options . mediaType . valueOf ( ) : QBImagePickerMediaType . Any ; imagePickerController . allowsMultipleSelection = options . mode !== 'single' ; imagePickerController . minimumNumberOfSelection = options . minimumNumberOfSelection || 0 ; imagePickerController . maximumNumberOfSelection = options . maximumNumberOfSelection || 0 ; imagePickerController . showsNumberOfSelectedAssets = options . showsNumberOfSelectedAssets || true ; imagePickerController . numberOfColumnsInPortrait = options . numberOfColumnsInPortrait || imagePickerController . numberOfColumnsInPortrait ; imagePickerController . numberOfColumnsInLandscape = options . numberOfColumnsInLandscape || imagePickerController . numberOfColumnsInLandscape ; imagePickerController . prompt = options . prompt || imagePickerController . prompt ; this . _imagePickerController = imagePickerController ; } authorize ( ) { console . log ( "authorizing..." ) ; return new Promise < void > ( ( resolve , reject ) => { let runloop = CFRunLoopGetCurrent ( ) ; PHPhotoLibrary . requestAuthorization ( function ( result ) { if ( result === PHAuthorizationStatus . Authorized ) { resolve ( ) ; } else { reject ( new Error ( "Authorization failed. Status: " + result ) ) ; } } ) ; } ) ; } present ( ) { return new Promise < void > ( ( resolve , reject ) => { const imagePickerControllerDelegate = ImagePickerControllerDelegate . new ( ) ; imagePickerControllerDelegate . _resolve = resolve ; imagePickerControllerDelegate . _reject = reject ; this . _imagePickerController . delegate = imagePickerControllerDelegate ; this . hostController . presentViewControllerAnimatedCompletion ( this . _imagePickerController , true , null ) ; } ) ; } } export class ImagePickerControllerDelegate extends NSObject implements QBImagePickerControllerDelegate { _resolve : any ; _reject : any ; qb_imagePickerControllerDidCancel ? ( imagePickerController ) { imagePickerController . dismissViewControllerAnimatedCompletion ( true , null ) ; this . _reject ( new Error ( "Selection canceled." ) ) ; this . deRegisterFromGlobal ( ) ; } qb_imagePickerControllerDidFinishPickingAssets ? ( imagePickerController , iosAssets ) { let assets = [ ] ; for ( let i = 0 ; i < iosAssets . count ; i ++ ) { let asset = new imageAssetModule . ImageAsset ( iosAssets [ i ] ) ; if ( ! asset . options ) { asset . options = { keepAspectRatio : true } ; } assets . push ( asset ) ; } this . _resolve ( assets ) ; imagePickerController . dismissViewControllerAnimatedCompletion ( true , ( ) => { this . deRegisterFromGlobal ( ) ; setTimeout ( utils . GC , 200 ) ; } ) ; } private registerToGlobal ( ) { ( < any > global ) . imagePickerControllerDelegate = this ; } private deRegisterFromGlobal ( ) { ( < any > global ) . imagePickerControllerDelegate = null ; } public static ObjCProtocols = [ QBImagePickerControllerDelegate ] ; static new ( ) { const instance = < ImagePickerControllerDelegate > super . new ( ) ; instance . registerToGlobal ( ) ; return instance ; } } export function create ( options ? , hostView ? ) { return new ImagePicker ( options , hostView ) ; }