mirror of https://github.com/tasks/tasks
Add server selector
parent
0cfb27df22
commit
e8f231b288
@ -0,0 +1,61 @@
|
||||
package org.tasks.compose
|
||||
|
||||
import android.content.res.Configuration
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material.MaterialTheme
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.google.android.material.composethemeadapter.MdcTheme
|
||||
import org.tasks.R
|
||||
|
||||
@Composable
|
||||
fun ServerSelector(selected: Int, onSelected: (Int) -> Unit) {
|
||||
var expanded by rememberSaveable { mutableStateOf(false) }
|
||||
Column(modifier = Modifier
|
||||
.padding(16.dp)
|
||||
.clickable { expanded = !expanded }) {
|
||||
Text(
|
||||
text = stringResource(id = R.string.caldav_server_type),
|
||||
style = MaterialTheme.typography.caption.copy(
|
||||
color = colorResource(id = R.color.text_secondary)
|
||||
),
|
||||
)
|
||||
Spinner(
|
||||
options = LocalContext.current.resources.getStringArray(R.array.caldav_servers)
|
||||
.toList(),
|
||||
values = LocalContext.current.resources.getIntArray(R.array.caldav_server_values)
|
||||
.toList(),
|
||||
selected = selected,
|
||||
expanded = expanded,
|
||||
onSelected = {
|
||||
expanded = false
|
||||
onSelected(it)
|
||||
},
|
||||
setExpanded = { expanded = it },
|
||||
modifier = Modifier
|
||||
.padding(vertical = 6.dp)
|
||||
.fillMaxWidth()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Preview(showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
|
||||
@Composable
|
||||
fun ServerSelectorPreview() =
|
||||
MdcTheme {
|
||||
ServerSelector(1) {}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package org.tasks.compose
|
||||
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.material.DropdownMenu
|
||||
import androidx.compose.material.DropdownMenuItem
|
||||
import androidx.compose.material.Icon
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.ArrowDropDown
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
|
||||
@Composable
|
||||
fun <T> Spinner(
|
||||
options: List<String>,
|
||||
values: List<T>,
|
||||
selected: T,
|
||||
expanded: Boolean,
|
||||
modifier: Modifier = Modifier,
|
||||
onSelected: (T) -> Unit,
|
||||
setExpanded: (Boolean) -> Unit,
|
||||
) {
|
||||
val selectedIndex = values.indexOf(selected)
|
||||
val selectedOption = options[selectedIndex]
|
||||
Row(modifier = modifier) {
|
||||
Text(text = selectedOption)
|
||||
Icon(imageVector = Icons.Outlined.ArrowDropDown, contentDescription = "")
|
||||
DropdownMenu(expanded = expanded, onDismissRequest = {
|
||||
setExpanded(false)
|
||||
}) {
|
||||
options.forEach {
|
||||
DropdownMenuItem(onClick = {
|
||||
onSelected(values[options.indexOf(it)])
|
||||
}) {
|
||||
Text(text = it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue