导入 v0.1.0 / v0.2.0 源码:五模块首版、交互重构、重复任务、任务分布

- 源码镜像 src/main(81 个文件):首页双模式、任务、项目、统计、设置
- 补齐 README / LICENSE / CHANGELOG / CONTRIBUTING / .gitignore
- 不含构建配置、签名材料、编译产物与备份数据
This commit is contained in:
XIGE
2026-11-28 00:25:22 +08:00
parent f87c29f232
commit d52deeff5e
85 changed files with 6215 additions and 1 deletions

View File

@@ -0,0 +1,154 @@
package com.focusbox.app.ui.tasks
import android.graphics.Paint
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.focusbox.app.R
import com.focusbox.app.model.Task
import com.focusbox.app.util.Dates
import com.focusbox.app.util.circleBg
import com.focusbox.app.util.col
import com.focusbox.app.util.roundBg
import com.focusbox.app.util.tint
import com.focusbox.app.util.visible
class TaskAdapter(
private val onEdit: (Task) -> Unit,
private val onToggle: (Task) -> Unit,
private val onStart: (Task) -> Unit,
private val onFooterClick: () -> Unit,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
sealed class Row {
data class Item(
val task: Task,
val projectName: String,
val projectColor: Int,
val overdue: Boolean,
/** 已完成任务的"日期 星期 起-止",无记录时为 null */
val doneRange: String? = null,
) : Row()
data class Footer(val text: String) : Row()
}
private var rows: List<Row> = emptyList()
fun submit(list: List<Row>) {
rows = list
notifyDataSetChanged()
}
override fun getItemViewType(position: Int): Int =
if (rows[position] is Row.Footer) TYPE_FOOTER else TYPE_TASK
override fun getItemCount(): Int = rows.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return if (viewType == TYPE_FOOTER) {
FooterViewHolder(inflater.inflate(R.layout.item_footer, parent, false))
} else {
TaskViewHolder(inflater.inflate(R.layout.item_task, parent, false))
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (val row = rows[position]) {
is Row.Item -> (holder as TaskViewHolder).bind(row)
is Row.Footer -> (holder as FooterViewHolder).bind(row)
}
}
private inner class TaskViewHolder(v: View) : RecyclerView.ViewHolder(v) {
private val tvTitle: TextView = v.findViewById(R.id.tvTitle)
private val tvMeta: TextView = v.findViewById(R.id.tvMeta)
private val tvNote: TextView = v.findViewById(R.id.tvNote)
private val checkCircle: View = v.findViewById(R.id.checkCircle)
private val checkMark: ImageView = v.findViewById(R.id.checkMark)
private val checkWrap: View = v.findViewById(R.id.checkWrap)
private val btnPlay: ImageView = v.findViewById(R.id.btnPlay)
fun bind(row: Row.Item) {
val task = row.task
val ctx = itemView.context
tvTitle.text = task.title
tvTitle.paintFlags = if (task.done) {
tvTitle.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
} else {
tvTitle.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
}
tvTitle.setTextColor(ctx.col(if (task.done) R.color.text_tertiary else R.color.text))
tvTitle.typeface = if (task.priority >= 2 && !task.done) Typeface.DEFAULT_BOLD else Typeface.DEFAULT
if (task.done) {
checkCircle.background = ctx.circleBg(ctx.col(R.color.brand))
checkMark.visible(true)
checkMark.tint(ctx.col(R.color.on_brand))
} else {
checkCircle.background = ctx.roundBg(
android.graphics.Color.TRANSPARENT, 100f, ctx.col(R.color.field_stroke), 1.5f
)
checkMark.visible(false)
}
tvMeta.text = buildMeta(row)
tvMeta.setTextColor(
ctx.col(
when {
row.overdue && !task.done -> R.color.brand
else -> R.color.text_tertiary
}
)
)
// 备注单独一行,跟在"项目 · 日期"下面,避免挤在元信息里被截断
tvNote.text = task.note
tvNote.visible(task.note.isNotBlank())
btnPlay.visible(!task.done)
btnPlay.tint(ctx.col(R.color.text_tertiary))
checkWrap.setOnClickListener { onToggle(task) }
itemView.setOnClickListener { onEdit(task) }
btnPlay.setOnClickListener { onStart(task) }
}
private fun buildMeta(row: Row.Item): String {
val task = row.task
val parts = ArrayList<String>(4)
parts.add(row.projectName)
if (task.done) {
row.doneRange?.let { parts.add(it) }
return parts.joinToString(" · ")
}
parts.add("${task.focusMinutes} 分钟")
if (task.due > 0) {
val label = Dates.dueLabel(task.due)
parts.add(if (row.overdue) "已逾期 · $label" else label)
}
if (task.priority >= 2) parts.add("高优先")
return parts.joinToString(" · ")
}
}
private inner class FooterViewHolder(v: View) : RecyclerView.ViewHolder(v) {
private val tv: TextView = v.findViewById(R.id.tvFooter)
fun bind(row: Row.Footer) {
tv.text = row.text
itemView.setOnClickListener { onFooterClick() }
}
}
companion object {
private const val TYPE_TASK = 0
private const val TYPE_FOOTER = 1
}
}

View File

@@ -0,0 +1,233 @@
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()
}
}

View File

@@ -0,0 +1,212 @@
package com.focusbox.app.ui.tasks
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.focusbox.app.R
import com.focusbox.app.data.Store
import com.focusbox.app.model.Project
import com.focusbox.app.model.SessionSpan
import com.focusbox.app.model.Task
import com.focusbox.app.ui.MainActivity
import com.focusbox.app.util.Dates
import com.focusbox.app.util.makeChip
import com.focusbox.app.util.visible
/**
* 任务页。
*
* 顶部胶囊分两组:
* - 视图:全部 / 今日 / 已完成
* - 项目:收件箱与各项目
* 视图与项目互斥,选了项目就回到"全部",避免出现"今日 + 某项目"这种没人能解释的组合。
*/
class TasksFragment : Fragment(R.layout.fragment_tasks) {
private enum class ViewFilter { ALL, TODAY, DONE }
private lateinit var recycler: RecyclerView
private lateinit var tvCount: TextView
private lateinit var tvEmpty: TextView
private lateinit var chipRow: LinearLayout
private lateinit var adapter: TaskAdapter
private var filterProjectId = 0L
private var viewFilter = ViewFilter.ALL
private var expandedDone = false
private var projects: List<Project> = emptyList()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recycler = view.findViewById(R.id.recycler)
tvCount = view.findViewById(R.id.tvCount)
tvEmpty = view.findViewById(R.id.tvEmpty)
chipRow = view.findViewById(R.id.chipRow)
adapter = TaskAdapter(
onEdit = { openSheet(it) },
onToggle = { task ->
Store.setTaskDone(task.id, !task.done)
reload()
},
onStart = { task -> (activity as? MainActivity)?.startFocusOn(task) },
onFooterClick = {
expandedDone = !expandedDone
reload()
},
)
recycler.layoutManager = LinearLayoutManager(requireContext())
recycler.adapter = adapter
recycler.itemAnimator = null
view.findViewById<View>(R.id.btnAdd).setOnClickListener {
TaskSheet(requireContext(), null, filterProjectId) { reload() }.show()
}
reload()
}
override fun onResume() {
super.onResume()
if (view != null) reload()
}
override fun onHiddenChanged(hidden: Boolean) {
super.onHiddenChanged(hidden)
if (!hidden && view != null) reload()
}
/** 从项目页点进来:只看这个项目的全部任务 */
fun applyProjectFilter(projectId: Long) {
filterProjectId = projectId
viewFilter = ViewFilter.ALL
if (view != null) reload()
}
fun reload() {
if (view == null) return
projects = Store.projects()
renderChips()
val all = Store.tasks(filterProjectId, includeDone = true)
val pending = all.filter { !it.done }
val done = all.filter { it.done }
val spans = if (done.isEmpty()) emptyMap() else Store.lastSessions()
val rows = ArrayList<TaskAdapter.Row>()
when (viewFilter) {
ViewFilter.ALL -> {
pending.forEach { rows.add(toRow(it, spans)) }
tvCount.text = getString(R.string.task_count, pending.size)
tvEmpty.setText(R.string.empty_tasks)
if (done.isNotEmpty()) {
if (expandedDone) done.forEach { rows.add(toRow(it, spans)) }
rows.add(
TaskAdapter.Row.Footer(
getString(if (expandedDone) R.string.hide_done else R.string.show_done)
)
)
}
}
ViewFilter.TODAY -> {
val from = Dates.todayStart()
val to = Dates.addDays(from, 1)
val today = pending.filter { it.due >= from && it.due < to }
today.forEach { rows.add(toRow(it, spans)) }
tvCount.text = getString(R.string.task_count_today, today.size)
tvEmpty.setText(R.string.empty_today)
}
ViewFilter.DONE -> {
done.forEach { rows.add(toRow(it, spans)) }
tvCount.text = getString(R.string.task_count_done, done.size)
tvEmpty.setText(R.string.empty_done)
}
}
adapter.submit(rows)
tvEmpty.visible(rows.isEmpty())
recycler.visible(rows.isNotEmpty())
}
private fun toRow(task: Task, spans: Map<Long, SessionSpan>): TaskAdapter.Row.Item {
val project = projects.firstOrNull { it.id == task.projectId }
val overdue = task.due > 0 && task.due < Dates.todayStart()
return TaskAdapter.Row.Item(
task = task,
projectName = project?.name ?: getString(R.string.inbox),
projectColor = project?.color ?: 0xFF7A756C.toInt(),
overdue = overdue,
doneRange = if (task.done) doneRange(task, spans) else null,
)
}
/** 11月27日 周五 09:12:03-09:37:03 */
private fun doneRange(task: Task, spans: Map<Long, SessionSpan>): String? {
val span = spans[task.id]
if (span != null) {
return "${Dates.dateWithWeek(span.startAt)} " +
"${Dates.hms(span.startAt)}-${Dates.hms(span.endAt)}"
}
if (task.doneAt > 0) {
return "${Dates.dateWithWeek(task.doneAt)} ${Dates.hms(task.doneAt)}"
}
return null
}
private fun renderChips() {
chipRow.removeAllViews()
val allSelected = viewFilter == ViewFilter.ALL && filterProjectId == 0L
chipRow.addView(
requireContext().makeChip(getString(R.string.filter_all), allSelected)
.also {
it.setOnClickListener {
viewFilter = ViewFilter.ALL
filterProjectId = 0L
reload()
}
}
)
chipRow.addView(
requireContext().makeChip(getString(R.string.filter_today), viewFilter == ViewFilter.TODAY)
.also {
it.setOnClickListener {
viewFilter = ViewFilter.TODAY
filterProjectId = 0L
reload()
}
}
)
chipRow.addView(
requireContext().makeChip(getString(R.string.filter_done), viewFilter == ViewFilter.DONE)
.also {
it.setOnClickListener {
viewFilter = ViewFilter.DONE
filterProjectId = 0L
reload()
}
}
)
projects.forEach { p ->
if (p.archived) return@forEach
val selected = viewFilter == ViewFilter.ALL && filterProjectId == p.id
val chip = requireContext().makeChip(p.name, selected, p.color)
chip.setOnClickListener {
viewFilter = ViewFilter.ALL
filterProjectId = p.id
reload()
}
chipRow.addView(chip)
}
}
private fun openSheet(task: Task?) {
TaskSheet(requireContext(), task, filterProjectId) { reload() }.show()
}
}