v79
08/26/2023, 6:59 AMv79
08/26/2023, 7:02 AMclass CorbelViewModel {
var mode = mutableStateOf(Mode.UNAUTHENTICATED)
private val _user = mutableStateOf(SubmitUser(username = null, password = null))
val user: State<SubmitUser>
get() = _user
// these would actually call a Service to authenticate (via AWS Cognito)
fun login(newUser: SubmitUser) {
println("Updating user ${user.value} to $newUser")
_user.value = newUser
}
fun logout() {
_user.value = SubmitUser(null,null)
}
}
And my composable LoginDialog function is called like this:
val viewModel = CorbelViewModel()
var mode by remember { viewModel.mode }
val user = remember { viewModel.user }
LoginDialog(
onDismiss = { mode = Mode.UNAUTHENTICATED },
onSubmit = { viewModel.login(it); mode = Mode.BUSY; })
So which is correct: var mode = mutableStateOf(Mode.UNAUTHENTICATED)
or the double fields of private val _user
and val user
? One requires by remember
and the other requires = remember
.s3rius
08/26/2023, 8:18 PMremember
parts are wrong: in generally you want to remember the ViewModel because otherwise it would get recreated every time your UI component is recomposed. On the other hand the individual states don't need to be remembered because they come from the already-remembered viewmodel.
val vm = remember { CorbelViewModel() }
var mode by vm.mode // no need for remember
val user by vm.user // both can use 'by'
val abc by remember { mutableStateOf("") } // this needs remember because this state doesn't originate from the viewModel.
Anyway, IMO the compose could should not touch mode
. Instead, set it inside the login and logout functions.
Besides, the two enum values UNAUTHENTICATED and BUSY sound like you're mixing two different kinds of information here. One is whether the user is authenticated or not, the other is whether there is a login currently in progress. If that is accurate then I suggest to split it in two.v79
08/26/2023, 10:04 PMs3rius
08/27/2023, 9:28 AM../success?token=abc&scopes=abc...
which you need to parse and read the important information from. And then you likely need a mechanism to refresh the authentication token from time to time. It's a bit of work, but all possible 🙂v79
08/27/2023, 5:41 PMv79
08/27/2023, 8:03 PM