From 8702781c120a4b546d6d8df08ca483ede0bbd760 Mon Sep 17 00:00:00 2001 From: howdyev Date: Wed, 3 May 2023 13:37:38 -0400 Subject: [PATCH] LoginStyle2 Viewmodel --- .../features/login_style_2/LoginScreen2.kt | 34 ++++++++++++------- .../login_style_2/LoginStyle2ViewModel.kt | 27 +++++++++++++++ .../ui/components/MainContainerOfApp.kt | 34 ++++++++----------- 3 files changed, 62 insertions(+), 33 deletions(-) create mode 100644 app/src/main/java/com/example/jetpack_compose_all_in_one/features/login_style_2/LoginStyle2ViewModel.kt diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/features/login_style_2/LoginScreen2.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/features/login_style_2/LoginScreen2.kt index 3f43e1c9..dd41c088 100644 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/features/login_style_2/LoginScreen2.kt +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/features/login_style_2/LoginScreen2.kt @@ -1,5 +1,6 @@ package com.example.jetpack_compose_all_in_one.features.login_style_2 +import android.widget.Toast import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.Image import androidx.compose.foundation.background @@ -31,6 +32,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.input.VisualTransformation @@ -42,16 +44,13 @@ import com.example.jetpack_compose_all_in_one.R import com.example.jetpack_compose_all_in_one.ui.theme.Blue10 import com.example.jetpack_compose_all_in_one.ui.theme.Pink10 import com.example.jetpack_compose_all_in_one.ui.theme.dp_50 +import com.example.jetpack_compose_all_in_one.utils.showToast -@Preview(showSystemUi = true, showBackground = true) @Composable -fun LoginScreen2() { - var email: String by remember { - mutableStateOf("") - } - var password: String by remember { - mutableStateOf("") - } +fun LoginScreen2( + viewModel: LoginStyle2ViewModel +) { + var context = LocalContext.current var showPassword: Boolean by remember { mutableStateOf(false) } @@ -84,8 +83,8 @@ fun LoginScreen2() { TextField( modifier = Modifier .fillMaxWidth(), - value = email, - onValueChange = { email = it }, + value = viewModel.email.value, + onValueChange = { viewModel.email.value = it }, shape = RoundedCornerShape(24.dp), label = { Text(text = "Email", color = Color.White) }, placeholder = { Text(text = "", color = Color.White) }, @@ -94,8 +93,8 @@ fun LoginScreen2() { TextField( modifier = Modifier .fillMaxWidth(), - value = password, - onValueChange = { password = it }, + value = viewModel.password.value, + onValueChange = { viewModel.password.value = it }, shape = RoundedCornerShape(24.dp), label = { Text(text = "Password") }, placeholder = { Text(text = "") }, @@ -119,7 +118,16 @@ fun LoginScreen2() { OutlinedButton( border = BorderStroke(1.dp, Color.White), - onClick = { }) { + onClick = { + if (viewModel.email.value != "" && viewModel.password.value != "") { + viewModel.login { + showToast(context, "${viewModel.email.value} logged in!") + } + } else { + showToast(context, "Please check your email and password again!") + } + + }) { Text(text = "Login") } } diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/features/login_style_2/LoginStyle2ViewModel.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/features/login_style_2/LoginStyle2ViewModel.kt new file mode 100644 index 00000000..2c5a9ba6 --- /dev/null +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/features/login_style_2/LoginStyle2ViewModel.kt @@ -0,0 +1,27 @@ +package com.example.jetpack_compose_all_in_one.features.login_style_2 + +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.ui.platform.LocalContext +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.example.jetpack_compose_all_in_one.utils.showToast +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext + +class LoginStyle2ViewModel: ViewModel() { + var email = mutableStateOf("") + var password = mutableStateOf("") + var result = mutableStateOf("") + + fun login(onSucess:() -> Unit) { + viewModelScope.launch (Dispatchers.IO) { + delay(1000) + withContext(Dispatchers.Main) { + onSucess() + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/ui/components/MainContainerOfApp.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/ui/components/MainContainerOfApp.kt index 255634ce..6f95d940 100644 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/ui/components/MainContainerOfApp.kt +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/ui/components/MainContainerOfApp.kt @@ -22,21 +22,17 @@ import com.example.jetpack_compose_all_in_one.R import com.example.jetpack_compose_all_in_one.features.alarm.AlarmMainUI import com.example.jetpack_compose_all_in_one.features.chatmodule.ChatViewModel import com.example.jetpack_compose_all_in_one.features.download_manager.Download -import com.example.jetpack_compose_all_in_one.features.internet.InternetViewModel import com.example.jetpack_compose_all_in_one.features.login_style_1.LoginPage import com.example.jetpack_compose_all_in_one.features.login_style_2.LoginScreen2 -import com.example.jetpack_compose_all_in_one.features.login_style_1.LoginStyle1ViewModel +import com.example.jetpack_compose_all_in_one.features.login_style_2.LoginStyle2ViewModel +import com.example.jetpack_compose_all_in_one.features.news_sample.NewsSample import com.example.jetpack_compose_all_in_one.features.provideimages.ShowImages -import com.example.jetpack_compose_all_in_one.features.swipe_cards.QuoteStack -import com.example.jetpack_compose_all_in_one.features.weather_sample.view.WeatherSample -import com.example.jetpack_compose_all_in_one.lessons.lesson_2.Lesson_2_Chapter_2_Screen +import com.example.jetpack_compose_all_in_one.features.weather_sample.WeatherSample import com.example.jetpack_compose_all_in_one.lessons.lesson_2.Lesson_2_Chapter_Shape import com.example.jetpack_compose_all_in_one.lessons.lesson_2.Lesson_2_Screen import com.example.jetpack_compose_all_in_one.ui.views.chat.DemoFullChat2 -import com.example.jetpack_compose_all_in_one.ui.views.internet.InternetDemo import com.example.jetpack_compose_all_in_one.ui.views.lessons.ComposeLayouts import com.example.jetpack_compose_all_in_one.ui.views.quote_swipe.QuoteSwipe -import com.example.jetpack_compose_all_in_one.ui.views.news_ui.LatestNewsPage import com.example.jetpack_compose_all_in_one.ui.views.tmdbapi.PopularMoviesPage import com.example.jetpack_compose_all_in_one.utils.navigation.NavDes import com.example.jetpack_compose_all_in_one.view.Quote @@ -46,7 +42,7 @@ import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class) @Composable fun MainContainerOfApp( - internetViewModel: InternetViewModel, + isOffline: Boolean = false, playMusicFuncForeground: (Uri) -> Unit, stopMusicFuncForeground: () -> Unit, playMusicFuncBound: (Uri) -> Unit, @@ -81,14 +77,18 @@ fun MainContainerOfApp( ) } }, - snackbarHost = { SnackbarShow(snackbarHostState, internetViewModel.networkState) } + snackbarHost = { SnackbarShow(snackbarHostState, isOffline) } ) { NavHost(navController, currentRoute.value.route(), Modifier.padding(it)) { composable(NavDes.Home.route()) { Box {} } composable(NavDes.Internet.route()) { - InternetDemo() + if (isOffline) { + NetworkErrorDialog() + } else { + Text("Internet available") + } } composable(NavDes.ForegroundService.route()) { Box( @@ -167,18 +167,16 @@ fun MainContainerOfApp( } composable(NavDes.Login1.route()) { - val vm = hiltViewModel() - LoginPage( drawerState, - loginStateHolder = vm.loginDetail, - onLogin = { vm.login() }, + onLogin = { _, _, _ -> }, onRegister = { _, _ -> } ) } composable(NavDes.Login2.route()) { - LoginScreen2() + val vm = LoginStyle2ViewModel() + LoginScreen2(vm) } composable(NavDes.Tmdb.route()) { @@ -214,12 +212,8 @@ fun MainContainerOfApp( Lesson_2_Screen() } - composable(NavDes.L2Chapter3.route()) { - Lesson_2_Chapter_2_Screen() - } - composable(NavDes.NewsSample.route()) { - LatestNewsPage() + NewsSample() } composable(NavDes.WeatherSample.route()) { WeatherSample()