Дело пошло)))0)

This commit is contained in:
ElEgEv 2023-11-26 23:01:25 +04:00
parent 895fdee643
commit 8ff030127a
20 changed files with 67 additions and 41 deletions

Binary file not shown.

View File

@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">
<application
android:name=".TankApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"

View File

@ -20,15 +20,14 @@ import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
@ -38,9 +37,6 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.withContext
import ru.ulstu.`is`.pmu.R
import ru.ulstu.`is`.pmu.composeui.navigation.Screen
import ru.ulstu.`is`.pmu.tank.composeui.list.TankListViewModel
@ -65,14 +61,18 @@ fun TankList(
R.string.usa_list
)
val tankListUiState by viewModel.tankListUiState.collectAsState()
// Lazy Column, Pass the numbers array
LazyColumnExample(numbers = listIds)
TankList(numbers = listIds, listTanks = tankListUiState.tankList)
}
@Composable
fun ColumnItem(
number: Int
number: Int,
tanks: List<Tank>
) {
/*
val context = LocalContext.current
val tanks = remember { mutableStateListOf<Tank>() }
LaunchedEffect(Unit) {
@ -83,7 +83,7 @@ fun ColumnItem(
tanks.reverse()
}
}
}
}*/
Column(
modifier = Modifier.padding(0.dp, 10.dp)
) {
@ -159,11 +159,16 @@ fun ColumnItem(
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LazyColumnExample(numbers: List<Int>) {
private fun TankList(
numbers: List<Int>,
listTanks: List<Tank>
) {
LazyColumn(
verticalArrangement = Arrangement.Bottom,
modifier = Modifier.height(300.dp * numbers.size)
modifier = Modifier.height(300.dp * numbers.size),
) {
// item places one item on the LazyScope
item {
@ -179,8 +184,9 @@ fun LazyColumnExample(numbers: List<Int>) {
// items(list/array) places number of items
// same as the size of list/array and gives
// current list/array item in the lazyItemScope
// тут как раз и выводим 3 раза все танки по нациям
items(numbers) {arrayItem->
ColumnItem(number = arrayItem)
ColumnItem(number = arrayItem, tanks = listTanks)
}
// items(list/array) places number of
@ -196,12 +202,28 @@ fun LazyColumnExample(numbers: List<Int>) {
@Preview(name = "Light Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "Dark Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun StudentListPreview() {
fun TankListPreview() {
PmudemoTheme {
Surface(
color = CustomDark
) {
TankList(navController = null)
TankList(
numbers = listOf(1, 2, 3),
listTanks = listOf()
)
}
}
}
@Preview(name = "Light Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "Dark Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun TankEmptyListPreview() {
PmudemoTheme {
Surface(
color = CustomDark
) {
TankList(numbers = listOf(1, 2, 3), listTanks = listOf())
}
}
}

View File

@ -13,7 +13,7 @@ import ru.ulstu.`is`.pmu.tank.repository.TankRepository
class TankListViewModel(
private val tankRepository: TankRepository
) : ViewModel() {
val tankListUiState: StateFlow<TankListUiState> = tankRepository.getAllTanks().map {
val tankListUiState: StateFlow<TankListUiState> = tankRepository.getAll().map {
TankListUiState(it)
}.stateIn(
scope = viewModelScope,

View File

@ -14,7 +14,7 @@ import ru.ulstu.`is`.pmu.tank.model.NationWithTanks
@Dao
interface NationDao {
@Query("select * from nations")
fun getAll(): Flow<List<Nation>>
fun getAll(): List<Nation>
//получить нации с танками
@Transaction

View File

@ -13,7 +13,7 @@ import ru.ulstu.`is`.pmu.tank.model.Tank
@Dao
interface TankDao {
@Query("select * from tanks order by name collate nocase asc")
@Query("select * from tanks")
fun getAll(): Flow<List<Tank>>
//получить конкретный уровень

View File

@ -5,7 +5,7 @@ import ru.ulstu.`is`.pmu.tank.model.Level
import ru.ulstu.`is`.pmu.tank.model.LevelWithTanks
interface LevelRepository {
fun getAllLevels(): List<Level>
suspend fun getAllLevels(): List<Level>
fun getSimpleLevel(uid: Long): Flow<Level?>
fun getFullLevel(uid: Long): Flow<LevelWithTanks?>
suspend fun insertLevel(level: Level)

View File

@ -5,7 +5,7 @@ import ru.ulstu.`is`.pmu.tank.model.Nation
import ru.ulstu.`is`.pmu.tank.model.NationWithTanks
interface NationRepository {
fun getAllNations(): List<Nation>
suspend fun getAllNations(): List<Nation>
fun getSimpleNation(uid: Long): Flow<Nation?>
fun getFullNation(uid: Long): Flow<NationWithTanks?>
suspend fun insertNation(nation: Nation)

View File

@ -6,7 +6,7 @@ import ru.ulstu.`is`.pmu.tank.model.Level
import ru.ulstu.`is`.pmu.tank.model.LevelWithTanks
class OfflineLevelRepository(private val levelDao: LevelDao) : LevelRepository {
override fun getAllLevels(): List<Level> = levelDao.getAll()
override suspend fun getAllLevels(): List<Level> = levelDao.getAll()
override fun getSimpleLevel(uid: Long): Flow<Level?> = levelDao.getSimpleLevelUid(uid)

View File

@ -6,7 +6,7 @@ import ru.ulstu.`is`.pmu.tank.model.Nation
import ru.ulstu.`is`.pmu.tank.model.NationWithTanks
class OfflineNationRepository(private val nationDao: NationDao) : NationRepository {
override fun getAllNations(): Flow<List<Nation>> = nationDao.getAll()
override suspend fun getAllNations(): List<Nation> = nationDao.getAll()
override fun getSimpleNation(uid: Long): Flow<Nation?> = nationDao.getSimpleNationUid(uid)

View File

@ -5,7 +5,7 @@ import ru.ulstu.`is`.pmu.tank.dao.TankDao
import ru.ulstu.`is`.pmu.tank.model.Tank
class OfflineTankRepository(private val tankDao: TankDao) : TankRepository {
override fun getAllTanks(): Flow<List<Tank>> = tankDao.getAll()
override fun getAll(): Flow<List<Tank>> = tankDao.getAll()
override fun getTank(uid: Long): Flow<Tank?> = tankDao.getTankUid(uid)

View File

@ -4,7 +4,7 @@ import kotlinx.coroutines.flow.Flow
import ru.ulstu.`is`.pmu.tank.model.Tank
interface TankRepository {
fun getAllTanks(): Flow<List<Tank>>
fun getAll(): Flow<List<Tank>>
fun getTank(uid: Long): Flow<Tank?>
suspend fun insertTank(tank: Tank)
suspend fun updateTank(tank: Tank)

View File

@ -50,6 +50,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
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
@ -57,8 +58,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import ru.ulstu.`is`.pmu.R
import ru.ulstu.`is`.pmu.composeui.navigation.Screen
import ru.ulstu.`is`.pmu.tank.composeui.edit.UserEditViewModel
import ru.ulstu.`is`.pmu.tank.database.AppDatabase
import ru.ulstu.`is`.pmu.tank.model.User
import ru.ulstu.`is`.pmu.ui.AppViewModelProvider
import ru.ulstu.`is`.pmu.ui.theme.CustomDark
import ru.ulstu.`is`.pmu.ui.theme.CustomOrange
import ru.ulstu.`is`.pmu.ui.theme.CustomRed
@ -67,26 +70,23 @@ import ru.ulstu.`is`.pmu.ui.theme.PmudemoTheme
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Account(navController: NavController) {
fun Account(
navController: NavController,
viewModel: UserEditViewModel = viewModel(factory = AppViewModelProvider.Factory)
) {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
val currentScreen = currentDestination?.route?.let { Screen.getItem(it) }
val userUiState = viewModel.userUiState
//работа с БД
val context = LocalContext.current
val (userWithTanks, setUserWithTanks) = remember { mutableStateOf<User?>(null) }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
setUserWithTanks(AppDatabase.getInstance(context).userDao().getSimpleUserUid(100L))
}
}
//для работы текстовых полей
var nickname by remember { mutableStateOf(userWithTanks?.nickname) }
var nickname by remember { mutableStateOf(userUiState.userDetails.nickname) }
var password by remember { mutableStateOf(userWithTanks?.password) }
var password by remember { mutableStateOf(userUiState.userDetails.password) }
var balance by remember { mutableStateOf(userWithTanks?.balance.toString()) }
var balance by remember { mutableStateOf(userUiState.userDetails.balance.toString()) }
Column(
verticalArrangement = Arrangement.spacedBy(35.dp),
@ -123,21 +123,21 @@ fun Account(navController: NavController) {
}
Column {
TextField(
value = userWithTanks?.nickname ?: "",
value = userUiState.userDetails.nickname,
onValueChange = { nickname = it },
modifier = Modifier
.width(200.dp),
)
Spacer(Modifier.height(10.dp))
TextField(
value = userWithTanks?.password ?: "",
value = userUiState.userDetails.password,
onValueChange = { password = it },
modifier = Modifier
.width(200.dp),
)
Spacer(Modifier.height(10.dp))
TextField(
value = userWithTanks?.balance.toString(),
value = userUiState.userDetails.balance.toString(),
onValueChange = { balance = it },
modifier = Modifier
.width(200.dp),

View File

@ -8,16 +8,19 @@ import androidx.lifecycle.viewmodel.viewModelFactory
import ru.ulstu.`is`.pmu.TankApplication
import ru.ulstu.`is`.pmu.tank.composeui.edit.LevelDropDownViewModel
import ru.ulstu.`is`.pmu.tank.composeui.edit.NationDropDownViewModel
import ru.ulstu.`is`.pmu.tank.composeui.edit.UserEditViewModel
import ru.ulstu.`is`.pmu.tank.composeui.list.TankListViewModel
object AppViewModelProvider {
val Factory = viewModelFactory {
initializer {
StudentListViewModel(tankApplication().container.studentRepository)
TankListViewModel(tankApplication().container.tankRepository)
}
initializer {
StudentEditViewModel(
UserEditViewModel(
this.createSavedStateHandle(),
tankApplication().container.studentRepository
tankApplication().container.userRepository
)
}
initializer {