710 lines
30 KiB
Kotlin
710 lines
30 KiB
Kotlin
package com.focusbox.app.ui.focus
|
||
|
||
import android.Manifest
|
||
import android.content.Context
|
||
import android.content.pm.PackageManager
|
||
import android.os.Build
|
||
import android.os.Bundle
|
||
import android.os.Handler
|
||
import android.os.Looper
|
||
import android.view.MotionEvent
|
||
import android.view.View
|
||
import android.view.inputmethod.InputMethodManager
|
||
import android.view.ViewConfiguration
|
||
import android.view.WindowManager
|
||
import android.widget.EditText
|
||
import android.widget.ImageView
|
||
import android.widget.LinearLayout
|
||
import android.widget.TextView
|
||
import android.widget.Toast
|
||
import androidx.core.content.ContextCompat
|
||
import androidx.activity.result.contract.ActivityResultContracts
|
||
import androidx.fragment.app.Fragment
|
||
import com.focusbox.app.R
|
||
import com.focusbox.app.data.Store
|
||
import com.focusbox.app.model.Project
|
||
import com.focusbox.app.model.Task
|
||
import com.focusbox.app.service.TimerCore
|
||
import com.focusbox.app.ui.SheetDialog
|
||
import com.focusbox.app.ui.tasks.TaskSheet
|
||
import com.focusbox.app.util.Dates
|
||
import com.focusbox.app.util.Prefs
|
||
import com.focusbox.app.util.circleBg
|
||
import com.focusbox.app.util.col
|
||
import com.focusbox.app.util.dpi
|
||
import com.focusbox.app.util.roundBg
|
||
import com.focusbox.app.util.tint
|
||
import com.focusbox.app.util.visible
|
||
import com.focusbox.app.widget.RingView
|
||
import kotlin.math.abs
|
||
|
||
/**
|
||
* 首页。
|
||
*
|
||
* 圆环有两种计时语义,靠左右滑动切换:
|
||
* 1. 倒计时(默认取设置里的专注时长,可用圆环上下拖动临时增减)
|
||
* 2. 正计时(从 00:00 往上走,用于不设目标的专注)
|
||
*
|
||
* 手势都在 [dialWrap] 上统一处理:先判断主方向,纵向调时长,横向切页。
|
||
*/
|
||
class FocusFragment : Fragment(R.layout.fragment_focus) {
|
||
|
||
private lateinit var ring: RingView
|
||
private lateinit var tvTime: TextView
|
||
private lateinit var tvModeLabel: TextView
|
||
private lateinit var dots: LinearLayout
|
||
private lateinit var pageDots: LinearLayout
|
||
private lateinit var dialWrap: View
|
||
private lateinit var modeSwitch: View
|
||
private lateinit var btnMain: TextView
|
||
private lateinit var btnReset: ImageView
|
||
private lateinit var btnFinish: ImageView
|
||
private lateinit var tvTask: TextView
|
||
private lateinit var tvTaskMeta: TextView
|
||
private lateinit var tvTaskAction: TextView
|
||
private lateinit var taskDot: View
|
||
private lateinit var taskCard: View
|
||
private lateinit var segFocus: TextView
|
||
private lateinit var segBreak: TextView
|
||
|
||
private val handler = Handler(Looper.getMainLooper())
|
||
private var keepScreen = false
|
||
|
||
// 手势状态:0 未定 / 1 纵向调时长 / 2 横向翻页 / 3 忽略
|
||
private var touchMode = 0
|
||
private var downX = 0f
|
||
private var downY = 0f
|
||
private var dragStartMinutes = 25
|
||
private var dragging = false
|
||
private var slop = 12
|
||
|
||
// 当前任务缓存:render() 每 250ms 跑一次,不能每次都查库
|
||
private var cachedTaskId = Long.MIN_VALUE
|
||
private var cachedTask: Task? = null
|
||
private var cachedProject: Project? = null
|
||
|
||
private val ticker = object : Runnable {
|
||
override fun run() {
|
||
// 兜底推进:万一前台服务被系统限流没起来,只要页面还在前台,
|
||
// 计时与到时结算仍然准确。续命统一交给 render() 末尾的 syncTicker(),
|
||
// 避免"点开始时轮询已经停了却没人重新拉起"导致圆环数字冻住。
|
||
if (TimerCore.running) TimerCore.tick()
|
||
render()
|
||
}
|
||
}
|
||
|
||
private val onTimerChanged: () -> Unit = { if (isAdded && view != null) render() }
|
||
|
||
/** 通知权限:授权结果不需要回调处理,拒绝也不影响计时本身 */
|
||
private val notifPermissionLauncher =
|
||
registerForActivityResult(ActivityResultContracts.RequestPermission()) { }
|
||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||
super.onViewCreated(view, savedInstanceState)
|
||
ring = view.findViewById(R.id.ring)
|
||
tvTime = view.findViewById(R.id.tvTime)
|
||
tvModeLabel = view.findViewById(R.id.tvModeLabel)
|
||
dots = view.findViewById(R.id.dots)
|
||
pageDots = view.findViewById(R.id.pageDots)
|
||
dialWrap = view.findViewById(R.id.dialWrap)
|
||
modeSwitch = view.findViewById(R.id.modeSwitch)
|
||
btnMain = view.findViewById(R.id.btnMain)
|
||
btnReset = view.findViewById(R.id.btnReset)
|
||
btnFinish = view.findViewById(R.id.btnFinish)
|
||
tvTask = view.findViewById(R.id.tvTask)
|
||
tvTaskMeta = view.findViewById(R.id.tvTaskMeta)
|
||
tvTaskAction = view.findViewById(R.id.tvTaskAction)
|
||
taskDot = view.findViewById(R.id.taskDot)
|
||
taskCard = view.findViewById(R.id.taskCard)
|
||
segFocus = view.findViewById(R.id.segFocus)
|
||
segBreak = view.findViewById(R.id.segBreak)
|
||
|
||
slop = ViewConfiguration.get(requireContext()).scaledTouchSlop
|
||
btnReset.tint(col(R.color.text_secondary))
|
||
btnFinish.tint(col(R.color.text_secondary))
|
||
|
||
btnMain.setOnClickListener {
|
||
// 休息只有开始/结束:开始后按钮变为"结束休息",再点直接结束,没有暂停这套
|
||
if (TimerCore.mode != TimerCore.MODE_FOCUS) {
|
||
if (TimerCore.running) {
|
||
TimerCore.endBreak()
|
||
} else {
|
||
requestNotifPermissionIfNeeded()
|
||
TimerCore.start()
|
||
}
|
||
render()
|
||
return@setOnClickListener
|
||
}
|
||
if (TimerCore.running) {
|
||
showPauseSheet()
|
||
return@setOnClickListener
|
||
}
|
||
requestNotifPermissionIfNeeded()
|
||
// 暂停后点"继续"要走 resume,恢复时才会把暂停原因补写进备注
|
||
if (TimerCore.elapsedNow() > 0) TimerCore.resume() else TimerCore.start()
|
||
render()
|
||
}
|
||
btnReset.setOnClickListener {
|
||
TimerCore.reset()
|
||
render()
|
||
}
|
||
// 正计时的右侧按钮是「结束」:点一下直接走正常结束流程,没有"提前"一说;
|
||
// 倒计时仍是「提前完成」,保留原因输入框
|
||
btnFinish.setOnClickListener {
|
||
if (TimerCore.countUp) endCountUp() else showFinishSheet()
|
||
}
|
||
taskCard.setOnClickListener { pickTask() }
|
||
segFocus.setOnClickListener {
|
||
if (!TimerCore.running) {
|
||
TimerCore.setMode(TimerCore.MODE_FOCUS)
|
||
render()
|
||
}
|
||
}
|
||
segBreak.setOnClickListener {
|
||
if (!TimerCore.running) {
|
||
val every = Prefs.longBreakEvery.coerceAtLeast(2)
|
||
val long = TimerCore.round > 0 && TimerCore.round % every == 0
|
||
TimerCore.setMode(if (long) TimerCore.MODE_LONG else TimerCore.MODE_SHORT)
|
||
render()
|
||
}
|
||
}
|
||
|
||
buildPageDots()
|
||
dialWrap.setOnTouchListener { _, ev -> handleDialTouch(ev) }
|
||
render()
|
||
}
|
||
|
||
override fun onResume() {
|
||
super.onResume()
|
||
cachedTaskId = Long.MIN_VALUE // 任务可能被改名/改时长,回到页面强制重读
|
||
TimerCore.addListener(onTimerChanged)
|
||
// show/hide 模式下所有片段都处于 resumed,隐藏的页面不该继续跑 250ms 刷新;
|
||
// render() 末尾会按当前 running 与 isHidden 自行决定是否挂上轮询。
|
||
render()
|
||
}
|
||
|
||
override fun onPause() {
|
||
super.onPause()
|
||
TimerCore.removeListener(onTimerChanged)
|
||
handler.removeCallbacks(ticker)
|
||
applyKeepScreen(false)
|
||
}
|
||
|
||
override fun onHiddenChanged(hidden: Boolean) {
|
||
super.onHiddenChanged(hidden)
|
||
if (hidden) {
|
||
handler.removeCallbacks(ticker)
|
||
applyKeepScreen(false)
|
||
} else {
|
||
// 任务可能在别的页面被改名/改时长,重新显示时强制重读
|
||
cachedTaskId = Long.MIN_VALUE
|
||
render()
|
||
}
|
||
}
|
||
|
||
/** 数据被清空或批量改动后由 Activity 调用:丢掉任务缓存并按当前状态重画 */
|
||
fun reloadTask() {
|
||
cachedTaskId = Long.MIN_VALUE
|
||
if (view != null) render()
|
||
}
|
||
|
||
// ------------------------------------------------------------------ 绘制
|
||
|
||
private fun buildPageDots() {
|
||
pageDots.removeAllViews()
|
||
repeat(2) {
|
||
val v = View(requireContext())
|
||
val lp = LinearLayout.LayoutParams(dpi(6f), dpi(6f))
|
||
lp.marginStart = dpi(3f)
|
||
lp.marginEnd = dpi(3f)
|
||
v.layoutParams = lp
|
||
pageDots.addView(v)
|
||
}
|
||
}
|
||
|
||
private fun render() {
|
||
if (view == null) return
|
||
val display = TimerCore.displayNow()
|
||
val focusMode = TimerCore.mode == TimerCore.MODE_FOCUS
|
||
val accent = col(if (focusMode) R.color.brand else R.color.rest)
|
||
|
||
tvTime.text = Dates.clock(display)
|
||
ring.ringColor = accent
|
||
ring.trackColor = col(R.color.ring_track)
|
||
// 拖动时圆环按"选了多长"实时长出来,松手回到本轮进度
|
||
ring.progress = if (dragging) {
|
||
(TimerCore.durationMinutes() / 60f).coerceIn(0f, 1f)
|
||
} else {
|
||
TimerCore.progress()
|
||
}
|
||
|
||
// 圆环内标签与专注页完全一致:只说计时方向,不重复顶部的「工作/休息」
|
||
tvModeLabel.text = getString(if (TimerCore.countUp) R.string.count_up else R.string.count_down)
|
||
|
||
// 轮次圆点两种模式都保留;左右按钮在休息时改为隐形占位,
|
||
// 主按钮宽度和左右居中位置保持与专注页完全一致,不会被拉满整行
|
||
btnReset.visibility = if (focusMode) View.VISIBLE else View.INVISIBLE
|
||
btnFinish.visibility = if (focusMode) View.VISIBLE else View.INVISIBLE
|
||
taskCard.visible(focusMode)
|
||
dots.visibility = View.VISIBLE
|
||
// 右侧按钮随计时方向换形态:正计时=结束,倒计时=完成。
|
||
// 用 tag 记住当前形态,避免每 250ms 重建一次 drawable。
|
||
val endMode = focusMode && TimerCore.countUp
|
||
if (btnFinish.tag != endMode) {
|
||
btnFinish.tag = endMode
|
||
btnFinish.setImageResource(if (endMode) R.drawable.ic_end else R.drawable.ic_finish)
|
||
btnFinish.tint(col(R.color.text_secondary))
|
||
btnFinish.contentDescription =
|
||
getString(if (endMode) R.string.end_timing else R.string.finish)
|
||
}
|
||
|
||
btnMain.text = when {
|
||
!focusMode -> getString(if (TimerCore.running) R.string.end_break else R.string.start_break)
|
||
TimerCore.running -> getString(R.string.pause)
|
||
TimerCore.countUp -> if (display > 0) getString(R.string.resume) else getString(R.string.start_timing)
|
||
display < TimerCore.totalMs -> getString(R.string.resume)
|
||
else -> getString(R.string.start)
|
||
}
|
||
// 按钮底色随模式变,但每 250ms 新建一个 drawable 会造成无谓的 GC,
|
||
// 用 tag 记住当前颜色,只有真的变了才换。
|
||
if (btnMain.tag != accent) {
|
||
btnMain.tag = accent
|
||
btnMain.background = requireContext().roundBg(accent, 100f)
|
||
}
|
||
|
||
// 正计时与倒计时共用同一套布局:顶部模式分段、环内圆点都常驻,避免左右滑动时版面跳动
|
||
applySegments(focusMode)
|
||
renderPageDots()
|
||
renderDots(accent)
|
||
renderTask()
|
||
applyKeepScreen(TimerCore.running && Prefs.keepScreenOn)
|
||
syncTicker()
|
||
}
|
||
|
||
/**
|
||
* 轮询必须与 TimerCore.running 同步:点"开始"时先前空闲状态已经移除了回调,
|
||
* 若只靠 onResume 挂一次,圆环数字就会一直停在点击那一刻。
|
||
* 每次 render 都先撤后挂,保证任何时刻至多只有一个待执行的 ticker。
|
||
*/
|
||
private fun syncTicker() {
|
||
handler.removeCallbacks(ticker)
|
||
if (TimerCore.running && !isHidden && view != null) {
|
||
handler.postDelayed(ticker, 250L)
|
||
}
|
||
}
|
||
|
||
/** 通用的原因输入弹层:输入法弹出时整体上移,收起后归位 */
|
||
private fun reasonSheet(
|
||
titleRes: Int,
|
||
hintRes: Int,
|
||
inputHintRes: Int,
|
||
buttonRes: Int,
|
||
onConfirm: (String) -> Unit,
|
||
) {
|
||
val ctx = requireContext()
|
||
val sheet = SheetDialog(ctx)
|
||
val root = layoutInflater.inflate(R.layout.sheet_finish_reason, null)
|
||
root.findViewById<TextView>(R.id.tvReasonTitle).setText(titleRes)
|
||
root.findViewById<TextView>(R.id.tvReasonHint).setText(hintRes)
|
||
val etReason = root.findViewById<EditText>(R.id.etFinishReason)
|
||
etReason.setHint(inputHintRes)
|
||
val btnConfirm = root.findViewById<TextView>(R.id.btnFinishNow)
|
||
btnConfirm.setText(buttonRes)
|
||
btnConfirm.setOnClickListener {
|
||
// 备注里不保留任何方括号;历史数据由数据库升级一并清掉
|
||
val raw = etReason.text?.toString()?.trim().orEmpty()
|
||
.replace("【", "").replace("】", "")
|
||
onConfirm(raw)
|
||
sheet.dismiss()
|
||
render()
|
||
}
|
||
sheet.fitOnIme(root)
|
||
etReason.requestFocus()
|
||
sheet.show()
|
||
// show 之后窗口才有 InputMethodManager 的绑定关系;post 一次确保键盘真的弹出
|
||
etReason.post {
|
||
if (!isAdded) return@post
|
||
etReason.requestFocus()
|
||
runCatching {
|
||
(ctx.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager)
|
||
?.showSoftInput(etReason, InputMethodManager.SHOW_IMPLICIT)
|
||
}
|
||
}
|
||
}
|
||
|
||
/** 提前完成:按真实起止生成一条独立完成记录,原因只写在这一条上 */
|
||
private fun showFinishSheet() {
|
||
if (TimerCore.mode != TimerCore.MODE_FOCUS) return
|
||
if (TimerCore.elapsedNow() <= 0) {
|
||
Toast.makeText(requireContext(), R.string.finish_need_start, Toast.LENGTH_SHORT).show()
|
||
return
|
||
}
|
||
reasonSheet(
|
||
R.string.finish_reason_title,
|
||
R.string.finish_reason_hint,
|
||
R.string.finish_reason_input_hint,
|
||
R.string.finish_now,
|
||
) { raw ->
|
||
val finish = TimerCore.finishEarly(raw)
|
||
if (finish != null && finish.taskId > 0) {
|
||
(activity as? com.focusbox.app.ui.MainActivity)?.refreshAll()
|
||
Toast.makeText(requireContext(), R.string.finish_saved, Toast.LENGTH_SHORT).show()
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 正计时结束:秒表停下就是这一轮结束,直接按正常结束流程收尾
|
||
* (写专注记录、任务标记完成、停表复位),不弹原因输入框 ——
|
||
* 正计时没有"计划时长",也就谈不上"提前"。
|
||
*/
|
||
private fun endCountUp() {
|
||
if (TimerCore.mode != TimerCore.MODE_FOCUS) return
|
||
if (TimerCore.elapsedNow() <= 0) {
|
||
Toast.makeText(requireContext(), R.string.finish_need_start, Toast.LENGTH_SHORT).show()
|
||
return
|
||
}
|
||
TimerCore.stopEarly()
|
||
(activity as? com.focusbox.app.ui.MainActivity)?.refreshAll()
|
||
render()
|
||
}
|
||
|
||
/** 暂停也记原因;起止时段在点击"继续"时补齐并写入任务备注 */
|
||
private fun showPauseSheet() {
|
||
reasonSheet(
|
||
R.string.pause_reason_title,
|
||
R.string.pause_reason_hint,
|
||
R.string.pause_reason_input_hint,
|
||
R.string.pause_now,
|
||
) { raw ->
|
||
TimerCore.pause(raw)
|
||
if (raw.isNotEmpty()) {
|
||
Toast.makeText(requireContext(), R.string.pause_saved, Toast.LENGTH_SHORT).show()
|
||
}
|
||
}
|
||
}
|
||
|
||
private fun renderPageDots() {
|
||
val active = if (TimerCore.countUp) 1 else 0
|
||
val accent = col(R.color.brand)
|
||
val idle = col(R.color.ring_track)
|
||
for (i in 0 until pageDots.childCount) {
|
||
val v = pageDots.getChildAt(i)
|
||
val lp = v.layoutParams as LinearLayout.LayoutParams
|
||
lp.width = dpi(if (i == active) 16f else 6f)
|
||
v.layoutParams = lp
|
||
v.background = requireContext().roundBg(if (i == active) accent else idle, 100f)
|
||
}
|
||
}
|
||
|
||
private fun applySegments(focusMode: Boolean) {
|
||
val onText = col(R.color.title)
|
||
val offText = col(R.color.text_secondary)
|
||
fun style(tv: TextView, selected: Boolean) {
|
||
tv.background = if (selected) ContextCompat.getDrawable(requireContext(), R.drawable.bg_segment_on) else null
|
||
tv.setTextColor(if (selected) onText else offText)
|
||
tv.typeface = if (selected) android.graphics.Typeface.DEFAULT_BOLD else android.graphics.Typeface.DEFAULT
|
||
tv.alpha = if (selected) 1f else 0.9f
|
||
}
|
||
style(segFocus, focusMode)
|
||
style(segBreak, !focusMode)
|
||
segFocus.isEnabled = !TimerCore.running
|
||
segBreak.isEnabled = !TimerCore.running
|
||
}
|
||
|
||
private fun renderDots(accent: Int) {
|
||
val every = Prefs.longBreakEvery.coerceAtLeast(2)
|
||
if (dots.childCount != every) {
|
||
dots.removeAllViews()
|
||
repeat(every) {
|
||
val v = View(requireContext())
|
||
val lp = LinearLayout.LayoutParams(dpi(7f), dpi(7f))
|
||
lp.marginStart = dpi(4f)
|
||
lp.marginEnd = dpi(4f)
|
||
v.layoutParams = lp
|
||
// 轮次圆点只是进度装饰,读屏挨个念一遍反而吵
|
||
v.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO
|
||
dots.addView(v)
|
||
}
|
||
}
|
||
val filled = if (TimerCore.round > 0 && TimerCore.round % every == 0) every else TimerCore.round % every
|
||
for (i in 0 until every) {
|
||
dots.getChildAt(i).background = circleBg(
|
||
if (i < filled) accent else col(R.color.ring_track)
|
||
)
|
||
}
|
||
}
|
||
|
||
private fun renderTask() {
|
||
if (TimerCore.taskId != cachedTaskId) {
|
||
cachedTaskId = TimerCore.taskId
|
||
cachedTask = if (TimerCore.taskId > 0) Store.task(TimerCore.taskId) else null
|
||
cachedProject = cachedTask?.let { Store.project(it.projectId) }
|
||
}
|
||
val task = cachedTask
|
||
// 专注进行中,任务已经绑进这一轮:右侧文案改成「正在专注」,整卡不可点。
|
||
// 否则中途换任务会把 elapsed 清零重建(setTask 会重设 totalMs),这一轮就白跑了。
|
||
val locked = TimerCore.running && TimerCore.mode == TimerCore.MODE_FOCUS && task != null
|
||
taskCard.isClickable = !locked
|
||
if (task == null) {
|
||
taskDot.visible(false)
|
||
tvTask.text = getString(R.string.no_task)
|
||
tvTaskMeta.text = getString(R.string.current_task)
|
||
tvTaskAction.text = getString(R.string.pick_task)
|
||
return
|
||
}
|
||
val project = cachedProject
|
||
taskDot.visible(true)
|
||
taskDot.background = circleBg(project?.color ?: 0xFF7A756C.toInt())
|
||
tvTask.text = task.title
|
||
val projectName = project?.name ?: getString(R.string.inbox)
|
||
tvTaskMeta.text = getString(R.string.task_card_meta, projectName, task.focusMinutes, task.donePomodoros)
|
||
tvTaskAction.text = getString(if (locked) R.string.task_focusing else R.string.task_selected)
|
||
}
|
||
|
||
// ------------------------------------------------------------------ 手势
|
||
|
||
private fun handleDialTouch(ev: MotionEvent): Boolean {
|
||
when (ev.actionMasked) {
|
||
MotionEvent.ACTION_DOWN -> {
|
||
downX = ev.x
|
||
downY = ev.y
|
||
touchMode = 0
|
||
dragging = false
|
||
dragStartMinutes = TimerCore.durationMinutes()
|
||
}
|
||
|
||
MotionEvent.ACTION_MOVE -> {
|
||
val dx = ev.x - downX
|
||
val dy = ev.y - downY
|
||
if (touchMode == 0) {
|
||
if (abs(dx) > slop || abs(dy) > slop) {
|
||
touchMode = when {
|
||
abs(dx) > abs(dy) -> 2
|
||
TimerCore.running || TimerCore.countUp -> 3
|
||
else -> 1
|
||
}
|
||
}
|
||
}
|
||
if (touchMode == 1) {
|
||
val pxPerMinute = dpi(9f).toFloat().coerceAtLeast(1f)
|
||
val delta = ((downY - ev.y) / pxPerMinute).toInt()
|
||
val target = (dragStartMinutes + delta)
|
||
.coerceIn(TimerCore.MIN_MINUTES, TimerCore.MAX_MINUTES)
|
||
if (target != TimerCore.durationMinutes()) {
|
||
dragging = true
|
||
// 拖动过程不落盘,抬手时统一保存
|
||
TimerCore.setDurationMinutes(target, persist = false)
|
||
render()
|
||
}
|
||
}
|
||
}
|
||
|
||
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
||
if (touchMode == 1 && dragging) {
|
||
dragging = false
|
||
TimerCore.setDurationMinutes(TimerCore.durationMinutes(), persist = true)
|
||
render()
|
||
} else if (touchMode == 2) {
|
||
val dx = ev.x - downX
|
||
if (abs(dx) > dpi(44f)) switchPage(dx < 0)
|
||
}
|
||
touchMode = 0
|
||
}
|
||
}
|
||
return true
|
||
}
|
||
|
||
/** 左滑进正计时,右滑回倒计时;计时中不允许切,否则语义会打架 */
|
||
private fun switchPage(toCountUp: Boolean) {
|
||
if (TimerCore.running) {
|
||
Toast.makeText(requireContext(), R.string.timer_busy, Toast.LENGTH_SHORT).show()
|
||
return
|
||
}
|
||
if (TimerCore.countUp == toCountUp) return
|
||
// 两页布局完全一致,直接换状态即可;位移动画会在圆环上产生可见晃动
|
||
TimerCore.setCountUp(toCountUp)
|
||
render()
|
||
}
|
||
|
||
// ------------------------------------------------------------------ 任务
|
||
|
||
/**
|
||
* 选任务弹层第二步底部的「立即创建」:拉起新建任务弹层。
|
||
*
|
||
* [projectId] 是刚才在第二步看到的那个项目 —— 用户都已经点进这个项目的任务列表了,
|
||
* 新建的任务必然属于它,所以弹层里直接预选好项目,不用再选一次。
|
||
*
|
||
* 保存成功后把新任务设为当前任务(圆环套用它设定的专注时长),
|
||
* 与「选中已有任务」一样停在这一页等用户点开始,方便先调时长。
|
||
*/
|
||
/**
|
||
* 「立即创建」:建完任务后自动选中并停在这一页,**不自动开始**。
|
||
*
|
||
* 与选任务弹层同一个口径:选中只是把圆环换成该任务的时长,
|
||
* 还要让人有机会上下拖动调整,所以开跑仍然由主按钮触发。
|
||
*/
|
||
private fun createTaskAndSelect(projectId: Long) {
|
||
val ctx = context ?: return
|
||
val act = activity
|
||
val sheet = TaskSheet(ctx, null, projectId) { newId ->
|
||
if (newId > 0) {
|
||
Store.task(newId)?.let { t ->
|
||
TimerCore.setCountUp(false)
|
||
TimerCore.setTask(t.id, t.projectId, t.focusMinutes)
|
||
Toast.makeText(ctx, R.string.task_created, Toast.LENGTH_SHORT).show()
|
||
}
|
||
cachedTaskId = Long.MIN_VALUE
|
||
(act as? com.focusbox.app.ui.MainActivity)?.refreshAll()
|
||
}
|
||
if (view != null) render()
|
||
}
|
||
sheet.show()
|
||
}
|
||
|
||
/**
|
||
* 选择任务:第一步选项目,第二步只列出该项目的待办。
|
||
* 用自绘底部弹层而不是系统 AlertDialog,便于圆角、分步标题与选中态。
|
||
*/
|
||
private fun pickTask() {
|
||
val ctx = requireContext()
|
||
val sheet = SheetDialog(ctx)
|
||
val root = layoutInflater.inflate(R.layout.sheet_pick_task, null)
|
||
val tvTitle = root.findViewById<TextView>(R.id.pickTitle)
|
||
val tvStep = root.findViewById<TextView>(R.id.pickStep)
|
||
val titleBox = root.findViewById<LinearLayout>(R.id.pickTitleBox)
|
||
val tvHint = root.findViewById<TextView>(R.id.pickHint)
|
||
tvHint.visibility = View.GONE // 两步都只留标题与列表,不再占一行说明文字
|
||
val btnBack = root.findViewById<ImageView>(R.id.pickBack)
|
||
val btnClose = root.findViewById<ImageView>(R.id.pickClose)
|
||
val btnClear = root.findViewById<TextView>(R.id.pickClear)
|
||
val list = root.findViewById<LinearLayout>(R.id.pickList)
|
||
val scroll = root.findViewById<android.widget.ScrollView>(R.id.pickScroll)
|
||
|
||
fun addRow(
|
||
title: String,
|
||
meta: String,
|
||
color: Int?,
|
||
selected: Boolean,
|
||
alignLeft: Boolean = false,
|
||
onClick: () -> Unit,
|
||
) {
|
||
val row = layoutInflater.inflate(R.layout.item_pick_row, list, false)
|
||
val dot = row.findViewById<View>(R.id.rowDot)
|
||
dot.visible(!alignLeft && color != null)
|
||
if (color != null) dot.background = ctx.circleBg(color)
|
||
(row.findViewById<View>(R.id.rowInfo).layoutParams as LinearLayout.LayoutParams).apply {
|
||
marginStart = if (alignLeft || color == null) 0 else dpi(12f)
|
||
}
|
||
row.findViewById<TextView>(R.id.rowTitle).text = title
|
||
row.findViewById<TextView>(R.id.rowMeta).text = meta
|
||
row.findViewById<ImageView>(R.id.rowMark).visible(selected)
|
||
row.setOnClickListener { onClick() }
|
||
val lp = LinearLayout.LayoutParams(
|
||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||
LinearLayout.LayoutParams.WRAP_CONTENT,
|
||
)
|
||
lp.bottomMargin = dpi(8f)
|
||
row.layoutParams = lp
|
||
list.addView(row)
|
||
}
|
||
|
||
// 第二步当前停留的项目:底部的「立即创建」要把它带进新建任务弹层
|
||
var pickedProjectId = 0L
|
||
|
||
// 局部函数必须先声明后引用:showTasks 在前,showProjects 的点击回调才能调用它
|
||
fun showTasks(projectId: Long) {
|
||
pickedProjectId = projectId
|
||
val project = Store.project(projectId)
|
||
tvTitle.text = project?.name ?: getString(R.string.inbox)
|
||
tvStep.text = getString(R.string.pick_step, 2)
|
||
(titleBox.layoutParams as LinearLayout.LayoutParams).marginStart = dpi(10f)
|
||
btnBack.visibility = View.VISIBLE
|
||
btnClear.visibility = View.VISIBLE
|
||
list.removeAllViews()
|
||
// 选任务只按项目列任务:不按完成、逾期过滤,也不显示完成记录
|
||
val tasks = Store.projectTasks(projectId)
|
||
if (tasks.isEmpty()) {
|
||
list.addView(TextView(ctx).apply {
|
||
text = getString(R.string.pick_task_empty)
|
||
gravity = android.view.Gravity.CENTER
|
||
setPadding(dpi(12f), dpi(28f), dpi(12f), dpi(28f))
|
||
textSize = 13f
|
||
setTextColor(col(R.color.text_tertiary))
|
||
})
|
||
}
|
||
tasks.forEach { t ->
|
||
// 行内只写任务时间(专注时长),不挂截止/随时、也不标完成或逾期
|
||
val meta = getString(R.string.value_minutes, t.focusMinutes)
|
||
addRow(
|
||
title = t.title,
|
||
meta = meta,
|
||
color = project?.color,
|
||
selected = t.id == TimerCore.taskId,
|
||
) {
|
||
// 选中任务后回到倒计时,并把圆环切到该任务设定的时长。
|
||
// 这里**不自动开始**:选完任务常常还要上下拖动圆环调整本轮时长,
|
||
// 一旦自动跑起来,纵向拖动就被 handleDialTouch 判为忽略(running 时才不让调)。
|
||
TimerCore.setCountUp(false)
|
||
TimerCore.setTask(t.id, t.projectId, t.focusMinutes)
|
||
sheet.dismiss()
|
||
render()
|
||
}
|
||
}
|
||
scroll.scrollTo(0, 0)
|
||
}
|
||
|
||
fun showProjects() {
|
||
tvTitle.text = getString(R.string.pick_project)
|
||
tvStep.text = getString(R.string.pick_step, 1)
|
||
// 第一步没有返回键,连占位一起收掉,标题与步骤贴左边缘
|
||
(titleBox.layoutParams as LinearLayout.LayoutParams).marginStart = 0
|
||
btnBack.visibility = View.GONE
|
||
btnClear.visibility = View.GONE
|
||
list.removeAllViews()
|
||
val currentPid = if (TimerCore.taskId > 0) TimerCore.projectId else -1L
|
||
Store.projects().forEach { p ->
|
||
addRow(
|
||
title = p.name,
|
||
meta = getString(R.string.pick_project_meta, p.totalTasks),
|
||
color = p.color,
|
||
selected = p.id == currentPid,
|
||
alignLeft = true, // 项目列表不要左侧色点缩进,名称贴左对齐
|
||
) { showTasks(p.id) }
|
||
}
|
||
scroll.scrollTo(0, 0)
|
||
}
|
||
|
||
btnBack.setOnClickListener { showProjects() }
|
||
btnClose.setOnClickListener { sheet.dismiss() }
|
||
// 第二步底部:没有合适的任务就当场建一个,建完自动选中并回到专注页
|
||
btnClear.text = getString(R.string.create_now)
|
||
btnClear.setTextColor(col(R.color.brand))
|
||
btnClear.setOnClickListener {
|
||
sheet.dismiss()
|
||
createTaskAndSelect(pickedProjectId)
|
||
}
|
||
sheet.setContentView(root)
|
||
showProjects()
|
||
sheet.show()
|
||
}
|
||
|
||
private fun applyKeepScreen(on: Boolean) {
|
||
if (keepScreen == on) return
|
||
keepScreen = on
|
||
val w = requireActivity().window
|
||
if (on) w.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||
else w.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||
}
|
||
|
||
private fun requestNotifPermissionIfNeeded() {
|
||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return
|
||
val ctx = context ?: return
|
||
if (ContextCompat.checkSelfPermission(ctx, Manifest.permission.POST_NOTIFICATIONS)
|
||
!= PackageManager.PERMISSION_GRANTED
|
||
) {
|
||
notifPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
|
||
}
|
||
}
|
||
}
|