@@ -89,6 +89,7 @@ import com.didi.dimina.bean.MergedPageConfig
8989import com.didi.dimina.bean.MiniProgram
9090import com.didi.dimina.bean.PathInfo
9191import com.didi.dimina.bean.TabBarConfig
92+ import com.didi.dimina.bean.TabBarItem
9293import com.didi.dimina.common.LogUtils
9394import com.didi.dimina.common.PathUtils
9495import com.didi.dimina.common.Utils
@@ -130,6 +131,9 @@ class DiminaActivity : ComponentActivity() {
130131 private val backgroundColor = mutableStateOf(" #FFFFFF" )
131132 private val tabBarConfigState = mutableStateOf<TabBarConfig ?>(null )
132133 private val selectedTabIndex = mutableIntStateOf(- 1 )
134+ private val tabBarVisible = mutableStateOf(true )
135+ private val tabBarBadges = mutableStateOf<List <String >>(emptyList())
136+ private val tabBarRedDots = mutableStateOf<List <Boolean >>(emptyList())
133137 private val currentPagePath = mutableStateOf(" " )
134138 private val useTabBarContainer = mutableStateOf(false )
135139 private val loadedTabIndices = mutableStateOf<Set <Int >>(emptySet())
@@ -477,6 +481,7 @@ class DiminaActivity : ComponentActivity() {
477481 withContext(Dispatchers .Main ) {
478482 // 4.设置标题栏以及状态栏颜色模式
479483 tabBarConfigState.value = appConfig.app.tabBar
484+ resetTabBarDynamicState(appConfig.app.tabBar)
480485 val initialTabIndex = getTabBarIndex(pathInfo.pagePath)
481486 useTabBarContainer.value = miniProgram.root && initialTabIndex >= 0
482487 syncTabBarState(pathInfo.pagePath)
@@ -747,6 +752,136 @@ class DiminaActivity : ComponentActivity() {
747752 } ? : - 1
748753 }
749754
755+ private fun resetTabBarDynamicState (config : TabBarConfig ? ) {
756+ val listLength = config?.list?.size ? : 0
757+ tabBarVisible.value = true
758+ tabBarBadges.value = List (listLength) { " " }
759+ tabBarRedDots.value = List (listLength) { false }
760+ }
761+
762+ fun getTabBarItemCount (): Int {
763+ val stateCount = tabBarConfigState.value?.list?.size ? : 0
764+ if (stateCount > 0 ) {
765+ return stateCount
766+ }
767+ if (! ::appConfig.isInitialized) {
768+ return 0
769+ }
770+ return appConfig.app.tabBar?.list?.size ? : 0
771+ }
772+
773+ fun setTabBarStyle (
774+ color : String? ,
775+ selectedColor : String? ,
776+ backgroundColor : String? ,
777+ borderStyle : String? ,
778+ ) {
779+ runOnUiThread {
780+ val config = tabBarConfigState.value ? : return @runOnUiThread
781+ val safeBorderStyle = if (borderStyle == " black" || borderStyle == " white" ) {
782+ borderStyle
783+ } else {
784+ null
785+ }
786+ tabBarConfigState.value = config.copy(
787+ color = color ? : config.color,
788+ selectedColor = selectedColor ? : config.selectedColor,
789+ backgroundColor = backgroundColor ? : config.backgroundColor,
790+ borderStyle = safeBorderStyle ? : config.borderStyle,
791+ )
792+ }
793+ }
794+
795+ fun setTabBarItem (
796+ index : Int ,
797+ text : String? ,
798+ iconPath : String? ,
799+ selectedIconPath : String? ,
800+ ) {
801+ runOnUiThread {
802+ val config = tabBarConfigState.value ? : return @runOnUiThread
803+ val oldItem = config.list.getOrNull(index) ? : return @runOnUiThread
804+ val newList = config.list.toMutableList()
805+ newList[index] = TabBarItem (
806+ pagePath = oldItem.pagePath,
807+ iconPath = iconPath ? : oldItem.iconPath,
808+ selectedIconPath = selectedIconPath ? : oldItem.selectedIconPath,
809+ text = text ? : oldItem.text,
810+ )
811+ tabBarConfigState.value = config.copy(list = newList)
812+ }
813+ }
814+
815+ fun showTabBar () {
816+ runOnUiThread {
817+ tabBarVisible.value = true
818+ }
819+ }
820+
821+ fun hideTabBar () {
822+ runOnUiThread {
823+ tabBarVisible.value = false
824+ }
825+ }
826+
827+ fun setTabBarBadge (index : Int , text : String ) {
828+ runOnUiThread {
829+ val listLength = getTabBarItemCount()
830+ val badges = normalizedBadgeList(listLength).toMutableList()
831+ val redDots = normalizedRedDotList(listLength).toMutableList()
832+ if (index in badges.indices) {
833+ badges[index] = text
834+ redDots[index] = false
835+ tabBarBadges.value = badges
836+ tabBarRedDots.value = redDots
837+ }
838+ }
839+ }
840+
841+ fun removeTabBarBadge (index : Int ) {
842+ runOnUiThread {
843+ val listLength = getTabBarItemCount()
844+ val badges = normalizedBadgeList(listLength).toMutableList()
845+ if (index in badges.indices) {
846+ badges[index] = " "
847+ tabBarBadges.value = badges
848+ }
849+ }
850+ }
851+
852+ fun showTabBarRedDot (index : Int ) {
853+ runOnUiThread {
854+ val listLength = getTabBarItemCount()
855+ val badges = normalizedBadgeList(listLength).toMutableList()
856+ val redDots = normalizedRedDotList(listLength).toMutableList()
857+ if (index in redDots.indices) {
858+ redDots[index] = true
859+ badges[index] = " "
860+ tabBarBadges.value = badges
861+ tabBarRedDots.value = redDots
862+ }
863+ }
864+ }
865+
866+ fun hideTabBarRedDot (index : Int ) {
867+ runOnUiThread {
868+ val listLength = getTabBarItemCount()
869+ val redDots = normalizedRedDotList(listLength).toMutableList()
870+ if (index in redDots.indices) {
871+ redDots[index] = false
872+ tabBarRedDots.value = redDots
873+ }
874+ }
875+ }
876+
877+ private fun normalizedBadgeList (listLength : Int ): List <String > {
878+ return List (listLength) { index -> tabBarBadges.value.getOrElse(index) { " " } }
879+ }
880+
881+ private fun normalizedRedDotList (listLength : Int ): List <Boolean > {
882+ return List (listLength) { index -> tabBarRedDots.value.getOrElse(index) { false } }
883+ }
884+
750885 private fun tabWebViewIdentifier (index : Int ): String {
751886 val pagePath = appConfig.app.tabBar?.list?.getOrNull(index)?.pagePath ? : " unknown"
752887 return " tab_${miniProgram.appId} _${index} _${pagePath} "
@@ -1181,7 +1316,10 @@ class DiminaActivity : ComponentActivity() {
11811316 val navBarBgColor = parseCssColor(navigationBarBackgroundColor.value)
11821317 val isCustomNavigation = ! showNavigationBar.value
11831318 val tabBarConfig = tabBarConfigState.value
1184- val shouldShowTabBar = ! isLoading.value && tabBarConfig != null && getTabBarIndex(currentPagePath.value) >= 0
1319+ val shouldShowTabBar = ! isLoading.value &&
1320+ tabBarConfig != null &&
1321+ tabBarVisible.value &&
1322+ getTabBarIndex(currentPagePath.value) >= 0
11851323 val statusBarHeight = ComposeWindowInsets .statusBars.asPaddingValues().calculateTopPadding()
11861324
11871325 Box (modifier = modifier.fillMaxSize()) {
@@ -1274,6 +1412,8 @@ class DiminaActivity : ComponentActivity() {
12741412 selectedIndex = selectedTabIndex.intValue,
12751413 appId = miniProgram.appId,
12761414 filesDir = filesDir,
1415+ badges = tabBarBadges.value,
1416+ redDots = tabBarRedDots.value,
12771417 onSelected = { index ->
12781418 visibleTabBarConfig.list.getOrNull(index)?.let { item ->
12791419 switchTab(item.pagePath)
0 commit comments