-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBase.kt
More file actions
178 lines (164 loc) · 6.6 KB
/
DataBase.kt
File metadata and controls
178 lines (164 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.example.projectfigma.DataBase
import android.content.Context
import android.text.style.TtsSpan
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.projectfigma.Converters.ConvertToDishesCategory
import com.example.projectfigma.Converters.ConvertersList
import com.example.projectfigma.DAO.DishesDao
import com.example.projectfigma.DAO.SettingsDao
import com.example.projectfigma.DAO.SessionDao
import com.example.projectfigma.DAO.UserDao
import com.example.projectfigma.Entites.AppSettings
import com.example.projectfigma.Entites.Dishes
import com.example.projectfigma.Entites.Session
import com.example.projectfigma.Entites.User
import com.example.projectfigma.Enums.DishCategory
import com.example.projectfigma.R
import com.example.projectfigma.Converters.ConvertersToDateTime
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import java.util.Date
@Database(
entities = [User::class, Dishes::class, Session::class, AppSettings::class],
version = 6
)
@TypeConverters(
ConvertersToDateTime::class,
ConvertToDishesCategory::class,
ConvertersList::class
)
abstract class DataBase : RoomDatabase() {
abstract fun getUserDao(): UserDao
abstract fun getDishesDao(): DishesDao
abstract fun getSessionDao(): SessionDao
abstract fun getSettingsDao(): SettingsDao
companion object {
@Volatile
private var INSTANCE: DataBase? = null
private val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
private fun prepopulateUsers(): List<User> = listOf(
User(
name = "TestUser",
gmail = "test@yandex.ru",
password = "1111",
mobileNumber = "818412481",
dateOfBirth = Date(),
favoriteDishesId = listOf(1,2,3)
)
)
private fun prepopulateBestSellers(packageName: String) = listOf(
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.best_seller_card_1}",
price = 103.0,
isBestSeller = true,
isRecommend = false,
rating = 3.0,
name = "Суши",
description = "Суши",
category = DishCategory.MEAL
),
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.best_seller_card_2}",
price = 50.0,
isBestSeller = true,
isRecommend = false,
rating = 4.0,
name = "Рис с курицей",
description = "Рис с курицей в подливе",
category = DishCategory.MEAL
),
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.best_seller_card_3}",
price = 12.99,
isBestSeller = true,
isRecommend = false,
rating = 4.0,
name = "Лазанья",
description = "Лазанья",
category = DishCategory.VEGAN
),
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.best_seller_card_4}",
price = 8.20,
isBestSeller = true,
isRecommend = false,
rating = 4.0,
name = "Пироженное",
description = "Пироженное",
category = DishCategory.DESERT
),
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.burger}",
price = 10.0,
isBestSeller = false,
isRecommend = true,
rating = 5.0,
name = "Бургер",
description = "Бургер",
category = DishCategory.MEAL
),
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.roll}",
price = 25.0,
isBestSeller = false,
isRecommend = true,
rating = 5.0,
name = "Роллы",
description = "Роллы",
category = DishCategory.VEGAN
),
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.roll}",
price = 25.0,
isBestSeller = false,
isRecommend = true,
rating = 5.0,
name = "Роллы",
description = "Роллы",
category = DishCategory.VEGAN
)
)
fun getDb(context: Context): DataBase {
val appContext = context.applicationContext
val pkg = appContext.packageName
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
appContext,
DataBase::class.java,
"app_database"
)
.fallbackToDestructiveMigration()
.addCallback(object : Callback() {
override fun onOpen(db: SupportSQLiteDatabase) {
super.onOpen(db)
applicationScope.launch {
val database = getDb(appContext)
val dishesDao = database.getDishesDao()
if (dishesDao.getAll().isEmpty()) {
dishesDao.insertAll(prepopulateBestSellers(pkg))
}
val userDao = database.getUserDao()
if (userDao.getAllUsers().isEmpty()) {
userDao.insertAll(prepopulateUsers())
}
val settingsDao = database.getSettingsDao()
settingsDao.upsert(AppSettings(id = 0, isFirstRun = true))
val sessionDao = database.getSessionDao()
sessionDao.upsert(Session(id = 0, isLoggedIn = false, userEmail = null))
}
}
})
.allowMainThreadQueries()
.build()
INSTANCE = instance
instance
}
}
}
}