|
@@ -1,45 +1,239 @@
|
|
|
package com.example.tasks.screens
|
|
|
|
|
|
+import android.content.Context
|
|
|
+import androidx.compose.foundation.Image
|
|
|
+import androidx.compose.foundation.background
|
|
|
+import androidx.compose.foundation.border
|
|
|
+import androidx.compose.foundation.clickable
|
|
|
+import androidx.compose.foundation.layout.Arrangement
|
|
|
+import androidx.compose.foundation.layout.Box
|
|
|
import androidx.compose.foundation.layout.Column
|
|
|
-import androidx.compose.foundation.shape.CircleShape
|
|
|
+import androidx.compose.foundation.layout.Row
|
|
|
+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.foundation.layout.width
|
|
|
+import androidx.compose.foundation.shape.RoundedCornerShape
|
|
|
+import androidx.compose.material.icons.Icons
|
|
|
+import androidx.compose.material.icons.filled.ExitToApp
|
|
|
import androidx.compose.material3.CircularProgressIndicator
|
|
|
+import androidx.compose.material3.Icon
|
|
|
import androidx.compose.material3.Text
|
|
|
import androidx.compose.runtime.Composable
|
|
|
-import androidx.compose.runtime.LaunchedEffect
|
|
|
import androidx.compose.runtime.getValue
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
import androidx.compose.runtime.remember
|
|
|
-import androidx.compose.runtime.setValue
|
|
|
-import com.example.tasks.components.Constants
|
|
|
-import com.example.tasks.model.User
|
|
|
+import androidx.compose.ui.Alignment
|
|
|
+import androidx.compose.ui.Modifier
|
|
|
+import androidx.compose.ui.draw.clip
|
|
|
+import androidx.compose.ui.graphics.Color
|
|
|
+import androidx.compose.ui.layout.ContentScale
|
|
|
+import androidx.compose.ui.platform.LocalContext
|
|
|
+import androidx.compose.ui.text.font.FontWeight
|
|
|
+import androidx.compose.ui.unit.dp
|
|
|
+import androidx.compose.ui.unit.sp
|
|
|
+import androidx.navigation.NavController
|
|
|
+import coil.compose.AsyncImagePainter
|
|
|
+import coil.compose.rememberAsyncImagePainter
|
|
|
+import coil.request.ImageRequest
|
|
|
+import coil.size.Size
|
|
|
+import com.example.tasks.model.UserState
|
|
|
+import com.example.tasks.navigation.Screen
|
|
|
+import com.example.tasks.viewModels.AuthViewModel
|
|
|
import com.example.tasks.viewModels.DatabaseViewModel
|
|
|
-import io.github.jan.supabase.gotrue.auth
|
|
|
-import io.github.jan.supabase.postgrest.from
|
|
|
-import kotlinx.coroutines.Dispatchers
|
|
|
-import kotlinx.coroutines.withContext
|
|
|
|
|
|
@Composable
|
|
|
-fun Profile() {
|
|
|
- val viewModel = DatabaseViewModel()
|
|
|
- //viewModel.profileData()
|
|
|
- var users by remember { mutableStateOf<List<User>>(listOf()) }
|
|
|
- LaunchedEffect(Unit) {
|
|
|
- withContext(Dispatchers.IO) {
|
|
|
- users = Constants.supabase.from("Users").select {
|
|
|
- filter {
|
|
|
- User::UID eq Constants.supabase.auth.currentUserOrNull()!!.id
|
|
|
+fun Profile(
|
|
|
+ viewModel: DatabaseViewModel,
|
|
|
+ viewModelAuth: AuthViewModel,
|
|
|
+ navController: NavController
|
|
|
+) {
|
|
|
+ val userState by viewModel.userState
|
|
|
+ val users by viewModel.users
|
|
|
+ val flag = remember { mutableStateOf(true) }
|
|
|
+ val context = LocalContext.current
|
|
|
+ if (flag.value) {
|
|
|
+ viewModel.profileData()
|
|
|
+ flag.value = false
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (users.isNotEmpty()) {
|
|
|
+ val avatar = rememberAsyncImagePainter(
|
|
|
+ model = ImageRequest.Builder(LocalContext.current).data(users.last().avatar)
|
|
|
+ .size(Size.ORIGINAL).build()
|
|
|
+ ).state
|
|
|
+ Column(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxSize()
|
|
|
+ .padding(30.dp)
|
|
|
+ .padding(top = 30.dp, bottom = 100.dp),
|
|
|
+ verticalArrangement = Arrangement.SpaceAround,
|
|
|
+ horizontalAlignment = Alignment.CenterHorizontally
|
|
|
+ ) {
|
|
|
+ Row(
|
|
|
+ modifier = Modifier.fillMaxWidth(0.7f),
|
|
|
+ horizontalArrangement = Arrangement.SpaceBetween
|
|
|
+ ) {
|
|
|
+ Text(
|
|
|
+ "Профиль",
|
|
|
+ fontSize = 30.sp,
|
|
|
+ fontWeight = FontWeight.W700,
|
|
|
+ color = Color(0xFF4C2259)
|
|
|
+ )
|
|
|
+ Icon(Icons.Filled.ExitToApp, "",
|
|
|
+ Modifier
|
|
|
+ .background(Color.Red)
|
|
|
+ .clickable {
|
|
|
+ Logout(viewModelAuth, context,navController)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ if (avatar is AsyncImagePainter.State.Success) {
|
|
|
+ Box(
|
|
|
+ modifier = Modifier
|
|
|
+ .width(100.dp)
|
|
|
+ .height(100.dp)
|
|
|
+ .clip(RoundedCornerShape(50.dp))
|
|
|
+ ) {
|
|
|
+ Image(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .height(200.dp),
|
|
|
+ painter = avatar.painter,
|
|
|
+ contentDescription = "",
|
|
|
+ contentScale = ContentScale.Crop
|
|
|
+ )
|
|
|
}
|
|
|
- }.decodeList<User>()
|
|
|
- }
|
|
|
+ }
|
|
|
+ if (avatar is AsyncImagePainter.State.Error) {
|
|
|
+ Box(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .height(200.dp),
|
|
|
+ contentAlignment = Alignment.Center
|
|
|
+ ) {
|
|
|
+ CircularProgressIndicator()
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
- if (users.size == 0) {
|
|
|
- CircularProgressIndicator()
|
|
|
- }
|
|
|
- Column {
|
|
|
- Text(text = viewModel.users.value.last().LastName)
|
|
|
- Text(text = viewModel.users.value.last().FirstName)
|
|
|
- Text(text = viewModel.users.value.last().DateOfBirth)
|
|
|
+ Box(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .height(50.dp)
|
|
|
+ .border(1.dp, Color(0xFF4C2259), RoundedCornerShape(10.dp))
|
|
|
+ .clip(RoundedCornerShape(10.dp))
|
|
|
+ .background(
|
|
|
+ Color(0xFFF2D6FA)
|
|
|
+ )
|
|
|
+ .padding(10.dp),
|
|
|
+ contentAlignment = Alignment.CenterStart
|
|
|
+ ) {
|
|
|
+ Text(users.last().LastName)
|
|
|
+ }
|
|
|
+ Box(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .height(50.dp)
|
|
|
+ .border(1.dp, Color(0xFF4C2259), RoundedCornerShape(10.dp))
|
|
|
+ .clip(RoundedCornerShape(10.dp))
|
|
|
+ .background(
|
|
|
+ Color(0xFFF2D6FA)
|
|
|
+ )
|
|
|
+ .padding(10.dp),
|
|
|
+ contentAlignment = Alignment.CenterStart
|
|
|
+ ) {
|
|
|
+ Text(users.last().FirstName)
|
|
|
+ }
|
|
|
+ if (users.last().Patronymic != "") {
|
|
|
+ Box(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .height(50.dp)
|
|
|
+ .border(1.dp, Color(0xFF4C2259), RoundedCornerShape(10.dp))
|
|
|
+ .clip(RoundedCornerShape(10.dp))
|
|
|
+ .background(
|
|
|
+ Color(0xFFF2D6FA)
|
|
|
+ )
|
|
|
+ .padding(10.dp),
|
|
|
+ contentAlignment = Alignment.CenterStart
|
|
|
+ ) {
|
|
|
+ Text(users.last().Patronymic!!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Box(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .height(50.dp)
|
|
|
+ .border(1.dp, Color(0xFF4C2259), RoundedCornerShape(10.dp))
|
|
|
+ .clip(RoundedCornerShape(10.dp))
|
|
|
+ .background(
|
|
|
+ Color(0xFFF2D6FA)
|
|
|
+ )
|
|
|
+ .padding(10.dp),
|
|
|
+ contentAlignment = Alignment.CenterStart
|
|
|
+ ) {
|
|
|
+ Text(users.last().DateOfBirth)
|
|
|
+ }
|
|
|
+ if (users.last().Gender == 1) {
|
|
|
+ Box(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .height(50.dp)
|
|
|
+ .border(1.dp, Color(0xFF4C2259), RoundedCornerShape(10.dp))
|
|
|
+ .clip(RoundedCornerShape(10.dp))
|
|
|
+ .background(
|
|
|
+ Color(0xFFF2D6FA)
|
|
|
+ )
|
|
|
+ .padding(10.dp),
|
|
|
+ contentAlignment = Alignment.CenterStart
|
|
|
+ ) {
|
|
|
+ Text("Женский")
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Box(
|
|
|
+ modifier = Modifier
|
|
|
+ .fillMaxWidth()
|
|
|
+ .height(50.dp)
|
|
|
+ .border(1.dp, Color(0xFF4C2259), RoundedCornerShape(10.dp))
|
|
|
+ .clip(RoundedCornerShape(10.dp))
|
|
|
+ .background(
|
|
|
+ Color(0xFFF2D6FA)
|
|
|
+ )
|
|
|
+ .padding(10.dp),
|
|
|
+ contentAlignment = Alignment.CenterStart
|
|
|
+ ) {
|
|
|
+ Text("Мужской")
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Column(Modifier.fillMaxSize()) {
|
|
|
+ CircularProgressIndicator()
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
+private fun Logout(viewModelAuth: AuthViewModel, context: Context, navController: NavController) {
|
|
|
+ val userState by viewModelAuth.userState
|
|
|
+ viewModelAuth.logout(context)
|
|
|
+
|
|
|
+ when (userState) {
|
|
|
+ is UserState.Loading -> {
|
|
|
+ }
|
|
|
+
|
|
|
+ is UserState.Success -> {
|
|
|
+ navController.navigate(Screen.Start.route)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ is UserState.Error -> {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|