From 153661a5f0386b22bb2143fda744930939f7c11d Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Wed, 27 Dec 2023 20:10:22 +0400 Subject: [PATCH] Yes, i'am the best --- .../com/example/mobileapp/MainActivity.kt | 7 +- .../mobileapp/api/model/RemoteConverters.kt | 26 ++++++- .../mobileapp/components/ListContent.kt | 14 ++++ .../mobileapp/screens/Authorization.kt | 9 ++- .../example/mobileapp/screens/EditScreens.kt | 77 +++++++++++++------ .../example/mobileapp/screens/Registration.kt | 9 ++- .../example/mobileapp/screens/ViewScreens.kt | 75 +++++++++++------- 7 files changed, 159 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/com/example/mobileapp/MainActivity.kt b/app/src/main/java/com/example/mobileapp/MainActivity.kt index d84ae90..37e8de0 100644 --- a/app/src/main/java/com/example/mobileapp/MainActivity.kt +++ b/app/src/main/java/com/example/mobileapp/MainActivity.kt @@ -6,6 +6,7 @@ import androidx.activity.compose.setContent import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface +import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.Modifier import androidx.navigation.compose.rememberNavController import com.example.mobileapp.components.NavBar @@ -31,14 +32,14 @@ class MainActivity : ComponentActivity() { } class GlobalUser private constructor() { - private var user: User? = null + private var user = mutableStateOf(null) fun setUser(user: User?) { - this.user = user + this.user.value = user } fun getUser(): User? { - return user + return user.value } companion object { diff --git a/app/src/main/java/com/example/mobileapp/api/model/RemoteConverters.kt b/app/src/main/java/com/example/mobileapp/api/model/RemoteConverters.kt index 016ad12..cdc6f2d 100644 --- a/app/src/main/java/com/example/mobileapp/api/model/RemoteConverters.kt +++ b/app/src/main/java/com/example/mobileapp/api/model/RemoteConverters.kt @@ -8,10 +8,15 @@ import java.io.ByteArrayOutputStream class RemoteConverters { companion object { private const val CHARSET_UTF8 = "UTF-8" + private const val MAX_IMAGE_SIZE = 1024 // Размер в килобайтах, который вы хотите использовать fun fromBitmap(bitmap: Bitmap): String { 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() return Base64.encodeToString(byteArray, Base64.NO_WRAP) } @@ -20,5 +25,22 @@ class RemoteConverters { val byteArray = Base64.decode(base64String, Base64.NO_WRAP) 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 + } } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/example/mobileapp/components/ListContent.kt b/app/src/main/java/com/example/mobileapp/components/ListContent.kt index d6cf1f3..8623a57 100644 --- a/app/src/main/java/com/example/mobileapp/components/ListContent.kt +++ b/app/src/main/java/com/example/mobileapp/components/ListContent.kt @@ -25,6 +25,7 @@ import androidx.compose.material3.Button import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults +import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.Text import androidx.compose.runtime.Composable 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() + } +} diff --git a/app/src/main/java/com/example/mobileapp/screens/Authorization.kt b/app/src/main/java/com/example/mobileapp/screens/Authorization.kt index 8d3b67c..ca044dc 100644 --- a/app/src/main/java/com/example/mobileapp/screens/Authorization.kt +++ b/app/src/main/java/com/example/mobileapp/screens/Authorization.kt @@ -21,6 +21,7 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavHostController +import com.example.mobileapp.GlobalUser import com.example.mobileapp.R import com.example.mobileapp.components.ActiveButton import com.example.mobileapp.components.NavigationButton @@ -40,7 +41,12 @@ fun Authorization(navController: NavHostController, userViewModel: UserViewModel = viewModel( 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 password = remember { mutableStateOf("") } @@ -75,7 +81,6 @@ fun Authorization(navController: NavHostController, email = String() ) ) - navController.navigate("main") } }) NavigationButton(navController = navController, destination = "registration", label = "Регистрация", diff --git a/app/src/main/java/com/example/mobileapp/screens/EditScreens.kt b/app/src/main/java/com/example/mobileapp/screens/EditScreens.kt index 00d7807..351dbff 100644 --- a/app/src/main/java/com/example/mobileapp/screens/EditScreens.kt +++ b/app/src/main/java/com/example/mobileapp/screens/EditScreens.kt @@ -8,14 +8,19 @@ import android.os.Build import android.provider.MediaStore import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.Image import androidx.compose.foundation.border import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size +import androidx.compose.foundation.rememberScrollState 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.runtime.Composable import androidx.compose.runtime.LaunchedEffect @@ -25,8 +30,11 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.blur import androidx.compose.ui.draw.clip 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.layout.ContentScale import androidx.compose.ui.platform.LocalContext @@ -90,17 +98,32 @@ fun EditStoryScreen(navController: NavHostController, storyId: Int? = null, Column( modifier = Modifier .fillMaxSize() - .padding(bottom = 8.dp), - verticalArrangement = Arrangement.Bottom + .padding(bottom = 8.dp) + .verticalScroll(rememberScrollState()), + verticalArrangement = Arrangement.Bottom, ) { - Image( - bitmap = cover.value.asImageBitmap(), - contentDescription = "editplaceholder", - contentScale = ContentScale.Crop, + Box( modifier = Modifier - .size(384.dp) - .padding(8.dp) - .align(Alignment.CenterHorizontally)) + .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( + bitmap = cover.value.asImageBitmap(), + contentDescription = "cover", + contentScale = ContentScale.Crop, + modifier = Modifier + .padding(8.dp) + .size(384.dp) + .clip(RoundedCornerShape(16.dp))) + } ActiveButton(label = "Выбрать обложку", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = { launcher.launch("image/*") }) @@ -221,22 +244,32 @@ fun EditUserScreen(navController: NavHostController, Column( modifier = Modifier .fillMaxSize() - .padding(bottom = 8.dp), + .padding(bottom = 8.dp) + .verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.Bottom ) { - Image( - bitmap = photo.value.asImageBitmap(), - contentDescription = "editplaceholder", - contentScale = ContentScale.Crop, + Box( modifier = Modifier - .padding(8.dp) - .clip(CircleShape) - .size(384.dp) - .border( - width = 2.dp, - color = MaterialTheme.colorScheme.onPrimary, - ) - .align(Alignment.CenterHorizontally)) + .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( + bitmap = photo.value.asImageBitmap(), + contentDescription = "editplaceholder", + contentScale = ContentScale.Crop, + modifier = Modifier + .padding(8.dp) + .clip(CircleShape) + .size(384.dp)) + } ActiveButton(label = "Выбрать фото", backgroundColor = ButtonColor1, textColor = Color.Black, onClickAction = { launcher.launch("image/*") }) diff --git a/app/src/main/java/com/example/mobileapp/screens/Registration.kt b/app/src/main/java/com/example/mobileapp/screens/Registration.kt index f4fcfc9..48c102b 100644 --- a/app/src/main/java/com/example/mobileapp/screens/Registration.kt +++ b/app/src/main/java/com/example/mobileapp/screens/Registration.kt @@ -17,6 +17,7 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavHostController +import com.example.mobileapp.GlobalUser import com.example.mobileapp.R import com.example.mobileapp.components.ActiveButton import com.example.mobileapp.components.NavigationButton @@ -33,6 +34,13 @@ fun Registration(navController: NavHostController, userViewModel: UserViewModel = viewModel( 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 email = remember { mutableStateOf("") } val password = remember { mutableStateOf("") } @@ -75,7 +83,6 @@ fun Registration(navController: NavHostController, ) ) } - navController.navigate("main") }) NavigationButton(navController = navController, destination = "authorization", label = "Назад", backgroundColor = ButtonColor1, textColor = Color.Black) diff --git a/app/src/main/java/com/example/mobileapp/screens/ViewScreens.kt b/app/src/main/java/com/example/mobileapp/screens/ViewScreens.kt index 3f69274..ca314fe 100644 --- a/app/src/main/java/com/example/mobileapp/screens/ViewScreens.kt +++ b/app/src/main/java/com/example/mobileapp/screens/ViewScreens.kt @@ -5,11 +5,13 @@ import android.graphics.BitmapFactory import androidx.compose.foundation.Image import androidx.compose.foundation.border import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable @@ -18,8 +20,11 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.blur import androidx.compose.ui.draw.clip 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.layout.ContentScale 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.database.viewmodels.MailViewModel 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.UserViewModel import com.example.mobileapp.ui.theme.ButtonColor2 @@ -53,14 +57,6 @@ fun StoryViewScreen(navController: NavHostController, storyId: Int, val description = remember { mutableStateOf("") } val postdate = remember { mutableStateOf(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) { storyId?.let { val story = storyViewModel.getStoryById(storyId) @@ -79,14 +75,28 @@ fun StoryViewScreen(navController: NavHostController, storyId: Int, .padding(bottom = 8.dp), verticalArrangement = Arrangement.Center ) { - Image( - bitmap = cover.value.asImageBitmap(), - contentDescription = "cover", - contentScale = ContentScale.Crop, + Box( modifier = Modifier - .size(512.dp) - .padding(8.dp) - .align(Alignment.CenterHorizontally)) + .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( + bitmap = cover.value.asImageBitmap(), + contentDescription = "cover", + contentScale = ContentScale.Crop, + modifier = Modifier + .padding(8.dp) + .size(384.dp) + .clip(RoundedCornerShape(16.dp))) + } Text(text = "Название: ${title.value}", fontSize = 20.sp, fontWeight = FontWeight.Bold, @@ -143,19 +153,28 @@ fun MailViewScreen(navController: NavHostController, mailId: Int, .padding(bottom = 8.dp), verticalArrangement = Arrangement.Center ) { - Image( - bitmap = photo.value.asImageBitmap(), - contentDescription = "editplaceholder", - contentScale = ContentScale.Crop, + Box( modifier = Modifier - .padding(8.dp) - .clip(CircleShape) - .size(384.dp) - .border( - width = 2.dp, - color = MaterialTheme.colorScheme.onPrimary, - ) - .align(Alignment.CenterHorizontally)) + .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( + bitmap = photo.value.asImageBitmap(), + contentDescription = "editplaceholder", + contentScale = ContentScale.Crop, + modifier = Modifier + .padding(8.dp) + .clip(CircleShape) + .size(384.dp)) + } Text(text = "Автор: ${userName.value}", fontSize = 20.sp, fontWeight = FontWeight.Bold,