Files
FocusBox/src/main/java/com/focusbox/app/ui/tasks/TaskSheet.kt

234 lines
8.2 KiB
Kotlin
Raw Normal View History

package com.focusbox.app.ui.tasks
import android.app.DatePickerDialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.focusbox.app.R
import com.focusbox.app.data.Store
import com.focusbox.app.model.Repeat
import com.focusbox.app.model.Task
import com.focusbox.app.service.TimerCore
import com.focusbox.app.ui.SheetDialog
import com.focusbox.app.util.Dates
import com.focusbox.app.util.Prefs
import com.focusbox.app.util.col
import com.focusbox.app.util.makeChip
import com.focusbox.app.util.visible
import java.util.Calendar
/**
* 新建 / 编辑任务。
*
* 项目、优先级、重复单位都用互斥胶囊,而不是下拉菜单——
* 常用项不超过 8 个,一屏点完比"打开选择器再确认"少两次操作。
*/
class TaskSheet(
context: Context,
private val existing: Task?,
presetProjectId: Long,
private val onSaved: () -> Unit,
) : SheetDialog(context) {
private val root = LayoutInflater.from(context).inflate(R.layout.sheet_task, null)
private val etTitle: EditText = root.findViewById(R.id.etTitle)
private val etNote: EditText = root.findViewById(R.id.etNote)
private val projectRow: LinearLayout = root.findViewById(R.id.projectRow)
private val priorityRow: LinearLayout = root.findViewById(R.id.priorityRow)
private val repeatRow: LinearLayout = root.findViewById(R.id.repeatRow)
private val repeatEveryRow: View = root.findViewById(R.id.repeatEveryRow)
private val tvFocus: TextView = root.findViewById(R.id.tvFocus)
private val tvEvery: TextView = root.findViewById(R.id.tvEvery)
private val btnDue: TextView = root.findViewById(R.id.btnDue)
private val btnClearDue: View = root.findViewById(R.id.btnClearDue)
private var projectId: Long = existing?.projectId
?: if (presetProjectId > 0) presetProjectId else Store.INBOX_ID
private var priority: Int = existing?.priority ?: 1
// 旧数据的时长可能超出新范围(早先是"番茄数×25"),这里统一夹紧
private var focusMin: Int = (existing?.focusMinutes ?: Prefs.focusMinutes)
.coerceIn(TimerCore.MIN_MINUTES, TimerCore.MAX_MINUTES)
private var due: Long = existing?.due ?: 0L
private var repeatType: Int = existing?.repeatType ?: Repeat.NONE
private var repeatEvery: Int = existing?.repeatEvery ?: 1
init {
setContentView(root)
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
root.findViewById<TextView>(R.id.sheetTitle).setText(
if (existing == null) R.string.add_task else R.string.edit
)
etTitle.setText(existing?.title ?: "")
etNote.setText(existing?.note ?: "")
renderFocus()
root.findViewById<View>(R.id.btnFocusMinus).setOnClickListener {
focusMin = (focusMin - 5).coerceAtLeast(TimerCore.MIN_MINUTES)
renderFocus()
}
root.findViewById<View>(R.id.btnFocusPlus).setOnClickListener {
focusMin = (focusMin + 5).coerceAtMost(TimerCore.MAX_MINUTES)
renderFocus()
}
root.findViewById<View>(R.id.btnEveryMinus).setOnClickListener {
repeatEvery = (repeatEvery - 1).coerceAtLeast(1)
renderRepeat()
}
root.findViewById<View>(R.id.btnEveryPlus).setOnClickListener {
repeatEvery = (repeatEvery + 1).coerceAtMost(30)
renderRepeat()
}
btnDue.setOnClickListener { pickDate() }
btnClearDue.setOnClickListener {
due = 0L
updateDue()
}
val btnDelete = root.findViewById<TextView>(R.id.btnDelete)
btnDelete.visible(existing != null)
btnDelete.setOnClickListener {
AlertDialog.Builder(context)
.setMessage(R.string.delete_task_confirm)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.delete) { _, _ ->
existing?.let { Store.deleteTask(it.id) }
onSaved()
dismiss()
}
.show()
}
root.findViewById<View>(R.id.btnSave).setOnClickListener { save() }
renderProjects()
renderPriority()
renderRepeat()
updateDue()
}
private fun renderProjects() {
val ctx = context
projectRow.removeAllViews()
Store.projects().filter { !it.archived || it.id == projectId }.forEach { p ->
val chip = ctx.makeChip(p.name, p.id == projectId, p.color)
chip.setOnClickListener {
projectId = p.id
renderProjects()
}
projectRow.addView(chip)
}
}
private fun renderPriority() {
val ctx = context
priorityRow.removeAllViews()
val labels = listOf(R.string.p_low, R.string.p_mid, R.string.p_high)
labels.forEachIndexed { index, res ->
val chip = ctx.makeChip(ctx.getString(res), index == priority)
chip.setOnClickListener {
priority = index
renderPriority()
}
priorityRow.addView(chip)
}
}
private fun renderFocus() {
tvFocus.text = context.getString(R.string.minutes_value, focusMin)
}
/** 重复单位四选一;选了非"不重复"才露出间隔步进器 */
private fun renderRepeat() {
val ctx = context
repeatRow.removeAllViews()
val labels = listOf(
Repeat.NONE to R.string.repeat_none,
Repeat.DAY to R.string.repeat_day,
Repeat.WEEK to R.string.repeat_week,
Repeat.MONTH to R.string.repeat_month,
)
labels.forEach { (type, res) ->
val chip = ctx.makeChip(ctx.getString(res), repeatType == type)
chip.setOnClickListener {
repeatType = type
renderRepeat()
}
repeatRow.addView(chip)
}
val repeating = repeatType != Repeat.NONE
repeatEveryRow.visible(repeating)
if (repeating) {
val unit = ctx.getString(
when (repeatType) {
Repeat.WEEK -> R.string.repeat_week_unit
Repeat.MONTH -> R.string.repeat_month_unit
else -> R.string.repeat_day_unit
}
)
tvEvery.text = "$repeatEvery $unit"
}
}
private fun updateDue() {
btnDue.text = if (due > 0) Dates.mdLong(due) else context.getString(R.string.no_due)
btnDue.setTextColor(context.col(if (due > 0) R.color.text else R.color.text_secondary))
btnClearDue.visible(due > 0)
}
private fun pickDate() {
val c = Calendar.getInstance()
if (due > 0) c.timeInMillis = due
DatePickerDialog(
context,
{ _, y, m, d ->
val cal = Calendar.getInstance()
cal.set(y, m, d, 0, 0, 0)
cal.set(Calendar.MILLISECOND, 0)
due = cal.timeInMillis
updateDue()
},
c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH),
).show()
}
private fun save() {
val title = etTitle.text.toString().trim()
if (title.isEmpty()) {
etTitle.error = context.getString(R.string.task_title_hint)
return
}
val note = etNote.text.toString().trim()
val every = if (repeatType == Repeat.NONE) 1 else repeatEvery.coerceAtLeast(1)
if (existing == null) {
Store.addTask(title, note, projectId, priority, focusMin, due, repeatType, every)
} else {
Store.updateTask(
existing.copy(
title = title,
note = note,
projectId = projectId,
priority = priority,
focusMinutes = focusMin,
due = due,
repeatType = repeatType,
repeatEvery = every,
)
)
}
onSaved()
dismiss()
}
}