Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.pollub.android.masterand
- import android.net.Uri
- import android.os.Bundle
- import androidx.activity.ComponentActivity
- import androidx.activity.compose.rememberLauncherForActivityResult
- import androidx.activity.compose.setContent
- import androidx.activity.enableEdgeToEdge
- import androidx.activity.result.contract.ActivityResultContracts
- import androidx.compose.foundation.Image
- import androidx.compose.foundation.background
- import androidx.compose.foundation.layout.*
- import androidx.compose.foundation.rememberScrollState
- import androidx.compose.foundation.shape.CircleShape
- import androidx.compose.foundation.verticalScroll
- import androidx.compose.material3.Icon
- import androidx.compose.material3.IconButton
- import androidx.compose.material3.MaterialTheme
- import androidx.compose.material3.OutlinedTextField
- import androidx.compose.material3.Scaffold
- import androidx.compose.material3.Text
- import androidx.compose.runtime.*
- import androidx.compose.runtime.saveable.rememberSaveable
- 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.res.painterResource
- import androidx.compose.ui.tooling.preview.Preview
- import androidx.compose.ui.unit.dp
- import coil.compose.rememberAsyncImagePainter
- import pl.pollub.android.masterand.ui.theme.MasterAndTheme
- class MainActivity : ComponentActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- enableEdgeToEdge()
- setContent {
- MasterAndTheme {
- ProfileScreenInitial()
- }
- }
- }
- }
- @Composable
- fun ProfileScreenInitial() {
- val name = rememberSaveable { mutableStateOf("") }
- val email = rememberSaveable { mutableStateOf("") }
- val numberColors = rememberSaveable { mutableStateOf("") }
- var selectedImageUri by remember { mutableStateOf<Uri?>(null) }
- val nameError = remember { mutableStateOf<String?>(null) }
- val emailError = remember { mutableStateOf<String?>(null) }
- val numberColorsError = remember { mutableStateOf<String?>(null) }
- val imagePickerLauncher = rememberLauncherForActivityResult(
- contract = ActivityResultContracts.GetContent()
- ) { uri: Uri? ->
- selectedImageUri = uri
- }
- Column(
- modifier = Modifier
- .fillMaxSize()
- .padding(16.dp)
- .background(MaterialTheme.colorScheme.background)
- .verticalScroll(rememberScrollState()),
- verticalArrangement = Arrangement.Center,
- horizontalAlignment = Alignment.CenterHorizontally
- ) {
- Text(
- text = "MasterAnd",
- style = MaterialTheme.typography.displayLarge,
- modifier = Modifier.padding(bottom = 48.dp)
- )
- Box(contentAlignment = Alignment.TopEnd) {
- if (selectedImageUri != null) {
- Image(
- painter = rememberAsyncImagePainter(selectedImageUri),
- contentDescription = "Profile photo",
- modifier = Modifier
- .size(100.dp)
- .clip(CircleShape),
- contentScale = ContentScale.Crop
- )
- } else {
- Image(
- painter = painterResource(id = R.drawable.ic_baseline_question_mark_24),
- contentDescription = "Default profile photo",
- modifier = Modifier
- .size(100.dp)
- .clip(CircleShape),
- contentScale = ContentScale.Crop
- )
- }
- IconButton(
- onClick = { imagePickerLauncher.launch("image/*") },
- modifier = Modifier
- .size(36.dp)
- .align(Alignment.TopEnd)
- .padding(4.dp)
- ) {
- Icon(
- painter = painterResource(id = R.drawable.ic_baseline_photo_library_24),
- contentDescription = "Select image",
- tint = Color.Gray
- )
- }
- }
- Spacer(modifier = Modifier.height(16.dp))
- // Imię
- OutlinedTextField(
- modifier = Modifier.fillMaxWidth(),
- value = name.value,
- onValueChange = {
- name.value = it
- nameError.value = if (it.isEmpty()) "Name can't be empty." else null
- },
- label = { Text("Enter name") },
- singleLine = true,
- isError = nameError.value != null
- )
- nameError.value?.let {
- Row(verticalAlignment = Alignment.CenterVertically) {
- Icon(painter = painterResource(id = R.drawable.ic_baseline_error_24), contentDescription = "Error", tint = Color.Red)
- Text(text = it, color = Color.Red, modifier = Modifier.padding(start = 4.dp))
- }
- }
- // Email
- OutlinedTextField(
- modifier = Modifier.fillMaxWidth(),
- value = email.value,
- onValueChange = {
- email.value = it
- emailError.value = if (!android.util.Patterns.EMAIL_ADDRESS.matcher(it).matches()) "Pass valid email." else null
- },
- label = { Text("Enter email") },
- singleLine = true,
- isError = emailError.value != null
- )
- emailError.value?.let {
- Row(verticalAlignment = Alignment.CenterVertically) {
- Icon(painter = painterResource(id = R.drawable.ic_baseline_error_24), contentDescription = "Error", tint = Color.Red)
- Text(text = it, color = Color.Red, modifier = Modifier.padding(start = 4.dp))
- }
- }
- // Liczba kolorów
- OutlinedTextField(
- modifier = Modifier.fillMaxWidth(),
- value = numberColors.value,
- onValueChange = {
- numberColors.value = it
- numberColorsError.value = when {
- it.isEmpty() -> null
- it.toIntOrNull() !in 5..10 -> "Enter number between 5 & 10."
- else -> null
- }
- },
- label = { Text("Enter number of colors") },
- singleLine = true,
- isError = numberColorsError.value != null
- )
- numberColorsError.value?.let {
- Row(verticalAlignment = Alignment.CenterVertically) {
- Icon(painter = painterResource(id = R.drawable.ic_baseline_error_24), contentDescription = "Error", tint = Color.Red)
- Text(text = it, color = Color.Red, modifier = Modifier.padding(start = 4.dp))
- }
- }
- }
- }
- @Preview
- @Composable
- fun ProfileScreenInitialPreview() {
- MasterAndTheme {
- ProfileScreenInitial()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement