Лаб 2 нало работы с пользователем

This commit is contained in:
Kate 2023-10-11 20:30:50 +04:00
parent 555dca89be
commit 4d537f08f0
17 changed files with 197 additions and 111 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
.gradle/file-system.probe Normal file

Binary file not shown.

View File

@ -9,29 +9,19 @@
<option name="autoReloadType" value="NONE" />
</component>
<component name="ChangeListManager">
<list default="true" id="dc3793c7-c725-42e8-8eda-044c95f334c1" name="Changes" comment="добавление отчета лаб 1">
<change afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/Home.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/Login.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/Profile.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/navigation/MyPage.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/navigation/Screen.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/models/user/composeui/StudentView.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/models/user/composeui/UserList.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/models/user/model/Student.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.gradle/8.0/checksums/checksums.lock" beforeDir="false" afterPath="$PROJECT_DIR$/.gradle/8.0/checksums/checksums.lock" afterDir="false" />
<list default="true" id="dc3793c7-c725-42e8-8eda-044c95f334c1" name="Changes" comment="Лаб 2 начало">
<change afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/models/user/model/AuthViewModel.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.gradle/8.0/executionHistory/executionHistory.lock" beforeDir="false" afterPath="$PROJECT_DIR$/.gradle/8.0/executionHistory/executionHistory.lock" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.gradle/8.0/fileHashes/fileHashes.bin" beforeDir="false" afterPath="$PROJECT_DIR$/.gradle/8.0/fileHashes/fileHashes.bin" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.gradle/8.0/fileHashes/fileHashes.lock" beforeDir="false" afterPath="$PROJECT_DIR$/.gradle/8.0/fileHashes/fileHashes.lock" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.gradle/buildOutputCleanup/buildOutputCleanup.lock" beforeDir="false" afterPath="$PROJECT_DIR$/.gradle/buildOutputCleanup/buildOutputCleanup.lock" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.gradle/buildOutputCleanup/outputFiles.bin" beforeDir="false" afterPath="$PROJECT_DIR$/.gradle/buildOutputCleanup/outputFiles.bin" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/gradle.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/gradle.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/app/build.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/src/main/AndroidManifest.xml" beforeDir="false" afterPath="$PROJECT_DIR$/app/src/main/AndroidManifest.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/MainActivity.kt" beforeDir="false" afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/MainComposeActivity.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/src/main/res/values/strings.xml" beforeDir="false" afterPath="$PROJECT_DIR$/app/src/main/res/values/strings.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/gradle.properties" beforeDir="false" afterPath="$PROJECT_DIR$/gradle.properties" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/Login.kt" beforeDir="false" afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/Login.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/Profile.kt" beforeDir="false" afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/Profile.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/navigation/MyPage.kt" beforeDir="false" afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/navigation/MyPage.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/models/user/composeui/StudentView.kt" beforeDir="false" afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/models/user/composeui/StudentView.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/models/user/composeui/UserList.kt" beforeDir="false" afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/models/user/composeui/UserList.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/models/user/model/Student.kt" beforeDir="false" afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/models/user/model/Student.kt" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -101,6 +91,9 @@
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProblemsViewState">
<option name="selectedTabId" value="CurrentFile" />
</component>
<component name="ProjectId" id="2WToW0mvjPzND9QiCAmC6MXKLwF" />
<component name="ProjectLevelVcsManager">
<ConfirmationsSetting value="2" id="Add" />
@ -226,7 +219,14 @@
<option name="project" value="LOCAL" />
<updated>1696766847352</updated>
</task>
<option name="localTasksCounter" value="3" />
<task id="LOCAL-00003" summary="Лаб 2 начало">
<created>1697038354150</created>
<option name="number" value="00003" />
<option name="presentableId" value="LOCAL-00003" />
<option name="project" value="LOCAL" />
<updated>1697038354150</updated>
</task>
<option name="localTasksCounter" value="4" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
@ -256,6 +256,7 @@
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
<MESSAGE value=оммит1" />
<MESSAGE value="добавление отчета лаб 1" />
<option name="LAST_COMMIT_MESSAGE" value="добавление отчета лаб 1" />
<MESSAGE value="Лаб 2 начало" />
<option name="LAST_COMMIT_MESSAGE" value="Лаб 2 начало" />
</component>
</project>

View File

@ -10,6 +10,7 @@ import androidx.compose.foundation.layout.Column
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.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
@ -31,17 +32,22 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import com.example.pmuapp.R
import com.example.pmuapp.composeui.navigation.Screen
import com.example.pmuapp.models.user.model.getStudents
import com.example.pmuapp.models.user.model.AuthViewModel
import com.example.pmuapp.models.user.model.User
import com.example.pmuapp.ui.theme.PMUappTheme
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Login(navController: NavController) {
fun Login(
navController: NavHostController,
authViewModel: AuthViewModel,
users: List<User> // Используйте ViewModel как параметр
) {
var username by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
val users = getStudents()
Column(
modifier = Modifier
@ -69,17 +75,15 @@ fun Login(navController: NavController) {
.padding(bottom = 16.dp)
)
Spacer(modifier = Modifier.height(16.dp)) // Промежуток
Button(
onClick = {
// Check if the entered username and password match a user
val authenticatedUser = users.find { it.login == username && it.password == password }
if (authenticatedUser != null) {
// Navigate to the Home screen if authentication is successful
navController.navigate(Screen.Home.route)
authViewModel.currentUser = authenticatedUser
navController.navigate(Screen.Profile.route)
} else {
// Show an error message or handle authentication failure
// For example, display a Toast or Text message
// For simplicity, we'll just print a message
println("Authentication failed")
}
},
@ -91,7 +95,6 @@ fun Login(navController: NavController) {
}
//@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

View File

@ -1,5 +1,6 @@
package com.example.pmuapp.composeui
import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.Configuration
import android.net.Uri
@ -8,16 +9,22 @@ import android.widget.TextView
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
@ -25,28 +32,61 @@ import com.example.pmuapp.R
import com.example.pmuapp.models.user.model.User
import com.example.pmuapp.ui.theme.PMUappTheme
@OptIn(ExperimentalMaterial3Api::class)
@SuppressLint("RememberReturnType")
@Composable
fun Profile(user: User) {
val localContext = LocalContext.current
fun Profile(currentUser: User?, onSaveClick: (User) -> Unit) {
val nameState = remember { mutableStateOf(currentUser?.name.orEmpty()) }
val loginState = remember { mutableStateOf(currentUser?.login.orEmpty()) }
val passwordState = remember { mutableStateOf(currentUser?.password.orEmpty()) }
val urlOnClick = {
val openURL = Intent(Intent.ACTION_VIEW)
openURL.data = Uri.parse("https://ulstu.ru/")
localContext.startActivity(openURL)
}
Column(Modifier.padding(all = 10.dp)) {
Text("Name: ${user.name}")
Text("Login: ${user.login}")
Text("Password: ${user.password}")
Spacer(Modifier.padding(bottom = 10.dp))
AndroidView(
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
) {
TextField(
value = nameState.value,
onValueChange = { nameState.value = it },
label = { Text("Name") },
modifier = Modifier
.fillMaxWidth()
.clickable(onClick = urlOnClick),
factory = { context -> TextView(context) },
update = { it.text = R.string.app_name.toString() }
.padding(bottom = 16.dp)
)
TextField(
value = loginState.value,
onValueChange = { loginState.value = it },
label = { Text("Login") },
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 16.dp)
)
TextField(
value = passwordState.value,
onValueChange = { passwordState.value = it },
label = { Text("Password") },
visualTransformation = PasswordVisualTransformation(),
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 16.dp)
)
Button(
onClick = {
val updatedUser = User(
nameState.value,
loginState.value,
passwordState.value
)
onSaveClick(updatedUser) // Вызывает функцию updateUser в UserViewModel
},
modifier = Modifier.fillMaxWidth()
) {
Text("Save")
}
}
}

View File

@ -18,6 +18,7 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
@ -35,9 +36,11 @@ import com.example.pmuapp.R
import com.example.pmuapp.composeui.Home
import com.example.pmuapp.composeui.Login
import com.example.pmuapp.composeui.Profile
import com.example.pmuapp.models.user.composeui.UserList
import com.example.pmuapp.models.user.composeui.StudentView
import com.example.pmuapp.models.user.composeui.UserList
import com.example.pmuapp.models.user.model.AuthViewModel
import com.example.pmuapp.models.user.model.User
import com.example.pmuapp.models.user.model.UserViewModel
import com.example.pmuapp.ui.theme.PMUappTheme
@OptIn(ExperimentalMaterial3Api::class)
@ -98,33 +101,37 @@ fun Navbar(
}
@Composable
fun Navhost(
navController: NavHostController,
innerPadding: PaddingValues, modifier:
Modifier = Modifier
fun NavHost(
navController: NavHostController,
innerPadding: PaddingValues,
authViewModel: AuthViewModel, // Передайте AuthViewModel как параметр
userViewModel: UserViewModel // Передайте UserViewModel как параметр
) {
NavHost(
navController,
navController = navController,
startDestination = Screen.Login.route,
modifier.padding(innerPadding)
modifier = Modifier.padding(innerPadding)
) {
composable(Screen.UserList.route) { UserList(navController) }
composable(Screen.Login.route) {
Login(navController, authViewModel,userViewModel.getUsers())
}
composable(Screen.Profile.route) {
Profile(
currentUser = authViewModel.currentUser,
onSaveClick = { updatedUser ->
userViewModel.updateUser(updatedUser)
authViewModel.currentUser=updatedUser// Обновление пользователя в ViewModel
}
)
}
composable(Screen.UserList.route) { UserList(navController, userViewModel.getUsers()) }
composable(Screen.Home.route) { Home() }
composable(Screen.Profile.route + "/{name}/{username}/{password}") { backStackEntry ->
val name = backStackEntry.arguments?.getString("name")
val username = backStackEntry.arguments?.getString("username")
val password = backStackEntry.arguments?.getString("password")
if (name != null && username != null && password != null) {
val user = User(name, username, password)
Profile(user)
}
}
composable(Screen.Login.route) { Login(navController) }
composable(
Screen.UserView.route,
arguments = listOf(navArgument("id") { type = NavType.IntType })
) { backStackEntry ->
backStackEntry.arguments?.let { StudentView(it.getInt("id")) }
backStackEntry.arguments?.let { StudentView(userViewModel.getUsers()[it.getInt("id")]) }
}
}
}
@ -133,6 +140,8 @@ fun Navhost(
@Composable
fun MainNavbar() {
val navController = rememberNavController()
val authViewModel = remember { AuthViewModel() }
val userViewModel =UserViewModel() // Создайте экземпляр AuthViewModel
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
val currentScreen = currentDestination?.route?.let { Screen.getItem(it) }
@ -147,7 +156,7 @@ fun MainNavbar() {
}
}
) { innerPadding ->
Navhost(navController, innerPadding)
NavHost(navController, innerPadding, authViewModel, userViewModel)
}
}

View File

@ -15,32 +15,32 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.pmuapp.R
import com.example.pmuapp.models.user.model.getStudents
import com.example.pmuapp.models.user.model.User
import com.example.pmuapp.ui.theme.PMUappTheme
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun StudentView(id: Int) {
val student = getStudents()[id]
fun StudentView(user: User) {
Column(
Modifier
.fillMaxWidth()
.padding(all = 10.dp)
) {
OutlinedTextField(modifier = Modifier.fillMaxWidth(),
value = student.name, onValueChange = {}, readOnly = true,
value = user.name, onValueChange = {}, readOnly = true,
label = {
Text(stringResource(id = R.string.user_name))
}
)
OutlinedTextField(modifier = Modifier.fillMaxWidth(),
value = student.login, onValueChange = {}, readOnly = true,
value = user.login, onValueChange = {}, readOnly = true,
label = {
Text(stringResource(id = R.string.user_login))
}
)
OutlinedTextField(modifier = Modifier.fillMaxWidth(),
value = student.password, onValueChange = {}, readOnly = true,
value = user.password, onValueChange = {}, readOnly = true,
label = {
Text(stringResource(id = R.string.user_login))
}
@ -48,15 +48,15 @@ fun StudentView(id: Int) {
}
}
@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
fun StudentViewPreview() {
PMUappTheme {
Surface(
color = MaterialTheme.colorScheme.background
) {
StudentView(id = 0)
}
}
}
//@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
//fun StudentViewPreview() {
// PMUappTheme {
// Surface(
// color = MaterialTheme.colorScheme.background
// ) {
// StudentView(id = 0)
// }
// }
//}

View File

@ -14,34 +14,35 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.example.pmuapp.composeui.navigation.Screen
import com.example.pmuapp.models.user.model.getStudents
import com.example.pmuapp.models.user.model.User
import com.example.pmuapp.ui.theme.PMUappTheme
@Composable
fun UserList(navController: NavController?) {
fun UserList(navController: NavController?,
users: List<User> ) {
Column(Modifier.padding(all = 10.dp)) {
getStudents().forEachIndexed() { index, student ->
val studentId = Screen.UserView.route.replace("{id}", index.toString())
users.forEachIndexed() { index, user ->
val userId = Screen.UserView.route.replace("{id}", index.toString())
Button(
modifier = Modifier
.fillMaxWidth()
.padding(all = 10.dp),
onClick = { navController?.navigate(studentId) }) {
Text("${student.login}")
onClick = { navController?.navigate(userId) }) {
Text("${user.login}")
}
}
}
}
@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
fun StudentListPreview() {
PMUappTheme {
Surface(
color = MaterialTheme.colorScheme.background
) {
UserList(navController = null)
}
}
}
//@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
//fun StudentListPreview() {
// PMUappTheme {
// Surface(
// color = MaterialTheme.colorScheme.background
// ) {
// UserList(navController = null)
// }
// }
//}

View File

@ -0,0 +1,10 @@
package com.example.pmuapp.models.user.model
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
class AuthViewModel : ViewModel() {
var currentUser: User? by mutableStateOf(null)
}

View File

@ -1,5 +1,8 @@
package com.example.pmuapp.models.user.model
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import java.io.Serializable
data class User(
@ -8,10 +11,29 @@ data class User(
val password: String,
) : Serializable
fun getStudents(): List<User> {
return listOf(
class UserViewModel : ViewModel() {
var users: MutableState<List<User>> = mutableStateOf(
listOf(
User("Иван", "ivan", "111111"),
User("Анна", "ann", "111111"),
User("Лиза", "liza", "111111")
)
)
fun getUsers(): List<User> {
return users.value
}
fun updateUser(updatedUser: User) {
val updatedUsers = users.value.toMutableList()
val index = updatedUsers.indexOfFirst { it.login == updatedUser.login }
if (index != -1) {
updatedUsers[index] = updatedUser
users.value = updatedUsers
}
}
}