diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 29d2c40..3659f9a 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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") diff --git a/app/src/main/java/com/example/myapplication/database/MobileDatabase.kt b/app/src/main/java/com/example/myapplication/database/MobileDatabase.kt index c982351..88f64f3 100644 --- a/app/src/main/java/com/example/myapplication/database/MobileDatabase.kt +++ b/app/src/main/java/com/example/myapplication/database/MobileDatabase.kt @@ -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 + ) + ) } } } diff --git a/app/src/main/java/com/example/myapplication/database/dao/CardDao.kt b/app/src/main/java/com/example/myapplication/database/dao/CardDao.kt index dd3ff98..baccba4 100644 --- a/app/src/main/java/com/example/myapplication/database/dao/CardDao.kt +++ b/app/src/main/java/com/example/myapplication/database/dao/CardDao.kt @@ -15,7 +15,7 @@ interface CardDao { fun getAll(): Flow> @Query("select * from cards where cards.id = :id") - fun getById(id: Int): Card? + fun getById(id: Int): Flow @Query("select * from cards where cards.user_id = :userId") fun getByUserId(userId: Int): Flow> diff --git a/app/src/main/java/com/example/myapplication/database/dao/UserDao.kt b/app/src/main/java/com/example/myapplication/database/dao/UserDao.kt index c092ae7..dbdcb09 100644 --- a/app/src/main/java/com/example/myapplication/database/dao/UserDao.kt +++ b/app/src/main/java/com/example/myapplication/database/dao/UserDao.kt @@ -19,7 +19,7 @@ interface UserDao { fun getAll(): Flow> @Query("select * from users where users.id = :id") - fun getById(id: Int): User? + fun getById(id: Int): Flow @Insert(onConflict = OnConflictStrategy.IGNORE) suspend fun insert(user: User) diff --git a/app/src/main/java/com/example/myapplication/screens/authorization.kt b/app/src/main/java/com/example/myapplication/screens/authorization.kt index aab6223..f220213 100644 --- a/app/src/main/java/com/example/myapplication/screens/authorization.kt +++ b/app/src/main/java/com/example/myapplication/screens/authorization.kt @@ -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) - database.userDao().getAll().collect { data -> - users.clear() - users.addAll(data) - } - } catch (e: Exception) { - Log.e("DatabaseError", "Error accessing database", e) + val database = MobileAppDataBase.getInstance(context) + database.userDao().getAll().collect { data -> + users.clear() + data.forEach { users.add(it) } } } } diff --git a/app/src/main/java/com/example/myapplication/screens/mainScreen.kt b/app/src/main/java/com/example/myapplication/screens/mainScreen.kt index cee6796..e2d8c26 100644 --- a/app/src/main/java/com/example/myapplication/screens/mainScreen.kt +++ b/app/src/main/java/com/example/myapplication/screens/mainScreen.kt @@ -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) - database.cardDao().getAll().collect { data -> - cards.clear() - cards.addAll(data) - } - } catch (e: Exception) { - Log.e("DatabaseError", "Error accessing database", e) + val database = MobileAppDataBase.getInstance(context) + database.cardDao().getAll().collect { data -> + cards.clear() + data.forEach { cards.add(it) } } } } - - Box() { - Column( - modifier = Modifier - .fillMaxSize() - .background(Color.White) - .fillMaxHeight(0.9f) - ) { - DataListScroll(navController, cards) + 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 diff --git a/app/src/main/res/drawable/ferrari_laferrari_car2.jpg b/app/src/main/res/drawable/ferrari_laferrari_car2.jpg new file mode 100644 index 0000000..74e3749 Binary files /dev/null and b/app/src/main/res/drawable/ferrari_laferrari_car2.jpg differ diff --git a/app/src/main/res/drawable/moscowich_car3.jpeg b/app/src/main/res/drawable/moscowich_car3.jpeg new file mode 100644 index 0000000..c3f3b2f Binary files /dev/null and b/app/src/main/res/drawable/moscowich_car3.jpeg differ