Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LoginStyle2 Viewmodel #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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) },
Expand All @@ -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 = "") },
Expand All @@ -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")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -167,18 +167,16 @@ fun MainContainerOfApp(
}

composable(NavDes.Login1.route()) {
val vm = hiltViewModel<LoginStyle1ViewModel>()

LoginPage(
drawerState,
loginStateHolder = vm.loginDetail,
onLogin = { vm.login() },
onLogin = { _, _, _ -> },
onRegister = { _, _ -> }
)
}

composable(NavDes.Login2.route()) {
LoginScreen2()
val vm = LoginStyle2ViewModel()
LoginScreen2(vm)
}

composable(NavDes.Tmdb.route()) {
Expand Down Expand Up @@ -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()
Expand Down