11package com.example.projectfigma.DataBase
22
33import android.content.Context
4+ import android.text.style.TtsSpan
45import androidx.room.Database
56import androidx.room.Room
67import androidx.room.RoomDatabase
78import androidx.room.TypeConverters
89import androidx.sqlite.db.SupportSQLiteDatabase
910import com.example.projectfigma.Converters.ConvertToDishesCategory
11+ import com.example.projectfigma.Converters.ConvertersList
1012import com.example.projectfigma.DAO.DishesDao
1113import com.example.projectfigma.DAO.SettingsDao
1214import com.example.projectfigma.DAO.SessionDao
@@ -22,12 +24,17 @@ import kotlinx.coroutines.CoroutineScope
2224import kotlinx.coroutines.Dispatchers
2325import kotlinx.coroutines.SupervisorJob
2426import kotlinx.coroutines.launch
27+ import java.util.Date
2528
2629@Database(
2730 entities = [User ::class , Dishes ::class , Session ::class , AppSettings ::class ],
28- version = 5
31+ version = 6
32+ )
33+ @TypeConverters(
34+ ConvertersToDateTime ::class ,
35+ ConvertToDishesCategory ::class ,
36+ ConvertersList ::class
2937)
30- @TypeConverters(ConvertersToDateTime ::class , ConvertToDishesCategory ::class )
3138abstract class DataBase : RoomDatabase () {
3239
3340 abstract fun getUserDao (): UserDao
@@ -41,6 +48,17 @@ abstract class DataBase : RoomDatabase() {
4148
4249 private val applicationScope = CoroutineScope (SupervisorJob () + Dispatchers .IO )
4350
51+ private fun prepopulateUsers (): List <User > = listOf (
52+ User (
53+ name = " TestUser" ,
54+ gmail = " test@yandex.ru" ,
55+ password = " 1111" ,
56+ mobileNumber = " 818412481" ,
57+ dateOfBirth = Date (),
58+ favoriteDishesId = listOf (1 ,2 ,3 )
59+ )
60+ )
61+
4462 private fun prepopulateBestSellers (packageName : String ) = listOf (
4563 Dishes (
4664 imageUri = " android.resource://$packageName /${R .drawable.best_seller_card_1} " ,
@@ -92,6 +110,16 @@ abstract class DataBase : RoomDatabase() {
92110 description = " Бургер" ,
93111 category = DishCategory .MEAL
94112 ),
113+ Dishes (
114+ imageUri = " android.resource://$packageName /${R .drawable.roll} " ,
115+ price = 25.0 ,
116+ isBestSeller = false ,
117+ isRecommend = true ,
118+ rating = 5.0 ,
119+ name = " Роллы" ,
120+ description = " Роллы" ,
121+ category = DishCategory .VEGAN
122+ ),
95123 Dishes (
96124 imageUri = " android.resource://$packageName /${R .drawable.roll} " ,
97125 price = 25.0 ,
@@ -121,10 +149,16 @@ abstract class DataBase : RoomDatabase() {
121149 applicationScope.launch {
122150 val database = getDb(appContext)
123151
124- val bestSellerDao = database.getDishesDao()
125- if (bestSellerDao.getAll().isEmpty()) {
126- bestSellerDao.insertAll(prepopulateBestSellers(pkg))
152+ val dishesDao = database.getDishesDao()
153+ if (dishesDao.getAll().isEmpty()) {
154+ dishesDao.insertAll(prepopulateBestSellers(pkg))
155+ }
156+
157+ val userDao = database.getUserDao()
158+ if (userDao.getAllUsers().isEmpty()) {
159+ userDao.insertAll(prepopulateUsers())
127160 }
161+
128162 val settingsDao = database.getSettingsDao()
129163 settingsDao.upsert(AppSettings (id = 0 , isFirstRun = true ))
130164
0 commit comments