|
@@ -2,6 +2,7 @@ package com.vpn.fastestvpnservice.screens.bottomNavBarScreens
|
|
|
|
|
|
import android.content.Context
|
|
|
import android.content.res.Configuration
|
|
|
+import android.os.Handler
|
|
|
import android.util.Log
|
|
|
import android.widget.Toast
|
|
|
import androidx.activity.ComponentActivity
|
|
@@ -98,6 +99,7 @@ import com.vpn.fastestvpnservice.application.App
|
|
|
import com.vpn.fastestvpnservice.beans.Protocol
|
|
|
import com.vpn.fastestvpnservice.beans.Server
|
|
|
import com.vpn.fastestvpnservice.beans.isDarkTheme
|
|
|
+import com.vpn.fastestvpnservice.constants.AppEnum
|
|
|
import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
|
|
|
import com.vpn.fastestvpnservice.interfaces.ServerCallbacks
|
|
|
import com.vpn.fastestvpnservice.sealedClass.BottomBarScreen
|
|
@@ -114,16 +116,23 @@ import kotlinx.coroutines.CoroutineScope
|
|
|
|
|
|
lateinit var act: ComponentActivity
|
|
|
var isServerDialog: MutableState<Boolean> = mutableStateOf(false)
|
|
|
+var isProtocolDialog: MutableState<Boolean> = mutableStateOf(false)
|
|
|
var serverObj: MutableState<Server> = mutableStateOf(Server())
|
|
|
+var protocolObj: MutableState<Protocol> = mutableStateOf(
|
|
|
+ Protocol(AppEnum.WG_PROTOCOL.title,AppEnum.WG_PROTOCOL.key, 1)
|
|
|
+)
|
|
|
+lateinit var navHostController1: NavHostController
|
|
|
+lateinit var homeViewModel1: HomeViewModel
|
|
|
|
|
|
- @OptIn(ExperimentalMaterial3Api::class)
|
|
|
+@OptIn(ExperimentalMaterial3Api::class)
|
|
|
val onServer = object : ServerCallbacks {
|
|
|
- @Composable
|
|
|
+
|
|
|
+ @Composable
|
|
|
override fun onServerSelected(
|
|
|
context: Context, homeViewModel: HomeViewModel,
|
|
|
onClick: () -> Unit, isServerDialogShown: Boolean, server: Server) {
|
|
|
- val basePreferenceHelper = BasePreferenceHelper(context)
|
|
|
val wg = WireGuardConnection(context, act ,homeViewModel)
|
|
|
+ val basePreferenceHelper = BasePreferenceHelper(context)
|
|
|
|
|
|
Log.d("ServerCallbacks", "onServerSelected called!")
|
|
|
Log.d("ServerCallbacks", "onServerSelected server = ${server.server_name}")
|
|
@@ -153,15 +162,42 @@ var serverObj: MutableState<Server> = mutableStateOf(Server())
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Composable
|
|
|
- override fun OnChangeProtocol(protocol: Protocol) {
|
|
|
- Log.d("OnChangeProtocol", "OnChangeProtocol called!")
|
|
|
+ override fun onChangeProtocol(protocol: Protocol, context: Context) {
|
|
|
+ val prefHelper = BasePreferenceHelper(context)
|
|
|
+ val wg = WireGuardConnection(context, act , homeViewModel1)
|
|
|
+ protocolObj.value = protocol
|
|
|
+
|
|
|
+ Log.d("OnChangeProtocol", "OnChangeProtocol ${protocol.full_name} ${protocol.title}!")
|
|
|
+ if (getEnableProtocols(protocol.title, prefHelper)) {
|
|
|
+ Log.d("OnChangeProtocol", "getEnableProtocols yes!")
|
|
|
+
|
|
|
+ if (prefHelper.getProtocol().index != protocol.index) {
|
|
|
+
|
|
|
+ if (wg.isVPNConnected()) {
|
|
|
+ navHostController1.popBackStack()
|
|
|
+ isProtocolDialog.value = true
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ prefHelper.saveProtocol(protocol = protocol)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ Log.d("OnChangeProtocol", "getEnableProtocols no! ${navHostController1.currentDestination}")
|
|
|
+ navHostController1.let {
|
|
|
+ it.navigate(Screen.Subscription.route)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3Api::class)
|
|
|
@Composable
|
|
|
fun Home(navHostController: NavHostController, activity: ComponentActivity) {
|
|
|
+ Log.d("OnChangeProtocol", "Home called!")
|
|
|
+ navHostController1 = navHostController
|
|
|
|
|
|
val context = LocalContext.current
|
|
|
val basePreferenceHelper = BasePreferenceHelper(context)
|
|
@@ -183,6 +219,7 @@ fun Home(navHostController: NavHostController, activity: ComponentActivity) {
|
|
|
var server: Server = Server()
|
|
|
val wireGuardConnection = WireGuardConnection(context, activity, homeViewModel)
|
|
|
val wg = WireGuardConnection(context, activity, homeViewModel)
|
|
|
+ homeViewModel1 = homeViewModel
|
|
|
|
|
|
var isConnect: Boolean = homeViewModel.isConnect.observeAsState().value == true
|
|
|
Log.d("isConnect_State", "live: $isConnect")
|
|
@@ -305,10 +342,16 @@ fun Home(navHostController: NavHostController, activity: ComponentActivity) {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
+ if (isProtocolDialog.value) {
|
|
|
+ ShowProtocolDialog(
|
|
|
+ basePreferenceHelper,
|
|
|
+ wg
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
IconButton(
|
|
|
onClick = {
|
|
|
Log.d("isConnect_State", "onClick{} -> $isConnect")
|
|
|
-
|
|
|
if (isConnect) {
|
|
|
wireGuardConnection.stopVpn()
|
|
|
homeViewModel.getIp()
|
|
@@ -703,9 +746,143 @@ fun BoxScope.ShowServerDialog(
|
|
|
Log.d("test_button", "Yes")
|
|
|
isServerDialog.value = false
|
|
|
prefHelper.setServerObject(serverObj.value)
|
|
|
- wg.startVpn()
|
|
|
+ wg.stopVpn()
|
|
|
+ Handler().postDelayed(Runnable {
|
|
|
+ wg.startVpn()
|
|
|
+ }, 500)
|
|
|
Log.d("ServerCallbacks", "Yes click => ${isServerDialog.value}")
|
|
|
+ },
|
|
|
+ modifier = Modifier
|
|
|
+ .padding(
|
|
|
+ start = 5.dp, end = 15.dp,
|
|
|
+ bottom = 0.dp, top = 0.dp
|
|
|
+ )
|
|
|
+ .background(colorResource(id = R.color.transparent))
|
|
|
+ .weight(1F)
|
|
|
+ .height(52.dp),
|
|
|
+ shape = RoundedCornerShape(15.dp),
|
|
|
+ colors = ButtonDefaults.buttonColors(
|
|
|
+ contentColor = colorResource(id = R.color.white),
|
|
|
+ containerColor = colorResource(id = R.color.red),
|
|
|
+ ),
|
|
|
+// border = BorderStroke(2.dp,
|
|
|
+// colorResource(id = R.color.gray_icon))
|
|
|
+ )
|
|
|
+ {
|
|
|
+ Text(text = "Yes",
|
|
|
+ style = MaterialTheme.typography.labelLarge)
|
|
|
+ Log.d("test_button", "RowScope")
|
|
|
|
|
|
+// val logoutResponse = accountViewModel.liveDataLogout.observeAsState().value
|
|
|
+// if (logoutResponse == true) {
|
|
|
+// accountViewModel.setLogoutStatus(false)
|
|
|
+// Log.d("test_api_response","Logout live: $logoutResponse")
|
|
|
+// basePreferenceHelper.setLoggedInState(false)
|
|
|
+// settingsNavHostController.popBackStack()
|
|
|
+// settingsNavHostController.navigate(Screen.Started.route)
|
|
|
+// }
|
|
|
+// else {
|
|
|
+//// accountViewModel.setLogoutStatus(false)
|
|
|
+//// onCancel()
|
|
|
+// }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+@OptIn(ExperimentalMaterial3Api::class)
|
|
|
+@Composable
|
|
|
+fun BoxScope.ShowProtocolDialog(
|
|
|
+ prefHelper: BasePreferenceHelper,
|
|
|
+ wg: WireGuardConnection
|
|
|
+) {
|
|
|
+ AlertDialog(
|
|
|
+ onDismissRequest = { isProtocolDialog.value = false },
|
|
|
+ properties = DialogProperties(),
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .wrapContentHeight()
|
|
|
+ ) {
|
|
|
+ val lastServer = prefHelper.getServerObject()
|
|
|
+ val selectedProtocol = prefHelper.getProtocol()
|
|
|
+
|
|
|
+ val oldProtocolTitle = if (selectedProtocol.index == 0) AppEnum.AUTO_PROTOCOL.key else selectedProtocol.title
|
|
|
+ val newProtocolTitle = if (protocolObj.value.index == 0) AppEnum.AUTO_PROTOCOL.key else protocolObj.value.title
|
|
|
+
|
|
|
+ Surface(
|
|
|
+ color = colorResource(id = R.color.white),
|
|
|
+ modifier = Modifier
|
|
|
+ .background(Color.Transparent)
|
|
|
+ .fillMaxWidth()
|
|
|
+ ,
|
|
|
+ shape = RoundedCornerShape(18.dp)
|
|
|
+ ) {
|
|
|
+ Column(
|
|
|
+ verticalArrangement = Arrangement.Top,
|
|
|
+ horizontalAlignment = Alignment.CenterHorizontally,
|
|
|
+ modifier = Modifier
|
|
|
+ .background(MaterialTheme.colorScheme.onPrimary)
|
|
|
+ ) {
|
|
|
+ Log.d("islogoutClicked", "AlertDialog")
|
|
|
+ Text(text = "Confirm",
|
|
|
+ color = MaterialTheme.colorScheme.primary,
|
|
|
+ style = MaterialTheme.typography.bodyMedium,
|
|
|
+ modifier = Modifier.padding(top = 45.dp)
|
|
|
+ )
|
|
|
+ Text(text = "Are you sure to switch from $oldProtocolTitle to $newProtocolTitle?",
|
|
|
+ color = MaterialTheme.colorScheme.primary,
|
|
|
+ style = MaterialTheme.typography.labelSmall,
|
|
|
+ maxLines = 2,
|
|
|
+ modifier = Modifier.padding(start = 16.dp, end = 16.dp, top = 26.dp)
|
|
|
+ )
|
|
|
+ Row (
|
|
|
+ modifier = Modifier
|
|
|
+ .padding(top = 34.dp, bottom = 20.dp),
|
|
|
+ horizontalArrangement = Arrangement.SpaceBetween,
|
|
|
+ verticalAlignment = Alignment.Bottom
|
|
|
+ ) {
|
|
|
+ Button(
|
|
|
+ onClick = {
|
|
|
+ Log.d("ServerCallbacks", "No")
|
|
|
+ isProtocolDialog.value = false
|
|
|
+ },
|
|
|
+ modifier = Modifier
|
|
|
+ .padding(
|
|
|
+ start = 15.dp, end = 5.dp,
|
|
|
+ bottom = 0.dp, top = 0.dp
|
|
|
+ )
|
|
|
+ .background(colorResource(id = R.color.transparent))
|
|
|
+ .weight(1F)
|
|
|
+ .height(52.dp),
|
|
|
+
|
|
|
+ shape = RoundedCornerShape(15.dp),
|
|
|
+ colors = ButtonDefaults.buttonColors(
|
|
|
+ contentColor = colorResource(id = R.color.white),
|
|
|
+ containerColor = colorResource(id = R.color.light_blue),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ {
|
|
|
+ Text(text = "No",
|
|
|
+ style = MaterialTheme.typography.labelLarge)
|
|
|
+ Log.d("test_button", "RowScope")
|
|
|
+ }
|
|
|
+
|
|
|
+ Button(
|
|
|
+ onClick = {
|
|
|
+ Log.d("ServerCallbacks", "Yes")
|
|
|
+ val server = prefHelper.getServerObject()
|
|
|
+ prefHelper.setServerObject(server)
|
|
|
+ prefHelper.saveProtocol(protocol = protocolObj.value)
|
|
|
+ wg.stopVpn()
|
|
|
+ Handler().postDelayed(Runnable {
|
|
|
+ wg.startVpn()
|
|
|
+ }, 500)
|
|
|
+ Log.d("ServerCallbacks", "Yes click => ${isProtocolDialog.value}")
|
|
|
+ isProtocolDialog.value = false
|
|
|
},
|
|
|
modifier = Modifier
|
|
|
.padding(
|
|
@@ -944,6 +1121,15 @@ fun RowScope.AddText(text: String, size: TextUnit, color: Color) {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
+private fun getEnableProtocols(protocol_name: String, prefHelper: BasePreferenceHelper): Boolean {
|
|
|
+ prefHelper.getEnabledProtocols().let {
|
|
|
+ if (it.contains(protocol_name)) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
@Composable
|
|
|
fun pinkBackground(): Painter{
|
|
|
return painterResource(id = R.drawable.pinkbackground3x)
|