Prechádzať zdrojové kódy

working on custom navigation bottom bar...

Khubaib 9 mesiacov pred
rodič
commit
5726fda455

+ 97 - 43
app/src/main/java/com/vpn/fastestvpnservice/screensTV/BottomBarMainScreenTV.kt

@@ -3,6 +3,7 @@ package com.vpn.fastestvpnservice.screensTV
 import android.util.Log
 import androidx.activity.ComponentActivity
 import androidx.compose.foundation.background
+import androidx.compose.foundation.focusable
 import androidx.compose.foundation.layout.Arrangement
 import androidx.compose.foundation.layout.Box
 import androidx.compose.foundation.layout.Column
@@ -15,12 +16,12 @@ import androidx.compose.foundation.layout.size
 import androidx.compose.material.BottomNavigation
 import androidx.compose.material.BottomNavigationItem
 import androidx.compose.material3.Icon
-import androidx.compose.material3.MaterialTheme
 import androidx.compose.material3.Scaffold
-import androidx.compose.material3.Text
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.MutableState
 import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableIntStateOf
 import androidx.compose.runtime.mutableStateOf
 import androidx.compose.runtime.remember
 import androidx.compose.runtime.setValue
@@ -35,7 +36,6 @@ import androidx.compose.ui.platform.LocalContext
 import androidx.compose.ui.res.colorResource
 import androidx.compose.ui.res.painterResource
 import androidx.compose.ui.unit.dp
-import androidx.compose.ui.unit.sp
 import androidx.navigation.NavDestination
 import androidx.navigation.NavDestination.Companion.hierarchy
 import androidx.navigation.NavGraph.Companion.findStartDestination
@@ -45,22 +45,26 @@ import androidx.navigation.compose.rememberNavController
 import com.vpn.fastestvpnservice.R
 import com.vpn.fastestvpnservice.navigation.BottomBarNavGraphTV
 import com.vpn.fastestvpnservice.sealedClass.BottomBarScreen
-import com.vpn.fastestvpnservice.ui.theme.customTypography2
 import com.vpn.fastestvpnservice.utils.StaticMethods
 import com.vpn.fastestvpnservice.utils.isTablet
 
+val screens = listOf(
+    BottomBarScreen.Home,
+    BottomBarScreen.Settings,
+    BottomBarScreen.Help,
+    BottomBarScreen.Account
+)
 val focusRequesterNav1 = FocusRequester()
+var focusRequestersList = List(screens.size) { FocusRequester() }
+var selectedItemIndex: MutableState<Int> = mutableIntStateOf(0)
 @Composable
 fun BottomBarMainScreenTV(navHostController: NavHostController, activity: ComponentActivity) {
     val navController1 = rememberNavController()
     var isBottomBarVisible by remember { mutableStateOf(true) }
 
-    val screens = listOf(
-        BottomBarScreen.Home,
-        BottomBarScreen.Settings,
-        BottomBarScreen.Help,
-        BottomBarScreen.Account
-    )
+LaunchedEffect(key1 = Unit) {
+    focusRequestersList[selectedItemIndex.value].requestFocus()
+}
 
     navController1.addOnDestinationChangedListener { _,_,_ ->
         isBottomBarVisible = navController1.currentDestination?.route in listOf(
@@ -98,7 +102,7 @@ fun BottomBarTV(navHostController: NavHostController, screens: List<BottomBarScr
 
     val context = LocalContext.current
     val navBackStackEntry = navHostController.currentBackStackEntryAsState()
-    val currentDestination = navBackStackEntry.value?.destination
+    var currentDestination = navBackStackEntry.value?.destination
     Log.d("currentRoute -> BB ", currentDestination.toString())
 
     Column(
@@ -117,11 +121,12 @@ fun BottomBarTV(navHostController: NavHostController, screens: List<BottomBarScr
 //                .background(Color.LightGray)
 
         ) {
-            screens.forEach { screen ->
+            screens.forEachIndexed { index, screen ->
                 AddItemTV(
                     screen = screen,
                     currentDestination = currentDestination,
-                    navHostController = navHostController
+                    navHostController = navHostController,
+                    index
                 )
             }
         }
@@ -132,7 +137,8 @@ fun BottomBarTV(navHostController: NavHostController, screens: List<BottomBarScr
 fun RowScope.AddItemTV(
     screen: BottomBarScreen,
     currentDestination: NavDestination?,
-    navHostController: NavHostController
+    navHostController: NavHostController,
+    index: Int
 ) {
     var isClicked by remember { mutableStateOf(false) }
     var isFocused by remember { mutableStateOf(false) }
@@ -155,9 +161,9 @@ fun RowScope.AddItemTV(
                     popUpTo(
                         navHostController.graph
                             .findStartDestination().id) {
-                        saveState = false
+                        saveState = true
                     }
-                    launchSingleTop = false
+                    launchSingleTop = true
                 }
             }
         }
@@ -166,36 +172,87 @@ fun RowScope.AddItemTV(
     BottomNavigationItem(
         modifier = Modifier
 //            .focusRequester(focusRequesterNav1)
+            .focusRequester(focusRequestersList[selectedItemIndex.value])
+//            .onKeyEvent {
+//                when (it.key) {
+//                    Key.DirectionRight -> {
+//                        Log.d("test_bottombar", "DirectionRight from")
+//                        Toast
+//                            .makeText(
+//                                context, "DirectionRight from Help", Toast.LENGTH_SHORT
+//                            )
+//                            .show()
+//                        if (currentDestination?.route == BottomBarScreen.Help.route) {
+////                            screen.route = BottomBarScreen.Account.route
+//                            customNavigation(navHostController, BottomBarScreen.Account)
+//                        }
+//                        true
+//                    }
+//                    Key.DirectionLeft -> {
+//                        Log.d("test_bottombar", "DirectionLeft from ")
+//
+//                        if (currentDestination?.route == BottomBarScreen.Help.route) {
+//                            Toast
+//                                .makeText(
+//                                    context, "DirectionLeft from Help", Toast.LENGTH_SHORT
+//                                )
+//                                .show()
+////                            screen.route = BottomBarScreen.Settings.route
+//                            customNavigation(navHostController, BottomBarScreen.Settings)
+//                        }
+//                        true
+//                    }
+//                    else -> { false }
+//                }
+//            }
             .onFocusChanged {
                 isFocused = it.isFocused
+
+                Log.d(
+                    "test_bottom_navbar",
+                    "onFocusChanged: ${currentDestination?.route} ${screen.route}"
+                )
+//                if (currentDestination?.route == BottomBarScreen.Home.route) {
+//                    when (screen.route) {
+//                            BottomBarScreen.Home.route -> {
+//                                isFocused = it.isFocused
+//                                if (isFocused) {
+//                                    Toast.makeText(
+//                                        context, screen.route, Toast.LENGTH_SHORT
+//                                    ).show()
+//                                }
+//                            }
+////                            BottomBarScreen.Settings.route -> {
+////                                if (isFocused) {
+////                                    Toast.makeText(
+////                                        context, screen.route, Toast.LENGTH_SHORT
+////                                    ).show()
+////                                }
+////                            }
+////                            BottomBarScreen.Help.route -> {
+////                                if (isFocused) {
+////                                    Toast.makeText(
+////                                        context, screen.route, Toast.LENGTH_SHORT
+////                                    ).show()
+////                                }
+////                            }
+////                            BottomBarScreen.Account.route -> {
+////                                if (isFocused) {
+////                                    Toast.makeText(
+////                                        context, screen.route, Toast.LENGTH_SHORT
+////                                    ).show()
+////                                }
+////                            }
+//                        }
+//                }
+//                else {
+//                    isFocused = it.isFocused
+//                }
 //                isClicked = it.isFocused
-                Log.d("test_bottom_navbar", "onFocusChanged {}")
             }
+            .focusable()
             .background(if (isFocused) Color.LightGray else Color.White)
         ,
-//        label = {
-//            if (currentDestination?.route == screen.route) {
-//                Text(
-//                    text = screen.title,
-//                    style = MaterialTheme.typography.customTypography2.bodyLarge.copy(
-//                        fontSize = if (isTablet()) 15.sp else 11.sp
-//                    ),
-////                    color = MaterialTheme.colorScheme.tertiary,
-//                    color = colorResource(id = R.color.blue_text),
-//                    )
-//            }
-//            else {
-//                Text(text = screen.title,
-//                    style = MaterialTheme.typography.customTypography2.bodyLarge.copy(
-//                        fontSize = if (isTablet()) 15.sp else 11.sp
-//                    ),
-////                    color = MaterialTheme.colorScheme.onTertiary,
-//                    color = colorResource(id = R.color.dark_blue_gray_text),
-//                    modifier = Modifier.alpha(0.5F)
-//                )
-//            }
-//        },
-//        alwaysShowLabel = true,
         selected = currentDestination?.hierarchy?.any{
                     it.route == screen.route
         } == true,
@@ -203,9 +260,7 @@ fun RowScope.AddItemTV(
             isClicked = true
             Log.d("test_bottom_navbar", "onCLick = ${screen.route
             }")
-
             /* First time currentDestination is different when on BottomBar Item */
-
         },
         icon = {
             val iconWidth = if (StaticMethods.isTV(context)) 55.dp else if (isTablet()) 50.dp else 45.dp
@@ -219,7 +274,6 @@ fun RowScope.AddItemTV(
                     modifier = Modifier
                         .size(iconWidth, iconHeight)
                         .padding(top = 9.dp, bottom = if (isTablet()) 6.dp else 4.dp)
-
                 )
             }
             else {

+ 23 - 9
app/src/main/java/com/vpn/fastestvpnservice/screensTV/HomeScreenTV.kt

@@ -54,8 +54,10 @@ import androidx.compose.ui.graphics.Color
 import androidx.compose.ui.graphics.ColorFilter
 import androidx.compose.ui.graphics.painter.Painter
 import androidx.compose.ui.input.key.Key
+import androidx.compose.ui.input.key.KeyEventType
 import androidx.compose.ui.input.key.key
 import androidx.compose.ui.input.key.onKeyEvent
+import androidx.compose.ui.input.key.type
 import androidx.compose.ui.input.pointer.pointerInput
 import androidx.compose.ui.layout.ContentScale
 import androidx.compose.ui.platform.LocalContext
@@ -533,21 +535,33 @@ fun HomeTV(navHostController: NavHostController) {
                         .padding(top = 0.dp)
                         .height(60.dp)
                         .onKeyEvent {
-                            when (it.key) {
-                                Key.DirectionDown -> {
-                                    Log.d("test_settings_keys", "DirectionDown")
+//                            when (it.key) {
+//                                Key.DirectionDown -> {
+//                                    Log.d("test_settings_keys", "DirectionDown")
+//                                    Toast
+//                                        .makeText(
+//                                            context, "DirectionDown", Toast.LENGTH_SHORT
+//                                        )
+//                                        .show()
+//                                    customNavigation(navHostController, BottomBarScreen.Settings)
+////                                    focusRequesterNav1.requestFocus()
+//                                    true
+//                                }
+//                                Key.DirectionRight -> { true}
+//                                Key.DirectionLeft -> { true}
+//                                else -> { false }
+//                            }
+                                if (it.type == KeyEventType.KeyDown && it.key == Key.DirectionDown) {
                                     Toast
                                         .makeText(
-                                            context, "DirectionDown", Toast.LENGTH_SHORT
+                                            context, "KeyDown & DirectionDown", Toast.LENGTH_SHORT
                                         )
                                         .show()
+//                                    customNavigation(navHostController, BottomBarScreen.Home)
 //                                    focusRequesterNav1.requestFocus()
+                                    focusRequestersList[selectedItemIndex.value].requestFocus()
                                     true
-                                }
-                                else -> {
-                                    false
-                                }
-                            }
+                                } else { true }
                         }
                         .onFocusChanged {
                             isLayoutFocused = it.isFocused

+ 10 - 10
app/src/main/java/com/vpn/fastestvpnservice/screensTV/SettingsScreenTV.kt

@@ -475,16 +475,16 @@ fun ColumnScope.AddRowSettingsColumnTV(
                         focusRequester2.requestFocus()
                         true
                     }
-//                    Key.DirectionLeft -> {
-//                        Log.d("test_settings_keys", "DirectionLeft")
-//                        Toast
-//                            .makeText(
-//                                context, "DirectionUp", Toast.LENGTH_SHORT
-//                            )
-//                            .show()
-//                        customNavigation(navHostController = navHostController, screen = BottomBarScreen.Home)
-//                        true
-//                    }
+                    Key.DirectionLeft -> {
+                        Log.d("test_settings_keys", "DirectionLeft")
+                        Toast
+                            .makeText(
+                                context, "DirectionLeft", Toast.LENGTH_SHORT
+                            )
+                            .show()
+                        customNavigation(navHostController = navHostController, screen = BottomBarScreen.Settings)
+                        true
+                    }
 //
 //                    Key.DirectionRight -> {
 //                        Log.d("test_settings_keys", "DirectionRight")

+ 1 - 1
app/src/main/java/com/vpn/fastestvpnservice/sealedClass/BottomBarScreen.kt

@@ -12,7 +12,7 @@ import androidx.compose.ui.res.painterResource
 import com.vpn.fastestvpnservice.R
 
 sealed class BottomBarScreen(
-    val route: String,
+    var route: String,
     val title: String,
     val icon: Int,
     var isTrue: Boolean = false