@ -3,18 +3,35 @@
package com.tailscale.ipn.ui.view
package com.tailscale.ipn.ui.view
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import com.tailscale.ipn.R
import com.tailscale.ipn.R
@ -25,7 +42,7 @@ import com.tailscale.ipn.ui.viewModel.UserSwitcherViewModel
@OptIn ( ExperimentalMaterial3Api :: class )
@OptIn ( ExperimentalMaterial3Api :: class )
@Composable
@Composable
fun UserSwitcherView (
fun UserSwitcherView (
nav : BackNavigation ,
nav : BackNavigation ,
onNavigateHome : ( ) -> Unit ,
onNavigateHome : ( ) -> Unit ,
viewModel : UserSwitcherViewModel = viewModel ( )
viewModel : UserSwitcherViewModel = viewModel ( )
@ -33,60 +50,125 @@ fun UserSwitcherView(
val users = viewModel . loginProfiles . collectAsState ( ) . value
val users = viewModel . loginProfiles . collectAsState ( ) . value
val currentUser = viewModel . loggedInUser . collectAsState ( ) . value
val currentUser = viewModel . loggedInUser . collectAsState ( ) . value
val showHeaderMenu = viewModel . showHeaderMenu . collectAsState ( ) . value
Scaffold ( topBar = { Header ( R . string . accounts , onBack = nav . onBack ) } ) { innerPadding ->
Scaffold (
Column (
topBar = {
modifier = Modifier . padding ( innerPadding ) . fillMaxWidth ( ) ,
Header (
verticalArrangement = Arrangement . spacedBy ( 8. dp ) ) {
R . string . accounts ,
val showDialog = viewModel . showDialog . collectAsState ( ) . value
onBack = nav . onBack ,
actions = {
Row {
FusMenu ( viewModel = viewModel )
IconButton ( onClick = { viewModel . showHeaderMenu . set ( ! showHeaderMenu ) } ) {
Icon ( Icons . Default . MoreVert , " menu " )
}
}
} )
} ) { innerPadding ->
Column (
modifier = Modifier . padding ( innerPadding ) . fillMaxWidth ( ) ,
verticalArrangement = Arrangement . spacedBy ( 8. dp ) ) {
val showErrorDialog = viewModel . errorDialog . collectAsState ( ) . value
// Show the error overlay if need be
// Show the error overlay if need be
showDialog ?. let { ErrorDialog ( type = it , action = { viewModel . showDialog . set ( null ) } ) }
showErrorDialog ?. let {
ErrorDialog ( type = it , action = { viewModel . errorDialog . set ( null ) } )
}
// When switch is invoked, this stores the ID of the user we're trying to switch to
// When switch is invoked, this stores the ID of the user we're trying to switch to
// so we can decorate it with a spinner. The actual logged in user will not change
// so we can decorate it with a spinner. The actual logged in user will not change
// until
// until
// we get our first netmap update back with the new userId for SelfNode.
// we get our first netmap update back with the new userId for SelfNode.
// (jonathan) TODO: This user switch is not immediate. We may need to represent the
// (jonathan) TODO: This user switch is not immediate. We may need to represent the
// "switching users" state globally (if ipnState is insufficient)
// "switching users" state globally (if ipnState is insufficient)
val nextUserId = remember { mutableStateOf < String ? > ( null ) }
val nextUserId = remember { mutableStateOf < String ? > ( null ) }
LazyColumn {
LazyColumn {
itemsWithDividers ( users ?: emptyList ( ) ) { user ->
itemsWithDividers ( users ?: emptyList ( ) ) { user ->
if ( user . ID == currentUser ?. ID ) {
if ( user . ID == currentUser ?. ID ) {
UserView ( profile = user , actionState = UserActionState . CURRENT )
UserView ( profile = user , actionState = UserActionState . CURRENT )
} else {
} else {
val state =
val state =
if ( user . ID == nextUserId . value ) UserActionState . SWITCHING
if ( user . ID == nextUserId . value ) UserActionState . SWITCHING
else UserActionState . NONE
else UserActionState . NONE
UserView (
UserView (
profile = user ,
profile = user ,
actionState = state ,
actionState = state ,
onClick = {
onClick = {
nextUserId . value = user . ID
nextUserId . value = user . ID
viewModel . switchProfile ( user ) {
viewModel . switchProfile ( user ) {
if ( it . isFailure ) {
if ( it . isFailure ) {
viewModel . showDialog . set ( ErrorDialogType . LOGOUT _FAILED )
viewModel . errorDialog . set ( ErrorDialogType . LOGOUT _FAILED )
nextUserId . value = null
nextUserId . value = null
} else {
} else {
onNavigateHome ( )
onNavigateHome ( )
}
}
}
}
} )
} )
}
}
item {
Lists . SectionDivider ( )
SettingRow ( viewModel . addProfileSetting )
Lists . ItemDivider ( )
SettingRow ( viewModel . loginSetting )
if ( currentUser != null ) {
Lists . ItemDivider ( )
SettingRow ( viewModel . logoutSetting )
}
}
}
}
}
}
}
}
@Composable
fun FusMenu ( viewModel : UserSwitcherViewModel ) {
var url by remember { mutableStateOf ( " " ) }
val expanded = viewModel . showHeaderMenu . collectAsState ( ) . value
DropdownMenu (
expanded = expanded ,
onDismissRequest = {
url = " "
viewModel . showHeaderMenu . set ( false )
} ,
modifier = Modifier . background ( MaterialTheme . colorScheme . surfaceContainer ) ) {
DropdownMenuItem (
onClick = { } ,
text = {
Column {
Text (
stringResource ( id = R . string . custom _control _menu ) ,
style = MaterialTheme . typography . titleMedium )
Spacer ( modifier = Modifier . padding ( 2. dp ) )
Text (
stringResource ( id = R . string . custom _control _menu _desc ) ,
style = MaterialTheme . typography . bodySmall )
Spacer ( modifier = Modifier . padding ( 8. dp ) )
OutlinedTextField (
colors =
TextFieldDefaults . colors (
focusedContainerColor = Color . Transparent ,
unfocusedContainerColor = Color . Transparent ) ,
textStyle = MaterialTheme . typography . bodyMedium ,
value = url ,
onValueChange = { url = it } ,
placeholder = {
Text (
stringResource ( id = R . string . custom _control _placeholder ) ,
style = MaterialTheme . typography . bodySmall )
} )
Spacer ( modifier = Modifier . padding ( 8. dp ) )
item {
PrimaryActionButton ( onClick = { viewModel . setControlURL ( url ) } ) {
Lists . SectionDivider ( )
Text ( stringResource ( id = R . string . add _account _short ) )
SettingRow ( viewModel . addProfileSetting )
}
Lists . ItemDivider ( )
SettingRow ( viewModel . loginSetting )
if ( currentUser != null ) {
Lists . ItemDivider ( )
SettingRow ( viewModel . logoutSetting )
}
}
}
} )
}
}
}
}
}
}