Skip to content

Commit

Permalink
Add empty check in content of the task
Browse files Browse the repository at this point in the history
  • Loading branch information
Super12138 committed Jan 8, 2024
1 parent df53280 commit 254d45a
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 35 deletions.
4 changes: 2 additions & 2 deletions app/src/main/kotlin/cn/super12138/todo/logic/dao/ToDoRoom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import androidx.room.PrimaryKey
* @param uuid String 待办的uuid
* @param state Int 待办的完成状态,0表示未完成,1表示完成
* @param subject String 待办的学科
* @param context String 待办的内容
* @param content 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
@ColumnInfo(name = "content") val content: String
)
18 changes: 1 addition & 17 deletions app/src/main/kotlin/cn/super12138/todo/logic/model/ToDo.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
package cn.super12138.todo.logic.model

data class ToDo(val uuid: String, val context: 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
)
*/
data class ToDo(val uuid: String, val content: String, val subject: String)
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ import cn.super12138.todo.logic.model.ToDo
import cn.super12138.todo.views.progress.ProgressFragmentViewModel
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

class ToDoAdapter(val todoList: MutableList<ToDo>, val viewModelStoreOwner: ViewModelStoreOwner) :
RecyclerView.Adapter<ToDoAdapter.ViewHolder>() {

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 checkToDoBtn: Button = view.findViewById(R.id.check_item_btn)
val delToDoBtn: Button = view.findViewById(R.id.delete_item_btn)
Expand Down Expand Up @@ -92,7 +91,7 @@ class ToDoAdapter(val todoList: MutableList<ToDo>, val viewModelStoreOwner: View
if (todoList.size + 1 > 0) {
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

Expand All @@ -102,7 +101,7 @@ class ToDoAdapter(val todoList: MutableList<ToDo>, val viewModelStoreOwner: View
todo.uuid,
0,
todo.subject,
todo.context
todo.content
)
)
progressViewModel.updateProgress()
Expand All @@ -115,7 +114,7 @@ class ToDoAdapter(val todoList: MutableList<ToDo>, val viewModelStoreOwner: View

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val todo = todoList[position]
holder.todoContext.text = todo.context
holder.todoContext.text = todo.content
holder.todoSubject.text = todo.subject
}

Expand Down
26 changes: 17 additions & 9 deletions app/src/main/kotlin/cn/super12138/todo/views/todo/ToDoFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
Expand Down Expand Up @@ -64,7 +65,7 @@ class ToDoFragment : Fragment() {
val todos = Repository.getAllUncomplete()
var count = 0
for (todo in todos) {
todoList.add(ToDo(todo.uuid, todo.context, todo.subject))
todoList.add(ToDo(todo.uuid, todo.content, todo.subject))
count++
}
if (count == 0) {
Expand All @@ -82,12 +83,19 @@ class ToDoFragment : Fragment() {
ToDoDialogBinding = DialogAddTodoBinding.inflate(layoutInflater)

activity?.let { it1 ->
MaterialAlertDialogBuilder(it1)
val dialog = MaterialAlertDialogBuilder(it1)
.setTitle(R.string.add_task)
.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 todoContext = ToDoDialogBinding.todoContext.editText?.text.toString()
val todoSubject = when (ToDoDialogBinding.todoSubject.checkedChipId) {
R.id.subject_chinese -> "语文"
R.id.subject_math -> "数学"
Expand All @@ -107,7 +115,7 @@ class ToDoFragment : Fragment() {

// 添加到RecyclerView
todoList.add(
ToDo(randomUUID, todoContext, todoSubject)
ToDo(randomUUID, todoContent, todoSubject)
)

lifecycleScope.launch {
Expand All @@ -116,16 +124,16 @@ class ToDoFragment : Fragment() {
randomUUID,
0,
todoSubject,
todoContext
todoContent
)
)
progressViewModel.updateProgress()
}

binding.todoList.adapter?.notifyItemInserted(todoList.size + 1)
dialog.dismiss()
}
.setNegativeButton(R.string.cancel, null)
.show()
}
}
}

Expand Down Expand Up @@ -179,4 +187,4 @@ class ToDoFragment : Fragment() {
binding.todoList.adapter?.notifyItemInserted(todoList.size + 1)
})
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/dialog_add_todo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:orientation="vertical">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/todo_context"
android:id="@+id/todo_content"
style="?attr/textInputFilledExposedDropdownMenuStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/todo_item.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="?attr/materialCardViewElevatedStyle"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
Expand All @@ -21,7 +22,7 @@
android:orientation="vertical">

<TextView
android:id="@+id/todo_context"
android:id="@+id/todo_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<string name="no_tasks">无待办事项</string>
<string name="delete_undo">撤销</string>
<string name="task_deleted">该待办已删除</string>
<string name="content_cannot_be_empty">待办内容不能为空</string>
<string name="settings_label">设置</string>
<string name="appearance">外观</string>
<string name="dark_mode">深色模式</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<string name="no_tasks">No tasks</string>
<string name="delete_undo">Undo</string>
<string name="task_deleted">This task has been deleted</string>
<string name="content_cannot_be_empty">Task content cannot be empty</string>
<!--Settings-->
<string name="settings_label">Settings</string>
<string name="appearance">Appearance</string>
Expand Down

0 comments on commit 254d45a

Please sign in to comment.