-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49b50ef
commit 60f99bb
Showing
14 changed files
with
234 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 76 additions & 9 deletions
85
app/src/main/kotlin/cn/super12138/todo/logic/Repository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
app/src/main/kotlin/cn/super12138/todo/logic/dao/ToDoRoom.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
app/src/main/kotlin/cn/super12138/todo/logic/dao/ToDoRoomDB.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
app/src/main/kotlin/cn/super12138/todo/logic/dao/ToDoRoomDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
109
app/src/main/kotlin/cn/super12138/todo/logic/database/DBHelper.kt
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
app/src/main/kotlin/cn/super12138/todo/logic/model/Progress.kt
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
app/src/main/kotlin/cn/super12138/todo/logic/model/ToDoDatabase.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.