Skip to content

Commit

Permalink
viewmodel integrated to Jetpack Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex009 committed Apr 30, 2022
1 parent d628fb6 commit a93b9a3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
2 changes: 2 additions & 0 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ dependencies {
// Activity
implementation("androidx.activity:activity-compose:1.4.0")
implementation("androidx.appcompat:appcompat:1.4.1")
// viewmodel
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1")
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,35 @@ import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import androidx.lifecycle.viewmodel.compose.viewModel
import dev.icerock.moko.mvvm.flow.compose.observeAsActions
import ru.alex009.moko.mvvm.declarativeui.LoginViewModel

@Composable
fun LoginScreen() {
fun LoginScreen(
viewModel: LoginViewModel = viewModel()
) {
val context: Context = LocalContext.current
val coroutineScope: CoroutineScope = rememberCoroutineScope()

var login: String by remember { mutableStateOf("") }
var password: String by remember { mutableStateOf("") }
var isLoading: Boolean by remember { mutableStateOf(false) }
val login: String by viewModel.login.collectAsState()
val password: String by viewModel.password.collectAsState()
val isLoading: Boolean by viewModel.isLoading.collectAsState()
val isLoginButtonEnabled: Boolean by viewModel.isButtonEnabled.collectAsState()

val isLoginButtonEnabled: Boolean = login.isNotBlank() && password.isNotBlank() && !isLoading
viewModel.actions.observeAsActions { action ->
when (action) {
LoginViewModel.Action.LoginSuccess ->
Toast.makeText(context, "login success!", Toast.LENGTH_SHORT).show()
}
}

Column(
modifier = Modifier.padding(16.dp),
Expand All @@ -48,7 +52,7 @@ fun LoginScreen() {
value = login,
enabled = !isLoading,
label = { Text(text = "Login") },
onValueChange = { login = it }
onValueChange = { viewModel.login.value = it }
)
Spacer(modifier = Modifier.height(8.dp))
TextField(
Expand All @@ -57,22 +61,15 @@ fun LoginScreen() {
enabled = !isLoading,
label = { Text(text = "Password") },
visualTransformation = PasswordVisualTransformation(),
onValueChange = { password = it }
onValueChange = { viewModel.password.value = it }
)
Spacer(modifier = Modifier.height(8.dp))
Button(
modifier = Modifier
.fillMaxWidth()
.height(48.dp),
enabled = isLoginButtonEnabled,
onClick = {
coroutineScope.launch {
isLoading = true
delay(1000)
isLoading = false
Toast.makeText(context, "login success!", Toast.LENGTH_SHORT).show()
}
}
onClick = viewModel::onLoginPressed
) {
if (isLoading) CircularProgressIndicator(modifier = Modifier.size(24.dp))
else Text(text = "Login")
Expand Down

0 comments on commit a93b9a3

Please sign in to comment.