3-ая УРА БД ЗАРАБОТАЛАААА
This commit is contained in:
parent
ad397832d9
commit
7dedc80ec9
@ -1,3 +1,5 @@
|
||||
import org.jetbrains.kotlin.kapt3.base.Kapt.kapt
|
||||
|
||||
plugins {
|
||||
id("com.android.application")
|
||||
id("org.jetbrains.kotlin.android")
|
||||
@ -70,6 +72,7 @@ dependencies {
|
||||
debugImplementation("androidx.compose.ui:ui-tooling")
|
||||
debugImplementation("androidx.compose.ui:ui-test-manifest")
|
||||
|
||||
|
||||
// Room
|
||||
val room_version = "2.5.2"
|
||||
implementation("androidx.room:room-runtime:$room_version")
|
||||
|
@ -1,3 +1,5 @@
|
||||
package com.example.myapplication.database
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.BitmapFactory
|
||||
import android.util.Log
|
||||
@ -23,22 +25,19 @@ abstract class MobileAppDataBase : RoomDatabase() {
|
||||
abstract fun cardDao(): CardDao
|
||||
|
||||
companion object {
|
||||
private const val DB_NAME: String = "my-db"
|
||||
private const val DB_NAME: String = "my-db.db"
|
||||
|
||||
@Volatile
|
||||
private var INSTANCE: MobileAppDataBase? = null
|
||||
|
||||
fun getInstance(appContext: Context): MobileAppDataBase {
|
||||
return INSTANCE ?: synchronized(this) {
|
||||
INSTANCE?.let { return@let it } // Возвращаем INSTANCE, если он уже был создан
|
||||
|
||||
val instance = Room.databaseBuilder(
|
||||
Room.databaseBuilder(
|
||||
appContext,
|
||||
MobileAppDataBase::class.java,
|
||||
DB_NAME
|
||||
)
|
||||
.fallbackToDestructiveMigration()
|
||||
.addCallback(object : RoomDatabase.Callback() {
|
||||
.addCallback(object : Callback() {
|
||||
override fun onCreate(db: SupportSQLiteDatabase) {
|
||||
super.onCreate(db)
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
@ -46,12 +45,8 @@ abstract class MobileAppDataBase : RoomDatabase() {
|
||||
}
|
||||
}
|
||||
})
|
||||
.also { INSTANCE = it.build() } // Сохраняем INSTANCE
|
||||
.build()
|
||||
|
||||
Log.d("Database", "Database instance created")
|
||||
|
||||
instance // Возвращаем созданный instance
|
||||
.also { INSTANCE = it }
|
||||
}
|
||||
}
|
||||
|
||||
@ -77,17 +72,43 @@ abstract class MobileAppDataBase : RoomDatabase() {
|
||||
)
|
||||
cardDao.insert(
|
||||
Card(
|
||||
name = "Москоувич",
|
||||
location = "г. Москва",
|
||||
name = "Феррари Два",
|
||||
location = "г. Ульяновск",
|
||||
image = BitmapFactory.decodeResource(
|
||||
appContext.resources,
|
||||
R.drawable.moscowich_car3
|
||||
R.drawable.ferrari_laferrari_car2
|
||||
),
|
||||
mileage = 156771,
|
||||
price = 1000,
|
||||
mileage = 1233,
|
||||
price = 15000,
|
||||
userId = 2
|
||||
)
|
||||
)
|
||||
cardDao.insert(
|
||||
Card(
|
||||
name = "Феррари Имба",
|
||||
location = "г. Ульяновск",
|
||||
image = BitmapFactory.decodeResource(
|
||||
appContext.resources,
|
||||
R.drawable.ferrari_laferrari_car2
|
||||
),
|
||||
mileage = 7322,
|
||||
price = 125000,
|
||||
userId = 1
|
||||
)
|
||||
)
|
||||
cardDao.insert(
|
||||
Card(
|
||||
name = "Феррари Два",
|
||||
location = "г. Ульяновск",
|
||||
image = BitmapFactory.decodeResource(
|
||||
appContext.resources,
|
||||
R.drawable.ferrari_laferrari_car2
|
||||
),
|
||||
mileage = 1233,
|
||||
price = 15000,
|
||||
userId = 2
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ interface CardDao {
|
||||
fun getAll(): Flow<List<Card>>
|
||||
|
||||
@Query("select * from cards where cards.id = :id")
|
||||
fun getById(id: Int): Card?
|
||||
fun getById(id: Int): Flow<Card?>
|
||||
|
||||
@Query("select * from cards where cards.user_id = :userId")
|
||||
fun getByUserId(userId: Int): Flow<List<Card>>
|
||||
|
@ -19,7 +19,7 @@ interface UserDao {
|
||||
fun getAll(): Flow<List<User>>
|
||||
|
||||
@Query("select * from users where users.id = :id")
|
||||
fun getById(id: Int): User?
|
||||
fun getById(id: Int): Flow<User?>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
suspend fun insert(user: User)
|
||||
|
@ -31,6 +31,7 @@ import com.example.myapplication.R
|
||||
import com.example.myapplication.components.LoginField
|
||||
import com.example.myapplication.components.PasswordField
|
||||
import com.example.myapplication.components.navButton
|
||||
import com.example.myapplication.database.MobileAppDataBase
|
||||
import com.example.myapplication.database.entities.Card
|
||||
import com.example.myapplication.database.entities.User
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@ -43,14 +44,10 @@ fun Authorization(navController: NavHostController){
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val database = MobileAppDataBase.getInstance(context.applicationContext)
|
||||
val database = MobileAppDataBase.getInstance(context)
|
||||
database.userDao().getAll().collect { data ->
|
||||
users.clear()
|
||||
users.addAll(data)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("DatabaseError", "Error accessing database", e)
|
||||
data.forEach { users.add(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -37,6 +37,8 @@ import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.requiredHeight
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
@ -48,6 +50,7 @@ import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.example.myapplication.R
|
||||
import com.example.myapplication.database.MobileAppDataBase
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
@ -60,38 +63,33 @@ fun MainScreen(navController: NavHostController) {
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val database = MobileAppDataBase.getInstance(context.applicationContext)
|
||||
val database = MobileAppDataBase.getInstance(context)
|
||||
database.cardDao().getAll().collect { data ->
|
||||
cards.clear()
|
||||
cards.addAll(data)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("DatabaseError", "Error accessing database", e)
|
||||
data.forEach { cards.add(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Box() {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(Color.White)
|
||||
.fillMaxHeight(0.9f)
|
||||
) {
|
||||
Column {
|
||||
Box(modifier = Modifier
|
||||
.padding(horizontal = 10.dp)
|
||||
.fillMaxHeight(0.9f)) {
|
||||
Column()
|
||||
{
|
||||
DataListScroll(navController, cards)
|
||||
}
|
||||
}
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.fillMaxWidth()
|
||||
.background(SkyBlue)
|
||||
.fillMaxHeight(0.1f),
|
||||
.weight(1f),
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
navBar(navController = navController)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
@ -274,7 +272,7 @@ fun addNewListItem(navController: NavHostController, destination: String){
|
||||
},
|
||||
shape = RoundedCornerShape(15.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = Color.Magenta
|
||||
containerColor = SkyBlue
|
||||
),
|
||||
elevation = CardDefaults.cardElevation(
|
||||
defaultElevation = 8.dp
|
||||
|
BIN
app/src/main/res/drawable/ferrari_laferrari_car2.jpg
Normal file
BIN
app/src/main/res/drawable/ferrari_laferrari_car2.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 89 KiB |
BIN
app/src/main/res/drawable/moscowich_car3.jpeg
Normal file
BIN
app/src/main/res/drawable/moscowich_car3.jpeg
Normal file
Binary file not shown.
After Width: | Height: | Size: 78 KiB |
Loading…
Reference in New Issue
Block a user