Какой то кринж, но рабочий кринж

This commit is contained in:
Кашин Максим 2023-12-24 00:05:07 +04:00
parent ef9a391caf
commit 212ef57bff
3 changed files with 206 additions and 138 deletions

View File

@ -52,6 +52,7 @@ import com.example.myapplication.database.entities.composeui.AppViewModelProvide
import com.example.myapplication.database.entities.composeui.BikeList
import com.example.myapplication.database.entities.composeui.BikeView
import com.example.myapplication.database.entities.composeui.CurrentUserViewModel
import com.example.myapplication.database.entities.composeui.LoginScreen
import com.example.myapplication.database.entities.composeui.RentList
import com.example.myapplication.database.entities.composeui.RentView
import com.example.myapplication.database.entities.composeui.UserProfile
@ -188,7 +189,8 @@ fun Navhost(
composable(Screen.BikeList.route) { BikeList(navController, currentUserViewModel = currentUserViewModel) }
composable(Screen.RentList.route) { RentList(navController, currentUserViewModel = currentUserViewModel) }
composable(Screen.Cart.route) { Cart(currentUserViewModel = currentUserViewModel) }
composable(Screen.UserProfile.route) { UserProfile(isDarkTheme, dataStore, currentUserViewModel = currentUserViewModel) }
composable(Screen.UserProfile.route) { UserProfile(navController,isDarkTheme, dataStore, currentUserViewModel = currentUserViewModel) }
composable(Screen.LoginScreen.route) { LoginScreen(navController) }
composable(
Screen.BikeEdit.route,
arguments = listOf(navArgument("id") { type = NavType.IntType })

View File

@ -18,6 +18,9 @@ enum class Screen(
BikeList(
"Bike-list", R.string.Bike_main_title, Icons.Filled.Home
),
LoginScreen(
"Login-screen", R.string.Profile_title, Icons.Filled.Home
),
BikeEdit(
"Bike-edit/{id}", R.string.Bike_view_title, showInBottomBar = false
),

View File

@ -7,8 +7,10 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
@ -29,9 +31,13 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import com.example.myapplication.composeui.navigation.Screen
import com.example.myapplication.database.entities.model.User
import com.example.myapplication.datastore.DataStoreManager
import com.example.myapplication.datastore.SettingData
@ -40,6 +46,7 @@ import kotlinx.coroutines.launch
@SuppressLint("UnrememberedMutableState")
@Composable
fun UserProfile(
navController: NavController?,
isDarkTheme: MutableState<Boolean>,
dataStoreManager: DataStoreManager,
registerUserViewModel: RegisterUserViewModel = viewModel(factory = AppViewModelProvider.Factory),
@ -49,6 +56,7 @@ fun UserProfile(
var username by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var isRegistration by remember { mutableStateOf(false) }
var isAuto by remember { mutableStateOf(false) }
val coroutineScope = rememberCoroutineScope()
@ -61,164 +69,219 @@ fun UserProfile(
entryUserViewModel.setUserList()
val users_entry = mutableStateOf<List<User>>(entryUserViewModel.userList)
LazyColumn {
item {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Text(
text = "Логин",
modifier = Modifier.align(Alignment.CenterHorizontally)
)
BasicTextField(
value = username,
textStyle = TextStyle(color = MaterialTheme.colorScheme.background),
onValueChange = { newValue -> username = newValue },
modifier = Modifier
.fillMaxWidth()
.size(36.dp)
.background(MaterialTheme.colorScheme.onBackground, RoundedCornerShape(18.dp))
.padding(start = 13.dp, top = 8.dp)
)
var getUser by remember { mutableStateOf(currentUserViewModel.user) }
Text(
text = "Пароль",
modifier = Modifier.align(Alignment.CenterHorizontally)
)
BasicTextField(
value = password,
textStyle = TextStyle(color = MaterialTheme.colorScheme.background),
onValueChange = { newValue -> password = newValue },
modifier = Modifier
.fillMaxWidth()
.size(36.dp)
.background(MaterialTheme.colorScheme.onBackground, RoundedCornerShape(18.dp))
.padding(start = 13.dp, top = 8.dp),
visualTransformation = PasswordVisualTransformation()
)
// Проверяем, авторизован пользователь или нет
if (getUser?.uid != null) {
// Пользователь авторизован, отображаем страницу профиля
// Замените на ваш код для страницы профиля
// Например, использовать другой @Composable или навигировать на другой экран
Text("Это страница профиля")
} else {
if (isRegistration) {
Button(
onClick = {
var isExist = false;
if (password.isNotEmpty() && username.isNotEmpty()) {
users.value.forEach { user ->
if (user.login == username) {
Log.d("User already exist. User id: ", user.uid.toString())
isExist = true
}
}
if (!isExist) {
val newUser = User(null, username, password)
coroutineScope.launch {
val insertResult = async {
registerUserViewModel.insertUser(newUser)
LazyColumn {
item(key = isAuto) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Text(
text = "Логин",
modifier = Modifier.align(Alignment.CenterHorizontally)
)
BasicTextField(
value = username,
textStyle = TextStyle(color = MaterialTheme.colorScheme.background),
onValueChange = { newValue -> username = newValue },
modifier = Modifier
.fillMaxWidth()
.size(36.dp)
.background(
MaterialTheme.colorScheme.onBackground,
RoundedCornerShape(18.dp)
)
.padding(start = 13.dp, top = 8.dp)
)
Text(
text = "Пароль",
modifier = Modifier.align(Alignment.CenterHorizontally)
)
BasicTextField(
value = password,
textStyle = TextStyle(color = MaterialTheme.colorScheme.background),
onValueChange = { newValue -> password = newValue },
modifier = Modifier
.fillMaxWidth()
.size(36.dp)
.background(
MaterialTheme.colorScheme.onBackground,
RoundedCornerShape(18.dp)
)
.padding(start = 13.dp, top = 8.dp),
visualTransformation = PasswordVisualTransformation()
)
if (isRegistration) {
Button(
onClick = {
var isExist = false;
if (password.isNotEmpty() && username.isNotEmpty()) {
users.value.forEach { user ->
if (user.login == username) {
Log.d(
"User already exist. User id: ",
user.uid.toString()
)
isExist = true
}
}
if (!isExist) {
val newUser = User(null, username, password)
coroutineScope.launch {
val insertResult = async {
registerUserViewModel.insertUser(newUser)
}
insertResult.await()
insertResult.await()
registerUserViewModel.setUserList()
registerUserViewModel.users.observeForever { userList ->
users.value = userList
Log.println(Log.ASSERT, "UsersList", users.value.toString())
users.value?.forEach { user ->
if (user.password == password) {
currentUserViewModel.setArgument(user.uid.toString())
//navController.navigate(route = Graph.passUserId(user.id.toString()))
registerUserViewModel.setUserList()
registerUserViewModel.users.observeForever { userList ->
users.value = userList
Log.println(
Log.ASSERT,
"UsersList",
users.value.toString()
)
users.value?.forEach { user ->
if (user.password == password) {
currentUserViewModel.setArgument(user.uid.toString())
//navController.navigate(route = Graph.passUserId(user.id.toString()))
}
}
}
}
}
}
}
},
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
Text("Регистрация")
}
Text(
text = "Уже есть аккаунт? Войти",
modifier = Modifier
.clickable {
isRegistration = false
}
.align(Alignment.CenterHorizontally),
color = MaterialTheme.colorScheme.onBackground
)
} else {
Button(
onClick = {
Log.d("Кнопка","Тык")
if (password.isNotEmpty()) {
users_entry.value.forEach { user ->
if (user.password == password) {
currentUserViewModel.setArgument(user.uid.toString())
Log.d("Авторизация","Успешно")
//navController.navigate(route = Graph.passUserId(user.id.toString())) {
}else{
Log.d("Авторизация","Пароль не совпадает")
}
},
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
Text("Регистрация")
}
Text(
text = "Уже есть аккаунт? Войти",
modifier = Modifier
.clickable {
isRegistration = false
}
}
},
.align(Alignment.CenterHorizontally),
color = MaterialTheme.colorScheme.onBackground
)
} else {
Button(
onClick = {
Log.d("Кнопка", "Тык")
if (password.isNotEmpty()) {
users_entry.value.forEach { user ->
if (user.password == password) {
currentUserViewModel.setArgument(user.uid.toString())
navController?.navigate(Screen.LoginScreen.route)
/* navController?.navigate(Screen.UserProfile.route)
navController?.popBackStack()*/
//navController.navigate(route = Graph.passUserId(user.id.toString())) {
} else {
Log.d("Авторизация", "Пароль не совпадает")
}
}
}
},
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
Text("Вход")
}
Text(
text = "Нет аккаунта? Зарегистрироваться",
modifier = Modifier
.clickable {
isRegistration = true
}
.align(Alignment.CenterHorizontally),
color = MaterialTheme.colorScheme.onBackground
)
}
val switchColors = SwitchDefaults.colors(
checkedThumbColor = MaterialTheme.colorScheme.primary, // Change the color when the switch is checked
checkedTrackColor = MaterialTheme.colorScheme.secondary, // Change the color of the track when the switch is checked
uncheckedThumbColor = MaterialTheme.colorScheme.primary, // Change the color when the switch is unchecked
uncheckedTrackColor = MaterialTheme.colorScheme.onPrimary // Change the color of the track when the switch is unchecked
)
Row(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
.padding(16.dp),
horizontalArrangement = Arrangement.End
) {
Text("Вход")
Text(
"Темная тема", modifier = Modifier
.align(Alignment.CenterVertically)
.padding(5.dp)
)
val coroutine = rememberCoroutineScope()
Switch(
checked = isDarkTheme.value,
onCheckedChange = {
isDarkTheme.value = !isDarkTheme.value
coroutine.launch {
dataStoreManager.saveSettings(SettingData(isDarkTheme = isDarkTheme.value))
}
},
colors = switchColors
)
}
Text(
text = "Нет аккаунта? Зарегистрироваться",
modifier = Modifier
.clickable {
isRegistration = true
}
.align(Alignment.CenterHorizontally),
color = MaterialTheme.colorScheme.onBackground
)
}
val switchColors = SwitchDefaults.colors(
checkedThumbColor = MaterialTheme.colorScheme.primary, // Change the color when the switch is checked
checkedTrackColor = MaterialTheme.colorScheme.secondary, // Change the color of the track when the switch is checked
uncheckedThumbColor = MaterialTheme.colorScheme.primary, // Change the color when the switch is unchecked
uncheckedTrackColor = MaterialTheme.colorScheme.onPrimary // Change the color of the track when the switch is unchecked
)
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
horizontalArrangement = Arrangement.End
) {
Text(
"Темная тема", modifier = Modifier
.align(Alignment.CenterVertically)
.padding(5.dp)
)
val coroutine = rememberCoroutineScope()
Switch(
checked = isDarkTheme.value,
onCheckedChange = {
isDarkTheme.value = !isDarkTheme.value
coroutine.launch {
dataStoreManager.saveSettings(SettingData(isDarkTheme = isDarkTheme.value))
}
},
colors = switchColors
)
}
}
}
}
}
@Composable
fun LoginScreen(navController: NavController?) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text("Соедение с ЛК УлГТУ произошло успешно.", fontWeight = FontWeight.Bold)
Spacer(modifier = Modifier.height(8.dp))
Text(
"Для дальнейшей работы, нажмите кнопку \"Готово\".",
textAlign = TextAlign.Center
)
Button(
onClick = {
Log.d("Кнопка", "Тык")
navController?.navigate(Screen.BikeList.route)
},
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
Text("Готово")
}
}
}
/*@Preview(name = "Light Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "Dark Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable