正倒计时布局统一消除晃动;原因弹层适配输入法;已完成按钮位置固定;原因文案与暂停原因;科技蓝主色
This commit is contained in:
@@ -6,8 +6,10 @@ import android.os.Build
|
||||
import android.os.SystemClock
|
||||
import android.os.VibrationEffect
|
||||
import android.os.Vibrator
|
||||
import com.focusbox.app.R
|
||||
import com.focusbox.app.data.Store
|
||||
import com.focusbox.app.model.FocusFinish
|
||||
import com.focusbox.app.util.Dates
|
||||
import com.focusbox.app.util.Prefs
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
import kotlin.math.abs
|
||||
@@ -74,6 +76,11 @@ object TimerCore {
|
||||
private var endElapsed = 0L
|
||||
private var clockOffset = 0L
|
||||
|
||||
/** 暂停原因在恢复时才补写备注,这里暂存起点与任务 */
|
||||
private var pendingPauseReason = ""
|
||||
private var pendingPauseStartAt = 0L
|
||||
private var pendingPauseTaskId = 0L
|
||||
|
||||
private val listeners = CopyOnWriteArrayList<() -> Unit>()
|
||||
|
||||
fun addListener(l: () -> Unit) {
|
||||
@@ -105,6 +112,9 @@ object TimerCore {
|
||||
startedAt = Prefs.timerStartedAt
|
||||
endElapsed = Prefs.timerEndElapsed
|
||||
clockOffset = Prefs.timerClockOffset
|
||||
pendingPauseReason = Prefs.timerPauseReason
|
||||
pendingPauseStartAt = Prefs.timerPauseStartAt
|
||||
pendingPauseTaskId = Prefs.timerPauseTaskId
|
||||
|
||||
if (running) {
|
||||
if (countUp) {
|
||||
@@ -203,6 +213,7 @@ object TimerCore {
|
||||
startedAt = 0
|
||||
endAt = 0
|
||||
endElapsed = 0
|
||||
clearPause()
|
||||
save()
|
||||
notifyChanged()
|
||||
}
|
||||
@@ -244,6 +255,7 @@ object TimerCore {
|
||||
endAt = 0
|
||||
endElapsed = 0
|
||||
accReset()
|
||||
clearPause()
|
||||
save()
|
||||
notifyChanged()
|
||||
}
|
||||
@@ -290,8 +302,13 @@ object TimerCore {
|
||||
notifyChanged()
|
||||
}
|
||||
|
||||
fun pause() {
|
||||
fun pause(reason: String = "") {
|
||||
if (!running) return
|
||||
if (reason.isNotBlank()) {
|
||||
pendingPauseReason = reason.trim()
|
||||
pendingPauseStartAt = System.currentTimeMillis()
|
||||
pendingPauseTaskId = taskId
|
||||
}
|
||||
if (countUp) {
|
||||
accumMs += (System.currentTimeMillis() - startedAt).coerceAtLeast(0)
|
||||
startedAt = 0
|
||||
@@ -306,11 +323,31 @@ object TimerCore {
|
||||
notifyChanged()
|
||||
}
|
||||
|
||||
fun resume() = start()
|
||||
fun resume() {
|
||||
val reason = pendingPauseReason
|
||||
val startAt = pendingPauseStartAt
|
||||
val pausedTaskId = pendingPauseTaskId
|
||||
start()
|
||||
if (reason.isNotBlank() && startAt > 0L && pausedTaskId > 0L) {
|
||||
val endAtPause = System.currentTimeMillis()
|
||||
val seconds = ((endAtPause - startAt) / 1000).toInt().coerceAtLeast(0)
|
||||
val line = ctx.getString(
|
||||
R.string.task_note_pause,
|
||||
reason,
|
||||
Dates.hms(startAt),
|
||||
Dates.hms(endAtPause),
|
||||
Dates.duration(seconds),
|
||||
)
|
||||
appendTaskNote(pausedTaskId, line)
|
||||
}
|
||||
clearPause()
|
||||
save()
|
||||
}
|
||||
|
||||
/** 回到本轮起点,不记记录。倒计时保留当前设定的时长,正计时归零。 */
|
||||
fun reset() {
|
||||
running = false
|
||||
clearPause()
|
||||
endAt = 0
|
||||
startedAt = 0
|
||||
if (countUp) {
|
||||
@@ -349,6 +386,7 @@ object TimerCore {
|
||||
totalMs = if (totalMs > 0) totalMs else durationFor(mode)
|
||||
remainingMs = totalMs
|
||||
}
|
||||
clearPause()
|
||||
save()
|
||||
notifyChanged()
|
||||
TimerService.stop(ctx)
|
||||
@@ -372,6 +410,7 @@ object TimerCore {
|
||||
endElapsed = 0
|
||||
startedAt = 0
|
||||
if (countUp) accReset() else remainingMs = totalMs
|
||||
clearPause()
|
||||
save()
|
||||
notifyChanged()
|
||||
TimerService.stop(ctx)
|
||||
@@ -399,6 +438,7 @@ object TimerCore {
|
||||
totalMs = durationFor(MODE_FOCUS)
|
||||
remainingMs = totalMs
|
||||
}
|
||||
clearPause()
|
||||
save()
|
||||
notifyChanged()
|
||||
TimerService.stop(ctx)
|
||||
@@ -485,6 +525,21 @@ object TimerCore {
|
||||
Prefs.timerAccumMs = 0
|
||||
}
|
||||
|
||||
private fun clearPause() {
|
||||
pendingPauseReason = ""
|
||||
pendingPauseStartAt = 0L
|
||||
pendingPauseTaskId = 0L
|
||||
}
|
||||
|
||||
private fun appendTaskNote(id: Long, line: String) {
|
||||
runCatching {
|
||||
Store.task(id)?.let { t ->
|
||||
val note = if (t.note.isBlank()) line else "${t.note}\n$line"
|
||||
Store.updateTask(t.copy(note = note))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun save() {
|
||||
Prefs.timerMode = mode
|
||||
Prefs.timerRunning = running
|
||||
@@ -494,6 +549,9 @@ object TimerCore {
|
||||
Prefs.timerEndAt = endAt
|
||||
Prefs.timerEndElapsed = if (endAt > 0) endElapsed else 0L
|
||||
Prefs.timerClockOffset = clockOffset
|
||||
Prefs.timerPauseReason = pendingPauseReason
|
||||
Prefs.timerPauseStartAt = pendingPauseStartAt
|
||||
Prefs.timerPauseTaskId = pendingPauseTaskId
|
||||
Prefs.timerStartedAt = startedAt
|
||||
Prefs.timerTaskId = taskId
|
||||
Prefs.timerProjectId = projectId
|
||||
|
||||
@@ -3,10 +3,12 @@ package com.focusbox.app.ui
|
||||
import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.graphics.Color
|
||||
import android.graphics.Rect
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.view.Gravity
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.ViewTreeObserver
|
||||
import android.view.WindowManager
|
||||
import com.focusbox.app.R
|
||||
|
||||
@@ -18,6 +20,9 @@ import com.focusbox.app.R
|
||||
*/
|
||||
open class SheetDialog(context: Context) : Dialog(context, R.style.SheetDialog) {
|
||||
|
||||
private var imeRoot: View? = null
|
||||
private var imeListener: ViewTreeObserver.OnGlobalLayoutListener? = null
|
||||
|
||||
init {
|
||||
window?.apply {
|
||||
setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
|
||||
@@ -35,6 +40,35 @@ open class SheetDialog(context: Context) : Dialog(context, R.style.SheetDialog)
|
||||
(view.parent as? View)?.setBackgroundColor(Color.TRANSPARENT)
|
||||
}
|
||||
|
||||
/**
|
||||
* 让带输入框的弹层跟随软键盘:弹出时整体上移,收起后回到页面底部。
|
||||
* ADJUST_RESIZE 在部分 EMUI 上不会推动 Dialog 内容,只能自己按可见区域平移。
|
||||
*/
|
||||
fun fitOnIme(content: View) {
|
||||
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)
|
||||
val root = content.rootView
|
||||
imeRoot = root
|
||||
val threshold = (context.resources.displayMetrics.density * 100).toInt()
|
||||
val screenHeight = context.resources.displayMetrics.heightPixels
|
||||
val listener = object : ViewTreeObserver.OnGlobalLayoutListener {
|
||||
override fun onGlobalLayout() {
|
||||
val frame = Rect()
|
||||
root.getWindowVisibleDisplayFrame(frame)
|
||||
val keyboard = screenHeight - frame.bottom
|
||||
content.translationY = if (keyboard > threshold) -keyboard.toFloat() else 0f
|
||||
}
|
||||
}
|
||||
root.viewTreeObserver.addOnGlobalLayoutListener(listener)
|
||||
imeListener = listener
|
||||
}
|
||||
|
||||
override fun dismiss() {
|
||||
imeListener?.let { imeRoot?.viewTreeObserver?.removeOnGlobalLayoutListener(it) }
|
||||
imeListener = null
|
||||
imeRoot = null
|
||||
super.dismiss()
|
||||
}
|
||||
|
||||
override fun show() {
|
||||
super.show()
|
||||
window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||
|
||||
@@ -116,11 +116,12 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
|
||||
|
||||
btnMain.setOnClickListener {
|
||||
if (TimerCore.running) {
|
||||
TimerCore.pause()
|
||||
} else {
|
||||
requestNotifPermissionIfNeeded()
|
||||
TimerCore.start()
|
||||
showPauseSheet()
|
||||
return@setOnClickListener
|
||||
}
|
||||
requestNotifPermissionIfNeeded()
|
||||
// 暂停后点"继续"要走 resume,恢复时才会把暂停原因补写进备注
|
||||
if (TimerCore.elapsedNow() > 0) TimerCore.resume() else TimerCore.start()
|
||||
render()
|
||||
}
|
||||
btnReset.setOnClickListener {
|
||||
@@ -231,7 +232,7 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
|
||||
btnMain.background = requireContext().roundBg(accent, 100f)
|
||||
}
|
||||
|
||||
modeSwitch.visible(!TimerCore.countUp)
|
||||
// 正计时与倒计时共用同一套布局:顶部模式分段、环内圆点都常驻,避免左右滑动时版面跳动
|
||||
applySegments(focusMode)
|
||||
renderPageDots()
|
||||
renderDots(accent)
|
||||
@@ -241,41 +242,87 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
|
||||
if (!TimerCore.running && !dragging) handler.removeCallbacks(ticker)
|
||||
}
|
||||
|
||||
/**
|
||||
* 提前完成:先写原因,再按真实起止结束本轮。
|
||||
* 原因会追加到任务备注并标记任务完成,任务页的已完成列表能看到备注与实际时段。
|
||||
*/
|
||||
/** 通用的原因输入弹层:输入法弹出时整体上移,收起后归位 */
|
||||
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()
|
||||
onConfirm(raw)
|
||||
sheet.dismiss()
|
||||
render()
|
||||
}
|
||||
sheet.setContentView(root)
|
||||
sheet.fitOnIme(root)
|
||||
etReason.requestFocus()
|
||||
sheet.show()
|
||||
}
|
||||
|
||||
/** 提前完成:按真实起止结束,并把"原计划 / 原因 / 提前多久"写进任务备注 */
|
||||
private fun showFinishSheet() {
|
||||
if (TimerCore.elapsedNow() <= 0) {
|
||||
Toast.makeText(requireContext(), R.string.finish_need_start, Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
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 plannedMinutes = TimerCore.durationMinutes().takeIf { it > 0 } ?: Prefs.focusMinutes
|
||||
reasonSheet(
|
||||
R.string.finish_reason_title,
|
||||
R.string.finish_reason_hint,
|
||||
R.string.finish_reason_input_hint,
|
||||
R.string.finish_now,
|
||||
) { raw ->
|
||||
// 休息阶段点完成不算任务完成,只按实际时长记一条未完成记录
|
||||
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 diff = plannedMinutes * 60 - finish.seconds
|
||||
val ending = when {
|
||||
diff > 0 -> "提前${Dates.duration(diff)}结束"
|
||||
diff < 0 -> "超时${Dates.duration(-diff)}结束"
|
||||
else -> "按时结束"
|
||||
}
|
||||
val line = getString(
|
||||
R.string.task_note_finish, plannedMinutes, raw, Dates.hms(finish.endAt), ending
|
||||
)
|
||||
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()
|
||||
Toast.makeText(requireContext(), R.string.finish_saved, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 暂停也记原因;起止时段在点击"继续"时补齐并写入任务备注 */
|
||||
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()
|
||||
}
|
||||
sheet.dismiss()
|
||||
render()
|
||||
}
|
||||
sheet.setContentView(root)
|
||||
sheet.show()
|
||||
}
|
||||
|
||||
private fun renderPageDots() {
|
||||
@@ -308,8 +355,6 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
|
||||
|
||||
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) {
|
||||
@@ -412,20 +457,9 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
|
||||
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()
|
||||
// 两页布局完全一致,直接换状态即可;位移动画会在圆环上产生可见晃动
|
||||
TimerCore.setCountUp(toCountUp)
|
||||
render()
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 任务
|
||||
|
||||
@@ -105,12 +105,14 @@ class TasksFragment : Fragment(R.layout.fragment_tasks) {
|
||||
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)
|
||||
)
|
||||
)
|
||||
if (expandedDone) done.forEach { rows.add(toRow(it, spans)) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -97,6 +97,19 @@ object Prefs {
|
||||
get() = sp.getLong("t_clock_offset", 0L)
|
||||
set(v) = sp.edit().putLong("t_clock_offset", v).apply()
|
||||
|
||||
/** 暂停原因与起点,恢复时补写"暂停 X 分钟"到任务备注 */
|
||||
var timerPauseReason: String
|
||||
get() = sp.getString("t_pause_reason", "").orEmpty()
|
||||
set(v) = sp.edit().putString("t_pause_reason", v).apply()
|
||||
|
||||
var timerPauseStartAt: Long
|
||||
get() = sp.getLong("t_pause_start", 0L)
|
||||
set(v) = sp.edit().putLong("t_pause_start", v).apply()
|
||||
|
||||
var timerPauseTaskId: Long
|
||||
get() = sp.getLong("t_pause_task", 0L)
|
||||
set(v) = sp.edit().putLong("t_pause_task", v).apply()
|
||||
|
||||
var timerTaskId: Long
|
||||
get() = sp.getLong("t_task", 0L)
|
||||
set(v) = sp.edit().putLong("t_task", v).apply()
|
||||
@@ -129,6 +142,9 @@ object Prefs {
|
||||
.putLong("t_remaining", 0L)
|
||||
.putLong("t_end", 0L)
|
||||
.putLong("t_end_elapsed", 0L)
|
||||
.putString("t_pause_reason", "")
|
||||
.putLong("t_pause_start", 0L)
|
||||
.putLong("t_pause_task", 0L)
|
||||
.putLong("t_started", 0L)
|
||||
.putLong("t_accum", 0L)
|
||||
.apply()
|
||||
|
||||
Reference in New Issue
Block a user