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