Advertisement
QwertyAvatar

kod profilu

Nov 5th, 2024
135
0
132 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 6.99 KB | Software | 0 0
  1. package pl.pollub.android.masterand
  2.  
  3. import android.net.Uri
  4. import android.os.Bundle
  5. import androidx.activity.ComponentActivity
  6. import androidx.activity.compose.rememberLauncherForActivityResult
  7. import androidx.activity.compose.setContent
  8. import androidx.activity.enableEdgeToEdge
  9. import androidx.activity.result.contract.ActivityResultContracts
  10. import androidx.compose.foundation.Image
  11. import androidx.compose.foundation.background
  12. import androidx.compose.foundation.layout.*
  13. import androidx.compose.foundation.rememberScrollState
  14. import androidx.compose.foundation.shape.CircleShape
  15. import androidx.compose.foundation.verticalScroll
  16. import androidx.compose.material3.Icon
  17. import androidx.compose.material3.IconButton
  18. import androidx.compose.material3.MaterialTheme
  19. import androidx.compose.material3.OutlinedTextField
  20. import androidx.compose.material3.Scaffold
  21. import androidx.compose.material3.Text
  22. import androidx.compose.runtime.*
  23. import androidx.compose.runtime.saveable.rememberSaveable
  24. import androidx.compose.ui.Alignment
  25. import androidx.compose.ui.Modifier
  26. import androidx.compose.ui.draw.clip
  27. import androidx.compose.ui.graphics.Color
  28. import androidx.compose.ui.layout.ContentScale
  29. import androidx.compose.ui.res.painterResource
  30. import androidx.compose.ui.tooling.preview.Preview
  31. import androidx.compose.ui.unit.dp
  32. import coil.compose.rememberAsyncImagePainter
  33. import pl.pollub.android.masterand.ui.theme.MasterAndTheme
  34.  
  35. class MainActivity : ComponentActivity() {
  36.     override fun onCreate(savedInstanceState: Bundle?) {
  37.         super.onCreate(savedInstanceState)
  38.         enableEdgeToEdge()
  39.         setContent {
  40.             MasterAndTheme {
  41.                 ProfileScreenInitial()
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. @Composable
  48. fun ProfileScreenInitial() {
  49.     val name = rememberSaveable { mutableStateOf("") }
  50.     val email = rememberSaveable { mutableStateOf("") }
  51.     val numberColors = rememberSaveable { mutableStateOf("") }
  52.  
  53.     var selectedImageUri by remember { mutableStateOf<Uri?>(null) }
  54.  
  55.     val nameError = remember { mutableStateOf<String?>(null) }
  56.     val emailError = remember { mutableStateOf<String?>(null) }
  57.     val numberColorsError = remember { mutableStateOf<String?>(null) }
  58.  
  59.     val imagePickerLauncher = rememberLauncherForActivityResult(
  60.         contract = ActivityResultContracts.GetContent()
  61.     ) { uri: Uri? ->
  62.         selectedImageUri = uri
  63.     }
  64.  
  65.     Column(
  66.         modifier = Modifier
  67.             .fillMaxSize()
  68.             .padding(16.dp)
  69.             .background(MaterialTheme.colorScheme.background)
  70.             .verticalScroll(rememberScrollState()),
  71.         verticalArrangement = Arrangement.Center,
  72.         horizontalAlignment = Alignment.CenterHorizontally
  73.     ) {
  74.         Text(
  75.             text = "MasterAnd",
  76.             style = MaterialTheme.typography.displayLarge,
  77.             modifier = Modifier.padding(bottom = 48.dp)
  78.         )
  79.  
  80.         Box(contentAlignment = Alignment.TopEnd) {
  81.             if (selectedImageUri != null) {
  82.                 Image(
  83.                     painter = rememberAsyncImagePainter(selectedImageUri),
  84.                     contentDescription = "Profile photo",
  85.                     modifier = Modifier
  86.                         .size(100.dp)
  87.                         .clip(CircleShape),
  88.                     contentScale = ContentScale.Crop
  89.                 )
  90.             } else {
  91.                 Image(
  92.                     painter = painterResource(id = R.drawable.ic_baseline_question_mark_24),
  93.                     contentDescription = "Default profile photo",
  94.                     modifier = Modifier
  95.                         .size(100.dp)
  96.                         .clip(CircleShape),
  97.                     contentScale = ContentScale.Crop
  98.                 )
  99.             }
  100.  
  101.             IconButton(
  102.                 onClick = { imagePickerLauncher.launch("image/*") },
  103.                 modifier = Modifier
  104.                     .size(36.dp)
  105.                     .align(Alignment.TopEnd)
  106.                     .padding(4.dp)
  107.             ) {
  108.                 Icon(
  109.                     painter = painterResource(id = R.drawable.ic_baseline_photo_library_24),
  110.                     contentDescription = "Select image",
  111.                     tint = Color.Gray
  112.                 )
  113.             }
  114.         }
  115.  
  116.         Spacer(modifier = Modifier.height(16.dp))
  117.  
  118.         // Imię
  119.         OutlinedTextField(
  120.             modifier = Modifier.fillMaxWidth(),
  121.             value = name.value,
  122.             onValueChange = {
  123.                 name.value = it
  124.                 nameError.value = if (it.isEmpty()) "Name can't be empty." else null
  125.             },
  126.             label = { Text("Enter name") },
  127.             singleLine = true,
  128.             isError = nameError.value != null
  129.         )
  130.         nameError.value?.let {
  131.             Row(verticalAlignment = Alignment.CenterVertically) {
  132.                 Icon(painter = painterResource(id = R.drawable.ic_baseline_error_24), contentDescription = "Error", tint = Color.Red)
  133.                 Text(text = it, color = Color.Red, modifier = Modifier.padding(start = 4.dp))
  134.             }
  135.         }
  136.  
  137.         // Email
  138.         OutlinedTextField(
  139.             modifier = Modifier.fillMaxWidth(),
  140.             value = email.value,
  141.             onValueChange = {
  142.                 email.value = it
  143.                 emailError.value = if (!android.util.Patterns.EMAIL_ADDRESS.matcher(it).matches()) "Pass valid email." else null
  144.             },
  145.             label = { Text("Enter email") },
  146.             singleLine = true,
  147.             isError = emailError.value != null
  148.         )
  149.         emailError.value?.let {
  150.             Row(verticalAlignment = Alignment.CenterVertically) {
  151.                 Icon(painter = painterResource(id = R.drawable.ic_baseline_error_24), contentDescription = "Error", tint = Color.Red)
  152.                 Text(text = it, color = Color.Red, modifier = Modifier.padding(start = 4.dp))
  153.             }
  154.         }
  155.  
  156.         // Liczba kolorów
  157.         OutlinedTextField(
  158.             modifier = Modifier.fillMaxWidth(),
  159.             value = numberColors.value,
  160.             onValueChange = {
  161.                 numberColors.value = it
  162.                 numberColorsError.value = when {
  163.                     it.isEmpty() -> null
  164.                     it.toIntOrNull() !in 5..10 -> "Enter number between 5 & 10."
  165.                     else -> null
  166.                 }
  167.             },
  168.             label = { Text("Enter number of colors") },
  169.             singleLine = true,
  170.             isError = numberColorsError.value != null
  171.         )
  172.         numberColorsError.value?.let {
  173.             Row(verticalAlignment = Alignment.CenterVertically) {
  174.                 Icon(painter = painterResource(id = R.drawable.ic_baseline_error_24), contentDescription = "Error", tint = Color.Red)
  175.                 Text(text = it, color = Color.Red, modifier = Modifier.padding(start = 4.dp))
  176.             }
  177.         }
  178.     }
  179. }
  180.  
  181. @Preview
  182. @Composable
  183. fun ProfileScreenInitialPreview() {
  184.     MasterAndTheme {
  185.         ProfileScreenInitial()
  186.     }
  187. }
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement