Add empty check in content of the task

This commit is contained in:
Super12138 2024-01-08 20:04:39 +08:00
parent df53280b2d
commit 254d45a02e
8 changed files with 29 additions and 35 deletions

View file

@ -8,12 +8,12 @@ import androidx.room.PrimaryKey
* @param uuid String 待办的uuid * @param uuid String 待办的uuid
* @param state Int 待办的完成状态0表示未完成1表示完成 * @param state Int 待办的完成状态0表示未完成1表示完成
* @param subject String 待办的学科 * @param subject String 待办的学科
* @param context String 待办的内容 * @param content String 待办的内容
*/ */
@Entity(tableName = "todo") @Entity(tableName = "todo")
data class ToDoRoom( data class ToDoRoom(
@PrimaryKey @ColumnInfo(name = "uuid") val uuid: String, @PrimaryKey @ColumnInfo(name = "uuid") val uuid: String,
@ColumnInfo(name = "state") val state: Int, @ColumnInfo(name = "state") val state: Int,
@ColumnInfo(name = "subject") val subject: String, @ColumnInfo(name = "subject") val subject: String,
@ColumnInfo(name = "context") val context: String @ColumnInfo(name = "content") val content: String
) )

View file

@ -1,19 +1,3 @@
package cn.super12138.todo.logic.model package cn.super12138.todo.logic.model
data class ToDo(val uuid: String, val context: String, val subject: String) data class ToDo(val uuid: String, val content: String, val subject: String)
/*
package cn.super12138.todo.logic.model
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "todo")
data class ToDo (
@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
)
*/

View file

@ -15,14 +15,13 @@ import cn.super12138.todo.logic.model.ToDo
import cn.super12138.todo.views.progress.ProgressFragmentViewModel import cn.super12138.todo.views.progress.ProgressFragmentViewModel
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class ToDoAdapter(val todoList: MutableList<ToDo>, val viewModelStoreOwner: ViewModelStoreOwner) : class ToDoAdapter(val todoList: MutableList<ToDo>, val viewModelStoreOwner: ViewModelStoreOwner) :
RecyclerView.Adapter<ToDoAdapter.ViewHolder>() { RecyclerView.Adapter<ToDoAdapter.ViewHolder>() {
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val todoContext: TextView = view.findViewById(R.id.todo_context) val todoContext: TextView = view.findViewById(R.id.todo_content)
val todoSubject: TextView = view.findViewById(R.id.todo_subject) val todoSubject: TextView = view.findViewById(R.id.todo_subject)
val checkToDoBtn: Button = view.findViewById(R.id.check_item_btn) val checkToDoBtn: Button = view.findViewById(R.id.check_item_btn)
val delToDoBtn: Button = view.findViewById(R.id.delete_item_btn) val delToDoBtn: Button = view.findViewById(R.id.delete_item_btn)
@ -92,7 +91,7 @@ class ToDoAdapter(val todoList: MutableList<ToDo>, val viewModelStoreOwner: View
if (todoList.size + 1 > 0) { if (todoList.size + 1 > 0) {
todoViewModel.emptyTipVis.value = View.GONE todoViewModel.emptyTipVis.value = View.GONE
} }
todoList.add(ToDo(todo.uuid, todo.context, todo.subject)) todoList.add(ToDo(todo.uuid, todo.content, todo.subject))
todoViewModel.refreshData.value = 1 todoViewModel.refreshData.value = 1
@ -102,7 +101,7 @@ class ToDoAdapter(val todoList: MutableList<ToDo>, val viewModelStoreOwner: View
todo.uuid, todo.uuid,
0, 0,
todo.subject, todo.subject,
todo.context todo.content
) )
) )
progressViewModel.updateProgress() progressViewModel.updateProgress()
@ -115,7 +114,7 @@ class ToDoAdapter(val todoList: MutableList<ToDo>, val viewModelStoreOwner: View
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val todo = todoList[position] val todo = todoList[position]
holder.todoContext.text = todo.context holder.todoContext.text = todo.content
holder.todoSubject.text = todo.subject holder.todoSubject.text = todo.subject
} }

View file

@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
@ -64,7 +65,7 @@ class ToDoFragment : Fragment() {
val todos = Repository.getAllUncomplete() val todos = Repository.getAllUncomplete()
var count = 0 var count = 0
for (todo in todos) { for (todo in todos) {
todoList.add(ToDo(todo.uuid, todo.context, todo.subject)) todoList.add(ToDo(todo.uuid, todo.content, todo.subject))
count++ count++
} }
if (count == 0) { if (count == 0) {
@ -82,12 +83,19 @@ class ToDoFragment : Fragment() {
ToDoDialogBinding = DialogAddTodoBinding.inflate(layoutInflater) ToDoDialogBinding = DialogAddTodoBinding.inflate(layoutInflater)
activity?.let { it1 -> activity?.let { it1 ->
MaterialAlertDialogBuilder(it1) val dialog = MaterialAlertDialogBuilder(it1)
.setTitle(R.string.add_task) .setTitle(R.string.add_task)
.setView(ToDoDialogBinding.root) .setView(ToDoDialogBinding.root)
.setPositiveButton(R.string.ok) { dialog, which -> .setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.show()
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val todoContent = ToDoDialogBinding.todoContent.editText?.text.toString()
if (todoContent.isEmpty()) {
ToDoDialogBinding.todoContent.error =
getString(R.string.content_cannot_be_empty)
} else {
val randomUUID = UUID.randomUUID().toString() val randomUUID = UUID.randomUUID().toString()
val todoContext = ToDoDialogBinding.todoContext.editText?.text.toString()
val todoSubject = when (ToDoDialogBinding.todoSubject.checkedChipId) { val todoSubject = when (ToDoDialogBinding.todoSubject.checkedChipId) {
R.id.subject_chinese -> "语文" R.id.subject_chinese -> "语文"
R.id.subject_math -> "数学" R.id.subject_math -> "数学"
@ -107,7 +115,7 @@ class ToDoFragment : Fragment() {
// 添加到RecyclerView // 添加到RecyclerView
todoList.add( todoList.add(
ToDo(randomUUID, todoContext, todoSubject) ToDo(randomUUID, todoContent, todoSubject)
) )
lifecycleScope.launch { lifecycleScope.launch {
@ -116,16 +124,16 @@ class ToDoFragment : Fragment() {
randomUUID, randomUUID,
0, 0,
todoSubject, todoSubject,
todoContext todoContent
) )
) )
progressViewModel.updateProgress() progressViewModel.updateProgress()
} }
binding.todoList.adapter?.notifyItemInserted(todoList.size + 1) binding.todoList.adapter?.notifyItemInserted(todoList.size + 1)
dialog.dismiss()
} }
.setNegativeButton(R.string.cancel, null) }
.show()
} }
} }
@ -179,4 +187,4 @@ class ToDoFragment : Fragment() {
binding.todoList.adapter?.notifyItemInserted(todoList.size + 1) binding.todoList.adapter?.notifyItemInserted(todoList.size + 1)
}) })
} }
} }

View file

@ -10,7 +10,7 @@
android:orientation="vertical"> android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:id="@+id/todo_context" android:id="@+id/todo_content"
style="?attr/textInputFilledExposedDropdownMenuStyle" style="?attr/textInputFilledExposedDropdownMenuStyle"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android" <com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
style="?attr/materialCardViewElevatedStyle"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="80dp" android:layout_height="80dp"
android:layout_marginLeft="10dp" android:layout_marginLeft="10dp"
@ -21,7 +22,7 @@
android:orientation="vertical"> android:orientation="vertical">
<TextView <TextView
android:id="@+id/todo_context" android:id="@+id/todo_content"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="15dp" android:layout_marginStart="15dp"

View file

@ -33,6 +33,7 @@
<string name="no_tasks">无待办事项</string> <string name="no_tasks">无待办事项</string>
<string name="delete_undo">撤销</string> <string name="delete_undo">撤销</string>
<string name="task_deleted">该待办已删除</string> <string name="task_deleted">该待办已删除</string>
<string name="content_cannot_be_empty">待办内容不能为空</string>
<string name="settings_label">设置</string> <string name="settings_label">设置</string>
<string name="appearance">外观</string> <string name="appearance">外观</string>
<string name="dark_mode">深色模式</string> <string name="dark_mode">深色模式</string>

View file

@ -33,6 +33,7 @@
<string name="no_tasks">No tasks</string> <string name="no_tasks">No tasks</string>
<string name="delete_undo">Undo</string> <string name="delete_undo">Undo</string>
<string name="task_deleted">This task has been deleted</string> <string name="task_deleted">This task has been deleted</string>
<string name="content_cannot_be_empty">Task content cannot be empty</string>
<!--Settings--> <!--Settings-->
<string name="settings_label">Settings</string> <string name="settings_label">Settings</string>
<string name="appearance">Appearance</string> <string name="appearance">Appearance</string>