Подготовка к 4 лабе

This commit is contained in:
maxnes3 2023-11-25 21:43:54 +04:00
parent da844b15f7
commit 7076d707ea
12 changed files with 110 additions and 82 deletions

View File

@ -1,29 +1,16 @@
package com.example.mobileapp package com.example.mobileapp
import android.os.Bundle import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.example.mobileapp.components.NavBar import com.example.mobileapp.components.NavBar
import com.example.mobileapp.database.MobileAppDataBase import com.example.mobileapp.database.entities.User
import com.example.mobileapp.entities.Mail
import com.example.mobileapp.entities.MailSingleton
import com.example.mobileapp.entities.Story
import com.example.mobileapp.entities.StorySingleton
import com.example.mobileapp.ui.theme.MobileAppTheme import com.example.mobileapp.ui.theme.MobileAppTheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -42,3 +29,25 @@ class MainActivity : ComponentActivity() {
} }
} }
} }
class GlobalUser private constructor() {
private var user: User? = null
fun setUser(user: User?) {
this.user = user
}
fun getUser(): User? {
return user
}
companion object {
private var instance: GlobalUser? = null
fun getInstance(): GlobalUser {
return instance ?: synchronized(this) {
instance ?: GlobalUser().also { instance = it }
}
}
}
}

View File

@ -58,8 +58,11 @@ fun PlaceholderInputField(label: String, startValue: String? = null, isSingleLin
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun PasswordInputField(label: String, onPasswordChanged: (String) -> Unit){ fun PasswordInputField(label: String, startValue: String? = null, onPasswordChanged: (String) -> Unit){
var text = remember { mutableStateOf("") } var text = remember { mutableStateOf("") }
startValue?.let{
text.value = startValue
}
OutlinedTextField( OutlinedTextField(
value = text.value, value = text.value,

View File

@ -36,6 +36,7 @@ import com.example.mobileapp.R
import com.example.mobileapp.screens.Authorization import com.example.mobileapp.screens.Authorization
import com.example.mobileapp.screens.EditMailScreen import com.example.mobileapp.screens.EditMailScreen
import com.example.mobileapp.screens.EditStoryScreen import com.example.mobileapp.screens.EditStoryScreen
import com.example.mobileapp.screens.EditUserScreen
import com.example.mobileapp.screens.ListDataScreen import com.example.mobileapp.screens.ListDataScreen
import com.example.mobileapp.screens.ListMailScreen import com.example.mobileapp.screens.ListMailScreen
import com.example.mobileapp.screens.MainScreen import com.example.mobileapp.screens.MainScreen
@ -97,45 +98,49 @@ fun NavBar(navController: NavHostController) {
modifier = Modifier.padding(innerPaddings) modifier = Modifier.padding(innerPaddings)
) { ) {
composable("authorization"){ composable("authorization"){
Authorization(navController = navController)
bottomBarState.value = false bottomBarState.value = false
Authorization(navController = navController)
} }
composable("registration"){ composable("registration"){
Registration(navController = navController)
bottomBarState.value = false bottomBarState.value = false
Registration(navController = navController)
} }
composable("main"){ composable("main"){
MainScreen(navController = navController)
bottomBarState.value = true bottomBarState.value = true
MainScreen(navController = navController)
} }
composable("story"){ composable("story"){
ListDataScreen(navController = navController)
bottomBarState.value = true bottomBarState.value = true
ListDataScreen(navController = navController)
} }
composable("mail"){ composable("mail"){
ListMailScreen(navController = navController)
bottomBarState.value = true bottomBarState.value = true
ListMailScreen(navController = navController)
} }
composable("settings"){ composable("settings"){
SettingsScreen(navController = navController)
bottomBarState.value = true bottomBarState.value = true
SettingsScreen(navController = navController)
} }
composable("editstory"){ // Без аргумента composable("editstory"){ // Без аргумента
EditStoryScreen(navController = navController)
bottomBarState.value = false bottomBarState.value = false
EditStoryScreen(navController = navController)
} }
composable( composable(
"editstory/{id}", "editstory/{id}",
arguments = listOf(navArgument("id") { type = NavType.IntType }) arguments = listOf(navArgument("id") { type = NavType.IntType }) //С аргументом
) { backStackEntry -> ) { backStackEntry ->
backStackEntry.arguments?.let { backStackEntry.arguments?.let {
EditStoryScreen(navController = navController, storyId = it.getInt("id"))
bottomBarState.value = false bottomBarState.value = false
EditStoryScreen(navController = navController, storyId = it.getInt("id"))
} }
} }
composable("editmail"){ // Без аргумента composable("editmail"){ // Без аргумента
EditMailScreen(navController = navController)
bottomBarState.value = false bottomBarState.value = false
EditMailScreen(navController = navController)
}
composable("edituser"){
bottomBarState.value = false
EditUserScreen(navController = navController)
} }
} }
} }

View File

@ -4,7 +4,6 @@ import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.ForeignKey import androidx.room.ForeignKey
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import java.util.Calendar
import java.util.Date import java.util.Date
@Entity( @Entity(
@ -21,7 +20,7 @@ import java.util.Date
) )
data class Mail( data class Mail(
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
var id: Int? = null, val id: Int? = null,
@ColumnInfo(name = "message") @ColumnInfo(name = "message")
val message: String, val message: String,
@ColumnInfo(name = "postdate") @ColumnInfo(name = "postdate")

View File

@ -1,5 +1,6 @@
package com.example.mobileapp.database.entities package com.example.mobileapp.database.entities
import android.graphics.Bitmap
import androidx.room.ColumnInfo import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
@ -7,13 +8,15 @@ import androidx.room.PrimaryKey
@Entity(tableName = "users") @Entity(tableName = "users")
data class User( data class User(
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
var id: Int? = null, val id: Int? = null,
@ColumnInfo(name = "login") @ColumnInfo(name = "login")
val login: String, val login: String,
@ColumnInfo(name = "password") @ColumnInfo(name = "password")
val password: String, val password: String,
@ColumnInfo(name = "email") @ColumnInfo(name = "email")
val email: String, val email: String,
@ColumnInfo(name = "photo")
val photo: Bitmap? = null
){ ){
override fun hashCode(): Int { override fun hashCode(): Int {
return id ?: -1 return id ?: -1

View File

@ -1,8 +0,0 @@
package com.example.mobileapp.entities
data class Mail(
val id: Int,
val userId: Int,
val username: String,
val message: String
)

View File

@ -1,15 +0,0 @@
package com.example.mobileapp.entities
class MailSingleton {
companion object {
val mailList: MutableList<Mail> = mutableListOf()
}
fun addMail(mail: Mail) {
mailList.add(mail)
}
fun getMailList(): List<Mail> {
return mailList.toList()
}
}

View File

@ -1,8 +0,0 @@
package com.example.mobileapp.entities
data class Story(
val id: Int,
val title: String,
val description: String,
val cover: Int
)

View File

@ -1,15 +0,0 @@
package com.example.mobileapp.entities
class StorySingleton {
companion object {
val storyList: MutableList<Story> = mutableListOf()
}
fun addStory(story: Story) {
storyList.add(story)
}
fun getStoryList(): List<Story> {
return storyList.toList()
}
}

View File

@ -1,7 +0,0 @@
package com.example.mobileapp.entities
data class User(
val email: String,
val login: String,
val password: String
)

View File

@ -181,4 +181,67 @@ fun EditMailScreen(navController: NavHostController) {
NavigationButton(navController = navController, destination = "mail", label = "Назад", NavigationButton(navController = navController, destination = "mail", label = "Назад",
backgroundColor = ButtonColor2, textColor = Color.White) backgroundColor = ButtonColor2, textColor = Color.White)
} }
}
@Composable
fun EditUserScreen(navController: NavHostController){
val context = LocalContext.current
val photo = remember { mutableStateOf<Bitmap>(BitmapFactory.decodeResource(context.resources, R.drawable.photoplaceholder)) }
val name = remember { mutableStateOf("") }
val password = remember { mutableStateOf("") }
val email = remember { mutableStateOf("") }
val imageData = remember { mutableStateOf<Uri?>(null) }
val launcher =
rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) {uri: Uri? ->
imageData.value = uri
}
imageData.value?.let {
if (Build.VERSION.SDK_INT < 28) {
photo.value = MediaStore.Images
.Media.getBitmap(context.contentResolver, imageData.value)
} else {
val source = ImageDecoder
.createSource(context.contentResolver, imageData.value!!)
photo.value = ImageDecoder.decodeBitmap(source)
}
}
Column(
modifier = Modifier
.fillMaxSize()
.padding(bottom = 8.dp),
verticalArrangement = Arrangement.Bottom
) {
Image(
bitmap = photo.value.asImageBitmap(),
contentDescription = "editplaceholder",
contentScale = ContentScale.Crop,
modifier = Modifier
.size(384.dp)
.padding(8.dp)
.align(Alignment.CenterHorizontally))
ActiveButton(label = "Выбрать фото", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {
launcher.launch("image/*")
})
PlaceholderInputField(label = "Никнейм", isSingleLine = true,
startValue = name.value, onTextChanged = { newName ->
name.value = newName
})
PlaceholderInputField(label = "Пароль", isSingleLine = true,
startValue = password.value, onTextChanged = { newPassword ->
password.value = newPassword
})
PlaceholderInputField(label = "Почта", isSingleLine = true,
startValue = email.value, onTextChanged = { newEmail ->
email.value = newEmail
})
ActiveButton(label = "Сохранить", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {
//edit.value = !edit.value
})
NavigationButton(navController = navController, destination = "story", label = "Назад",
backgroundColor = ButtonColor2, textColor = Color.White)
}
} }

View File

@ -18,7 +18,6 @@ import com.example.mobileapp.components.DataListScroll
import com.example.mobileapp.components.NavBar import com.example.mobileapp.components.NavBar
import com.example.mobileapp.database.MobileAppDataBase import com.example.mobileapp.database.MobileAppDataBase
import com.example.mobileapp.database.entities.Story import com.example.mobileapp.database.entities.Story
import com.example.mobileapp.entities.StorySingleton
import com.example.mobileapp.ui.theme.BackgroundItem1 import com.example.mobileapp.ui.theme.BackgroundItem1
import com.example.mobileapp.ui.theme.BackgroundItem2 import com.example.mobileapp.ui.theme.BackgroundItem2
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers