2026-11-28 00:25:22 +08:00
|
|
|
|
package com.focusbox.app.ui.focus
|
|
|
|
|
|
|
|
|
|
|
|
import android.Manifest
|
|
|
|
|
|
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.ViewConfiguration
|
|
|
|
|
|
import android.view.WindowManager
|
|
|
|
|
|
import android.widget.ImageView
|
|
|
|
|
|
import android.widget.LinearLayout
|
|
|
|
|
|
import android.widget.TextView
|
|
|
|
|
|
import android.widget.Toast
|
|
|
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
|
|
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
|
2026-11-28 07:29:37 +08:00
|
|
|
|
import com.focusbox.app.ui.SheetDialog
|
2026-11-28 00:25:22 +08:00
|
|
|
|
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 tvDialHint: 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 btnSkip: ImageView
|
|
|
|
|
|
private lateinit var tvTask: TextView
|
|
|
|
|
|
private lateinit var tvTaskMeta: TextView
|
|
|
|
|
|
private lateinit var tvTaskAction: TextView
|
|
|
|
|
|
private lateinit var taskDot: 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() {
|
|
|
|
|
|
// 兜底推进:万一前台服务被系统限流没起来,只要页面还在前台,
|
|
|
|
|
|
// 计时与到时结算仍然准确。
|
|
|
|
|
|
if (TimerCore.running) TimerCore.tick()
|
|
|
|
|
|
render()
|
|
|
|
|
|
handler.postDelayed(this, 250L)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private val onTimerChanged: () -> Unit = { if (isAdded && view != null) render() }
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
tvDialHint = view.findViewById(R.id.tvDialHint)
|
|
|
|
|
|
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)
|
|
|
|
|
|
btnSkip = view.findViewById(R.id.btnSkip)
|
|
|
|
|
|
tvTask = view.findViewById(R.id.tvTask)
|
|
|
|
|
|
tvTaskMeta = view.findViewById(R.id.tvTaskMeta)
|
|
|
|
|
|
tvTaskAction = view.findViewById(R.id.tvTaskAction)
|
|
|
|
|
|
taskDot = view.findViewById(R.id.taskDot)
|
|
|
|
|
|
segFocus = view.findViewById(R.id.segFocus)
|
|
|
|
|
|
segBreak = view.findViewById(R.id.segBreak)
|
|
|
|
|
|
|
|
|
|
|
|
slop = ViewConfiguration.get(requireContext()).scaledTouchSlop
|
|
|
|
|
|
btnReset.tint(col(R.color.text_secondary))
|
|
|
|
|
|
btnSkip.tint(col(R.color.text_secondary))
|
|
|
|
|
|
|
|
|
|
|
|
btnMain.setOnClickListener {
|
|
|
|
|
|
if (TimerCore.running) {
|
|
|
|
|
|
TimerCore.pause()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
requestNotifPermissionIfNeeded()
|
|
|
|
|
|
TimerCore.start()
|
|
|
|
|
|
}
|
|
|
|
|
|
render()
|
|
|
|
|
|
}
|
|
|
|
|
|
btnReset.setOnClickListener {
|
|
|
|
|
|
TimerCore.reset()
|
|
|
|
|
|
render()
|
|
|
|
|
|
}
|
|
|
|
|
|
btnSkip.setOnClickListener {
|
|
|
|
|
|
TimerCore.skip()
|
|
|
|
|
|
render()
|
|
|
|
|
|
}
|
|
|
|
|
|
view.findViewById<View>(R.id.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)
|
|
|
|
|
|
render()
|
|
|
|
|
|
handler.removeCallbacks(ticker)
|
|
|
|
|
|
// show/hide 模式下所有片段都处于 resumed,隐藏的页面不该继续跑 250ms 刷新
|
|
|
|
|
|
if (!isHidden) handler.post(ticker)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
render()
|
|
|
|
|
|
handler.removeCallbacks(ticker)
|
|
|
|
|
|
handler.post(ticker)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------ 绘制
|
|
|
|
|
|
|
|
|
|
|
|
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 = when {
|
|
|
|
|
|
TimerCore.countUp -> getString(R.string.count_up)
|
|
|
|
|
|
TimerCore.mode == TimerCore.MODE_SHORT -> getString(R.string.mode_break)
|
|
|
|
|
|
TimerCore.mode == TimerCore.MODE_LONG -> getString(R.string.mode_long_break)
|
|
|
|
|
|
else -> getString(R.string.count_down)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
btnMain.text = when {
|
|
|
|
|
|
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)
|
|
|
|
|
|
focusMode -> getString(R.string.start)
|
|
|
|
|
|
else -> getString(R.string.start_break)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 按钮底色随模式变,但每 250ms 新建一个 drawable 会造成无谓的 GC,
|
|
|
|
|
|
// 用 tag 记住当前颜色,只有真的变了才换。
|
|
|
|
|
|
if (btnMain.tag != accent) {
|
|
|
|
|
|
btnMain.tag = accent
|
|
|
|
|
|
btnMain.background = requireContext().roundBg(accent, 100f)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
modeSwitch.visible(!TimerCore.countUp)
|
|
|
|
|
|
applySegments(focusMode)
|
|
|
|
|
|
renderPageDots()
|
|
|
|
|
|
renderDots(accent)
|
|
|
|
|
|
renderTask()
|
|
|
|
|
|
renderHint()
|
|
|
|
|
|
applyKeepScreen(TimerCore.running && Prefs.keepScreenOn)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun renderHint() {
|
|
|
|
|
|
val text = when {
|
|
|
|
|
|
TimerCore.running -> ""
|
|
|
|
|
|
TimerCore.countUp -> getString(R.string.dial_hint_count_up)
|
|
|
|
|
|
// 休息时长属于设置项,不引导用户在休息环上拖动
|
|
|
|
|
|
TimerCore.mode == TimerCore.MODE_FOCUS -> getString(R.string.dial_hint)
|
|
|
|
|
|
else -> ""
|
|
|
|
|
|
}
|
|
|
|
|
|
tvDialHint.text = text
|
|
|
|
|
|
tvDialHint.visible(text.isNotEmpty())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
dots.visible(!TimerCore.countUp)
|
|
|
|
|
|
if (TimerCore.countUp) return
|
|
|
|
|
|
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
|
|
|
|
|
|
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
|
|
|
|
|
|
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 = "$projectName · ${task.focusMinutes} 分钟 · 已完成 ${task.donePomodoros} 次"
|
|
|
|
|
|
tvTaskAction.text = getString(R.string.pick_task)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------ 手势
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
val dir = if (toCountUp) -1f else 1f
|
|
|
|
|
|
val shift = dpi(36f).toFloat()
|
|
|
|
|
|
dialWrap.animate()
|
|
|
|
|
|
.translationX(dir * shift)
|
|
|
|
|
|
.alpha(0f)
|
|
|
|
|
|
.setDuration(110)
|
|
|
|
|
|
.withEndAction {
|
|
|
|
|
|
if (view == null) return@withEndAction
|
|
|
|
|
|
TimerCore.setCountUp(toCountUp)
|
|
|
|
|
|
render()
|
|
|
|
|
|
dialWrap.translationX = -dir * shift
|
|
|
|
|
|
dialWrap.animate().translationX(0f).alpha(1f).setDuration(140).start()
|
|
|
|
|
|
}
|
|
|
|
|
|
.start()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------ 任务
|
|
|
|
|
|
|
2026-11-28 07:29:37 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 选择任务:第一步选项目,第二步只列出该项目的待办。
|
|
|
|
|
|
* 用自绘底部弹层而不是系统 AlertDialog,便于圆角、分步标题与选中态。
|
|
|
|
|
|
*/
|
2026-11-28 00:25:22 +08:00
|
|
|
|
private fun pickTask() {
|
2026-11-28 07:29:37 +08:00
|
|
|
|
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 tvHint = root.findViewById<TextView>(R.id.pickHint)
|
|
|
|
|
|
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,
|
|
|
|
|
|
onClick: () -> Unit,
|
|
|
|
|
|
) {
|
|
|
|
|
|
val row = layoutInflater.inflate(R.layout.item_pick_row, list, false)
|
|
|
|
|
|
row.findViewById<View>(R.id.rowDot).apply {
|
|
|
|
|
|
visible(color != null)
|
|
|
|
|
|
if (color != null) background = ctx.circleBg(color)
|
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
2026-11-28 07:29:37 +08:00
|
|
|
|
|
|
|
|
|
|
// 局部函数必须先声明后引用:showTasks 在前,showProjects 的点击回调才能调用它
|
|
|
|
|
|
fun showTasks(projectId: Long) {
|
|
|
|
|
|
val project = Store.project(projectId)
|
|
|
|
|
|
tvTitle.text = project?.name ?: getString(R.string.inbox)
|
|
|
|
|
|
tvStep.text = getString(R.string.pick_step, 2)
|
|
|
|
|
|
tvHint.text = getString(R.string.pick_task_hint)
|
|
|
|
|
|
btnBack.visibility = View.VISIBLE
|
|
|
|
|
|
btnClear.visibility = View.VISIBLE
|
|
|
|
|
|
list.removeAllViews()
|
|
|
|
|
|
val tasks = Store.tasks(projectId, includeDone = false)
|
|
|
|
|
|
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 = buildString {
|
|
|
|
|
|
append(getString(R.string.minutes_value, t.focusMinutes))
|
|
|
|
|
|
if (t.due > 0) append(" · ${Dates.dueLabel(t.due)}截止")
|
|
|
|
|
|
}
|
|
|
|
|
|
addRow(
|
|
|
|
|
|
title = t.title,
|
|
|
|
|
|
meta = meta,
|
|
|
|
|
|
color = project?.color,
|
|
|
|
|
|
selected = t.id == TimerCore.taskId,
|
|
|
|
|
|
) {
|
2026-11-28 00:25:22 +08:00
|
|
|
|
// 选中任务后回到倒计时,并把圆环切到该任务设定的时长
|
|
|
|
|
|
TimerCore.setCountUp(false)
|
|
|
|
|
|
TimerCore.setTask(t.id, t.projectId, t.focusMinutes)
|
2026-11-28 07:29:37 +08:00
|
|
|
|
sheet.dismiss()
|
|
|
|
|
|
render()
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-28 07:29:37 +08:00
|
|
|
|
scroll.scrollTo(0, 0)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun showProjects() {
|
|
|
|
|
|
tvTitle.text = getString(R.string.pick_project)
|
|
|
|
|
|
tvStep.text = getString(R.string.pick_step, 1)
|
|
|
|
|
|
tvHint.text = getString(R.string.pick_project_hint)
|
|
|
|
|
|
btnBack.visibility = View.INVISIBLE
|
|
|
|
|
|
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.openTasks),
|
|
|
|
|
|
color = p.color,
|
|
|
|
|
|
selected = p.id == currentPid,
|
|
|
|
|
|
) { showTasks(p.id) }
|
|
|
|
|
|
}
|
|
|
|
|
|
scroll.scrollTo(0, 0)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
btnBack.setOnClickListener { showProjects() }
|
|
|
|
|
|
btnClose.setOnClickListener { sheet.dismiss() }
|
|
|
|
|
|
btnClear.setOnClickListener {
|
|
|
|
|
|
TimerCore.setTask(0, 0)
|
|
|
|
|
|
sheet.dismiss()
|
|
|
|
|
|
render()
|
|
|
|
|
|
}
|
|
|
|
|
|
sheet.setContentView(root)
|
|
|
|
|
|
showProjects()
|
|
|
|
|
|
sheet.show()
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
) {
|
|
|
|
|
|
requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), 101)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|