import { BottomBarBase , items , androidLabelVisibility , inactiveTintColor , activeTintColor , barBackgroundColor } from '../bottombar.base' ; import { Color } from 'tns-core-modules/color/color' ; import { BottomBarItem } from '../../bottombar-item/android/bottombar-item' ; import { createIconsStateListDrawable } from '../../utils/android/utils' ; import { LABEL_VISIBILITY } from '../bottombar.common' ; export declare type BottomNavigationViewType = com . google . android . material . bottomnavigation . BottomNavigationView ; export declare type MenuItemType = android . view . MenuItem ; export declare type MenuType = android . view . Menu ; const { BottomNavigationView } = com . google . android . material . bottomnavigation ; const { Menu } = android . view ; const { ColorStateList } = android . content . res ; export class BottomBar extends BottomBarBase { nativeView : BottomNavigationViewType ; _items : BottomBarItem [ ] ; get android ( ) { return this . nativeView ; } public createNativeView ( ) { const bottomBar = new BottomNavigationView ( this . _context ) ; const owner = new WeakRef ( this ) ; bottomBar . setOnNavigationItemSelectedListener ( new BottomNavigationView . OnNavigationItemSelectedListener ( { onNavigationItemSelected : ( item ) => { owner . get ( ) . onTabSelected ( item . getItemId ( ) ) ; return true ; } } ) ) ; this . createItems ( bottomBar ) ; bottomBar . setBackgroundColor ( this . barBackgroundColor . android ) ; this . setItemsColorStateList ( this . activeTintColor . android , this . inactiveTintColor . android , bottomBar ) ; ( bottomBar as any ) . setLabelVisibilityMode ( LABEL_VISIBILITY . AUTO ) ; return bottomBar ; } public _addChildFromBuilder ( name , value ) { if ( name === 'BottomBarItem' ) { this . _items . push ( value ) ; } } private createItem ( item , id , bottomBar ) { const nativeItem = bottomBar . getMenu ( ) . add ( Menu . NONE , id , Menu . NONE , '' ) ; item . index = id ; if ( item . _icon && item . _title ) { nativeItem . setTitle ( item . _title ) ; nativeItem . setIcon ( createIconsStateListDrawable ( item . _icon , item . _checkedIcon ) ) ; } return nativeItem ; } protected createItems ( bottomNavigationView ) { const menu = bottomNavigationView . getMenu ( ) ; if ( menu . size ( ) > 0 ) { menu . clear ( ) ; } this . _items . forEach ( ( item , index ) => { const menuItem = this . createItem ( item , index , bottomNavigationView ) ; item . setNativeView ( menuItem ) ; } ) ; this . createBadges ( bottomNavigationView ) ; } private createBadges ( bottomNavigationView ) { this . _items . forEach ( item => { if ( item . _badge ) { item . setViewBadge ( bottomNavigationView , this . _context ) ; } } ) ; } private setItemsColorStateList ( activeTintColor , inactiveTintColor , bottomBarValue ? ) { const stateUnChecked = Array . create ( 'int' , 1 ) ; stateUnChecked [ 0 ] = - android . R . attr . state_checked ; const defaultState = Array . create ( 'int' , 0 ) ; const states = java . lang . reflect . Array . newInstance ( stateUnChecked . getClass ( ) || defaultState . getClass ( ) , 2 ) ; states [ 0 ] = stateUnChecked ; states [ 1 ] = defaultState ; const colors = Array . create ( 'int' , 2 ) ; colors [ 0 ] = inactiveTintColor ; colors [ 1 ] = activeTintColor ; const bottomBar = bottomBarValue ? bottomBarValue : this . nativeView ; const colorStateList = new ColorStateList ( states , colors ) ; bottomBar . setItemTextColor ( colorStateList ) ; bottomBar . setItemIconTintList ( colorStateList ) ; } onTabSelected ( index ) { super . onTabSelected ( index ) ; } [ items . setNative ] ( items ) { if ( items ) { this . _items = [ ... items ] ; this . createItems ( this . nativeView ) ; } } [ inactiveTintColor . setNative ] ( color ) { this . setItemsColorStateList ( this . activeTintColor . android , color . android ) ; } [ activeTintColor . setNative ] ( color ) { this . setItemsColorStateList ( color . android , this . inactiveTintColor . android ) ; } [ barBackgroundColor . setNative ] ( color ) { this . nativeView . setBackgroundColor ( color . android ) ; } [ androidLabelVisibility . setNative ] ( labelVisibility ) { ( this . nativeView as any ) . setLabelVisibilityMode ( labelVisibility ) ; } private getItemByIndex ( index ) { const selectedItem = this . _items . find ( item => item . index === index ) ; if ( ! selectedItem ) { throw new Error ( `Couldn't find the BottomBarItem with the index: ${ index }` ) ; } return selectedItem ; } selectItem ( index ) { const selectedItem = this . getItemByIndex ( index ) ; this . nativeView . setSelectedItemId ( selectedItem . nativeView . getItemId ( ) ) ; } }