import { FeedbackCommon , FeedbackShowOptions , FeedbackHideOptions , FeedbackType , FeedbackPosition } from "./feedback.common" ; import { Application , Color , Utils } from "@nativescript/core" ; declare const com ; export { FeedbackType } ; export { FeedbackPosition } ; export class Feedback extends FeedbackCommon { private lastAlert : any = null ; show ( options ) { return new Promise < void > ( ( resolve , reject ) => { this . lastAlert = null ; let alerter = com . tapadoo . alerter . Alerter . create ( Application . android . foregroundActivity ) . setLayoutGravity ( Feedback . getPosition ( options . position ) ) . setIconColorFilter ( 0 ) . setDuration ( options . duration ? options . duration : 4000 ) ; if ( options . title ) { alerter . setTitle ( options . title ) ; } if ( options . message ) { alerter . setText ( options . message ) ; } if ( options . icon ) { let resourceId = Feedback . getIconResourceId ( options . icon ) ; if ( resourceId === 0 ) { console . log ( `icon '${ options . icon }' resource not found` ) ; } else { alerter . setIcon ( resourceId ) ; } } else { let resourcename = Feedback . getIconName ( options . type ) ; if ( resourcename !== null ) { alerter . setIcon ( Feedback . getIconResourceId ( resourcename ) ) ; } else { alerter . showIcon ( false ) ; } } if ( options . android && options . android . iconPulseEnabled !== undefined ) { alerter . enableIconPulse ( options . android . iconPulseEnabled ) ; } if ( options . titleFont ) { const assetManger = Utils . ad . getApplicationContext ( ) . getAssets ( ) ; const fontPath = `app/fonts/${ options . titleFont }` ; const typeface = android . graphics . Typeface . createFromAsset ( assetManger , fontPath ) ; alerter . setTitleTypeface ( typeface ) ; } if ( options . messageFont ) { const assetManger = Utils . ad . getApplicationContext ( ) . getAssets ( ) ; const fontPath = `app/fonts/${ options . messageFont }` ; const typeface = android . graphics . Typeface . createFromAsset ( assetManger , fontPath ) ; alerter . setTextTypeface ( typeface ) ; } alerter . setOnClickListener ( new android . view . View . OnClickListener ( { onClick : ( view => { com . tapadoo . alerter . Alerter . hide ( ) ; if ( options . onTap ) { options . onTap ( ) ; } } ) } ) ) ; if ( options . onShow ) { alerter . setOnShowListener ( new com . tapadoo . alerter . OnShowAlertListener ( { onShow : ( ) => options . onShow ( ) , } ) ) ; } if ( options . onHide ) { alerter . setOnHideListener ( new com . tapadoo . alerter . OnHideAlertListener ( { onHide : ( ) => options . onHide ( ) , } ) ) ; } this . lastAlert = alerter . show ( ) ; if ( options . backgroundColor ) { this . lastAlert . setAlertBackgroundColor ( options . backgroundColor . android ) ; } else { this . lastAlert . setAlertBackgroundColor ( Feedback . getBackgroundColor ( options . type ) . android ) ; } if ( options . titleColor ) { let titleView = this . lastAlert . getTitle ( ) ; titleView . setTextColor ( options . titleColor . android ) ; } if ( options . messageColor ) { let messageView = this . lastAlert . getText ( ) ; messageView . setTextColor ( options . messageColor . android ) ; } const titleSize = options . titleSize || 16 ; const messageSize = options . messageSize || 13 ; this . lastAlert . getTitle ( ) . setTextSize ( titleSize ) ; this . lastAlert . getText ( ) . setTextSize ( messageSize ) ; if ( options . android && options . android . iconColor ) { alerter . setIconColorFilter ( options . android . iconColor . android ) ; } resolve ( ) ; } ) ; } private static getBackgroundColor ( type ? ) { if ( type === undefined || type === null || type as FeedbackType === FeedbackType . Custom ) { return new Color ( "#73b7e8" ) ; } else if ( type as FeedbackType === FeedbackType . Warning ) { return new Color ( "#f18b34" ) ; } else if ( type as FeedbackType === FeedbackType . Error ) { return new Color ( "#ee664c" ) ; } else if ( type as FeedbackType === FeedbackType . Info ) { return new Color ( "#516a78" ) ; } else { return new Color ( "#51ae8c" ) ; } } private static getIconResourceId ( resourcename ) { let res = Utils . ad . getApplicationContext ( ) . getResources ( ) ; return res . getIdentifier ( resourcename , "drawable" , Utils . ad . getApplication ( ) . getPackageName ( ) ) ; } private static getIconName ( type ? ) { if ( type === undefined || type === null || type as FeedbackType === FeedbackType . Custom ) { return null ; } else if ( type as FeedbackType === FeedbackType . Warning ) { return "warningicon" ; } else if ( type as FeedbackType === FeedbackType . Error ) { return "erroricon" ; } else if ( type as FeedbackType === FeedbackType . Info ) { return "infoicon" ; } else { return "successicon" ; } } private static getPosition ( position ? ) { if ( ! position || ( position as FeedbackPosition ) === FeedbackPosition . Top ) { return android . view . Gravity . TOP ; } else { return android . view . Gravity . BOTTOM ; } } hide ( options ? ) { return new Promise < void > ( ( resolve ) => { if ( com . tapadoo . alerter . Alerter . isShowing ( ) ) { com . tapadoo . alerter . Alerter . hide ( ) ; this . lastAlert = null ; } resolve ( ) ; } ) ; } }