feat: 数据库架构初步实现
This commit is contained in:
parent
daab0172a5
commit
daa9972651
6 changed files with 102 additions and 1 deletions
|
|
@ -3,6 +3,7 @@
|
|||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<application
|
||||
android:name=".TodoApp"
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
|
|
@ -11,7 +12,7 @@
|
|||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.ToDo"
|
||||
tools:targetApi="31">
|
||||
tools:targetApi="35">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
|
|
|
|||
17
app/src/main/kotlin/cn/super12138/todo/TodoApp.kt
Normal file
17
app/src/main/kotlin/cn/super12138/todo/TodoApp.kt
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package cn.super12138.todo
|
||||
|
||||
import android.app.Application
|
||||
import cn.super12138.todo.logic.database.TodoDatabase
|
||||
|
||||
class TodoApp : Application() {
|
||||
private val database by lazy { TodoDatabase.getDatabase(this) }
|
||||
|
||||
companion object {
|
||||
lateinit var db: TodoDatabase
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
db = database
|
||||
}
|
||||
}
|
||||
20
app/src/main/kotlin/cn/super12138/todo/logic/Repository.kt
Normal file
20
app/src/main/kotlin/cn/super12138/todo/logic/Repository.kt
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package cn.super12138.todo.logic
|
||||
|
||||
import cn.super12138.todo.TodoApp
|
||||
import cn.super12138.todo.logic.database.TodoEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
object Repository {
|
||||
private val db get() = TodoApp.db
|
||||
private val toDoDao = db.toDoDao()
|
||||
|
||||
suspend fun insertTodo(toDo: TodoEntity) {
|
||||
toDoDao.insert(toDo)
|
||||
}
|
||||
|
||||
fun getAllTodos(): Flow<List<TodoEntity>> = toDoDao.getAll()
|
||||
|
||||
suspend fun updateTodo(toDo: TodoEntity){
|
||||
toDoDao.update(toDo)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package cn.super12138.todo.logic.database
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface TodoDao {
|
||||
@Insert
|
||||
suspend fun insert(toDo: TodoEntity)
|
||||
|
||||
@Query("SELECT * FROM todo")
|
||||
fun getAll(): Flow<List<TodoEntity>>
|
||||
|
||||
@Update
|
||||
suspend fun update(toDo: TodoEntity)
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package cn.super12138.todo.logic.database
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
|
||||
@Database(entities = [TodoEntity::class], version = 2)
|
||||
abstract class TodoDatabase : RoomDatabase() {
|
||||
abstract fun toDoDao(): TodoDao
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var INSTANCE: TodoDatabase? = null
|
||||
fun getDatabase(context: Context): TodoDatabase {
|
||||
return INSTANCE ?: synchronized(this) {
|
||||
val instance = Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
TodoDatabase::class.java,
|
||||
"todo"
|
||||
)
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
|
||||
INSTANCE = instance
|
||||
return instance
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package cn.super12138.todo.logic.database
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "todo")
|
||||
data class TodoEntity(
|
||||
@ColumnInfo(name = "type") val type: Int,
|
||||
@ColumnInfo(name = "content") val content: String,
|
||||
@ColumnInfo(name = "subject") val subject: String,
|
||||
@ColumnInfo(name = "completed") val isCompleted: Boolean = false,
|
||||
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") val id: Long = 0L,
|
||||
)
|
||||
Loading…
Reference in a new issue