Create drop down.

This commit is contained in:
ElEgEv 2023-12-07 23:49:07 +04:00
parent 5520140746
commit 3860540d9c
13 changed files with 142 additions and 119 deletions

Binary file not shown.

View File

@ -193,7 +193,7 @@ fun DropDownList(
.width(200.dp),
readOnly = true,
value = mySelectedOption,
onValueChange = {},
onValueChange = { mySelectedOption = it },
label = { Text("") },
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expandedOptions) },
colors = ExposedDropdownMenuDefaults.textFieldColors(),

View File

@ -38,13 +38,10 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import ru.ulstu.`is`.pmu.R
import ru.ulstu.`is`.pmu.composeui.navigation.Screen
import ru.ulstu.`is`.pmu.tank.composeui.list.TankListViewModel
import ru.ulstu.`is`.pmu.tank.database.AppDatabase
import ru.ulstu.`is`.pmu.tank.model.Tank
import ru.ulstu.`is`.pmu.tank.model.getStudents
import ru.ulstu.`is`.pmu.ui.AppViewModelProvider
import ru.ulstu.`is`.pmu.ui.theme.CustomDark
import ru.ulstu.`is`.pmu.ui.theme.CustomOrange
@ -69,12 +66,11 @@ fun TankList(
if (navController != null) {
TankList(
numbers = listIds,
listTanks = tankListUiState.tankList,
onClick = { uid: Long ->
val route = Screen.Constructor.route.replace("{id}", uid.toString())
navController.navigate(route)
}
)
listTanks = tankListUiState.tankList
) { uid: Long ->
val route = Screen.Constructor.route.replace("{id}", uid.toString())
navController.navigate(route)
}
}
}
@ -213,9 +209,8 @@ fun TankListPreview() {
) {
TankList(
numbers = listOf(1, 2, 3),
listTanks = (1..20).map { i -> Tank.getTank(i.toLong()) },
onClick = { }
)
listTanks = (1..20).map { i -> Tank.getTank(i.toLong()) }
) { }
}
}
}
@ -230,9 +225,8 @@ fun TankEmptyListPreview() {
) {
TankList(
numbers = listOf(1, 2, 3),
listTanks = listOf(),
onClick = { }
)
listTanks = listOf()
) { }
}
}
}

View File

@ -82,7 +82,7 @@ fun TankDetails.toTank(uid: Long = 0): Tank = Tank(
price = price,
image = image,
levelId = levelId,
nationId = levelId
nationId = nationId
)
fun Tank.toDetails(): TankDetails = TankDetails(

View File

@ -14,10 +14,10 @@ import ru.ulstu.`is`.pmu.tank.model.TankWithNationAndLevel
@Dao
interface TankDao {
@Query("select * from tanks")
@Query("select * from tanks GROUP BY nationId, levelId ORDER BY nationId")
fun getAll(): Flow<List<Tank>>
//получить конкретный уровень
//получить конкретный танк
@Query("select * from tanks where tanks.tankId = :uid")
fun getTankUid(uid: Long): Flow<Tank?>
@ -27,7 +27,7 @@ interface TankDao {
"LEFT JOIN tanks as t on ut.tankId = t.tankId " +
"LEFT JOIN levels as l on t.levelId = l.uid " +
"LEFT JOIN nations as n on t.nationId = n.uid " +
"WHERE ut.userId = :uid"
"WHERE ut.userId = :uid GROUP BY t.nationId, t.levelId ORDER BY t.nationId"
)
fun getUserTanks(uid: Long): Flow<List<TankWithNationAndLevel>>

View File

@ -29,7 +29,7 @@ abstract class AppDatabase : RoomDatabase() {
abstract fun userDao() : UserDao
companion object {
private const val DB_NAME: String = "ten-db"
private const val DB_NAME: String = "12-db"
@Volatile
private var INSTANCE: AppDatabase? = null
@ -88,14 +88,14 @@ abstract class AppDatabase : RoomDatabase() {
val tankDao = database.tankDao()
val tank1 = Tank(20L,"МС-1", 1000, R.drawable.t_34_85, level1.uid, nation1.uid)
val tank2 = Tank(21L, "Т-34-85", 960000, R.drawable.t_34_85, level6.uid, nation1.uid)
val tank3 = Tank(22L, "ИС-2", 1230000, R.drawable.t_34_85, level7.uid, nation1.uid)
val tank4 = Tank(23L, "ИСУ-152", 2350000, R.drawable.t_34_85, level8.uid, nation1.uid)
val tank5 = Tank(24L, "Tiger 1", 1430000, R.drawable.tiger_1, level7.uid, nation2.uid)
val tank6 = Tank(25L, "Ferdinand", 2500000, R.drawable.tiger_1, level8.uid, nation2.uid)
val tank7 = Tank(26L, "Tiger 2", 2500000, R.drawable.tiger_1, level8.uid, nation2.uid)
val tank8 = Tank(27L, "Panther", 1350000, R.drawable.tiger_1, level7.uid, nation2.uid)
val tank9 = Tank(28L, "M4A2E3", 990000, R.drawable.sherman, level6.uid, nation3.uid)
val tank10 = Tank(29L, "Pershing", 1260000, R.drawable.sherman, level8.uid, nation3.uid)
val tank10 = Tank(22L, "Pershing", 1260000, R.drawable.sherman, level8.uid, nation3.uid)
val tank6 = Tank(23L, "Ferdinand", 2500000, R.drawable.tiger_1, level8.uid, nation2.uid)
val tank3 = Tank(24L, "ИС-2", 1230000, R.drawable.t_34_85, level7.uid, nation1.uid)
val tank4 = Tank(25L, "ИСУ-152", 2350000, R.drawable.t_34_85, level8.uid, nation1.uid)
val tank5 = Tank(26L, "Tiger 1", 1430000, R.drawable.tiger_1, level7.uid, nation2.uid)
val tank7 = Tank(27L, "Tiger 2", 2500000, R.drawable.tiger_1, level8.uid, nation2.uid)
val tank8 = Tank(28L, "Panther", 1350000, R.drawable.tiger_1, level7.uid, nation2.uid)
val tank9 = Tank(29L, "M4A2E3", 990000, R.drawable.sherman, level6.uid, nation3.uid)
val tank11 = Tank(30L, "Hellcat", 940000, R.drawable.sherman, level7.uid, nation3.uid)
tankDao.insert(tank1)

View File

@ -13,16 +13,17 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.ExposedDropdownMenuDefaults.TrailingIcon
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
@ -38,11 +39,8 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import kotlinx.coroutines.launch
import ru.ulstu.`is`.pmu.R
import ru.ulstu.`is`.pmu.composeui.navigation.Screen
import ru.ulstu.`is`.pmu.tank.composeui.edit.LevelDropDownViewModel
import ru.ulstu.`is`.pmu.tank.composeui.edit.LevelUiState
import ru.ulstu.`is`.pmu.tank.composeui.edit.LevelsListUiState
@ -53,12 +51,9 @@ import ru.ulstu.`is`.pmu.tank.composeui.edit.TankDetails
import ru.ulstu.`is`.pmu.tank.composeui.edit.TankEditViewModel
import ru.ulstu.`is`.pmu.tank.composeui.edit.TankUiState
import ru.ulstu.`is`.pmu.tank.composeui.edit.toUiState
import ru.ulstu.`is`.pmu.tank.composeui.list.TankListViewModel
import ru.ulstu.`is`.pmu.tank.model.Level
import ru.ulstu.`is`.pmu.tank.model.Nation
import ru.ulstu.`is`.pmu.tank.model.Tank
import ru.ulstu.`is`.pmu.tank.model.getLevels
import ru.ulstu.`is`.pmu.tank.model.getNations
import ru.ulstu.`is`.pmu.ui.AppViewModelProvider
import ru.ulstu.`is`.pmu.ui.theme.CustomDark
import ru.ulstu.`is`.pmu.ui.theme.CustomOrange
@ -73,15 +68,12 @@ fun Constructor(
nationDropDownViewModel: NationDropDownViewModel = viewModel(factory = AppViewModelProvider.Factory)
){
val coroutineScope = rememberCoroutineScope()
val tankUiState = tankEditViewModel.tankUiState
Log.d("hi-2", tankUiState.tankDetails.name)
levelDropDownViewModel.setCurrentLevel(tankEditViewModel.tankUiState.tankDetails.levelId ?: 1)
nationDropDownViewModel.setCurrentNation(tankUiState.tankDetails.nationId ?: 1)
nationDropDownViewModel.setCurrentNation(tankEditViewModel.tankUiState.tankDetails.nationId ?: 1)
Constructor(
tankUiState = tankUiState,
tankUiState = tankEditViewModel.tankUiState,
levelUiState = levelDropDownViewModel.levelUiState,
levelsListUiState = levelDropDownViewModel.levelsListUiState,
onLevelUpdate = levelDropDownViewModel::updateUiState,
@ -98,7 +90,106 @@ fun Constructor(
)
}
@OptIn(ExperimentalMaterial3Api::class, ExperimentalTextApi::class)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun LevelDropDown(
levelUiState: LevelUiState,
levelsListUiState: LevelsListUiState,
onLevelUpdate: (Level) -> Unit
) {
var expanded: Boolean by remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
modifier = Modifier
.padding(top = 7.dp),
expanded = expanded,
onExpandedChange = {
expanded = !expanded
}
) {
TextField(
value = levelUiState.level?.level.toString(),
onValueChange = {},
readOnly = true,
trailingIcon = {
TrailingIcon(expanded = expanded)
},
modifier = Modifier
.width(200.dp)
.menuAnchor()
)
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
modifier = Modifier
.background(Color.White)
.exposedDropdownSize()
) {
levelsListUiState.levelList.forEach { level ->
DropdownMenuItem(
text = {
Text(text = level.level.toString())
},
onClick = {
onLevelUpdate(level)
expanded = false
}
)
}
}
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun NationDropDown(
nationUiState: NationUiState,
nationsListUiState: NationsListUiState,
onNationUpdate: (Nation) -> Unit
) {
var expanded: Boolean by remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
modifier = Modifier
.padding(top = 7.dp),
expanded = expanded,
onExpandedChange = {
expanded = !expanded
}
) {
TextField(
value = nationUiState.nation?.nationName ?: "",
onValueChange = {},
readOnly = true,
trailingIcon = {
TrailingIcon(expanded = expanded)
},
modifier = Modifier
.width(200.dp)
.menuAnchor()
)
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
modifier = Modifier
.background(Color.White)
.exposedDropdownSize()
) {
nationsListUiState.nationList.forEach { nation ->
DropdownMenuItem(
text = {
Text(text = nation.nationName)
},
onClick = {
onNationUpdate(nation)
expanded = false
}
)
}
}
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun Constructor(
tankUiState: TankUiState,
@ -160,86 +251,24 @@ private fun Constructor(
)
Spacer(Modifier.height(10.dp))
// Выпадающий список уровней
ExposedDropdownMenuBox(
expanded = expandedLevels,
onExpandedChange = {
expandedLevels = !expandedLevels
LevelDropDown(
levelUiState = levelUiState,
levelsListUiState = levelsListUiState,
onLevelUpdate = {
onUpdate(tankUiState.tankDetails.copy(levelId = it.uid))
onLevelUpdate(it)
},
) {
// textfield
TextField(
modifier = Modifier
.menuAnchor() // menuAnchor modifier must be passed to the text field for correctness.
.width(200.dp),
readOnly = true,
value = levelUiState.level?.level.toString(),
onValueChange = {},
label = { Text("") },
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expandedLevels) },
colors = ExposedDropdownMenuDefaults.textFieldColors(),
)
// menu
ExposedDropdownMenu(
expanded = expandedLevels,
onDismissRequest = {
expandedLevels = false
},
) {
// menu items
levelsListUiState.levelList.forEach { selectionOption ->
DropdownMenuItem(
text = { Text(selectionOption.level.toString()) },
onClick = {
onLevelUpdate(selectionOption)
expandedLevels = false
},
contentPadding = ExposedDropdownMenuDefaults.ItemContentPadding,
)
}
}
}
)
Spacer(Modifier.height(10.dp))
// Выпадающий список наций
ExposedDropdownMenuBox(
expanded = expandedNation,
onExpandedChange = {
expandedNation = !expandedNation
}
) {
// textfield
TextField(
modifier = Modifier
.menuAnchor() // menuAnchor modifier must be passed to the text field for correctness.
.width(200.dp),
readOnly = true,
value = nationUiState.nation?.nationName ?: "",
onValueChange = {},
label = { Text("") },
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expandedNation) },
colors = ExposedDropdownMenuDefaults.textFieldColors(),
)
// menu
ExposedDropdownMenu(
expanded = expandedNation,
onDismissRequest = {
expandedNation = false
},
) {
// menu items
nationsListUiState.nationList.forEach { selectionOption ->
DropdownMenuItem(
text = { Text(selectionOption.nationName) },
onClick = {
onNationUpdate(selectionOption)
expandedNation = false
},
contentPadding = ExposedDropdownMenuDefaults.ItemContentPadding,
)
}
}
}
NationDropDown(
nationUiState = nationUiState,
nationsListUiState = nationsListUiState,
onNationUpdate = {
onUpdate(tankUiState.tankDetails.copy(nationId = it.uid))
onNationUpdate(it)
},
)
Spacer(Modifier.height(10.dp))
TextField(
value = tankUiState.tankDetails.price.toString(),