右侧按钮改为完成并加入原因弹层;已完成任务显示真实时长;删除首页说明;修复审查发现的计时与数据缺陷
This commit is contained in:
@@ -27,7 +27,7 @@ object Store {
|
||||
const val INBOX_ID = 1L
|
||||
|
||||
private const val DB_NAME = "focusbox.db"
|
||||
private const val DB_VERSION = 2
|
||||
private const val DB_VERSION = 3
|
||||
|
||||
private lateinit var helper: Helper
|
||||
|
||||
@@ -68,7 +68,8 @@ object Store {
|
||||
sort INTEGER NOT NULL DEFAULT 0,
|
||||
focus_min INTEGER NOT NULL DEFAULT 25,
|
||||
repeat_type INTEGER NOT NULL DEFAULT 0,
|
||||
repeat_every INTEGER NOT NULL DEFAULT 1
|
||||
repeat_every INTEGER NOT NULL DEFAULT 1,
|
||||
origin_id INTEGER NOT NULL DEFAULT 0
|
||||
)
|
||||
""".trimIndent()
|
||||
)
|
||||
@@ -104,6 +105,10 @@ object Store {
|
||||
db.execSQL("ALTER TABLE tasks ADD COLUMN repeat_every INTEGER NOT NULL DEFAULT 1")
|
||||
db.execSQL("UPDATE tasks SET focus_min = est_pomodoros * 25 WHERE est_pomodoros > 0")
|
||||
}
|
||||
// v2 → v3:记录重复任务的来源,取消完成时能精确删掉自动生成的新一条
|
||||
if (oldVersion < 3) {
|
||||
db.execSQL("ALTER TABLE tasks ADD COLUMN origin_id INTEGER NOT NULL DEFAULT 0")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,6 +273,7 @@ object Store {
|
||||
*/
|
||||
fun setTaskDone(id: Long, done: Boolean) {
|
||||
if (!done) {
|
||||
val t = task(id)
|
||||
db.update(
|
||||
"tasks",
|
||||
ContentValues().apply {
|
||||
@@ -276,6 +282,14 @@ object Store {
|
||||
},
|
||||
"_id = ?", arrayOf(id.toString())
|
||||
)
|
||||
// 撤销重复任务时,上次勾选自动生成的新一条要一起撤掉,否则清单会多出一条
|
||||
if (t != null && t.repeating) {
|
||||
db.delete(
|
||||
"tasks",
|
||||
"origin_id = ? AND done = 0 AND _id != ?",
|
||||
arrayOf(id.toString(), id.toString())
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
val t = task(id) ?: return
|
||||
@@ -298,7 +312,7 @@ object Store {
|
||||
if (next == base) return
|
||||
// 下一次的截止日至少比今天晚一天,否则刚生成就会显示"已逾期"
|
||||
val due = if (next <= Dates.todayStart()) Dates.addDays(Dates.todayStart(), 1) else next
|
||||
addTask(
|
||||
val childId = addTask(
|
||||
title = t.title,
|
||||
note = t.note,
|
||||
projectId = t.projectId,
|
||||
@@ -308,6 +322,13 @@ object Store {
|
||||
repeatType = t.repeatType,
|
||||
repeatEvery = t.repeatEvery,
|
||||
)
|
||||
if (childId > 0) {
|
||||
db.update(
|
||||
"tasks",
|
||||
ContentValues().apply { put("origin_id", t.id) },
|
||||
"_id = ?", arrayOf(childId.toString())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** 把日期按重复规则往后推一个周期 */
|
||||
@@ -539,7 +560,7 @@ object Store {
|
||||
val map = HashMap<Long, SessionSpan>()
|
||||
db.rawQuery(
|
||||
"""
|
||||
SELECT s.task_id, s.start_at, s.end_at FROM sessions s
|
||||
SELECT s.task_id, s.start_at, s.end_at, s.seconds FROM sessions s
|
||||
JOIN (
|
||||
SELECT task_id, MAX(end_at) AS max_end FROM sessions
|
||||
WHERE task_id > 0 GROUP BY task_id
|
||||
@@ -549,7 +570,7 @@ object Store {
|
||||
).use { c ->
|
||||
while (c.moveToNext()) {
|
||||
val id = c.getLong(0)
|
||||
if (!map.containsKey(id)) map[id] = SessionSpan(c.getLong(1), c.getLong(2))
|
||||
if (!map.containsKey(id)) map[id] = SessionSpan(c.getLong(1), c.getLong(2), c.getInt(3))
|
||||
}
|
||||
}
|
||||
return map
|
||||
@@ -609,6 +630,8 @@ object Store {
|
||||
db.delete("sessions", null, null)
|
||||
db.delete("tasks", null, null)
|
||||
db.delete("projects", null, null)
|
||||
// 不清自增序列的话,清空后第一个项目 id 会继续往上跑,永远不再是收件箱的 1
|
||||
db.delete("sqlite_sequence", "name IN (?,?,?)", arrayOf("projects", "tasks", "sessions"))
|
||||
db.execSQL(
|
||||
"INSERT INTO projects (name, color, archived, sort, created_at) VALUES (?,?,?,?,?)",
|
||||
arrayOf<Any>("收件箱", 0xFF7A756C.toInt(), 0, 0, System.currentTimeMillis())
|
||||
|
||||
@@ -43,8 +43,17 @@ data class Task(
|
||||
val repeating: Boolean get() = repeatType != Repeat.NONE && repeatEvery > 0
|
||||
}
|
||||
|
||||
/** 一条专注记录的起止时刻,用于在已完成任务上显示实际工作时段。 */
|
||||
data class SessionSpan(val startAt: Long, val endAt: Long)
|
||||
/** 一条专注记录的起止时刻与实际秒数,用于在已完成任务上显示真实工作时段。 */
|
||||
data class SessionSpan(val startAt: Long, val endAt: Long, val seconds: Int = 0)
|
||||
|
||||
/** 提前完成时交给 UI 落备注的实际专注信息 */
|
||||
data class FocusFinish(
|
||||
val taskId: Long,
|
||||
val projectId: Long,
|
||||
val startAt: Long,
|
||||
val endAt: Long,
|
||||
val seconds: Int,
|
||||
)
|
||||
|
||||
data class DayPoint(
|
||||
val key: String, // yyyy-MM-dd
|
||||
|
||||
@@ -168,10 +168,12 @@ object Notif {
|
||||
base = now + TimerCore.remainingNow()
|
||||
countDown = true
|
||||
}
|
||||
rv.setChronometer(R.id.notifChrono, base, null, running)
|
||||
// 方向必须在 base 之前下发:部分 ROM 会按 RemoteViews 指令顺序应用属性,
|
||||
// 先 setChronometer 再切 countDown 会出现先按正计时算、再倒着走的错读数。
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
rv.setChronometerCountDown(R.id.notifChrono, countDown)
|
||||
}
|
||||
rv.setChronometer(R.id.notifChrono, base, null, running)
|
||||
}
|
||||
|
||||
fun updateOngoing(context: Context) {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -59,6 +59,8 @@ class TimerService : Service() {
|
||||
|
||||
private val onStateChanged: () -> Unit = {
|
||||
Notif.updateOngoing(this)
|
||||
// 暂停/结束时同步停掉 1 秒轮询,别让 Handler 空转
|
||||
syncTicker()
|
||||
syncWakeLock()
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import androidx.fragment.app.Fragment
|
||||
import com.focusbox.app.R
|
||||
import com.focusbox.app.model.Task
|
||||
import com.focusbox.app.service.TimerCore
|
||||
import com.focusbox.app.service.TimerService
|
||||
import com.focusbox.app.ui.focus.FocusFragment
|
||||
import com.focusbox.app.ui.projects.ProjectsFragment
|
||||
import com.focusbox.app.ui.settings.SettingsFragment
|
||||
@@ -80,6 +81,13 @@ class MainActivity : AppCompatActivity() {
|
||||
showTab(start, force = true)
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
// 进程曾被系统回收时,前台服务也随之消失;回到前台补一次拉起,
|
||||
// 否则计时状态还在走,却没有常驻通知也拿不到到点提醒。
|
||||
if (TimerCore.running) TimerService.ensureRunning(this)
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
outState.putInt(KEY_TAB, current)
|
||||
super.onSaveInstanceState(outState)
|
||||
|
||||
@@ -10,6 +10,7 @@ import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.ViewConfiguration
|
||||
import android.view.WindowManager
|
||||
import android.widget.EditText
|
||||
import android.widget.ImageView
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
@@ -47,14 +48,13 @@ 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 btnFinish: ImageView
|
||||
private lateinit var tvTask: TextView
|
||||
private lateinit var tvTaskMeta: TextView
|
||||
private lateinit var tvTaskAction: TextView
|
||||
@@ -84,7 +84,8 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
|
||||
// 计时与到时结算仍然准确。
|
||||
if (TimerCore.running) TimerCore.tick()
|
||||
render()
|
||||
handler.postDelayed(this, 250L)
|
||||
// 空闲且没拖动时 render 已经移除轮询,这里不再续命
|
||||
if (TimerCore.running || dragging) handler.postDelayed(this, 250L)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,14 +96,13 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
|
||||
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)
|
||||
btnFinish = view.findViewById(R.id.btnFinish)
|
||||
tvTask = view.findViewById(R.id.tvTask)
|
||||
tvTaskMeta = view.findViewById(R.id.tvTaskMeta)
|
||||
tvTaskAction = view.findViewById(R.id.tvTaskAction)
|
||||
@@ -112,7 +112,7 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
|
||||
|
||||
slop = ViewConfiguration.get(requireContext()).scaledTouchSlop
|
||||
btnReset.tint(col(R.color.text_secondary))
|
||||
btnSkip.tint(col(R.color.text_secondary))
|
||||
btnFinish.tint(col(R.color.text_secondary))
|
||||
|
||||
btnMain.setOnClickListener {
|
||||
if (TimerCore.running) {
|
||||
@@ -127,10 +127,7 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
|
||||
TimerCore.reset()
|
||||
render()
|
||||
}
|
||||
btnSkip.setOnClickListener {
|
||||
TimerCore.skip()
|
||||
render()
|
||||
}
|
||||
btnFinish.setOnClickListener { showFinishSheet() }
|
||||
view.findViewById<View>(R.id.taskCard).setOnClickListener { pickTask() }
|
||||
segFocus.setOnClickListener {
|
||||
if (!TimerCore.running) {
|
||||
@@ -175,6 +172,8 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
|
||||
handler.removeCallbacks(ticker)
|
||||
applyKeepScreen(false)
|
||||
} else {
|
||||
// 任务可能在别的页面被改名/改时长,重新显示时强制重读
|
||||
cachedTaskId = Long.MIN_VALUE
|
||||
render()
|
||||
handler.removeCallbacks(ticker)
|
||||
handler.post(ticker)
|
||||
@@ -237,20 +236,46 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
|
||||
renderPageDots()
|
||||
renderDots(accent)
|
||||
renderTask()
|
||||
renderHint()
|
||||
applyKeepScreen(TimerCore.running && Prefs.keepScreenOn)
|
||||
// 空闲且没在拖动时不必每 250ms 重绘一次,省电也避免无谓 GC
|
||||
if (!TimerCore.running && !dragging) handler.removeCallbacks(ticker)
|
||||
}
|
||||
|
||||
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 -> ""
|
||||
/**
|
||||
* 提前完成:先写原因,再按真实起止结束本轮。
|
||||
* 原因会追加到任务备注并标记任务完成,任务页的已完成列表能看到备注与实际时段。
|
||||
*/
|
||||
private fun showFinishSheet() {
|
||||
if (TimerCore.elapsedNow() <= 0) {
|
||||
Toast.makeText(requireContext(), R.string.finish_need_start, Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
tvDialHint.text = text
|
||||
tvDialHint.visible(text.isNotEmpty())
|
||||
val ctx = requireContext()
|
||||
val sheet = SheetDialog(ctx)
|
||||
val root = layoutInflater.inflate(R.layout.sheet_finish_reason, null)
|
||||
val etReason = root.findViewById<EditText>(R.id.etFinishReason)
|
||||
root.findViewById<View>(R.id.btnFinishNow).setOnClickListener {
|
||||
val raw = etReason.text?.toString()?.trim().orEmpty()
|
||||
// 休息阶段点完成不算任务完成,只按实际时长记一条未完成记录
|
||||
val wasFocus = TimerCore.mode == TimerCore.MODE_FOCUS
|
||||
val finish = TimerCore.finishEarly(wasFocus)
|
||||
if (finish != null && wasFocus && finish.taskId > 0) {
|
||||
Store.task(finish.taskId)?.let { task ->
|
||||
if (raw.isNotEmpty()) {
|
||||
val line = getString(R.string.task_note_finish, Dates.hms(finish.endAt), raw)
|
||||
val note = if (task.note.isBlank()) line else "${task.note}\n$line"
|
||||
Store.updateTask(task.copy(note = note))
|
||||
}
|
||||
Store.setTaskDone(finish.taskId, true)
|
||||
}
|
||||
(activity as? com.focusbox.app.ui.MainActivity)?.refreshAll()
|
||||
Toast.makeText(ctx, R.string.finish_saved, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
sheet.dismiss()
|
||||
render()
|
||||
}
|
||||
sheet.setContentView(root)
|
||||
sheet.show()
|
||||
}
|
||||
|
||||
private fun renderPageDots() {
|
||||
|
||||
@@ -161,8 +161,10 @@ class TasksFragment : Fragment(R.layout.fragment_tasks) {
|
||||
private fun doneRange(task: Task, spans: Map<Long, SessionSpan>): String? {
|
||||
val span = spans[task.id]
|
||||
if (span != null) {
|
||||
return "${Dates.dateWithWeek(span.startAt)} " +
|
||||
val range = "${Dates.dateWithWeek(span.startAt)} " +
|
||||
"${Dates.hms(span.startAt)}-${Dates.hms(span.endAt)}"
|
||||
// 记录里存的是实际专注秒数(不含暂停),不能拿起止时间差冒充
|
||||
return if (span.seconds > 0) "$range · 专注${Dates.duration(span.seconds)}" else range
|
||||
}
|
||||
if (task.doneAt > 0) {
|
||||
return "${Dates.dateWithWeek(task.doneAt)} ${Dates.hms(task.doneAt)}"
|
||||
|
||||
@@ -87,6 +87,16 @@ object Prefs {
|
||||
get() = sp.getLong("t_end", 0L)
|
||||
set(v) = sp.edit().putLong("t_end", v).apply()
|
||||
|
||||
/** 单调钟终点,墙钟被 NTP 改动时继续按它走时 */
|
||||
var timerEndElapsed: Long
|
||||
get() = sp.getLong("t_end_elapsed", 0L)
|
||||
set(v) = sp.edit().putLong("t_end_elapsed", v).apply()
|
||||
|
||||
/** 保存终点时的 elapsedRealtime - currentTimeMillis,用于识别开机 */
|
||||
var timerClockOffset: Long
|
||||
get() = sp.getLong("t_clock_offset", 0L)
|
||||
set(v) = sp.edit().putLong("t_clock_offset", v).apply()
|
||||
|
||||
var timerTaskId: Long
|
||||
get() = sp.getLong("t_task", 0L)
|
||||
set(v) = sp.edit().putLong("t_task", v).apply()
|
||||
@@ -118,6 +128,7 @@ object Prefs {
|
||||
.putBoolean("t_running", false)
|
||||
.putLong("t_remaining", 0L)
|
||||
.putLong("t_end", 0L)
|
||||
.putLong("t_end_elapsed", 0L)
|
||||
.putLong("t_started", 0L)
|
||||
.putLong("t_accum", 0L)
|
||||
.apply()
|
||||
|
||||
Reference in New Issue
Block a user