Start lab work 4.

This commit is contained in:
ElEgEv 2023-11-26 13:56:51 +04:00
parent 9c86858b71
commit 5b26f60938
21 changed files with 174 additions and 8 deletions

Binary file not shown.

View File

@ -11,8 +11,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.compose.rememberNavController
import ru.ulstu.`is`.pmu.tanks.composeui.Login
import ru.ulstu.`is`.pmu.composeui.navigation.MainNavbar
import ru.ulstu.`is`.pmu.composeui.navigation.NavGraph
import ru.ulstu.`is`.pmu.ui.theme.PmudemoTheme

View File

@ -24,13 +24,13 @@ interface LevelDao {
//получить конкретный уровень
@Query("select * from levels where levels.uid = :uid")
suspend fun getLevelUid(uid: Long): LevelWithTanks
fun getLevelUid(uid: Long): Flow<LevelWithTanks?>
//получить уровень без списка танков
@Query(
"SELECT level FROM levels where levels.uid = :uid"
)
open fun getSimpleLevelUid(uid: Long): Level
open fun getSimpleLevelUid(uid: Long): Flow<Level?>
@Insert
suspend fun insert(level: Level)

View File

@ -23,13 +23,13 @@ interface NationDao {
//получить конкретную нацию
@Query("select * from nations where nations.uid = :uid")
suspend fun getNationUid(uid: Long): NationWithTanks
fun getNationUid(uid: Long): Flow<NationWithTanks?>
//получить нацию без списка танков
@Query(
"SELECT nationName FROM nations where nations.uid = :uid"
)
open fun getSimpleNationUid(uid: Long): Nation
open fun getSimpleNationUid(uid: Long): Flow<Nation?>
@Insert
suspend fun insert(nation: Nation)

View File

@ -18,7 +18,7 @@ interface TankDao {
//получить конкретный уровень
@Query("select * from tanks where tanks.tankId = :uid")
suspend fun getTankUid(uid: Long): Tank
fun getTankUid(uid: Long): Flow<Tank?>
@Insert
suspend fun insert(tank: Tank)

View File

@ -30,7 +30,7 @@ interface UserDao {
fun getUserUid(uid: Long): Flow<Map<User, List<TankWithNationAndLevel>>>
@Query("select * from users where users.userId = :uid")
suspend fun getSimpleUserUid(uid: Long): User
fun getSimpleUserUid(uid: Long): Flow<User?>
//добавить танк в ангар пользователя
@Insert

View File

@ -0,0 +1,37 @@
package ru.ulstu.`is`.pmu.tank.database
import android.content.Context
import ru.ulstu.`is`.pmu.tank.repository.LevelRepository
import ru.ulstu.`is`.pmu.tank.repository.NationRepository
import ru.ulstu.`is`.pmu.tank.repository.OfflineLevelRepository
import ru.ulstu.`is`.pmu.tank.repository.OfflineNationRepository
import ru.ulstu.`is`.pmu.tank.repository.OfflineTankRepository
import ru.ulstu.`is`.pmu.tank.repository.OfflineUserRepository
import ru.ulstu.`is`.pmu.tank.repository.TankRepository
import ru.ulstu.`is`.pmu.tank.repository.UserRepository
interface AppContainer {
val levelRepository: LevelRepository
val nationRepository: NationRepository
val tankRepository: TankRepository
val userRepository: UserRepository
}
class AppDataContainer(private val context: Context) : AppContainer {
override val levelRepository: LevelRepository by lazy {
OfflineLevelRepository(AppDatabase.getInstance(context).levelDao())
}
override val nationRepository: NationRepository by lazy {
OfflineNationRepository(AppDatabase.getInstance(context).nationDao())
}
override val tankRepository: TankRepository by lazy {
OfflineTankRepository(AppDatabase.getInstance(context).tankDao())
}
override val userRepository: UserRepository by lazy {
OfflineUserRepository(AppDatabase.getInstance(context).userDao())
}
companion object {
const val TIMEOUT = 5000L
}
}

View File

@ -0,0 +1,14 @@
package ru.ulstu.`is`.pmu.tank.repository
import kotlinx.coroutines.flow.Flow
import ru.ulstu.`is`.pmu.tank.model.Level
import ru.ulstu.`is`.pmu.tank.model.LevelWithTanks
interface LevelRepository {
fun getAllLevels(): Flow<List<Level>>
fun getSimpleLevel(uid: Long): Flow<Level?>
fun getFullLevel(uid: Long): Flow<LevelWithTanks?>
suspend fun insertLevel(level: Level)
suspend fun updateLevel(level: Level)
suspend fun deleteLevel(level: Level)
}

View File

@ -0,0 +1,14 @@
package ru.ulstu.`is`.pmu.tank.repository
import kotlinx.coroutines.flow.Flow
import ru.ulstu.`is`.pmu.tank.model.Nation
import ru.ulstu.`is`.pmu.tank.model.NationWithTanks
interface NationRepository {
fun getAllNations(): Flow<List<Nation>>
fun getSimpleNation(uid: Long): Flow<Nation?>
fun getFullNation(uid: Long): Flow<NationWithTanks?>
suspend fun insertNation(nation: Nation)
suspend fun updateNation(nation: Nation)
suspend fun deleteNation(nation: Nation)
}

View File

@ -0,0 +1,20 @@
package ru.ulstu.`is`.pmu.tank.repository
import kotlinx.coroutines.flow.Flow
import ru.ulstu.`is`.pmu.tank.dao.LevelDao
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(): Flow<List<Level>> = levelDao.getAll()
override fun getSimpleLevel(uid: Long): Flow<Level?> = levelDao.getSimpleLevelUid(uid)
override fun getFullLevel(uid: Long): Flow<LevelWithTanks?> = levelDao.getLevelUid(uid)
override suspend fun insertLevel(level: Level) = levelDao.insert(level)
override suspend fun updateLevel(level: Level) = levelDao.update(level)
override suspend fun deleteLevel(level: Level) = levelDao.delete(level)
}

View File

@ -0,0 +1,20 @@
package ru.ulstu.`is`.pmu.tank.repository
import kotlinx.coroutines.flow.Flow
import ru.ulstu.`is`.pmu.tank.dao.NationDao
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 fun getSimpleNation(uid: Long): Flow<Nation?> = nationDao.getSimpleNationUid(uid)
override fun getFullNation(uid: Long): Flow<NationWithTanks?> = nationDao.getNationUid(uid)
override suspend fun insertNation(nation: Nation) = nationDao.insert(nation)
override suspend fun updateNation(nation: Nation) = nationDao.update(nation)
override suspend fun deleteNation(nation: Nation) = nationDao.delete(nation)
}

View File

@ -0,0 +1,17 @@
package ru.ulstu.`is`.pmu.tank.repository
import kotlinx.coroutines.flow.Flow
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 getTank(uid: Long): Flow<Tank?> = tankDao.getTankUid(uid)
override suspend fun insertTank(tank: Tank) = tankDao.insert(tank)
override suspend fun updateTank(tank: Tank) = tankDao.update(tank)
override suspend fun deleteTank(tank: Tank) = tankDao.delete(tank)
}

View File

@ -0,0 +1,20 @@
package ru.ulstu.`is`.pmu.tank.repository
import kotlinx.coroutines.flow.Flow
import ru.ulstu.`is`.pmu.tank.dao.UserDao
import ru.ulstu.`is`.pmu.tank.model.TankWithNationAndLevel
import ru.ulstu.`is`.pmu.tank.model.User
class OfflineUserRepository(private val userDao: UserDao) : UserRepository {
override fun getAllUsers(): Flow<List<User>> = userDao.getAll()
override fun getSimpleUser(uid: Long): Flow<User?> = userDao.getSimpleUserUid(uid)
override fun getFullUser(uid: Long): Flow<Map<User, List<TankWithNationAndLevel>>> = userDao.getUserUid(uid)
override suspend fun insertUser(student: User) = userDao.insert(student)
override suspend fun updateUser(student: User) = userDao.update(student)
override suspend fun deleteUser(student: User) = userDao.delete(student)
}

View File

@ -0,0 +1,12 @@
package ru.ulstu.`is`.pmu.tank.repository
import kotlinx.coroutines.flow.Flow
import ru.ulstu.`is`.pmu.tank.model.Tank
interface TankRepository {
fun getAllTanks(): Flow<List<Tank>>
fun getTank(uid: Long): Flow<Tank?>
suspend fun insertTank(tank: Tank)
suspend fun updateTank(tank: Tank)
suspend fun deleteTank(tank: Tank)
}

View File

@ -0,0 +1,14 @@
package ru.ulstu.`is`.pmu.tank.repository
import kotlinx.coroutines.flow.Flow
import ru.ulstu.`is`.pmu.tank.model.TankWithNationAndLevel
import ru.ulstu.`is`.pmu.tank.model.User
interface UserRepository {
fun getAllUsers(): Flow<List<User>>
fun getSimpleUser(uid: Long): Flow<User?>
fun getFullUser(uid: Long): Flow<Map<User, List<TankWithNationAndLevel>>>
suspend fun insertUser(user: User)
suspend fun updateUser(user: User)
suspend fun deleteUser(user: User)
}