浏览代码

set focus on settings screen

Khubaib 9 月之前
父节点
当前提交
79f6ab4f44

+ 29 - 26
app/src/main/java/com/vpn/fastestvpnservice/screensTV/BottomBarMainScreenTV.kt

@@ -109,7 +109,7 @@ fun BottomBarTV(navHostController: NavHostController, screens: List<BottomBarScr
             elevation = 0.dp,
 //        contentColor = Color.Gray,
             modifier = Modifier
-                .height(if (StaticMethods.isTV(context)) 70.dp else if (isTablet()) 150.dp else 110.dp)
+                .height(if (StaticMethods.isTV(context)) 60.dp else if (isTablet()) 150.dp else 110.dp)
                 .fillMaxWidth(fraction = 1f)
 //                .background(Color.LightGray)
 
@@ -133,6 +133,7 @@ fun RowScope.AddItemTV(
 ) {
     var isClicked by remember { mutableStateOf(false) }
     val route = currentRouteTV(navController = navHostController)
+    val context = LocalContext.current
     Log.d("currentRoute -> ", route.toString())
 
     if (isClicked) {
@@ -159,43 +160,45 @@ fun RowScope.AddItemTV(
         modifier = Modifier
             .onFocusChanged {
                 if (it.isFocused) isClicked = true
+                Log.d("test_bottom_navbar", "onFocusChanged {}")
             }
         ,
-        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,
+//        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,
         onClick = {
             isClicked = true
+            Log.d("test_bottom_navbar", "onCLick = {}")
 
             /* First time currentDestination is different when on BottomBar Item */
 
         },
         icon = {
-            val iconWidth = if (isTablet()) 50.dp else 45.dp
-            val iconHeight = if (isTablet()) 44.dp else 39.dp
+            val iconWidth = if (StaticMethods.isTV(context)) 55.dp else if (isTablet()) 50.dp else 45.dp
+            val iconHeight = if (StaticMethods.isTV(context)) 55.dp else if (isTablet()) 44.dp else 39.dp
             if (currentDestination?.route == screen.route) {
                 Icon(
                     painter = painterResource(id = screen.icon),

+ 0 - 5
app/src/main/java/com/vpn/fastestvpnservice/screensTV/LoginScreenTV.kt

@@ -1,11 +1,8 @@
 package com.vpn.fastestvpnservice.screensTV
 
-import android.util.Log
-import androidx.compose.foundation.BorderStroke
 import androidx.compose.foundation.Image
 import androidx.compose.foundation.background
 import androidx.compose.foundation.border
-import androidx.compose.foundation.focusable
 import androidx.compose.foundation.layout.Arrangement
 import androidx.compose.foundation.layout.Box
 import androidx.compose.foundation.layout.Column
@@ -50,14 +47,12 @@ import androidx.compose.ui.text.input.ImeAction
 import androidx.compose.ui.text.input.KeyboardType
 import androidx.compose.ui.unit.dp
 import androidx.navigation.NavHostController
-import androidx.tv.material3.Border
 import androidx.tv.material3.Button
 import androidx.tv.material3.ButtonDefaults
 import androidx.tv.material3.Surface
 import androidx.tv.material3.SurfaceDefaults
 import com.vpn.fastestvpnservice.R
 import com.vpn.fastestvpnservice.beans.isDarkTheme
-import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
 import com.vpn.fastestvpnservice.sealedClass.ScreenTV
 import com.vpn.fastestvpnservice.ui.theme.customTypography
 

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

@@ -11,6 +11,7 @@ import androidx.compose.foundation.ExperimentalFoundationApi
 import androidx.compose.foundation.Image
 import androidx.compose.foundation.LocalOverscrollConfiguration
 import androidx.compose.foundation.background
+import androidx.compose.foundation.clickable
 import androidx.compose.foundation.gestures.detectTapGestures
 import androidx.compose.foundation.isSystemInDarkTheme
 import androidx.compose.foundation.layout.Arrangement
@@ -30,8 +31,6 @@ import androidx.compose.foundation.rememberScrollState
 import androidx.compose.foundation.selection.selectable
 import androidx.compose.foundation.shape.RoundedCornerShape
 import androidx.compose.foundation.verticalScroll
-import androidx.compose.material.icons.Icons
-import androidx.compose.material.icons.filled.DarkMode
 import androidx.compose.material3.DockedSearchBar
 import androidx.compose.material3.ExperimentalMaterial3Api
 import androidx.compose.material3.Icon
@@ -60,12 +59,14 @@ import androidx.compose.ui.Modifier
 import androidx.compose.ui.draw.alpha
 import androidx.compose.ui.draw.clip
 import androidx.compose.ui.draw.scale
+import androidx.compose.ui.focus.FocusRequester
+import androidx.compose.ui.focus.focusRequester
+import androidx.compose.ui.focus.onFocusChanged
 import androidx.compose.ui.graphics.Color
 import androidx.compose.ui.graphics.ColorFilter
 import androidx.compose.ui.graphics.toArgb
 import androidx.compose.ui.graphics.vector.ImageVector
 import androidx.compose.ui.input.pointer.pointerInput
-import androidx.compose.ui.platform.LocalConfiguration
 import androidx.compose.ui.platform.LocalContext
 import androidx.compose.ui.platform.LocalSoftwareKeyboardController
 import androidx.compose.ui.platform.LocalView
@@ -87,12 +88,11 @@ import com.vpn.fastestvpnservice.customItems.ServerSpecificItem
 import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
 import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.AddTextSettings
 import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.getAvailableProtocols
-import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.selectProtocolCallback
 import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.vpnConnectionsUtil
 import com.vpn.fastestvpnservice.screens.searchListViewModelSplash
-import com.vpn.fastestvpnservice.screens.serverListViewModelSplash
 import com.vpn.fastestvpnservice.sealedClass.BottomBarScreen
 import com.vpn.fastestvpnservice.sealedClass.Screen
+import com.vpn.fastestvpnservice.utils.isTablet
 import com.vpn.fastestvpnservice.viewmodels.HomeViewModel
 import de.blinkt.openvpn.core.App
 import kotlinx.coroutines.delay
@@ -102,6 +102,12 @@ import kotlinx.coroutines.launch
 @Composable
 fun SettingsTV(navHostController: NavHostController) {
     val context = LocalContext.current
+    val focusRequester1 = remember { FocusRequester() }
+    val focusRequester2 = remember { FocusRequester() }
+    val focusRequester3 = remember { FocusRequester() }
+    val focusRequester4 = remember { FocusRequester() }
+    val focusRequester5 = remember { FocusRequester() }
+
 
     CompositionLocalProvider(
         LocalOverscrollConfiguration provides null
@@ -118,6 +124,10 @@ fun SettingsTV(navHostController: NavHostController) {
             window.statusBarColor = Color.Transparent.toArgb()
             window.navigationBarColor = Color.Transparent.toArgb()
 
+            LaunchedEffect(Unit) {
+                focusRequester1.requestFocus()
+            }
+
             Column(
                 verticalArrangement = Arrangement.Top,
                 horizontalAlignment = Alignment.Start,
@@ -148,32 +158,23 @@ fun SettingsTV(navHostController: NavHostController) {
 
                 AddRowSettingsColumnTV(
                     icon = R.drawable.vpn_protocols3x,
-                    text = "VPN Protocols"
+                    text = "VPN Protocols",
+                    focusRequester1,
+                    focusRequester2
                 )
-                /* Commenting so far ...*/
-
-
-//                AddRowSwitch(
-//                    icon = R.drawable.autoconnect3x,
-//                    text = "Auto Connect"
-//                )
-
-//            AddRowSettingsTV(
-//                icon = R.drawable.autoconnect3x,
-//                text = "Auto Connect",
-//                onClick = {}
-//            )
-
                 AddRowSettingsSmartTV(
                     icon = R.drawable.smart_connect3x,
                     text = "Smart Connect",
                     isRowShown = true,
                     isSheetShown = false,
-                    navHostController
+                    navHostController,
+                    focusRequester2,
+                    focusRequester3
                 )
                 AddRowSwitchTV(
                     icon = R.drawable.adblock3x,
-                    text = "AdBlock"
+                    text = "AdBlock",
+                    focusRequester3
                 )
                 AddRowSettingsTV(
                     icon = R.drawable.kill_switch3x,
@@ -221,17 +222,24 @@ fun ColumnScope.AddRowSettingsTV(
     topPadding: Dp = 40.dp,
     onClick: () -> Unit
 ) {
+    var isRowFocused by remember { mutableStateOf(false) }
     Row(
         modifier = Modifier
             .fillMaxWidth()
             .padding(top = 5.dp, end = 27.dp)
             .clip(RoundedCornerShape(4.dp))
-            .background(Color.White)
+            .background(if (isRowFocused) Color.LightGray else Color.White)
+            .onFocusChanged {
+                isRowFocused = it.isFocused
+            }
             .height(61.dp)
             .pointerInput(Unit) {
                 detectTapGestures {
                     onClick()
                 }
+            }
+            .clickable {
+                onClick()
             },
         horizontalArrangement = Arrangement.Start,
         verticalAlignment = Alignment.CenterVertically
@@ -308,15 +316,38 @@ fun ColumnScope.AddTextSettingsTV(
 }
 
 @Composable
-fun ColumnScope.AddRowSwitchTV(icon: Int, text: String) {
+fun ColumnScope.AddRowSwitchTV(icon: Int, text: String, focusRequester3: FocusRequester) {
     val context = LocalContext.current
     val basePreferenceHelper = BasePreferenceHelper(context)
+    var isRowFocused by remember { mutableStateOf(false) }
+    var isSwitch by remember { mutableStateOf(basePreferenceHelper.getAdBlockState()) }
+    val scope = rememberCoroutineScope()
+    val homeViewModel: HomeViewModel = viewModel{
+        HomeViewModel(context, scope)
+    }
+    val isConnect: Int? = homeViewModel.isConnect.observeAsState().value
+
     Row(
         modifier = Modifier
             .fillMaxWidth()
             .padding(top = 5.dp, end = 27.dp)
             .clip(RoundedCornerShape(4.dp))
-            .background(Color.White)
+            .background(if (isRowFocused) Color.LightGray else Color.White)
+//            .focusRequester(focusRequester3)
+            .onFocusChanged {
+                isRowFocused = it.isFocused
+            }
+            .clickable {
+                isSwitch = !isSwitch
+                basePreferenceHelper.saveAdBlockState(isSwitch)
+                if (isConnect == App.CONNECTED || isConnect == App.CONNECTING) {
+                    Log.d("isConnect_State_vpn", "stopVPN")
+                    vpnConnectionsUtil.stopVpn()
+                    Handler().postDelayed(Runnable {
+                        vpnConnectionsUtil.startVpn()
+                    }, 500)
+                }
+            }
             .height(61.dp),
         horizontalArrangement = Arrangement.Start,
         verticalAlignment = Alignment.CenterVertically
@@ -351,11 +382,6 @@ fun ColumnScope.AddRowSwitchTV(icon: Int, text: String) {
             )
         }
         Spacer(modifier = Modifier.weight(1f))
-        val scope = rememberCoroutineScope()
-        val homeViewModel: HomeViewModel = viewModel{
-            HomeViewModel(context, scope)
-        }
-        val isConnect: Int? = homeViewModel.isConnect.observeAsState().value
 
 //        val vpnConnectionsUtil = VPNConnectionsUtil(context, act, homeViewModel)
 
@@ -365,7 +391,6 @@ fun ColumnScope.AddRowSwitchTV(icon: Int, text: String) {
                 .align(Alignment.CenterVertically),
             color = Color.Transparent
         ) {
-            var isSwitch by remember { mutableStateOf(basePreferenceHelper.getAdBlockState()) }
             Switch(
                 checked = isSwitch,
                 onCheckedChange = {
@@ -400,37 +425,49 @@ fun ColumnScope.AddRowSwitchTV(icon: Int, text: String) {
 @Composable
 fun ColumnScope.AddRowSettingsColumnTV(
     icon: Int,
-    text: String
+    text: String,
+    focusRequester1: FocusRequester,
+    focusRequester2: FocusRequester
 ) {
     val context = LocalContext.current
     val basePreferenceHelper = BasePreferenceHelper(context = context)
     val availableProtocols = getAvailableProtocols(basePreferenceHelper)
     var isSheetOpen by remember { mutableStateOf(false) }
-    val sheetState = rememberModalBottomSheetState()
+    val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
     val protocols = listOf(
         "Auto", "WireGuard", "IKEv2", "OpenVPN TCP", "OpenVPN UDP"
     )
     var selectedProtocol by remember { mutableStateOf(basePreferenceHelper.getProtocol().full_name) }
+    var isFirstRowFocused by remember { mutableStateOf(false) }
+
+
 //    val serverListViewModel = serverListViewModelSplash
 //    val serverListViewModel: ServerListViewModel = viewModel{
 //        ServerListViewModel(context)
 //    }
-    val configuration = LocalConfiguration.current
-    val isTablet = configuration.screenWidthDp > 840
+//    val configuration = LocalConfiguration.current
+//    val isTablet = configuration.screenWidthDp > 840
+
 
     Row(
         modifier = Modifier
             .fillMaxWidth()
             .padding(top = 5.dp, end = 27.dp)
             .clip(RoundedCornerShape(4.dp))
-            .background(Color.White)
+            .background(if (isFirstRowFocused) Color.LightGray else Color.White)
             .height(61.dp)
-//            .clickable { isSheetOpen = true }
+            .focusRequester(focusRequester1)
+            .onFocusChanged {
+                isFirstRowFocused = it.isFocused
+//                if (!it.isFocused) focusRequester2.requestFocus()
+
+            }
             .pointerInput(Unit) {
                 detectTapGestures {
                     isSheetOpen = true
                 }
             }
+            .clickable { isSheetOpen = true }
         ,
         horizontalArrangement = Arrangement.Start,
         verticalAlignment = Alignment.CenterVertically
@@ -453,7 +490,8 @@ fun ColumnScope.AddRowSettingsColumnTV(
         }
 
         Surface(
-            modifier = Modifier.padding(start = 0.dp)
+            modifier = Modifier
+                .padding(start = 0.dp)
                 .align(Alignment.CenterVertically),
             color = Color.Transparent
         ) {
@@ -472,7 +510,7 @@ fun ColumnScope.AddRowSettingsColumnTV(
                 Spacer(modifier = Modifier.height(2.dp))
                 Text(text = selectedProtocol,
                     style = MaterialTheme.typography.headlineSmall.copy(
-                        fontSize = if (isTablet) 14.sp else 12.sp
+                        fontSize = if (isTablet()) 14.sp else 12.sp
                     ),
                     color = MaterialTheme.colorScheme.inversePrimary,
                     maxLines = 1,
@@ -511,7 +549,7 @@ fun ColumnScope.AddRowSettingsColumnTV(
                 onDismissRequest = { isSheetOpen = false },
                 sheetState = sheetState,
 //                containerColor = MaterialTheme.colorScheme.onBackground
-                containerColor = Color.White
+                containerColor = Color.White,
             ) {
                 Box(modifier = Modifier
                     .background(Color.Transparent)
@@ -589,13 +627,15 @@ fun ColumnScope.AddRowSettingsSmartTV(
     text: String,
     isRowShown: Boolean,
     isSheetShown: Boolean,
-    navHostController: NavHostController
+    navHostController: NavHostController,
+    focusRequester2: FocusRequester,
+    focusRequester3: FocusRequester
 ) {
     var isSmartSheetOpen by remember { mutableStateOf(false) }
     var isAnySpecificSheetOpen by remember { mutableStateOf(false) }
     val context = LocalContext.current
     val basePreferenceHelper = BasePreferenceHelper(context)
-    val keyboardController = LocalSoftwareKeyboardController.current
+    var isRowFocused by remember { mutableStateOf(false) }
 
 //    if (isRowShown) { }
 
@@ -604,13 +644,19 @@ fun ColumnScope.AddRowSettingsSmartTV(
             .fillMaxWidth()
             .padding(top = 5.dp, end = 27.dp)
             .clip(RoundedCornerShape(4.dp))
-            .background(Color.White)
+            .background(if (isRowFocused) Color.LightGray else Color.White)
+//            .focusRequester(focusRequester2)
+            .onFocusChanged {
+                isRowFocused = it.isFocused
+//                if (!it.isFocused) focusRequester3.requestFocus()
+            }
             .pointerInput(Unit) {
                 detectTapGestures {
                     isSmartSheetOpen = true
                 }
             }
-            .height(61.dp),
+            .height(61.dp)
+            .clickable { isSmartSheetOpen = true },
         horizontalArrangement = Arrangement.Start,
         verticalAlignment = Alignment.CenterVertically
     ) {
@@ -627,7 +673,8 @@ fun ColumnScope.AddRowSettingsSmartTV(
                     .size(24.dp)
                     .weight(1f),
                 colorFilter = ColorFilter.tint(
-                    MaterialTheme.colorScheme.inversePrimary)                )
+                    MaterialTheme.colorScheme.inversePrimary)
+            )
         }
 
         Surface(
@@ -665,7 +712,7 @@ fun ColumnScope.AddRowSettingsSmartTV(
     }
 
     if (isSmartSheetOpen) {
-        val sheetState = rememberModalBottomSheetState()
+        val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
         var selectedSmartConnect by remember {
             mutableStateOf(basePreferenceHelper.getSmartList()) }