refactor: 优化学科的存储方式
This commit is contained in:
parent
88dfabfa9c
commit
d19311b6bf
8 changed files with 73 additions and 34 deletions
|
|
@ -7,7 +7,7 @@ import androidx.room.PrimaryKey
|
||||||
@Entity(tableName = "todo")
|
@Entity(tableName = "todo")
|
||||||
data class TodoEntity(
|
data class TodoEntity(
|
||||||
@ColumnInfo(name = "content") val content: String,
|
@ColumnInfo(name = "content") val content: String,
|
||||||
@ColumnInfo(name = "subject") val subject: String,
|
@ColumnInfo(name = "subject") val subject: Int,
|
||||||
@ColumnInfo(name = "completed") val isCompleted: Boolean = false,
|
@ColumnInfo(name = "completed") val isCompleted: Boolean = false,
|
||||||
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") val id: Int = 0,
|
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") val id: Int = 0,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
package cn.super12138.todo.logic.model
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import cn.super12138.todo.R
|
||||||
|
|
||||||
|
enum class Subjects(val id: Int) {
|
||||||
|
Chinese(0),
|
||||||
|
Math(1),
|
||||||
|
English(2),
|
||||||
|
Biology(3),
|
||||||
|
Geography(4),
|
||||||
|
Physics(5),
|
||||||
|
Moral(6),
|
||||||
|
Chemistry(7),
|
||||||
|
History(8),
|
||||||
|
Others(99);
|
||||||
|
|
||||||
|
fun getDisplayName(context: Context): String {
|
||||||
|
val resId = when (this) {
|
||||||
|
Chinese -> R.string.subject_chinese
|
||||||
|
Math -> R.string.subject_math
|
||||||
|
English -> R.string.subject_english
|
||||||
|
Biology -> R.string.subject_biology
|
||||||
|
Geography -> R.string.subject_geography
|
||||||
|
Physics -> R.string.subject_physics
|
||||||
|
Moral -> R.string.subject_moral
|
||||||
|
Chemistry -> R.string.subject_chemistry
|
||||||
|
History -> R.string.subject_history
|
||||||
|
Others -> R.string.subject_others
|
||||||
|
}
|
||||||
|
return context.getString(resId) // 返回资源中的文本
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
// 根据 ID 获取 Subjects
|
||||||
|
fun fromId(id: Int): Subjects {
|
||||||
|
return entries.find { it.id == id } ?: Others
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -13,10 +13,12 @@ import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import cn.super12138.todo.R
|
import cn.super12138.todo.R
|
||||||
import cn.super12138.todo.logic.database.TodoEntity
|
import cn.super12138.todo.logic.database.TodoEntity
|
||||||
|
import cn.super12138.todo.logic.model.Subjects
|
||||||
import cn.super12138.todo.ui.pages.main.components.TodoCard
|
import cn.super12138.todo.ui.pages.main.components.TodoCard
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
|
@ -27,6 +29,7 @@ fun ManagerFragment(
|
||||||
onItemChecked: (TodoEntity) -> Unit = {},
|
onItemChecked: (TodoEntity) -> Unit = {},
|
||||||
modifier: Modifier = Modifier
|
modifier: Modifier = Modifier
|
||||||
) {
|
) {
|
||||||
|
val context = LocalContext.current
|
||||||
LazyColumn(
|
LazyColumn(
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
state = state,
|
state = state,
|
||||||
|
|
@ -55,7 +58,7 @@ fun ManagerFragment(
|
||||||
) { item ->
|
) { item ->
|
||||||
TodoCard(
|
TodoCard(
|
||||||
content = item.content,
|
content = item.content,
|
||||||
subject = item.subject,
|
subject = Subjects.fromId(item.subject).getDisplayName(context),
|
||||||
onCardClick = { onItemClick(item) },
|
onCardClick = { onItemClick(item) },
|
||||||
onChecked = { onItemChecked(item) },
|
onChecked = { onItemChecked(item) },
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import cn.super12138.todo.R
|
import cn.super12138.todo.R
|
||||||
import cn.super12138.todo.logic.database.TodoEntity
|
import cn.super12138.todo.logic.database.TodoEntity
|
||||||
|
import cn.super12138.todo.logic.model.Subjects
|
||||||
import cn.super12138.todo.ui.pages.main.components.FilterChipGroup
|
import cn.super12138.todo.ui.pages.main.components.FilterChipGroup
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
|
|
@ -76,7 +77,11 @@ fun TodoBottomSheet(
|
||||||
|
|
||||||
Spacer(Modifier.size(5.dp))
|
Spacer(Modifier.size(5.dp))
|
||||||
|
|
||||||
val subjects = remember { context.resources.getStringArray(R.array.subjects).toList() }
|
val subjects = remember {
|
||||||
|
Subjects.entries.map {
|
||||||
|
it.getDisplayName(context)
|
||||||
|
}
|
||||||
|
}
|
||||||
var selectedItemIndex by rememberSaveable { mutableIntStateOf(0) }
|
var selectedItemIndex by rememberSaveable { mutableIntStateOf(0) }
|
||||||
FilterChipGroup(
|
FilterChipGroup(
|
||||||
items = subjects,
|
items = subjects,
|
||||||
|
|
@ -85,6 +90,7 @@ fun TodoBottomSheet(
|
||||||
},
|
},
|
||||||
modifier = Modifier.fillMaxWidth()
|
modifier = Modifier.fillMaxWidth()
|
||||||
)
|
)
|
||||||
|
|
||||||
Spacer(Modifier.size(20.dp))
|
Spacer(Modifier.size(20.dp))
|
||||||
|
|
||||||
Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) {
|
Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||||
|
|
@ -103,7 +109,7 @@ fun TodoBottomSheet(
|
||||||
onSave(
|
onSave(
|
||||||
TodoEntity(
|
TodoEntity(
|
||||||
content = text,
|
content = text,
|
||||||
subject = subjects[selectedItemIndex]
|
subject = selectedItemIndex
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<resources>
|
|
||||||
<string-array name="subjects">
|
|
||||||
<item>语文</item>
|
|
||||||
<item>数学</item>
|
|
||||||
<item>英语</item>
|
|
||||||
<item>生物</item>
|
|
||||||
<item>地理</item>
|
|
||||||
<item>物理</item>
|
|
||||||
<item>化学</item>
|
|
||||||
<item>道法</item>
|
|
||||||
<item>历史</item>
|
|
||||||
<item>其它</item>
|
|
||||||
</string-array>
|
|
||||||
</resources>
|
|
||||||
|
|
@ -10,4 +10,14 @@
|
||||||
<string name="error_no_task_content">没有输入待办内容</string>
|
<string name="error_no_task_content">没有输入待办内容</string>
|
||||||
<string name="tip_select_this">选择该项</string>
|
<string name="tip_select_this">选择该项</string>
|
||||||
<string name="tip_mark_completed">标记为已完成</string>
|
<string name="tip_mark_completed">标记为已完成</string>
|
||||||
|
<string name="subject_chinese">语文</string>
|
||||||
|
<string name="subject_math">数学</string>
|
||||||
|
<string name="subject_english">英语</string>
|
||||||
|
<string name="subject_biology">生物</string>
|
||||||
|
<string name="subject_geography">地理</string>
|
||||||
|
<string name="subject_physics">物理</string>
|
||||||
|
<string name="subject_moral">道法</string>
|
||||||
|
<string name="subject_chemistry">化学</string>
|
||||||
|
<string name="subject_history">历史</string>
|
||||||
|
<string name="subject_others">其它</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<resources>
|
|
||||||
<string-array name="subjects">
|
|
||||||
<item>Chinese</item>
|
|
||||||
<item>Math</item>
|
|
||||||
<item>English</item>
|
|
||||||
<item>Biology</item>
|
|
||||||
<item>Geography</item>
|
|
||||||
<item>Physics</item>
|
|
||||||
<item>Chemistry</item>
|
|
||||||
<item>Moral Education</item>
|
|
||||||
<item>History</item>
|
|
||||||
<item>Others</item>
|
|
||||||
</string-array>
|
|
||||||
</resources>
|
|
||||||
|
|
@ -9,4 +9,14 @@
|
||||||
<string name="error_no_task_content">No task content entered</string>
|
<string name="error_no_task_content">No task content entered</string>
|
||||||
<string name="tip_select_this">Select this</string>
|
<string name="tip_select_this">Select this</string>
|
||||||
<string name="tip_mark_completed">Mark as completed</string>
|
<string name="tip_mark_completed">Mark as completed</string>
|
||||||
|
<string name="subject_chinese">Chinese</string>
|
||||||
|
<string name="subject_math">Math</string>
|
||||||
|
<string name="subject_english">English</string>
|
||||||
|
<string name="subject_biology">Biology</string>
|
||||||
|
<string name="subject_geography">Geography</string>
|
||||||
|
<string name="subject_physics">Physics</string>
|
||||||
|
<string name="subject_moral">Morality and Rule of Law</string>
|
||||||
|
<string name="subject_chemistry">Chemistry</string>
|
||||||
|
<string name="subject_history">History</string>
|
||||||
|
<string name="subject_others">Others</string>
|
||||||
</resources>
|
</resources>
|
||||||
Loading…
Reference in a new issue