Yes, i'am the best

This commit is contained in:
maxnes3 2023-12-27 20:10:22 +04:00
parent 59e5f2b267
commit 153661a5f0
7 changed files with 159 additions and 58 deletions

View File

@ -6,6 +6,7 @@ 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.mutableStateOf
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.example.mobileapp.components.NavBar import com.example.mobileapp.components.NavBar
@ -31,14 +32,14 @@ class MainActivity : ComponentActivity() {
} }
class GlobalUser private constructor() { class GlobalUser private constructor() {
private var user: User? = null private var user = mutableStateOf<User?>(null)
fun setUser(user: User?) { fun setUser(user: User?) {
this.user = user this.user.value = user
} }
fun getUser(): User? { fun getUser(): User? {
return user return user.value
} }
companion object { companion object {

View File

@ -8,10 +8,15 @@ import java.io.ByteArrayOutputStream
class RemoteConverters { class RemoteConverters {
companion object { companion object {
private const val CHARSET_UTF8 = "UTF-8" private const val CHARSET_UTF8 = "UTF-8"
private const val MAX_IMAGE_SIZE = 1024 // Размер в килобайтах, который вы хотите использовать
fun fromBitmap(bitmap: Bitmap): String { fun fromBitmap(bitmap: Bitmap): String {
val outputStream = ByteArrayOutputStream() val outputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
// Сжимаем изображение до указанного максимального размера
val quality = calculateQuality(bitmap)
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream)
val byteArray = outputStream.toByteArray() val byteArray = outputStream.toByteArray()
return Base64.encodeToString(byteArray, Base64.NO_WRAP) return Base64.encodeToString(byteArray, Base64.NO_WRAP)
} }
@ -20,5 +25,22 @@ class RemoteConverters {
val byteArray = Base64.decode(base64String, Base64.NO_WRAP) val byteArray = Base64.decode(base64String, Base64.NO_WRAP)
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size) return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
} }
private fun calculateQuality(bitmap: Bitmap): Int {
val outputStream = ByteArrayOutputStream()
/*bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
val initialSize = outputStream.size()*/
// Уменьшаем качество изображения, пока его размер не станет меньше максимального
var quality = 100
while (outputStream.size() / 1024 > MAX_IMAGE_SIZE && quality > 0) {
outputStream.reset()
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream)
quality -= 10
}
// Возвращаем качество, при котором размер изображения удовлетворяет ограничению
return if (quality < 0) 0 else quality
}
} }
} }

View File

@ -25,6 +25,7 @@ import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
@ -349,3 +350,16 @@ fun DialogWindow(label: String, message: String, onConfirmAction: () -> Unit, on
} }
) )
} }
@Composable
fun LoadingScreen() {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
CircularProgressIndicator()
}
}

View File

@ -21,6 +21,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.mobileapp.GlobalUser
import com.example.mobileapp.R import com.example.mobileapp.R
import com.example.mobileapp.components.ActiveButton import com.example.mobileapp.components.ActiveButton
import com.example.mobileapp.components.NavigationButton import com.example.mobileapp.components.NavigationButton
@ -40,7 +41,12 @@ fun Authorization(navController: NavHostController,
userViewModel: UserViewModel = viewModel( userViewModel: UserViewModel = viewModel(
factory = MobileAppViewModelProvider.Factory factory = MobileAppViewModelProvider.Factory
)) { )) {
//val users = userViewModel.getAllUsers.collectAsState(emptyList()).value val isAuthorizated = remember { mutableStateOf(false) }
if(GlobalUser.getInstance().getUser() != null && !isAuthorizated.value) {
isAuthorizated.value = !isAuthorizated.value
navController.navigate("main")
}
val login = remember { mutableStateOf("") } val login = remember { mutableStateOf("") }
val password = remember { mutableStateOf("") } val password = remember { mutableStateOf("") }
@ -75,7 +81,6 @@ fun Authorization(navController: NavHostController,
email = String() email = String()
) )
) )
navController.navigate("main")
} }
}) })
NavigationButton(navController = navController, destination = "registration", label = "Регистрация", NavigationButton(navController = navController, destination = "registration", label = "Регистрация",

View File

@ -8,14 +8,19 @@ import android.os.Build
import android.provider.MediaStore import android.provider.MediaStore
import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.border import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
@ -25,8 +30,11 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ColorMatrix
import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
@ -90,17 +98,32 @@ fun EditStoryScreen(navController: NavHostController, storyId: Int? = null,
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
.padding(bottom = 8.dp), .padding(bottom = 8.dp)
verticalArrangement = Arrangement.Bottom .verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.Bottom,
) {
Box(
modifier = Modifier
.size(512.dp),
contentAlignment = Alignment.Center
) { ) {
Image( Image(
bitmap = cover.value.asImageBitmap(), bitmap = cover.value.asImageBitmap(),
contentDescription = "editplaceholder", contentDescription = "Background Image",
contentScale = ContentScale.Crop,
modifier = Modifier
.size(512.dp)
.blur(12.dp),
colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0f) }))
Image(
bitmap = cover.value.asImageBitmap(),
contentDescription = "cover",
contentScale = ContentScale.Crop, contentScale = ContentScale.Crop,
modifier = Modifier modifier = Modifier
.size(384.dp)
.padding(8.dp) .padding(8.dp)
.align(Alignment.CenterHorizontally)) .size(384.dp)
.clip(RoundedCornerShape(16.dp)))
}
ActiveButton(label = "Выбрать обложку", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = { ActiveButton(label = "Выбрать обложку", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {
launcher.launch("image/*") launcher.launch("image/*")
}) })
@ -221,9 +244,23 @@ fun EditUserScreen(navController: NavHostController,
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
.padding(bottom = 8.dp), .padding(bottom = 8.dp)
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.Bottom verticalArrangement = Arrangement.Bottom
) { ) {
Box(
modifier = Modifier
.size(512.dp),
contentAlignment = Alignment.Center
) {
Image(
bitmap = photo.value.asImageBitmap(),
contentDescription = "Background Image",
contentScale = ContentScale.Crop,
modifier = Modifier
.size(512.dp)
.blur(12.dp),
colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0f) }))
Image( Image(
bitmap = photo.value.asImageBitmap(), bitmap = photo.value.asImageBitmap(),
contentDescription = "editplaceholder", contentDescription = "editplaceholder",
@ -231,12 +268,8 @@ fun EditUserScreen(navController: NavHostController,
modifier = Modifier modifier = Modifier
.padding(8.dp) .padding(8.dp)
.clip(CircleShape) .clip(CircleShape)
.size(384.dp) .size(384.dp))
.border( }
width = 2.dp,
color = MaterialTheme.colorScheme.onPrimary,
)
.align(Alignment.CenterHorizontally))
ActiveButton(label = "Выбрать фото", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = { ActiveButton(label = "Выбрать фото", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = {
launcher.launch("image/*") launcher.launch("image/*")
}) })

View File

@ -17,6 +17,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.mobileapp.GlobalUser
import com.example.mobileapp.R import com.example.mobileapp.R
import com.example.mobileapp.components.ActiveButton import com.example.mobileapp.components.ActiveButton
import com.example.mobileapp.components.NavigationButton import com.example.mobileapp.components.NavigationButton
@ -33,6 +34,13 @@ fun Registration(navController: NavHostController,
userViewModel: UserViewModel = viewModel( userViewModel: UserViewModel = viewModel(
factory = MobileAppViewModelProvider.Factory factory = MobileAppViewModelProvider.Factory
)) { )) {
val isRegistrated = remember { mutableStateOf(false) }
if(GlobalUser.getInstance().getUser() != null && !isRegistrated.value) {
isRegistrated.value = !isRegistrated.value
navController.navigate("main")
}
val login = remember { mutableStateOf("") } val login = remember { mutableStateOf("") }
val email = remember { mutableStateOf("") } val email = remember { mutableStateOf("") }
val password = remember { mutableStateOf("") } val password = remember { mutableStateOf("") }
@ -75,7 +83,6 @@ fun Registration(navController: NavHostController,
) )
) )
} }
navController.navigate("main")
}) })
NavigationButton(navController = navController, destination = "authorization", NavigationButton(navController = navController, destination = "authorization",
label = "Назад", backgroundColor = ButtonColor1, textColor = Color.Black) label = "Назад", backgroundColor = ButtonColor1, textColor = Color.Black)

View File

@ -5,11 +5,13 @@ import android.graphics.BitmapFactory
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.border import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
@ -18,8 +20,11 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ColorMatrix
import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
@ -32,7 +37,6 @@ import com.example.mobileapp.R
import com.example.mobileapp.components.NavigationButton import com.example.mobileapp.components.NavigationButton
import com.example.mobileapp.database.viewmodels.MailViewModel import com.example.mobileapp.database.viewmodels.MailViewModel
import com.example.mobileapp.database.viewmodels.MobileAppViewModelProvider import com.example.mobileapp.database.viewmodels.MobileAppViewModelProvider
import com.example.mobileapp.database.viewmodels.ReportViewModel
import com.example.mobileapp.database.viewmodels.StoryViewModel import com.example.mobileapp.database.viewmodels.StoryViewModel
import com.example.mobileapp.database.viewmodels.UserViewModel import com.example.mobileapp.database.viewmodels.UserViewModel
import com.example.mobileapp.ui.theme.ButtonColor2 import com.example.mobileapp.ui.theme.ButtonColor2
@ -53,14 +57,6 @@ fun StoryViewScreen(navController: NavHostController, storyId: Int,
val description = remember { mutableStateOf("") } val description = remember { mutableStateOf("") }
val postdate = remember { mutableStateOf<Long>(0) } val postdate = remember { mutableStateOf<Long>(0) }
/*val story by storyViewModel.getStoryById(storyId).collectAsState(null)
story?.let {
cover.value = it.cover
title.value = it.title
description.value = it.description
postdate.value = it.postdate!!
}*/
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
storyId?.let { storyId?.let {
val story = storyViewModel.getStoryById(storyId) val story = storyViewModel.getStoryById(storyId)
@ -79,14 +75,28 @@ fun StoryViewScreen(navController: NavHostController, storyId: Int,
.padding(bottom = 8.dp), .padding(bottom = 8.dp),
verticalArrangement = Arrangement.Center verticalArrangement = Arrangement.Center
) { ) {
Box(
modifier = Modifier
.size(512.dp),
contentAlignment = Alignment.Center
) {
Image(
bitmap = cover.value.asImageBitmap(),
contentDescription = "Background Image",
contentScale = ContentScale.Crop,
modifier = Modifier
.size(512.dp)
.blur(12.dp),
colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0f) }))
Image( Image(
bitmap = cover.value.asImageBitmap(), bitmap = cover.value.asImageBitmap(),
contentDescription = "cover", contentDescription = "cover",
contentScale = ContentScale.Crop, contentScale = ContentScale.Crop,
modifier = Modifier modifier = Modifier
.size(512.dp)
.padding(8.dp) .padding(8.dp)
.align(Alignment.CenterHorizontally)) .size(384.dp)
.clip(RoundedCornerShape(16.dp)))
}
Text(text = "Название: ${title.value}", Text(text = "Название: ${title.value}",
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold, fontWeight = FontWeight.Bold,
@ -143,6 +153,19 @@ fun MailViewScreen(navController: NavHostController, mailId: Int,
.padding(bottom = 8.dp), .padding(bottom = 8.dp),
verticalArrangement = Arrangement.Center verticalArrangement = Arrangement.Center
) { ) {
Box(
modifier = Modifier
.size(512.dp),
contentAlignment = Alignment.Center
) {
Image(
bitmap = photo.value.asImageBitmap(),
contentDescription = "Background Image",
contentScale = ContentScale.Crop,
modifier = Modifier
.size(512.dp)
.blur(12.dp),
colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0f) }))
Image( Image(
bitmap = photo.value.asImageBitmap(), bitmap = photo.value.asImageBitmap(),
contentDescription = "editplaceholder", contentDescription = "editplaceholder",
@ -150,12 +173,8 @@ fun MailViewScreen(navController: NavHostController, mailId: Int,
modifier = Modifier modifier = Modifier
.padding(8.dp) .padding(8.dp)
.clip(CircleShape) .clip(CircleShape)
.size(384.dp) .size(384.dp))
.border( }
width = 2.dp,
color = MaterialTheme.colorScheme.onPrimary,
)
.align(Alignment.CenterHorizontally))
Text(text = "Автор: ${userName.value}", Text(text = "Автор: ${userName.value}",
fontSize = 20.sp, fontSize = 20.sp,
fontWeight = FontWeight.Bold, fontWeight = FontWeight.Bold,