Лаб 2 питомцы просто добавлены

This commit is contained in:
Kate 2023-10-12 00:06:50 +04:00
parent 4d537f08f0
commit 16d85c07d0
33 changed files with 413 additions and 86 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,41 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PreviewAnnotationInFunctionWithParameters" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewApiLevelMustBeValid" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewFontScaleMustBeGreaterThanZero" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewMultipleParameterProviders" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewMustBeTopLevelFunction" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewNeedsComposableAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewNotSupportedInUnitTestFiles" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
<inspection_tool class="PreviewPickerAnnotation" enabled="true" level="ERROR" enabled_by_default="true">
<option name="composableFile" value="true" />
<option name="previewFile" value="true" />
</inspection_tool>
</profile>
</component>

View File

@ -9,19 +9,39 @@
<option name="autoReloadType" value="NONE" />
</component>
<component name="ChangeListManager">
<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" />
<list default="true" id="dc3793c7-c725-42e8-8eda-044c95f334c1" name="Changes" comment="Лаб 2 нало работы с пользователем">
<change afterPath="$PROJECT_DIR$/.idea/inspectionProfiles/Project_Default.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/CreatePet.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/models/user/composeui/PetList.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/models/user/model/Pet.kt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/res/drawable/pet1.jpg" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/res/drawable/pet2.jpg" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/res/drawable/pet3.jpg" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/res/drawable/pet4.jpg" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/res/drawable/pet5.jpg" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/res/drawable/pet6.jpg" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/res/drawable/pet7.jpg" afterDir="false" />
<change afterPath="$PROJECT_DIR$/app/src/main/res/drawable/pet8.jpg" 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" />
<change beforePath="$PROJECT_DIR$/.gradle/8.0/checksums/md5-checksums.bin" beforeDir="false" afterPath="$PROJECT_DIR$/.gradle/8.0/checksums/md5-checksums.bin" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.gradle/8.0/checksums/sha1-checksums.bin" beforeDir="false" afterPath="$PROJECT_DIR$/.gradle/8.0/checksums/sha1-checksums.bin" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.gradle/8.0/executionHistory/executionHistory.bin" beforeDir="false" afterPath="$PROJECT_DIR$/.gradle/8.0/executionHistory/executionHistory.bin" 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/8.0/fileHashes/resourceHashesCache.bin" beforeDir="false" afterPath="$PROJECT_DIR$/.gradle/8.0/fileHashes/resourceHashesCache.bin" 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$/.gradle/file-system.probe" beforeDir="false" afterPath="$PROJECT_DIR$/.gradle/file-system.probe" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" 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/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/app/build.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/Home.kt" beforeDir="false" afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/Home.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/composeui/navigation/Screen.kt" beforeDir="false" afterPath="$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/navigation/Screen.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" />
<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/User.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" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -75,8 +95,8 @@
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Kotlin File" />
<option value="Kotlin Class" />
<option value="Kotlin File" />
</list>
</option>
</component>
@ -108,18 +128,20 @@
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.cidr.known.project.marker": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"cidr.known.project.marker": "true",
"com.android.tools.idea.devicemanager.tab": "Physical",
"last_opened_file_path": "C:/Users/Kate/AndroidStudioProjects/Ihonkina_PIbd-31_PMU/app/src/main/java/com/example/pmuapp/composeui",
"last_opened_file_path": "C:/Users/Kate/AndroidStudioProjects/Ihonkina_PIbd-31_PMU/app/src/main/java/com/example/pmuapp/models/user/composeui",
"settings.editor.selected.configurable": "experimental"
}
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="C:\Users\Kate\AndroidStudioProjects\Ihonkina_PIbd-31_PMU\app\src\main\java\com\example\pmuapp\models\user\composeui" />
<recent name="C:\Users\Kate\AndroidStudioProjects\Ihonkina_PIbd-31_PMU\app\src\main\java\com\example\pmuapp\composeui" />
<recent name="C:\Users\Kate\AndroidStudioProjects\Ihonkina_PIbd-31_PMU\app\src\main\res\drawable" />
<recent name="C:\Users\Kate\AndroidStudioProjects\Ihonkina_PIbd-31_PMU\app\src\main\java\com\example\pmuapp" />
<recent name="C:\Users\Kate\AndroidStudioProjects\Ihonkina_PIbd-31_PMU" />
<recent name="C:\Users\Kate\AndroidStudioProjects\Ihonkina_PIbd-31_PMU\app\src\main\res\values" />
</key>
<key name="MoveKotlinTopLevelDeclarationsDialog.RECENTS_KEY">
<recent name="com.example.pmuapp.composeui" />
@ -226,7 +248,14 @@
<option name="project" value="LOCAL" />
<updated>1697038354150</updated>
</task>
<option name="localTasksCounter" value="4" />
<task id="LOCAL-00004" summary="Лаб 2 нало работы с пользователем">
<created>1697041852651</created>
<option name="number" value="00004" />
<option name="presentableId" value="LOCAL-00004" />
<option name="project" value="LOCAL" />
<updated>1697041852651</updated>
</task>
<option name="localTasksCounter" value="5" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
@ -257,6 +286,18 @@
<MESSAGE value=оммит1" />
<MESSAGE value="добавление отчета лаб 1" />
<MESSAGE value="Лаб 2 начало" />
<option name="LAST_COMMIT_MESSAGE" value="Лаб 2 начало" />
<MESSAGE value="Лаб 2 нало работы с пользователем" />
<option name="LAST_COMMIT_MESSAGE" value="Лаб 2 нало работы с пользователем" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="kotlin-line">
<url>file://$PROJECT_DIR$/app/src/main/java/com/example/pmuapp/composeui/CreatePet.kt</url>
<line>73</line>
<option name="timeStamp" value="1" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
</project>

View File

@ -60,6 +60,8 @@ dependencies {
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("com.google.android.engage:engage-core:1.3.0")
implementation("androidx.appcompat:appcompat:1.6.1")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

View File

@ -0,0 +1,86 @@
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.example.pmuapp.R
import com.example.pmuapp.models.user.model.Pet
import com.example.pmuapp.models.user.model.User
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CreatePet(onSaveClick: (Pet) -> Unit) {
var selectedImage by remember { mutableStateOf(R.drawable.pet1) }
var petName by remember { mutableStateOf("") }
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
) {
// Отображение изображений для выбора
val imageIds = intArrayOf(
R.drawable.pet1,
R.drawable.pet2,
R.drawable.pet3,
R.drawable.pet4,
R.drawable.pet5,
R.drawable.pet6,
R.drawable.pet7,
R.drawable.pet8
)
LazyRow(
modifier = Modifier.fillMaxWidth(),
contentPadding = PaddingValues(horizontal = 8.dp)
) {
items(imageIds.size) { index ->
val imageId = imageIds[index]
Image(
painter = painterResource(id = imageId),
contentDescription = null,
modifier = Modifier
.size(100.dp)
.padding(4.dp)
.clickable {
selectedImage = imageId
}
)
}
}
Spacer(modifier = Modifier.height(16.dp))
// Поле для ввода имени питомца
TextField(
value = petName,
onValueChange = { petName = it },
label = { Text("Имя питомца") },
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.height(16.dp))
// Кнопка "Сохранить"
Button(
onClick = {
if (petName.isNotEmpty()) {
val newPet = Pet(0, petName, selectedImage)
onSaveClick(newPet)
petName = ""
selectedImage = R.drawable.pet1
}
},
modifier = Modifier.fillMaxWidth()
) {
Text("Сохранить")
}
}
}

View File

@ -6,50 +6,52 @@ import android.net.Uri
import android.provider.ContactsContract.Profile
import android.widget.TextView
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
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.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.navigation.NavController
import com.example.pmuapp.R
import com.example.pmuapp.composeui.navigation.Screen
import com.example.pmuapp.models.user.composeui.PetList
import com.example.pmuapp.models.user.model.AuthViewModel
import com.example.pmuapp.ui.theme.PMUappTheme
@Composable
fun Home() {
val localContext = LocalContext.current
val aboutText = localContext.resources.getText(R.string.about_text)
val urlOnClick = {
val openURL = Intent(Intent.ACTION_VIEW)
openURL.data = Uri.parse("https://ulstu.ru/")
localContext.startActivity(openURL)
}
Column(Modifier.padding(all = 10.dp)) {
AndroidView(
modifier = Modifier
.fillMaxWidth()
.clickable(onClick = urlOnClick),
factory = { context -> TextView(context) },
update = { it.text = aboutText }
)
Spacer(Modifier.padding(bottom = 10.dp))
fun Home(navController: NavController, authViewModel: AuthViewModel) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
// Add a button to navigate to the "Create Pet" screen
Button(
modifier = Modifier.fillMaxWidth(),
onClick = urlOnClick
onClick = {
// При нажатии кнопки, перейдите на экран создания питомца (CreatePet)
navController.navigate(Screen.CreatePet.route)
}
) {
Text(stringResource(id = R.string.user_my_title))
Text("Создать питомца")
}
Spacer(modifier = Modifier.height(16.dp))
// Вывод списка питомцев
PetList(navController, authViewModel)
}
}

View File

@ -76,15 +76,17 @@ fun Profile(currentUser: User?, onSaveClick: (User) -> Unit) {
Button(
onClick = {
val updatedUser = User(
currentUser?.id ?: 0,
nameState.value,
loginState.value,
passwordState.value
passwordState.value,
currentUser?.petId ?: emptyList()
)
onSaveClick(updatedUser) // Вызывает функцию updateUser в UserViewModel
},
modifier = Modifier.fillMaxWidth()
) {
Text("Save")
Text("Сохранить")
}
}

View File

@ -1,5 +1,6 @@
package com.example.pmuapp.composeui.navigation
import CreatePet
import android.content.res.Configuration
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
@ -116,17 +117,32 @@ userViewModel: UserViewModel // Передайте UserViewModel как пара
Login(navController, authViewModel,userViewModel.getUsers())
}
composable(Screen.Profile.route) {
Profile(
currentUser = authViewModel.currentUser,
onSaveClick = { updatedUser ->
userViewModel.updateUser(updatedUser)
authViewModel.currentUser=updatedUser// Обновление пользователя в ViewModel
}
)
val currentUser = authViewModel.currentUser ?: userViewModel.getUsers().firstOrNull()
if (currentUser != null) {
Profile(
currentUser = currentUser,
onSaveClick = { updatedUser ->
userViewModel.updateUser(updatedUser)
authViewModel.currentUser = updatedUser
}
)
} else {
// Обработка случая, если currentUser отсутствует как в AuthViewModel, так и в UserViewModel
}
}
composable(Screen.UserList.route) { UserList(navController, userViewModel.getUsers()) }
composable(Screen.Home.route) { Home() }
composable(Screen.UserList.route) { UserList(navController, userViewModel) }
composable(Screen.Home.route) { Home(navController, authViewModel) }
composable(Screen.CreatePet.route) {
val currentUser = authViewModel.currentUser ?: userViewModel.getUsers().firstOrNull()
CreatePet ( onSaveClick = { newPet ->
userViewModel.addPetToUser(currentUser?.id ?:0, newPet)
authViewModel.currentUser = userViewModel.getUser(currentUser?.id ?:0)
}
)
}
composable(
Screen.UserView.route,
arguments = listOf(navArgument("id") { type = NavType.IntType })
@ -141,7 +157,7 @@ userViewModel: UserViewModel // Передайте UserViewModel как пара
fun MainNavbar() {
val navController = rememberNavController()
val authViewModel = remember { AuthViewModel() }
val userViewModel =UserViewModel() // Создайте экземпляр AuthViewModel
val userViewModel =remember {UserViewModel() }// Создайте экземпляр AuthViewModel
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
val currentScreen = currentDestination?.route?.let { Screen.getItem(it) }

View File

@ -31,6 +31,9 @@ enum class Screen(
),
Login(
"login", R.string.login_title, showInBottomBar = false
),
CreatePet(
"createpet", R.string.create_pet_title, showInBottomBar = false
);
companion object {

View File

@ -0,0 +1,69 @@
package com.example.pmuapp.models.user.composeui
import android.content.res.Configuration
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
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.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(ExperimentalFoundationApi::class, ExperimentalLayoutApi::class)
@Composable
fun PetList(navController: NavController?, authViewModel: AuthViewModel) {
val currentUser = authViewModel.currentUser
val pets = currentUser?.petId.orEmpty()
LazyColumn {
item {
FlowRow(
) {
pets.forEach { pet ->
val imageId = pet.imageResId
val petName = pet.name
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.padding(8.dp)
) {
Image(
painter = painterResource(id = imageId),
contentDescription = null,
modifier = Modifier.size(100.dp)
)
Text(
text = petName,
modifier = Modifier.padding(top = 4.dp)
)
}
}
}
}
}
}

View File

@ -15,11 +15,16 @@ import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.example.pmuapp.composeui.navigation.Screen
import com.example.pmuapp.models.user.model.User
import com.example.pmuapp.models.user.model.UserViewModel
import com.example.pmuapp.ui.theme.PMUappTheme
@Composable
fun UserList(navController: NavController?,
users: List<User> ) {
userViewModel: UserViewModel
) {
val users = userViewModel.users.value
Column(Modifier.padding(all = 10.dp)) {
users.forEachIndexed() { index, user ->
val userId = Screen.UserView.route.replace("{id}", index.toString())

View File

@ -0,0 +1,16 @@
package com.example.pmuapp.models.user.model
import android.media.Image
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import java.io.Serializable
data class Pet(
val id: Int,
val name: String,
val imageResId: Int
) : Serializable

View File

@ -1,39 +0,0 @@
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(
val name: String,
val login: String,
val password: String,
) : Serializable
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
}
}
}

View File

@ -0,0 +1,72 @@
package com.example.pmuapp.models.user.model
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import com.example.pmuapp.R
import java.io.Serializable
data class User(
val id: Int,
val name: String,
val login: String,
val password: String,
val petId: List<Pet>
) : Serializable
class UserViewModel : ViewModel() {
var users: MutableState<List<User>> = mutableStateOf(
listOf(
User(0,"Иван", "ivan", "111111", emptyList()),
User(1,"Анна", "ann", "111111", emptyList()),
User(2,"Лиза", "liza", "111111",emptyList())
)
)
val availablePetImages: List<Int> = listOf(
R.drawable.pet1,
R.drawable.pet2,
R.drawable.pet3,
R.drawable.pet4,
R.drawable.pet5,
R.drawable.pet6,
R.drawable.pet7,
R.drawable.pet8,
)
fun getUsers(): List<User> {
return users.value
}
fun getUser(id:Int): User {
return users.value.get(id)
}
fun getPets(id:Int): List<Pet> {
return users.value.get(id).petId
}
// Function to add a pet to a user
fun addPetToUser(userId: Int, newPet: Pet) {
val updatedUsers = users.value.toMutableList()
val user = updatedUsers.find { it.id == userId }
user?.let {
val updatedPets = user.petId.toMutableList()
updatedPets.add(newPet)
val updatedUser = user.copy(petId = updatedPets)
val userIndex = updatedUsers.indexOf(user)
updatedUsers[userIndex] = updatedUser
users.value = updatedUsers
}
}
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
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

View File

@ -8,9 +8,20 @@
<string name="home_title">Мой дом</string>
<string name="user_my_title">Мой профиль</string>
<string name="login_title">Авторизация</string>
<string name="create_pet_title">Создание питомца</string>
<string name="about_text">
<p>Это текст <b>о нас</b>!</p>\n\n
<p>Здесь могла быть Ваша реклама!</p>\n\n
<p>Наш сайт <a href="https://ulstu.ru">ulstu.ru</a></p>
</string>
<string-array name="available_pet_images">
<item>@drawable/pet1</item>
<item>@drawable/pet2</item>
<item>@drawable/pet3</item>
<item>@drawable/pet4</item>
<item>@drawable/pet5</item>
<item>@drawable/pet6</item>
<item>@drawable/pet7</item>
<item>@drawable/pet8</item>
</string-array>
</resources>