From a8aa50f80b6d3224af5e5511ab50a9cc0892468e Mon Sep 17 00:00:00 2001 From: Super12138 <70494801+Super12138@users.noreply.github.com> Date: Sun, 23 Feb 2025 11:02:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=A3=E5=BC=8F=E5=8F=91=E5=B8=832.1.2?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复英文情况下字符串双引号不显示的问题 优化备份与恢复数据库逻辑(使用 GitHub Copilot) 增大待办圆环 修复部分遗留的错误变量名称 优化主页性能 --- app/build.gradle.kts | 5 +-- .../todo/logic/model/SortingMethod.kt | 6 +-- .../super12138/todo/ui/pages/main/MainPage.kt | 11 +++-- .../todo/ui/pages/main/ProgressFragment.kt | 3 +- .../todo/ui/viewmodels/MainViewModel.kt | 40 +++++++------------ app/src/main/res/values/strings.xml | 2 +- 6 files changed, 29 insertions(+), 38 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index ac516b3..0d540ef 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -34,8 +34,8 @@ android { applicationId = "cn.super12138.todo" minSdk = 24 targetSdk = 35 - versionCode = 564 - versionName = "2.0.2" + versionCode = 567 + versionName = "2.1.2" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" @@ -83,7 +83,6 @@ dependencies { implementation(libs.androidx.lifecycle.runtime.ktx) implementation(libs.androidx.lifecycle.viewmodel.compose) implementation(libs.androidx.compose.runtime.livedata) - // implementation(libs.androidx.security) // Compose implementation(libs.androidx.activity.compose) implementation(platform(libs.androidx.compose.bom)) diff --git a/app/src/main/kotlin/cn/super12138/todo/logic/model/SortingMethod.kt b/app/src/main/kotlin/cn/super12138/todo/logic/model/SortingMethod.kt index 46a1dd3..dff0552 100644 --- a/app/src/main/kotlin/cn/super12138/todo/logic/model/SortingMethod.kt +++ b/app/src/main/kotlin/cn/super12138/todo/logic/model/SortingMethod.kt @@ -4,7 +4,7 @@ import android.content.Context import cn.super12138.todo.R enum class SortingMethod(val id: Int) { - Date(1), + Sequential(1), Subject(2), Priority(3), Completion(4), @@ -13,7 +13,7 @@ enum class SortingMethod(val id: Int) { fun getDisplayName(context: Context): String { val resId = when (this) { - Date -> R.string.sorting_sequential + Sequential -> R.string.sorting_sequential Subject -> R.string.sorting_subject Priority -> R.string.sorting_priority Completion -> R.string.sorting_completion @@ -25,7 +25,7 @@ enum class SortingMethod(val id: Int) { companion object { fun fromId(id: Int): SortingMethod { - return SortingMethod.entries.find { it.id == id } ?: Date + return SortingMethod.entries.find { it.id == id } ?: Sequential } } } \ No newline at end of file diff --git a/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/MainPage.kt b/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/MainPage.kt index 6a3f69e..cb587bd 100644 --- a/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/MainPage.kt +++ b/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/MainPage.kt @@ -80,6 +80,9 @@ fun MainPage( val filteredTodoList = if (showCompleted) toDoList else toDoList.filter { item -> !item.isCompleted } + val totalTasks by remember { derivedStateOf { toDoList.size } } + val completedTasks by remember { derivedStateOf { toDoList.count { it.isCompleted } } } + BackHandler(enabled = !isSelectedIdsEmpty) { // 当按下返回键(或进行返回操作)时清空选择 viewModel.clearAllTodoSelection() @@ -126,8 +129,8 @@ fun MainPage( if (windowSizeClass.windowWidthSizeClass == WindowWidthSizeClass.COMPACT) { Column(modifier = Modifier.padding(innerPadding)) { ProgressFragment( - totalTasks = toDoList.size, - completedTasks = toDoList.count { it.isCompleted }, + totalTasks = totalTasks, + completedTasks = completedTasks, modifier = Modifier .weight(2f) .fillMaxSize() @@ -172,8 +175,8 @@ fun MainPage( } else { Row(modifier = Modifier.padding(innerPadding)) { ProgressFragment( - totalTasks = toDoList.size, - completedTasks = toDoList.count { it.isCompleted }, + totalTasks = totalTasks, + completedTasks = completedTasks, modifier = Modifier .weight(2f) .fillMaxSize() diff --git a/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/ProgressFragment.kt b/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/ProgressFragment.kt index 08b6e6c..1553e17 100644 --- a/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/ProgressFragment.kt +++ b/app/src/main/kotlin/cn/super12138/todo/ui/pages/main/ProgressFragment.kt @@ -45,8 +45,9 @@ fun ProgressFragment( progress = { animatedProgress }, strokeWidth = 10.dp, gapSize = 10.dp, - modifier = Modifier.size(170.dp) + modifier = Modifier.size(175.dp) ) + Column(horizontalAlignment = Alignment.CenterHorizontally) { Row(verticalAlignment = Alignment.CenterVertically) { Text( diff --git a/app/src/main/kotlin/cn/super12138/todo/ui/viewmodels/MainViewModel.kt b/app/src/main/kotlin/cn/super12138/todo/ui/viewmodels/MainViewModel.kt index a419255..ae9a516 100644 --- a/app/src/main/kotlin/cn/super12138/todo/ui/viewmodels/MainViewModel.kt +++ b/app/src/main/kotlin/cn/super12138/todo/ui/viewmodels/MainViewModel.kt @@ -30,7 +30,6 @@ import java.io.BufferedInputStream import java.io.BufferedOutputStream import java.io.File import java.io.FileOutputStream -import java.util.zip.ZipEntry import java.util.zip.ZipInputStream import java.util.zip.ZipOutputStream @@ -40,7 +39,7 @@ class MainViewModel : ViewModel() { var appSortingMethod by mutableStateOf(SortingMethod.fromId(GlobalValues.sortingMethod)) val sortedTodos: Flow> = toDos.map { list -> when (appSortingMethod) { - SortingMethod.Date -> list.sortedBy { it.id } + SortingMethod.Sequential -> list.sortedBy { it.id } SortingMethod.Subject -> list.sortedBy { it.subject } SortingMethod.Priority -> list.sortedByDescending { it.priority } // 优先级高的在前 SortingMethod.Completion -> list.sortedBy { it.isCompleted } // 未完成的在前 @@ -186,26 +185,21 @@ class MainViewModel : ViewModel() { // 开启输出文件流,输出位置为用户选取的文件夹URI context.contentResolver.openOutputStream(uri)?.use { outputStream -> ZipOutputStream(BufferedOutputStream(outputStream)).use { zipOutStream -> - val dbFile = context.getDatabasePath(Constants.DB_NAME) - val dbWal = File("$dbPath-wal") - val dbShm = File("$dbPath-shm") - - FileUtils.addFileToZip(dbFile, dbFile.name, zipOutStream) - FileUtils.addFileToZip(dbWal, dbWal.name, zipOutStream) - FileUtils.addFileToZip(dbShm, dbShm.name, zipOutStream) + listOf( + context.getDatabasePath(Constants.DB_NAME), + File("$dbPath-wal"), + File("$dbPath-shm") + ).forEach { file -> + FileUtils.addFileToZip(file, file.name, zipOutStream) + } } } - // 执行成功的回调 - withContext(Dispatchers.Main) { - onResult(true) // 成功 - } + withContext(Dispatchers.Main) { onResult(true) } } catch (e: Exception) { e.printStackTrace() // 执行失败的回调 - withContext(Dispatchers.Main) { - onResult(false) // 失败 - } + withContext(Dispatchers.Main) { onResult(false) } } } } @@ -213,12 +207,11 @@ class MainViewModel : ViewModel() { fun restoreDatabase(uri: Uri, context: Context, onResult: (completed: Boolean) -> Unit) { viewModelScope.launch(Dispatchers.IO) { try { - // 获取数据库文件路径 + // 获取数据库文件夹 val outputPath = context.getDatabasePath(Constants.DB_NAME).parent context.contentResolver.openInputStream(uri)?.use { inputStream -> ZipInputStream(BufferedInputStream(inputStream)).use { zipInputStream -> - var zipEntry: ZipEntry? = zipInputStream.nextEntry - while (zipEntry != null) { + generateSequence { zipInputStream.nextEntry }.forEach { zipEntry -> val outputFile = File(outputPath, zipEntry.name) if (zipEntry.isDirectory) { outputFile.mkdirs() @@ -227,18 +220,13 @@ class MainViewModel : ViewModel() { FileOutputStream(outputFile).use { zipInputStream.copyTo(it) } } zipInputStream.closeEntry() - zipEntry = zipInputStream.nextEntry } } } - withContext(Dispatchers.Main) { - onResult(true) // 成功 - } + withContext(Dispatchers.Main) { onResult(true) } } catch (e: Exception) { e.printStackTrace() - withContext(Dispatchers.Main) { - onResult(false) // 失败 - } + withContext(Dispatchers.Main) { onResult(false) } } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 31c9f35..db35b6d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -94,7 +94,7 @@ Haptic Feedback Provide slight vibration feedback for some operations Tips - Before using this feature, you need to enable haptic feedback in the system settings (usually under the "Sound & Vibration" section in the "Haptic Feedback" option) + Before using this feature, you need to enable haptic feedback in the system settings (usually under the \"Sound & Vibration\" section in the \"Haptic Feedback\" option) Data Backup and Restore, Manage Data Backup