Skip to content

Commit

Permalink
将数据库使用Jetpack Room重构
Browse files Browse the repository at this point in the history
  • Loading branch information
Super12138 committed Jan 7, 2024
1 parent 49b50ef commit 60f99bb
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 194 deletions.
6 changes: 6 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.google.devtools.ksp")
}

val baseVersionName = "1.0.0"
Expand Down Expand Up @@ -60,6 +61,11 @@ dependencies {
implementation("androidx.preference:preference-ktx:1.2.1")
// Material Design
implementation("com.google.android.material:material:1.12.0-alpha02")
// Room
implementation("androidx.room:room-runtime:2.6.1")
implementation("androidx.room:room-ktx:2.6.1")
annotationProcessor("androidx.room:room-compiler:2.6.1")
ksp("androidx.room:room-compiler:2.6.1")
// Test
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/ToDoApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ package cn.super12138.todo
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import cn.super12138.todo.logic.dao.ToDoRoomDB
import cn.super12138.todo.views.crash.CrashHandler
import com.google.android.material.color.DynamicColors
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ToDoApplication : Application() {
private val database by lazy { ToDoRoomDB.getDatabase(this) }
companion object {
@SuppressLint("StaticFieldLeak")
lateinit var context: Context
lateinit var db: ToDoRoomDB
}

override fun onCreate() {
Expand All @@ -19,5 +25,7 @@ class ToDoApplication : Application() {

val crashHandler = CrashHandler(this)
Thread.setDefaultUncaughtExceptionHandler(crashHandler)

db = database
}
}
85 changes: 76 additions & 9 deletions app/src/main/kotlin/cn/super12138/todo/logic/Repository.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,88 @@
package cn.super12138.todo.logic

import android.content.ContentValues
import android.content.Context
import cn.super12138.todo.logic.database.DBHelper
import cn.super12138.todo.ToDoApplication
import cn.super12138.todo.logic.dao.ToDoRoom
import cn.super12138.todo.logic.database.SPHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

object Repository {
fun getCompleteTotalCount() = DBHelper.getCompleteTotalCount()
/**
* 获取应用设置里的数据
*/
fun getPreferenceString(context: Context, key: String, defaultValue: String) =
SPHelper.getPreferenceString(context, key, defaultValue)

fun insertData(data: ContentValues) = DBHelper.insertData(data)
// Room
private val db get() = ToDoApplication.db
val todoDao = db.toDoRoomDao()

fun deleteData(deleteAll: Boolean, uuid: String?) = DBHelper.deleteData(deleteAll, uuid)
/**
* @param toDoRoom 要插入的数据
*/
suspend fun insert(toDoRoom: ToDoRoom) {
withContext(Dispatchers.IO) {
todoDao.insert(toDoRoom)
}
}

fun updateData(uuid: String, newData: ContentValues) = DBHelper.updateData(uuid, newData)
/**
* 获取全部未完成的待办
* @return List<ToDoRoom>
*/
suspend fun getAllUncomplete(): List<ToDoRoom> {
return withContext(Dispatchers.IO) {
todoDao.getAllUnfinished()
}
}

fun getAllData() = DBHelper.getAllData()
/**
* 获取全部已完成的待办
* @return List<ToDoRoom>
*/
suspend fun getAllComplete(): List<ToDoRoom> {
return withContext(Dispatchers.IO) {
todoDao.getAllComplete()
}
}

fun getPreferenceString(context: Context, key: String, defaultValue: String) =
SPHelper.getPreferenceString(context, key, defaultValue)
/**
* 获取全部待办
* @return List<ToDoRoom>
*/
suspend fun getAll(): List<ToDoRoom> {
return withContext(Dispatchers.IO) {
todoDao.getAll()
}
}

/**
* 根据待办的UUID删除指定待办
* @param uuid 待办的UUID
*/
suspend fun deleteByUUID(uuid: String) {
withContext(Dispatchers.IO) {
todoDao.deleteByUUID(uuid)
}
}

/**
* 删除全部代办
*/
suspend fun deleteAll() {
withContext(Dispatchers.IO) {
todoDao.deleteAll()
}
}

/**
* 根据代办的UUID来把待办状态更新为“已完成”
* @param uuid 待办的UUID
*/
suspend fun updateStateByUUID(uuid: String) {
withContext(Dispatchers.IO) {
todoDao.updateStateByUUID(uuid)
}
}
}
19 changes: 19 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/logic/dao/ToDoRoom.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cn.super12138.todo.logic.dao

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

/**
* @param uuid String 待办的uuid
* @param state Int 待办的完成状态,0表示未完成,1表示完成
* @param subject String 待办的学科
* @param context String 待办的内容
*/
@Entity(tableName = "todo")
data class ToDoRoom(
@PrimaryKey @ColumnInfo(name = "uuid") val uuid: String,
@ColumnInfo(name = "state") val state: Int,
@ColumnInfo(name = "subject") val subject: String,
@ColumnInfo(name = "context") val context: String
)
27 changes: 27 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/logic/dao/ToDoRoomDB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cn.super12138.todo.logic.dao

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [ToDoRoom::class], version = 1)
abstract class ToDoRoomDB : RoomDatabase() {
abstract fun toDoRoomDao(): ToDoRoomDao

companion object {
@Volatile
private var INSTANCE: ToDoRoomDB? = null
fun getDatabase(context: Context): ToDoRoomDB {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
ToDoRoomDB::class.java,
"todo"
).build()
INSTANCE = instance
instance
}
}
}
}
29 changes: 29 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/logic/dao/ToDoRoomDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cn.super12138.todo.logic.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query

@Dao
interface ToDoRoomDao {
@Insert
suspend fun insert(toDoRoom: ToDoRoom)

@Query("SELECT * FROM todo")
suspend fun getAll(): List<ToDoRoom>

@Query("SELECT * FROM todo WHERE state = 0")
suspend fun getAllUnfinished(): List<ToDoRoom>

@Query("SELECT * FROM todo WHERE state = 1")
suspend fun getAllComplete(): List<ToDoRoom>

@Query("DELETE FROM todo")
suspend fun deleteAll()

@Query("DELETE FROM todo WHERE uuid = :uuid")
suspend fun deleteByUUID(uuid: String)

@Query("UPDATE todo SET state = 1 WHERE uuid = :uuid")
suspend fun updateStateByUUID(uuid: String)
}
109 changes: 0 additions & 109 deletions app/src/main/kotlin/cn/super12138/todo/logic/database/DBHelper.kt

This file was deleted.

This file was deleted.

28 changes: 0 additions & 28 deletions app/src/main/kotlin/cn/super12138/todo/logic/model/ToDoDatabase.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cn.super12138.todo.views.crash
import android.content.Context
import android.content.Intent
import android.os.Process
import kotlin.system.exitProcess

class CrashHandler(private val context: Context) : Thread.UncaughtExceptionHandler {

Expand All @@ -18,11 +19,11 @@ class CrashHandler(private val context: Context) : Thread.UncaughtExceptionHandl
}
context.startActivity(intent)

// 杀掉崩溃的应用程序进程
Process.killProcess(Process.myPid())
System.exit(10)

// 传递异常给默认的异常处理器
defaultUEH?.uncaughtException(thread, ex)

// 杀掉崩溃的应用程序进程
Process.killProcess(Process.myPid())
exitProcess(10)
}
}
Loading

0 comments on commit 60f99bb

Please sign in to comment.