Вывод по нациям

This commit is contained in:
ElEgEv 2023-12-11 11:00:14 +04:00
parent f79449f90e
commit 91a0de0cc9
11 changed files with 103 additions and 77 deletions

View File

@ -10,10 +10,12 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
@ -28,6 +30,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.res.painterResource
@ -41,7 +44,10 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import ru.ulstu.`is`.pmu.R
import ru.ulstu.`is`.pmu.composeui.navigation.Screen
import ru.ulstu.`is`.pmu.tank.composeui.edit.NationDropDownViewModel
import ru.ulstu.`is`.pmu.tank.composeui.edit.NationsListUiState
import ru.ulstu.`is`.pmu.tank.composeui.list.TankListViewModel
import ru.ulstu.`is`.pmu.tank.model.Nation
import ru.ulstu.`is`.pmu.tank.model.Tank
import ru.ulstu.`is`.pmu.tanks.composeui.image.CuteImage
import ru.ulstu.`is`.pmu.ui.AppViewModelProvider
@ -54,20 +60,15 @@ import ru.ulstu.`is`.pmu.ui.theme.PmudemoTheme
@Composable
fun TankList(
navController: NavController?,
viewModel: TankListViewModel = viewModel(factory = AppViewModelProvider.Factory)
viewModel: TankListViewModel = viewModel(factory = AppViewModelProvider.Factory),
listNations: NationDropDownViewModel = viewModel(factory = AppViewModelProvider.Factory)
) {
val listIds = listOf(
R.string.ussr_list,
R.string.germany_list,
R.string.usa_list
)
val tankListUiState by viewModel.tankListUiState.collectAsState()
// Lazy Column, Pass the numbers array
if (navController != null) {
TankList(
numbers = listIds,
nations = listNations.nationsListUiState,
listTanks = tankListUiState.tankList
) { uid: Long ->
val route = Screen.Constructor.route.replace("{id}", uid.toString())
@ -78,8 +79,8 @@ fun TankList(
@Composable
fun ColumnItem(
number: Int,
tanks: List<Tank>,
nation: Nation,
tanks: List<Tank>?,
onClick: (uid: Long) -> Unit
) {
Column(
@ -92,7 +93,7 @@ fun ColumnItem(
.height(40.dp),
){
Text(
text = stringResource(id = number),
text = nation.nationName,
fontSize = 30.sp,
fontWeight = FontWeight.Bold,
color = CustomDark,
@ -110,48 +111,65 @@ fun ColumnItem(
modifier = Modifier.horizontalScroll(ScrollState(0))
)
{
tanks.forEach { tank ->
if(tank.tankId != null){
key(tank.tankId) {
//val studentId = Screen.StudentView.route.replace("{id}", index.toString())
Card(
colors = CardDefaults.cardColors(
containerColor = CustomDark,
),
modifier = Modifier
.size(width = 200.dp, height = 250.dp)
.padding(all = 5.dp)
.clickable { onClick(tank.tankId) }
) {
CuteImage(
imageBitmap = tank.miniature.asImageBitmap(),
modifier = Modifier.weight(0.5F)
)
Text(
text = tank.name,
fontSize = 30.sp,
fontWeight = FontWeight.Bold,
color = CustomOrange,
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth()
)
Button(
if(!tanks.isNullOrEmpty()){
tanks.forEach { tank ->
if(tank.tankId != null){
key(tank.tankId) {
//val studentId = Screen.StudentView.route.replace("{id}", index.toString())
Card(
colors = CardDefaults.cardColors(
containerColor = CustomDark,
),
modifier = Modifier
.fillMaxWidth()
.padding(10.dp, 0.dp, 10.dp, 10.dp),
colors = ButtonDefaults.buttonColors(
containerColor = CustomRed,
contentColor = CustomDark),
onClick = { }
.size(width = 200.dp, height = 250.dp)
.padding(all = 5.dp)
.clickable { onClick(tank.tankId) }
) {
//navController?.navigate(Screen.Hangar.route)
//navController?.navigate(studentId)
//"${student.firstName} ${student.lastName}"
Text(text = stringResource(id = R.string.purchase_button))
CuteImage(
imageBitmap = tank.miniature.asImageBitmap(),
modifier = Modifier.weight(0.5F)
)
Text(
text = tank.name,
fontSize = 30.sp,
fontWeight = FontWeight.Bold,
color = CustomOrange,
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth()
)
Button(
modifier = Modifier
.fillMaxWidth()
.padding(10.dp, 0.dp, 10.dp, 10.dp),
colors = ButtonDefaults.buttonColors(
containerColor = CustomRed,
contentColor = CustomDark),
onClick = { }
) {
//navController?.navigate(Screen.Hangar.route)
//navController?.navigate(studentId)
//"${student.firstName} ${student.lastName}"
Text(text = stringResource(id = R.string.purchase_button))
}
}
}
}
}
} else {
Column(
modifier = Modifier.width(400.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
){
Text(
text = "Скоро!!!",
fontSize = 30.sp,
fontWeight = FontWeight.Bold,
color = CustomDark,
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxSize()
)
}
}
}
}
@ -161,13 +179,13 @@ fun ColumnItem(
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun TankList(
numbers: List<Int>,
nations: NationsListUiState,
listTanks: List<Tank>,
onClick: (uid: Long) -> Unit,
) {
LazyColumn(
verticalArrangement = Arrangement.Bottom,
modifier = Modifier.height(300.dp * numbers.size),
modifier = Modifier.height(300.dp * nations.nationList.size),
) {
// item places one item on the LazyScope
item {
@ -184,15 +202,35 @@ private fun TankList(
// same as the size of list/array and gives
// current list/array item in the lazyItemScope
// тут как раз и выводим 3 раза все танки по нациям
items(numbers) {arrayItem->
ColumnItem(number = arrayItem, tanks = listTanks, onClick = onClick)
// алгоритм разбиения танков по нациям
var dictNationTanks: Map<Nation, List<Tank>> = mutableMapOf()
var totalDictionary: Map<Nation, List<Tank>> = mutableMapOf()
var newListTanks: List<Tank>
nations.nationList.forEach { nation ->
newListTanks = mutableListOf()
listTanks.forEach{tank ->
if(tank.nationId == nation.uid){
newListTanks = newListTanks.plus(tank)
}
}
if(newListTanks.size > 0){
totalDictionary = dictNationTanks.plus(nation to newListTanks)
dictNationTanks = totalDictionary
}
}
items(nations.nationList) {nation ->
ColumnItem(nation = nation, tanks = totalDictionary[nation], onClick = onClick)
}
// items(list/array) places number of
// items same as the size of list/array
// and gives current list/array item and
// currentIndex in the lazyItemScope
itemsIndexed(numbers) { index, item ->
itemsIndexed(nations.nationList) { index, item ->
//ColumnItem(number = index)
}
}
@ -207,7 +245,7 @@ fun TankListPreview() {
color = CustomDark
) {
TankList(
numbers = listOf(1, 2, 3),
nations = NationsListUiState(listOf()),
listTanks = (1..20).map { i -> Tank.getTank(i.toLong()) }
) { }
}
@ -223,7 +261,7 @@ fun TankEmptyListPreview() {
color = CustomDark
) {
TankList(
numbers = listOf(1, 2, 3),
nations = NationsListUiState(listOf()),
listTanks = listOf()
) { }
}

View File

@ -62,9 +62,9 @@ class TankEditViewModel(
return with(uiState) {
name.isNotBlank()
&& price > 0
&& levelId!! > 0
&& nationId!! > 0
&& imageId !! > 0
&& levelId!! != 0L
&& nationId!! != 0L
&& imageId !! != 0L
&& image != null
}
}

View File

@ -34,7 +34,7 @@ abstract class AppDatabase : RoomDatabase() {
abstract fun tankImageDao() : TankImageDao
companion object {
private const val DB_NAME: String = "17-db"
private const val DB_NAME: String = "18-db"
@Volatile
private var INSTANCE: AppDatabase? = null

View File

@ -210,12 +210,6 @@ private fun Constructor(
onClick: () -> Unit,
onUpdate: (TankDetails) -> Unit
) {
//для работы выпадающего списка уровней
var expandedLevels by remember { mutableStateOf(false) }
//для работы выпадающего списка наций
var expandedNation by remember { mutableStateOf(false) }
fun handleImageUpload(bitmap: Bitmap) {
tankViewModel.updateUiState(
tankUiState.tankDetails.copy(
@ -251,13 +245,13 @@ private fun Constructor(
modifier = Modifier.padding(0.dp, 5.dp)
) {
Text(text="Название:", fontSize = 30.sp, color = Color.Black, fontWeight = FontWeight.Bold)
Spacer(Modifier.height(25.dp))
Spacer(Modifier.height(40.dp))
Text(text="Изображение:", fontSize = 30.sp, color = Color.Black, fontWeight = FontWeight.Bold)
Spacer(Modifier.height(30.dp))
Spacer(Modifier.height(40.dp))
Text(text="Уровень:", fontSize = 30.sp, color = Color.Black, fontWeight = FontWeight.Bold)
Spacer(Modifier.height(30.dp))
Spacer(Modifier.height(40.dp))
Text(text="Нация:", fontSize = 30.sp, color = Color.Black, fontWeight = FontWeight.Bold)
Spacer(Modifier.height(25.dp))
Spacer(Modifier.height(40.dp))
Text(text="Стоимость:", fontSize = 30.sp, color = Color.Black, fontWeight = FontWeight.Bold)
}
Column {
@ -268,13 +262,6 @@ private fun Constructor(
modifier = Modifier
.width(200.dp),
)
OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
value = tankUiState.tankDetails.name,
onValueChange = { onUpdate(tankUiState.tankDetails.copy(name = it)) },
label = { Text(text = "Название", color = CustomDark) },
singleLine = true
)
Spacer(Modifier.height(10.dp))
CuteImageUploader(
bitmap = tankUiState.tankDetails.image,

View File

@ -14,6 +14,7 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
@ -80,7 +81,7 @@ fun CuteImageUploader(
if (bitmap != null) {
CuteImage(
imageBitmap = bitmap.asImageBitmap(),
modifier = Modifier.width(100.dp)
modifier = Modifier.fillMaxSize()
)
} else {
Icon(
@ -116,7 +117,7 @@ fun CuteImageUploader(
),
onClick = { handleUploadButtonClick() },
){
Text(text = "Загрузить изображение")
Text(text = "Загрузить")
}
}
}