import { Utils } from '@nativescript/core' ; import * as common from './pulltorefresh-common' ; export * from './pulltorefresh-common' ; @ NativeClass ( ) class PullToRefreshHandler extends NSObject { public static ObjCExposedMethods = { handleRefresh : { returns : interop . types . void , params : [ UIRefreshControl ] } , } ; private _owner : WeakRef < PullToRefresh > ; public static initWithOnwer ( owner ) { const impl = < PullToRefreshHandler > PullToRefreshHandler . new ( ) ; impl . _owner = owner ; return impl ; } public handleRefresh ( refreshControl ) { const pullToRefresh = this . _owner . get ( ) ; pullToRefresh . refreshing = true ; pullToRefresh . notify ( { eventName : common . PullToRefreshBase . refreshEvent , object : pullToRefresh , } ) ; } } const SUPPORT_REFRESH_CONTROL = Utils . ios . MajorVersion >= 10 ; export class PullToRefresh extends common . PullToRefreshBase { private _handler : PullToRefreshHandler ; public refreshControl : UIRefreshControl ; constructor ( ) { super ( ) ; this . refreshControl = UIRefreshControl . alloc ( ) . init ( ) ; this . _handler = PullToRefreshHandler . initWithOnwer ( new WeakRef ( this ) ) ; this . refreshControl . addTargetActionForControlEvents ( this . _handler , 'handleRefresh' , UIControlEvents . ValueChanged ) ; } public onLoaded ( ) { super . onLoaded ( ) ; if ( this . content . ios instanceof UIScrollView ) { if ( SUPPORT_REFRESH_CONTROL ) { this . content . ios . refreshControl = this . refreshControl ; } else { this . content . ios . alwaysBounceVertical = true ; this . content . ios . addSubview ( this . refreshControl ) ; } } else if ( this . content . ios instanceof WKWebView ) { if ( SUPPORT_REFRESH_CONTROL ) { this . content . ios . scrollView . refreshControl = this . refreshControl ; } else { this . content . ios . scrollView . alwaysBounceVertical = true ; this . content . ios . scrollView . addSubview ( this . refreshControl ) ; } } else if ( typeof TKListView !== 'undefined' && this . content . ios instanceof TKListView ) { if ( SUPPORT_REFRESH_CONTROL ) { this . content . ios . collectionView . refreshControl = this . refreshControl ; } else { this . content . ios . collectionView . alwaysBounceVertical = true ; this . content . ios . collectionView . addSubview ( this . refreshControl ) ; } } else if ( this . content . ios instanceof WKWebView ) { if ( SUPPORT_REFRESH_CONTROL ) { this . content . ios . scrollView . refreshControl = this . refreshControl ; } else { this . content . ios . scrollView . alwaysBounceVertical = true ; this . content . ios . scrollView . addSubview ( this . refreshControl ) ; } } else { throw new Error ( 'Content must inherit from either UIScrollView or WKWebView!' ) ; } } [ common . refreshingProperty . getDefault ] ( ) { return false ; } [ common . refreshingProperty . setNative ] ( value ) { if ( value ) { this . refreshControl . beginRefreshing ( ) ; } else { this . refreshControl . endRefreshing ( ) ; } } [ common . indicatorColorProperty . getDefault ] ( ) { return this . refreshControl . tintColor ; } [ common . indicatorColorProperty . setNative ] ( value ) { const color = value ? value . ios : this . color ; this . refreshControl . tintColor = color ; } [ common . indicatorColorStyleProperty . getDefault ] ( ) { return this . refreshControl . tintColor ; } [ common . indicatorColorStyleProperty . setNative ] ( value ) { if ( ( this as any ) . indicatorColor ) { return ; } const color = value ? value . ios : this . color ; this . refreshControl . tintColor = color ; } [ common . indicatorFillColorProperty . getDefault ] ( ) { return this . refreshControl . backgroundColor ; } [ common . indicatorFillColorProperty . setNative ] ( value ) { const color = value ? value . ios : this . backgroundColor ; this . refreshControl . backgroundColor = color ; } [ common . indicatorFillColorStyleProperty . getDefault ] ( ) { return this . refreshControl . backgroundColor ; } [ common . indicatorFillColorStyleProperty . setNative ] ( value ) { if ( ( this as any ) . indicatorFillColor ) { return ; } const color = value ? value . ios : this . backgroundColor ; this . refreshControl . backgroundColor = color ; } }