-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoginScreen.kt
More file actions
188 lines (164 loc) · 6.48 KB
/
Copy pathLoginScreen.kt
File metadata and controls
188 lines (164 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.example.mycomposecookbook.screen.auth
import android.view.animation.OvershootInterpolator
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.tween
import androidx.compose.animation.core.updateTransition
import androidx.compose.animation.slideInHorizontally
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import coil.compose.AsyncImage
import com.example.mycomposecookbook.R
import com.example.mycomposecookbook.util.component.MyButton
import com.example.mycomposecookbook.util.component.MyEditText
import com.example.mycomposecookbook.util.component.MyText
import com.google.accompanist.systemuicontroller.rememberSystemUiController
@Composable
@Preview
fun LoginScreen(
viewModel: AuthViewModel = androidx.lifecycle.viewmodel.compose.viewModel(),
navController: NavController = NavController(LocalContext.current)
) {
rememberSystemUiController().setStatusBarColor(Color.White)
val email = remember { mutableStateOf("eve.holt@reqres.in") }
val password = remember { mutableStateOf("cityslicka") }
val emailError = viewModel.emailError.collectAsState()
val passwordError = viewModel.passwordError.collectAsState()
val navigateHome = viewModel.navigateHome.collectAsState(false)
var logoVisible by remember { mutableStateOf(false) }
val density = LocalDensity.current
val logoScale = remember {
androidx.compose.animation.core.Animatable(0.5f)
}
LaunchedEffect(navigateHome.value) {
if (navigateHome.value) {
navController.navigate("home")
}
}
/*val scaffoldState = rememberScaffoldState() // this contains the `SnackbarHostState`
val (showSnackBar, setShowSnackBar) = remember {
mutableStateOf(false)
}*/
Surface(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight()
) {
Column(verticalArrangement = Arrangement.Center) {
AnimatedVisibility(
visible = logoVisible,
enter = slideInHorizontally(animationSpec = tween(durationMillis = 3000)) {
with(density) { -100.dp.roundToPx() }
}) {
Box(
modifier = Modifier
.weight(1.0f)
.fillMaxWidth()
)
{
Image(
painter = painterResource(id = R.drawable.jetpack),
contentDescription = "Image",
modifier = Modifier
.size(150.dp)
.scale(logoScale.value)
.align(Alignment.Center),
)
}
}
MyEditText(
hint = "Email",
value = email,
margin = 10.dp,
keyboardType = KeyboardType.Email,
errorMessage = emailError.value
) {
email.value = it
}
MyEditText(
hint = "Password",
value = password,
isPasswordField = true,
margin = 10.dp,
errorMessage = passwordError.value
) {
password.value = it
}
MyButton(value = "Login", margin = 10.dp) {
viewModel.login(email.value, password.value)
}
MyText(
value = "Forgot Password?", modifier = Modifier
.align(Alignment.End)
.padding(end = 10.dp)
) {
navController.navigate("forgot")
}
Text(
text = "Peoples are using...",
fontSize = 20.sp,
modifier = Modifier
.align(Alignment.CenterHorizontally)
.padding(vertical = 10.dp)
)
LazyHorizontalGrid(
contentPadding = PaddingValues(0.dp),
rows = GridCells.Fixed(3),
modifier = Modifier
.height(200.dp)
.padding(top = 20.dp),
horizontalArrangement = Arrangement.spacedBy(5.dp),
verticalArrangement = Arrangement.spacedBy(5.dp),
content = {
items(50, key = {
it
}) { item ->
AsyncImage(
model = "https://randomuser.me/api/portraits/men/$item.jpg",
contentDescription = "image",
contentScale = ContentScale.Crop,
modifier = Modifier
.border(1.dp, Color.Red, CircleShape)
.padding(2.dp)
.clip(CircleShape)
)
}
})
Spacer(modifier = Modifier.weight(1.0f))
MyButton(value = "Create an account", margin = 10.dp) {
navController.navigate("register?email=${email.value}")
}
//to animate logo
LaunchedEffect(key1 = Unit, block = {
logoVisible = true
logoScale.animateTo(
targetValue = 1.0f,
animationSpec = tween(
durationMillis = 300,
easing = { OvershootInterpolator(2f).getInterpolation(it) })
)
})
}
}
}