https://kotlinlang.org logo
#compose-android
Title
# compose-android
f

Flammer99

04/01/2024, 3:45 PM
hi everyone i have a problem, so i m trying to build a fitnessapp, tho i ve a problem with the ''edit'' button.
🧵 3
s

Stylianos Gakis

04/01/2024, 3:48 PM
Hey, this is a humongous message. Do you mind keeping only the text at the original message and move the entire code snippet into the thread? See this https://kotlinlang.slack.com/archives/CJLTWPH7S/p1616265877303000 for reference
f

Flammer99

04/01/2024, 3:50 PM
@Composable
fun edit( item: GymContents, onDismiss: () -> Unit, viewModel: GymViewModel ) { var name by remember { mutableStateOf(item.name) } var reps by remember { mutableStateOf(item.reps) } var sets by remember { mutableStateOf(item.sets) } var lift by remember { mutableStateOf(item.lift) } AlertDialog( onDismissRequest = onDismiss, confirmButton = { Row( modifier = Modifier .fillMaxWidth() .padding(8.dp), horizontalArrangement = Arrangement.SpaceBetween ) { Button(onClick = { if (name.isNotBlank()) { // Update the item with the edited values item.name = name item.reps = reps item.sets = sets item.lift = lift onDismiss() } }) { Text("Save") } Button(onClick = onDismiss) { Text("Cancel") } } }, text = { Column { OutlinedTextField( label = { Text("Exercise Name") }, value = name, onValueChange = { name = it }, singleLine = true, ) Row( verticalAlignment = Alignment.CenterVertically, modifier = Modifier .padding(15.dp) .height(50.dp) ) { Button( contentPadding = PaddingValues(), modifier = Modifier .size(width = 35.dp, height = 35.dp) .defaultMinSize(minWidth = 1.dp, minHeight = 1.dp), onClick = { sets-- }, enabled = sets > 0 ) { Text(text = "-") } Text(text = "$sets", modifier = Modifier.padding(8.dp)) Button( contentPadding = PaddingValues(), onClick = { sets++ }, modifier = Modifier .size(width = 35.dp, height = 35.dp) .defaultMinSize(minWidth = 1.dp, minHeight = 1.dp), ) { Text(text = "+") } } OutlinedTextField( value = reps.toString(), keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), onValueChange = { reps = it.toIntOrNull() ?: 0 }, label = { Text("Rep") }, singleLine = true, modifier = Modifier .padding(8.dp) .width(60.dp) .height(55.dp) ) OutlinedTextField( value = lift.toString(), keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), onValueChange = { lift = it.toIntOrNull() ?: 0 }, label = { Text("Lift") }, singleLine = true, modifier = Modifier .padding(8.dp) .width(80.dp) .height(55.dp) ) } } ) }class GymViewModel : ViewModel() { private var _upisEditing :MutableStateFlow<Boolean> = MutableStateFlow(false) var isEditing: MutableStateFlow<Boolean> =_upisEditing fun startEditing() { _upisEditing.value = true } fun endEditing() { _upisEditing.value = false } }@Composable fun all( viewModel: GymViewModel =androidx.lifecycle.viewmodel.compose.viewModel() ){ var showDialog by remember { mutableStateOf(false) } var Items by remember { mutableStateOf(listOf<GymContents>()) } var exName by remember { mutableStateOf("") } var sets by remember { mutableStateOf(0) } var reps by remember { mutableStateOf(0) } var lift by remember { mutableStateOf(0) } val upisEditing by viewModel.isEditing.collectAsState() Column( modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center ) { Button( onClick = { showDialog = true }, modifier = Modifier.align(Alignment.CenterHorizontally) ) { Text("Add Item") } LazyColumn( modifier = Modifier .fillMaxSize() .padding(16.dp) ) { items(Items) { item -> list( item = item, onEditClick = { viewModel.startEditing()}, onDeleteClick = { Items = Items.filter { it.id != item.id } }, viewModel = viewModel ) } } } if (showDialog) { AlertDialog( onDismissRequest = { showDialog = false }, confirmButton = { Row( modifier = Modifier .fillMaxWidth() .padding(8.dp), horizontalArrangement = Arrangement.SpaceBetween ) { Button(onClick = { if (exName.isNotBlank()) { val newItem = GymContents( name = exName, reps = reps, sets = sets, id = Items.size + 1, lift = lift ) Items = Items + newItem showDialog = false exName = "" reps = 0 sets = 0 lift = 0 } }) { Text("Add") } Button(onClick = { showDialog = false }) { Text("Cancel") } } }, text = { Column { OutlinedTextField( label = { Text("Exercise Name") }, value = exName, onValueChange = { exName = it }, singleLine = true, ) Row { Row( verticalAlignment = Alignment.CenterVertically, modifier = Modifier .padding(15.dp) .height(50.dp) ) { Button( contentPadding = PaddingValues(), modifier = Modifier .size(width = 35.dp, height = 35.dp) .defaultMinSize(minWidth = 1.dp, minHeight = 1.dp), onClick = { sets-- }, enabled = sets > 0 ) { Text(text = "-") } Text(text = "$sets", modifier = Modifier.padding(8.dp)) Button( contentPadding = PaddingValues(), onClick = { sets++ }, modifier = Modifier .size(width = 35.dp, height = 35.dp) .defaultMinSize(minWidth = 1.dp, minHeight = 1.dp), ) { Text(text = "+") } } OutlinedTextField( value = if (reps == 0) "" else reps.toString(), keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), onValueChange = { reps = it.toIntOrNull() ?: 0 }, label = { Text("Rep") }, singleLine = true, modifier = Modifier .padding(8.dp) .width(60.dp) .height(55.dp) ) OutlinedTextField( value = if (lift == 0) "" else lift.toString(), keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), onValueChange = { lift = it.toIntOrNull() ?: 0 }, label = { Text("Lift") }, singleLine = true, modifier = Modifier .padding(8.dp) .width(80.dp) .height(55.dp) ) } } } ) } }
👍 1
Copy code
main function:@Composable
fun all(
    viewModel: GymViewModel =androidx.lifecycle.viewmodel.compose.viewModel()
){
    var showDialog by remember { mutableStateOf(false) }
    var Items by remember { mutableStateOf(listOf<GymContents>()) }
    var exName by remember { mutableStateOf("") }
    var sets by remember { mutableStateOf(0) }
    var reps by remember { mutableStateOf(0) }
    var lift by remember { mutableStateOf(0) }
    val upisEditing by viewModel.isEditing.collectAsState()

    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center
    ) {
        Button(
            onClick = { showDialog = true },
            modifier = Modifier.align(Alignment.CenterHorizontally)
        ) {
            Text("Add Item")
        }
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp)
        ) {
            items(Items) { item ->
                list(
                    item = item,
                    onEditClick = { viewModel.startEditing()},
                    onDeleteClick = { Items = Items.filter { it.id != item.id } },
                    viewModel = viewModel
                )
            }
        }
    }

    if (showDialog) {
        AlertDialog(
            onDismissRequest = { showDialog = false },
            confirmButton = {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(8.dp),
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Button(onClick = {
                        if (exName.isNotBlank()) {
                            val newItem = GymContents(
                                name = exName,
                                reps = reps,
                                sets = sets,
                                id = Items.size + 1,
                                lift = lift
                            )
                            Items = Items + newItem
                            showDialog = false
                            exName = ""
                            reps = 0
                            sets = 0
                            lift = 0
                        }

                    }) {
                        Text("Add")
                    }
                    Button(onClick = { showDialog = false }) {
                        Text("Cancel")
                    }
                }

            },
            text = {
                Column {
                    OutlinedTextField(
                        label = { Text("Exercise Name") },
                        value = exName,
                        onValueChange = { exName = it },
                        singleLine = true,
                    )

                    Row {
                        Row(
                            verticalAlignment = Alignment.CenterVertically,
                            modifier = Modifier
                                .padding(15.dp)
                                .height(50.dp)
                        ) {
                            Button(
                                contentPadding = PaddingValues(),
                                modifier = Modifier
                                    .size(width = 35.dp, height = 35.dp)
                                    .defaultMinSize(minWidth = 1.dp, minHeight = 1.dp),
                                onClick = { sets-- },
                                enabled = sets > 0
                            ) {
                                Text(text = "-")
                            }
                            Text(text = "$sets", modifier = Modifier.padding(8.dp))
                            Button(
                                contentPadding = PaddingValues(),
                                onClick = { sets++ },
                                modifier = Modifier
                                    .size(width = 35.dp, height = 35.dp)
                                    .defaultMinSize(minWidth = 1.dp, minHeight = 1.dp),
                            ) {
                                Text(text = "+")
                            }
                        }
                        OutlinedTextField(
                            value = if (reps == 0) "" else reps.toString(),
                            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                            onValueChange = { reps = it.toIntOrNull() ?: 0 },
                            label = { Text("Rep") },
                            singleLine = true,
                            modifier = Modifier
                                .padding(8.dp)
                                .width(60.dp)
                                .height(55.dp)

                        )
                        OutlinedTextField(
                            value = if (lift == 0) "" else lift.toString(),
                            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                            onValueChange = { lift = it.toIntOrNull() ?: 0 },
                            label = { Text("Lift") },
                            singleLine = true,
                            modifier = Modifier
                                .padding(8.dp)
                                .width(80.dp)
                                .height(55.dp)

                        )

                    }
                }
            }

        )

    }
},edit funcxtion:@Composable
fun edit(
    item: GymContents,
    onDismiss: () -> Unit,
    viewModel: GymViewModel
) {
    var name by remember { mutableStateOf(item.name) }
    var reps by remember { mutableStateOf(item.reps) }
    var sets by remember { mutableStateOf(item.sets) }
    var lift by remember { mutableStateOf(item.lift) }

    AlertDialog(
        onDismissRequest = onDismiss,
        confirmButton = {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(8.dp),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Button(onClick = {
                    if (name.isNotBlank()) {
                        // Update the item with the edited values
                        item.name = name
                        item.reps = reps
                        item.sets = sets
                        item.lift = lift
                        onDismiss()
                    }
                }) {
                    Text("Save")
                }
                Button(onClick = onDismiss) {
                    Text("Cancel")
                }
            }
        },
        text = {
            Column {
                OutlinedTextField(
                    label = { Text("Exercise Name") },
                    value = name,
                    onValueChange = { name = it },
                    singleLine = true,
                )

                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier
                        .padding(15.dp)
                        .height(50.dp)
                ) {
                    Button(
                        contentPadding = PaddingValues(),
                        modifier = Modifier
                            .size(width = 35.dp, height = 35.dp)
                            .defaultMinSize(minWidth = 1.dp, minHeight = 1.dp),
                        onClick = { sets-- },
                        enabled = sets > 0
                    ) {
                        Text(text = "-")
                    }
                    Text(text = "$sets", modifier = Modifier.padding(8.dp))
                    Button(
                        contentPadding = PaddingValues(),
                        onClick = { sets++ },
                        modifier = Modifier
                            .size(width = 35.dp, height = 35.dp)
                            .defaultMinSize(minWidth = 1.dp, minHeight = 1.dp),
                    ) {
                        Text(text = "+")
                    }
                }
                OutlinedTextField(
                    value = reps.toString(),
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                    onValueChange = { reps = it.toIntOrNull() ?: 0 },
                    label = { Text("Rep") },
                    singleLine = true,
                    modifier = Modifier
                        .padding(8.dp)
                        .width(60.dp)
                        .height(55.dp)
                )
                OutlinedTextField(
                    value = lift.toString(),
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                    onValueChange = { lift = it.toIntOrNull() ?: 0 },
                    label = { Text("Lift") },
                    singleLine = true,
                    modifier = Modifier
                        .padding(8.dp)
                        .width(80.dp)
                        .height(55.dp)
                )
            }
        }
    )
} viewmodel: class GymViewModel : ViewModel() {
    private var _upisEditing :MutableStateFlow<Boolean> = MutableStateFlow(false)
   var isEditing: MutableStateFlow<Boolean> =_upisEditing
    fun startEditing() {
        _upisEditing.value = true
    }

    fun endEditing() {
        _upisEditing.value = false
    }

},list funxtion: @Composable
fun list(
    item: GymContents,
    onEditClick: () -> Unit,
    onDeleteClick: () -> Unit,
    viewModel: GymViewModel
) {
    val isEditing by viewModel.isEditing.collectAsState()
    Row(
        modifier = Modifier
            .padding(8.dp)
            .fillMaxWidth()
            .border(
                border = BorderStroke(2.dp, Color.Black),
                shape = RoundedCornerShape(20)
            )
    ) {
        Text(text = item.name, modifier = Modifier.padding(8.dp))
        Text(
            text = "Sets: ${item.sets}",
            modifier = Modifier.padding(8.dp)
        )
        Text(text = "Reps: ${item.reps}")
        Text(text = "Lift: ${item.lift}")
        Row(modifier = Modifier.padding(8.dp)) {
            IconButton(onClick = onEditClick) {
                Icon(imageVector = Icons.Default.Edit, contentDescription = null)
            }

            IconButton(onClick = onDeleteClick) {
                Icon(imageVector = Icons.Default.Delete, contentDescription = null)
            }

        }
    }}
k

Kirill Grouchnikov

04/01/2024, 5:01 PM
You’re probably not going to get a lot of activity here since your code is too long to read without an IDE, and can’t be directly copy-pasted into the IDE because of the missing dependencies. Cut out everything that is not related to the issue you have until you have an absolute smallest reproducer that you can make. And also - you need to say what the problem is. Is it not compiling? Crashing at runtime? Not doing what you want it to do?
Help people help you by asking a well formed question
f

Flammer99

04/01/2024, 5:26 PM
i m not able to make the edit button works
ok fixed