Insert information from a dropdown menu into a database in compose

Viewed 24

So I am trying to understand how I can insert values from a dropdown menu into a database. These values are of type Integers. I have created a quick dropdown menu to illustrate(see below). When the user picks something from a dropdown menu ( lets say their age from 1..100 ), I want to be able to store this in a local database, this includes if the user changes their age as well. I have already set up a simple database. How can I achieve this ?

Dropdown menu for Age:

@Composable
fun DropDownMenuAge(
    list: List<Age>,
    modifier: Modifier = Modifier,
    contentTitle: String,
    name: String,

    ){

    var displayingText by rememberSaveable { mutableStateOf(contentTitle)}

    var textFieldSize by remember{mutableStateOf(Size.Zero)} //To assign the dropdown the same width as TextField.
    var expanded by remember { mutableStateOf(false)}

    val icon = if(expanded)
        Icons.Filled.KeyboardArrowUp
    else
        Icons.Filled.KeyboardArrowDown

    Column(horizontalAlignment = Alignment.Start) {

        OutlinedTextField(
            value = displayingText,
            onValueChange = { displayingText = it },
            textStyle = TextStyle(fontSize = 19.sp, fontWeight = FontWeight.Bold),
            modifier = modifier
                .fillMaxWidth(0.33f)
                .onGloballyPositioned { coordinates ->
                    textFieldSize = coordinates.size.toSize()
                },
            label = { Text(text = name) },
            trailingIcon = {
                Icon(icon, contentDescription = "icon",
                    modifier
                        .wrapContentSize()
                        .clickable { expanded = !expanded })

            },
            readOnly = true
        )

        DropdownMenu(
            expanded = expanded,
            onDismissRequest = { expanded = false },
            modifier = modifier
                .width(with(LocalDensity.current) { textFieldSize.width.toDp() })

        ) {
            list.forEach { conversion ->
                DropdownMenuItem(onClick = {
                    displayingText = conversion.age.toString()
                    expanded = false
                }) {

                    Text(
                        text = conversion.age.toString(),
                        fontSize = 19.sp,
                        fontWeight = FontWeight.Bold,
                    )
                }

            }
        }
    }
}

Data class of Age:

data class Age(
    val age: Int
)

Repository:

interface AgeRepository {

suspend fun getAge(): Flow<List<Age>>

}

My Function in a viewmodel that displays the values to the dropdown menu:

class AgeViewModel: Viewmodel() {

    fun getAges() = listOf(
        Age(1), Age(2), Age(3), Age(4), Age(5),
        Age(6), Age(7), Age(8), Age(9), Age(10),
                          )
}

The screen that is called within another composable to display the dropdown menu and it's values:

@Composable
fun BaseScreen(
    modifier: Modifier = Modifier,
    converterViewModel: AgeViewModel = viewModel(),
) {

    val list = converterViewModel.getAges()

    Column(
        horizontalAlignment = Alignment.Start,

    ) {

        Row {
            AgeDrop(list)

        }

    }
}
0 Answers
Related