Кое-что добавил
This commit is contained in:
parent
1cfd40ae3e
commit
b45183ebcc
@ -1,11 +1,53 @@
|
|||||||
package com.example.mobileapp.database
|
package com.example.mobileapp.database
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import androidx.room.Database
|
import androidx.room.Database
|
||||||
|
import androidx.room.Room
|
||||||
import androidx.room.RoomDatabase
|
import androidx.room.RoomDatabase
|
||||||
|
import androidx.room.TypeConverters
|
||||||
|
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||||
|
import com.example.mobileapp.database.dao.MailDao
|
||||||
|
import com.example.mobileapp.database.dao.StoryDao
|
||||||
|
import com.example.mobileapp.database.dao.UserDao
|
||||||
|
import com.example.mobileapp.database.entities.Converters
|
||||||
import com.example.mobileapp.database.entities.Mail
|
import com.example.mobileapp.database.entities.Mail
|
||||||
import com.example.mobileapp.database.entities.Story
|
import com.example.mobileapp.database.entities.Story
|
||||||
import com.example.mobileapp.database.entities.User
|
import com.example.mobileapp.database.entities.User
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@Database(entities = [User::class, Story::class, Mail::class], version = 1, exportSchema = false)
|
@Database(entities = [User::class, Story::class, Mail::class], version = 1, exportSchema = false)
|
||||||
|
@TypeConverters(Converters::class)
|
||||||
abstract class MobileAppDataBase : RoomDatabase() {
|
abstract class MobileAppDataBase : RoomDatabase() {
|
||||||
|
abstract fun userDao(): UserDao
|
||||||
|
abstract fun storyDao(): StoryDao
|
||||||
|
abstract fun mailDao(): MailDao
|
||||||
|
|
||||||
|
companion object{
|
||||||
|
private const val DB_NAME: String = "mobileApp.db"
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private var INSTANCE: MobileAppDataBase? = null
|
||||||
|
|
||||||
|
fun getInstance(appContext: Context): MobileAppDataBase {
|
||||||
|
return INSTANCE ?: synchronized(this) {
|
||||||
|
Room.databaseBuilder(
|
||||||
|
appContext,
|
||||||
|
MobileAppDataBase::class.java,
|
||||||
|
DB_NAME
|
||||||
|
)
|
||||||
|
.addCallback(object : Callback() {
|
||||||
|
override fun onCreate(db: SupportSQLiteDatabase) {
|
||||||
|
super.onCreate(db)
|
||||||
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
|
//populateDatabase()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.build()
|
||||||
|
.also { INSTANCE = it }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,7 +1,11 @@
|
|||||||
package com.example.mobileapp.database.dao
|
package com.example.mobileapp.database.dao
|
||||||
|
|
||||||
import androidx.room.Dao
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Delete
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.OnConflictStrategy
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
|
import androidx.room.Update
|
||||||
import com.example.mobileapp.database.entities.Mail
|
import com.example.mobileapp.database.entities.Mail
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
@ -9,4 +13,16 @@ import kotlinx.coroutines.flow.Flow
|
|||||||
interface MailDao {
|
interface MailDao {
|
||||||
@Query("select * from mails")
|
@Query("select * from mails")
|
||||||
fun getAll(): Flow<List<Mail>>
|
fun getAll(): Flow<List<Mail>>
|
||||||
|
|
||||||
|
@Query("select * from mails where mails.id = :id")
|
||||||
|
fun getById(id: Int): Mail?
|
||||||
|
|
||||||
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
|
suspend fun insert(mail: Mail)
|
||||||
|
|
||||||
|
@Update
|
||||||
|
suspend fun update(mail: Mail)
|
||||||
|
|
||||||
|
@Delete
|
||||||
|
suspend fun delete(mail: Mail)
|
||||||
}
|
}
|
@ -1,7 +1,11 @@
|
|||||||
package com.example.mobileapp.database.dao
|
package com.example.mobileapp.database.dao
|
||||||
|
|
||||||
import androidx.room.Dao
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Delete
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.OnConflictStrategy
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
|
import androidx.room.Update
|
||||||
import com.example.mobileapp.database.entities.Story
|
import com.example.mobileapp.database.entities.Story
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
@ -9,4 +13,16 @@ import kotlinx.coroutines.flow.Flow
|
|||||||
interface StoryDao {
|
interface StoryDao {
|
||||||
@Query("select * from stories")
|
@Query("select * from stories")
|
||||||
fun getAll(): Flow<List<Story>>
|
fun getAll(): Flow<List<Story>>
|
||||||
|
|
||||||
|
@Query("select * from stories where stories.id = :id")
|
||||||
|
fun getById(id: Int): Story?
|
||||||
|
|
||||||
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
|
suspend fun insert(story: Story)
|
||||||
|
|
||||||
|
@Update
|
||||||
|
suspend fun update(story: Story)
|
||||||
|
|
||||||
|
@Delete
|
||||||
|
suspend fun delete(story: Story)
|
||||||
}
|
}
|
@ -1,7 +1,11 @@
|
|||||||
package com.example.mobileapp.database.dao
|
package com.example.mobileapp.database.dao
|
||||||
|
|
||||||
import androidx.room.Dao
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Delete
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.OnConflictStrategy
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
|
import androidx.room.Update
|
||||||
import com.example.mobileapp.database.entities.User
|
import com.example.mobileapp.database.entities.User
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
@ -9,4 +13,16 @@ import kotlinx.coroutines.flow.Flow
|
|||||||
interface UserDao {
|
interface UserDao {
|
||||||
@Query("select * from users")
|
@Query("select * from users")
|
||||||
fun getAll():Flow<List<User>>
|
fun getAll():Flow<List<User>>
|
||||||
|
|
||||||
|
@Query("select * from users where users.id = :id")
|
||||||
|
fun getById(id: Int): User?
|
||||||
|
|
||||||
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
|
suspend fun insert(user: User)
|
||||||
|
|
||||||
|
@Update
|
||||||
|
suspend fun update(user: User)
|
||||||
|
|
||||||
|
@Delete
|
||||||
|
suspend fun delete(user: User)
|
||||||
}
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.example.mobileapp.database.entities
|
||||||
|
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.graphics.BitmapFactory
|
||||||
|
import androidx.room.TypeConverter
|
||||||
|
import java.io.ByteArrayOutputStream
|
||||||
|
|
||||||
|
class Converters {
|
||||||
|
@TypeConverter
|
||||||
|
fun fromBitmap(bitmap: Bitmap) : ByteArray {
|
||||||
|
val outputStream = ByteArrayOutputStream();
|
||||||
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
|
||||||
|
return outputStream.toByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
fun toBitmap(byteArray: ByteArray): Bitmap {
|
||||||
|
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user