import { Color , Device , Enums , Frame , Length , Screen , Utils } from '@nativescript/core' ; import { ToastDuration , ToastPosition , ToastyOptions } from './toast.common' ; export * from './toast.common' ; export class Toasty { private _text : string ; private _duration : ToastDuration | number ; private _position : ToastPosition | number ; private _textColor : Color | string ; private _backgroundColor : Color | string ; private _iOSOpts : ToastyOptions [ 'ios' ] ; private _toastStyle ; private _x : Length ; private _y : Length ; private _tapToDismiss : boolean ; private _anchorView : any ; constructor ( opts ? ) { this . _toastStyle = ToastStyle . new ( ) ; this . _text = opts ?. text ; this . _duration = opts ?. duration ?? 2 ; this . _position = opts ?. position ?? 2 ; this . _textColor = opts ?. textColor ?? 'white' ; this . _backgroundColor = opts ?. backgroundColor || 'black' ; this . _iOSOpts = opts ?. ios ?? { } ; this . _x = opts ?. xAxisOffset ; this . _y = opts ?. yAxisOffset ; this . _tapToDismiss = opts ?. tapToDismiss ?? false ; this . _anchorView = opts ?. anchorView ; this . setToastDuration ( this . _duration ) . setToastPosition ( this . _position ) . setTextColor ( this . _textColor ) . setBackgroundColor ( this . _backgroundColor ) ; if ( this . _iOSOpts . displayShadow ) { this . _toastStyle . displayShadow = this . _iOSOpts . displayShadow ; if ( this . _iOSOpts . shadowColor ) { if ( typeof this . _iOSOpts . shadowColor === 'string' ) { this . _toastStyle . shadowColor = new Color ( this . _iOSOpts . shadowColor ) . ios ; } else { this . _toastStyle . shadowColor = ( this . _iOSOpts ?. shadowColor as any ) ?. ios ?? new Color ( 'black' ) . ios ; } } } if ( this . _iOSOpts . cornerRadius || this . _iOSOpts . cornerRadius === 0 ) { this . _toastStyle . cornerRadius = this . _iOSOpts . cornerRadius ; } if ( this . _iOSOpts . messageNumberOfLines ) { this . _toastStyle . messageNumberOfLines = this . _iOSOpts . messageNumberOfLines ; } ToastManager . shared . isTapToDismissEnabled = this . _tapToDismiss ; ToastManager . shared . style = this . _toastStyle ; return this ; } get position ( ) { return this . _position ; } set position ( value ) { if ( value ) { this . _position = value ; this . setToastPosition ( value ) ; } } get duration ( ) { return this . _duration ; } set duration ( value ) { if ( value ) { this . _duration = value ; this . setToastDuration ( value ) ; } } get textColor ( ) { return this . _textColor ; } set textColor ( value ) { if ( value ) { this . _textColor = value ; this . setTextColor ( value ) ; } } get backgroundColor ( ) { return this . _backgroundColor ; } set backgroundColor ( value ) { if ( value ) { this . _backgroundColor = value ; this . setBackgroundColor ( value ) ; } } set xAxisOffset ( value ) { this . _x = value ; this . _updateToastPosition ( ) ; } set yAxisOffset ( value ) { this . _y = value ; this . _updateToastPosition ( ) ; } get xAxisOffset ( ) : Length | number { return this . _x ; } get yAxisOffset ( ) : Length | number { return this . _y ; } private static isLength ( value ) { return ( value && ! Utils . isNullOrUndefined ( value . unit ) && ! Utils . isNullOrUndefined ( value . value ) ) ; } private get yPixels ( ) : number | undefined { let y ; if ( typeof this . _y === 'number' ) { y = this . _y ; } if ( Toasty . isLength ( this . _y ) ) { if ( ( this . _y as any ) . unit === 'px' ) { y = ( this . _y as any ) . value / Screen . mainScreen . scale ; } if ( ( this . _y as any ) . unit === 'dip' ) { y = ( this . _y as any ) . value ; } } if ( typeof this . _y === 'string' ) { if ( this . _y . includes ( 'px' ) ) { y = parseInt ( this . _y . replace ( 'px' , '' ) , 0 ) / Screen . mainScreen . scale ; } if ( this . _y . includes ( 'dip' ) ) { y = parseInt ( this . _y . replace ( 'dip' , '' ) , 0 ) ; } } return y ; } private get xPixels ( ) : number | undefined { let x ; if ( typeof this . _x === 'number' ) { x = this . _x ; } if ( Toasty . isLength ( this . _x ) ) { if ( ( this . _x as any ) . unit === 'px' ) { x = ( this . _x as any ) . value / Screen . mainScreen . scale ; } if ( ( this . _x as any ) . unit === 'dip' ) { x = ( this . _x as any ) . value ; } } if ( typeof this . _x === 'string' ) { if ( this . _x . includes ( 'px' ) ) { x = parseInt ( this . _x . replace ( 'px' , '' ) , 0 ) / Screen . mainScreen . scale ; } if ( this . _x . includes ( 'dip' ) ) { x = parseInt ( this . _x . replace ( 'dip' , '' ) , 0 ) ; } } return x ; } private getOffset ( offset ) { let symbol ; switch ( this . _position ) { case ToastPosition . TOP_LEFT : case ToastPosition . BOTTOM_LEFT : case ToastPosition . CENTER_LEFT : symbol = '-' ; break ; case ToastPosition . TOP_RIGHT : case ToastPosition . BOTTOM_RIGHT : case ToastPosition . CENTER_RIGHT : symbol = '+' ; break ; default : const value = `${ offset }` ; if ( value . includes ( '-' ) ) { symbol = '' ; } else { symbol = '-' ; } break ; } const result = Number ( `${ symbol }${ offset }` ) ; return Number . isNaN ( result ) ? undefined : result ; } show ( ) { if ( ! this . _text ) { throw new Error ( 'Text is not set' ) ; } else { this . _getView ( ) ?. makeToastWithOffset ( this . _text , CGPointMake ( this . getOffset ( this . xPixels ) ?? 0 , this . getOffset ( this . yPixels ) ?? 0 ) ) ; } } get width ( ) { return 0 ; } get height ( ) { return 0 ; } cancel ( ) { this . _getView ( ) ?. hideAllToastsWithIncludeActivityClearQueue ( true , true ) ; } setTextColor ( value ) { if ( value ) { this . _textColor = value ; if ( typeof value === 'string' ) { this . _toastStyle . messageColor = new Color ( value ) . ios ; } else { this . _toastStyle . messageColor = value . ios ; } ToastManager . shared . style = this . _toastStyle ; } return this ; } setBackgroundColor ( value ) { if ( value ) { this . _backgroundColor = value ; if ( typeof value === 'string' ) { this . _toastStyle . backgroundColor = new Color ( value ) . ios ; } else { this . _toastStyle . backgroundColor = value . ios ; } ToastManager . shared . style = this . _toastStyle ; } return this ; } private _updateToastPosition ( ) { switch ( this . _position ) { case ToastPosition . TOP : ToastManager . shared . position = 0 ; break ; case ToastPosition . TOP_LEFT : ToastManager . shared . position = 3 ; break ; case ToastPosition . TOP_RIGHT : ToastManager . shared . position = 4 ; break ; case ToastPosition . CENTER : ToastManager . shared . position = 1 ; break ; case ToastPosition . CENTER_LEFT : ToastManager . shared . position = 5 ; break ; case ToastPosition . CENTER_RIGHT : ToastManager . shared . position = 6 ; break ; case ToastPosition . BOTTOM : ToastManager . shared . position = 2 ; break ; case ToastPosition . BOTTOM_LEFT : ToastManager . shared . position = 7 ; break ; case ToastPosition . BOTTOM_RIGHT : ToastManager . shared . position = 8 ; break ; default : break ; } } setToastPosition ( value ) { this . _position = value ; this . _updateToastPosition ( ) ; return this ; } setToastDuration ( value ) { switch ( value ) { case ToastDuration . SHORT : ToastManager . shared . duration = 2.0 ; break ; case ToastDuration . LONG : ToastManager . shared . duration = 4.0 ; break ; default : ToastManager . shared . duration = 2.0 ; break ; } return this ; } private _getView ( ) { if ( ! Frame . topmost ( ) ) { const root = this . topViewController ; if ( ! root ) { throw new Error ( 'There is no topmost' ) ; } return root . view . window ; } else { if ( this . _iOSOpts && this . _anchorView ) { return this . _anchorView ; } else { let viewController = Frame . topmost ( ) . viewController as UIViewController ; if ( viewController . presentedViewController ) { if ( Device . deviceType !== Enums . DeviceType . Tablet ) { while ( viewController . presentedViewController ) { viewController = viewController . presentedViewController ; } } } return viewController . view ; } } } private static get rootViewController ( ) : UIViewController | undefined { const keyWindow = UIApplication . sharedApplication . keyWindow ; return keyWindow != null ? keyWindow . rootViewController : undefined ; } private get topViewController ( ) : UIViewController | undefined { const root = Toasty . rootViewController ; if ( root == null ) { return undefined ; } return this . findTopViewController ( root ) ; } private findTopViewController ( root ) { const presented = root . presentedViewController ; if ( presented != null ) { return this . findTopViewController ( presented ) ; } if ( root instanceof UISplitViewController ) { const last = root . viewControllers . lastObject ; if ( last == null ) { return root ; } return this . findTopViewController ( last ) ; } else if ( root instanceof UINavigationController ) { const top = root . topViewController ; if ( top == null ) { return root ; } return this . findTopViewController ( top ) ; } else if ( root instanceof UITabBarController ) { const selected = root . selectedViewController ; if ( selected == null ) { return root ; } return this . findTopViewController ( selected ) ; } else { return root ; } } }