import { state , stateSets } from '@nativescript-community/ui-material-core/android/utils' ; import { Color , ImageSource , Utils } from '@nativescript/core' ; import { BottomNavigationBarBase , BottomNavigationTabBase , TitleVisibility , activeColorCssProperty , iconProperty , inactiveColorCssProperty , tabsProperty , titleVisibilityProperty } from './bottomnavigationbar-common' ; declare type OnNavigationItemSelectedListener = com . google . android . material . bottomnavigation . BottomNavigationView . OnNavigationItemSelectedListener ; declare type OnNavigationItemReselectedListener = com . google . android . material . bottomnavigation . BottomNavigationView . OnNavigationItemReselectedListener ; type OnTabSelectedlistener = new ( owner ) => OnNavigationItemSelectedListener ; let OnTabSelectedlistener ; const getOnTabSelectedlistener = ( ) => { if ( OnTabSelectedlistener ) { return OnTabSelectedlistener ; } @ NativeClass @ Interfaces ( [ com . google . android . material . bottomnavigation . BottomNavigationView . OnNavigationItemSelectedListener ] ) class OnTabSelectedlistenerImpl extends java . lang . Object implements OnNavigationItemSelectedListener { constructor ( public owner ) { super ( ) ; return global . __native ( this ) ; } public onNavigationItemSelected ( menuItem ) { const index = menuItem . getItemId ( ) ; const bottomNavigationTab = this . owner . items [ index ] ; if ( bottomNavigationTab . isSelectable ) { this . owner . _emitTabSelected ( index ) ; } else { this . owner . _emitTabPressed ( index ) ; } return bottomNavigationTab . isSelectable ; } } OnTabSelectedlistener = OnTabSelectedlistenerImpl ; return OnTabSelectedlistener ; } ; type OnTabReselectedListener = new ( owner ) => OnNavigationItemReselectedListener ; let OnTabReselectedListener ; const getOnTabReselectedListener = ( ) => { if ( OnTabReselectedListener ) { return OnTabReselectedListener ; } @ NativeClass @ Interfaces ( [ com . google . android . material . bottomnavigation . BottomNavigationView . OnNavigationItemReselectedListener ] ) class OnTabReselectedListenerImpl extends java . lang . Object implements OnNavigationItemReselectedListener { constructor ( public owner ) { super ( ) ; return global . __native ( this ) ; } public onNavigationItemReselected ( menuItem ) { this . owner . _emitTabReselected ( menuItem . getItemId ( ) ) ; } } OnTabReselectedListener = OnTabReselectedListenerImpl ; return OnTabReselectedListener ; } ; function createColorStateList ( activeColor , inactiveColor ) { const stateChecked = Array . create ( 'int' , 1 ) ; stateChecked [ 0 ] = state . checked ; const stateUnChecked = Array . create ( 'int' , 0 ) ; const states = Array . create ( '[I' , 2 ) ; states [ 0 ] = stateChecked ; states [ 1 ] = stateUnChecked ; const colors = Array . create ( 'int' , 2 ) ; colors [ 0 ] = activeColor ; colors [ 1 ] = inactiveColor ; return new android . content . res . ColorStateList ( states , colors ) ; } export class BottomNavigationBar extends BottomNavigationBarBase { nativeViewProtected : com . google . android . material . bottomnavigation . BottomNavigationView ; _items : BottomNavigationTab [ ] ; reselectListener : OnNavigationItemReselectedListener ; selectListener : OnNavigationItemSelectedListener ; createNativeView ( ) { return new com . google . android . material . bottomnavigation . BottomNavigationView ( this . _context ) ; } initNativeView ( ) { super . initNativeView ( ) ; const OnTabReselectedListener = getOnTabReselectedListener ( ) ; const OnTabSelectedListener = getOnTabSelectedlistener ( ) ; this . reselectListener = new OnTabReselectedListener ( this ) ; this . selectListener = new OnTabSelectedListener ( this ) ; this . nativeViewProtected . setOnNavigationItemReselectedListener ( this . reselectListener ) ; this . nativeViewProtected . setOnNavigationItemSelectedListener ( this . selectListener ) ; this . createTabs ( this . _items ) ; this . selectTabNative ( this . selectedTabIndex ) ; } disposeNativeView ( ) { this . nativeViewProtected . setOnNavigationItemReselectedListener ( null ) ; this . nativeViewProtected . setOnNavigationItemSelectedListener ( null ) ; this . reselectListener = null ; this . selectListener = null ; this . _items . forEach ( ( item ) => this . _removeView ( item ) ) ; super . disposeNativeView ( ) ; } showBadge ( index , value ? ) { const badge = this . nativeViewProtected . getOrCreateBadge ( index ) ; if ( this . badgeColor ) { badge . setBackgroundColor ( this . badgeColor . android ) ; } if ( this . badgeTextColor ) { badge . setBadgeTextColor ( this . badgeTextColor . android ) ; } if ( value ) { badge . setNumber ( value ) ; } } removeBadge ( index ) { ( this . nativeViewProtected as any ) . removeBadge ( index ) ; } [ tabsProperty . setNative ] ( tabs ) { this . createTabs ( tabs ) ; } [ titleVisibilityProperty . setNative ] ( titleVisibility ) { this . nativeViewProtected . setLabelVisibilityMode ( titleVisibility ) ; } [ activeColorCssProperty . setNative ] ( activeColor ) { const color1 = activeColor instanceof Color ? activeColor . android : activeColor ; const color2 = this . inactiveColor instanceof Color ? this . inactiveColor . android : this . nativeViewProtected . getItemTextColor ( ) . getColorForState ( stateSets . BACKGROUND_DEFAULT_STATE_2 , color1 ) ; const colorStateList = createColorStateList ( color1 , color2 ) ; this . nativeViewProtected . setItemTextColor ( colorStateList ) ; this . nativeViewProtected . setItemIconTintList ( colorStateList ) ; } [ inactiveColorCssProperty . setNative ] ( inactiveColor ) { const color2 = inactiveColor instanceof Color ? inactiveColor . android : inactiveColor ; const color1 = this . activeColor instanceof Color ? this . activeColor . android : this . nativeViewProtected . getItemTextColor ( ) . getColorForState ( stateSets . FOCUSED_STATE_SET , color2 ) ; const colorStateList = createColorStateList ( color1 , color2 ) ; this . nativeViewProtected . setItemTextColor ( colorStateList ) ; this . nativeViewProtected . setItemIconTintList ( colorStateList ) ; } protected createTabs ( tabs ) { const bottomNavigationTabs = this . nativeViewProtected . getMenu ( ) ; if ( bottomNavigationTabs . size ( ) > 0 ) { bottomNavigationTabs . clear ( ) ; } if ( tabs ) { this . _items = tabs ; } this . _items . forEach ( ( item , index ) => { item . index = index ; this . _addView ( item ) ; } ) ; } protected selectTabNative ( index ) { const bottomNavigationTabs = this . nativeView . getMenu ( ) ; if ( bottomNavigationTabs . size ( ) === 0 ) { return ; } this . nativeViewProtected . setSelectedItemId ( index ) ; this . selectedTabIndex = index ; } } export class BottomNavigationTab extends BottomNavigationTabBase { nativeViewProtected : android . view . MenuItem ; index : number = android . view . Menu . NONE ; _isPaddingRelative = true ; createNativeView ( ) { const view = ( this . parent as BottomNavigationBar ) . nativeViewProtected . getMenu ( ) . add ( android . view . Menu . NONE , this . index , android . view . Menu . NONE , this . title ) ; ( view as any ) . defaultPaddings = { top : 0 , left : 0 , bottom : 0 , right : 0 } ; return view ; } initNativeView ( ) { } [ iconProperty . setNative ] ( iconSource ) { if ( ! iconSource ) { return null ; } let is ; if ( iconSource instanceof ImageSource ) { is = iconSource ; } else if ( Utils . isFontIconURI ( iconSource ) ) { const fontIconCode = iconSource . split ( '//' ) [ 1 ] ; const font = this . style . fontInternal ; is = ImageSource . fromFontIconCodeSync ( fontIconCode , font , new Color ( 'white' ) ) ; } else { is = ImageSource . fromFileOrResourceSync ( iconSource ) ; } this . nativeViewProtected . setIcon ( is ? new android . graphics . drawable . BitmapDrawable ( is . android ) : null ) ; } showBadge ( value ? ) { ( this . parent as BottomNavigationBar ) . showBadge ( this . index , value ) ; } removeBadge ( ) { ( this . parent as BottomNavigationBar ) . removeBadge ( this . index ) ; } [ activeColorCssProperty . setNative ] ( activeColor ) { const color1 = activeColor instanceof Color ? activeColor . android : activeColor ; const color2 = this . inactiveColor instanceof Color ? this . inactiveColor . android : this . nativeViewProtected . getIconTintList ( ) ? this . nativeViewProtected . getIconTintList ( ) . getColorForState ( stateSets . BACKGROUND_DEFAULT_STATE_2 , color1 ) : 0 ; const colorStateList = createColorStateList ( color1 , color2 ) ; this . nativeViewProtected . setIconTintList ( colorStateList ) ; } [ inactiveColorCssProperty . setNative ] ( inactiveColor ) { const color2 = inactiveColor instanceof Color ? inactiveColor . android : inactiveColor ; const color1 = this . activeColor instanceof Color ? this . activeColor . android : this . nativeViewProtected . getIconTintList ( ) ? this . nativeViewProtected . getIconTintList ( ) . getColorForState ( stateSets . SELECTED_STATE_SET , color2 ) : 0 ; const colorStateList = createColorStateList ( color1 , color2 ) ; this . nativeViewProtected . setIconTintList ( colorStateList ) ; } }