修复倒计时读数冻结、原因输入键盘遮挡、备注括号与任务备注截断

This commit is contained in:
XIGE
2026-11-28 12:36:26 +08:00
parent 5e352c646a
commit 7986a65dce
5 changed files with 83 additions and 28 deletions

View File

@@ -27,7 +27,7 @@ object Store {
const val INBOX_ID = 1L
private const val DB_NAME = "focusbox.db"
private const val DB_VERSION = 3
private const val DB_VERSION = 4
private lateinit var helper: Helper
@@ -109,6 +109,13 @@ object Store {
if (oldVersion < 3) {
db.execSQL("ALTER TABLE tasks ADD COLUMN origin_id INTEGER NOT NULL DEFAULT 0")
}
// v3 → v4:早期版本的原因备注带【】方括号,一次性清掉历史数据
if (oldVersion < 4) {
db.execSQL(
"UPDATE tasks SET note = REPLACE(REPLACE(note, '【', ''), '】', '') " +
"WHERE note LIKE '%【%' OR note LIKE '%】%'"
)
}
}
}

View File

@@ -10,6 +10,7 @@ import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.view.WindowManager
import android.widget.FrameLayout
import com.focusbox.app.R
/**
@@ -20,8 +21,9 @@ import com.focusbox.app.R
*/
open class SheetDialog(context: Context) : Dialog(context, R.style.SheetDialog) {
private var imeRoot: View? = null
private var imeContainer: FrameLayout? = null
private var imeListener: ViewTreeObserver.OnGlobalLayoutListener? = null
private var fullHeight = false
init {
window?.apply {
@@ -41,36 +43,61 @@ open class SheetDialog(context: Context) : Dialog(context, R.style.SheetDialog)
}
/**
* 让带输入框的弹层跟随软键盘:弹出时整体上移,收起后回到页面底部。
* ADJUST_RESIZE 在部分 EMUI 上不会推动 Dialog 内容,只能自己按可见区域平移。
* 让带输入框的弹层跟随软键盘。
*
* 关键点:窗口必须是"满屏 + 内容贴底",ADJUST_RESIZE 才有高度可缩。
* 之前用 WRAP_CONTENT 窗口 + ADJUST_NOTHING + 自己平移,在 EMUI/HarmonyOS 上
* 既拿不到正确的键盘高度,Dialog 本身也不被推动,输入框会被键盘盖住。
*
* 现在双保险:
* 1. 标准路径靠 ADJUST_RESIZE 收缩满屏容器,贴底内容自动抬到键盘之上;
* 2. 某些 ROM 忽略 resize 时,按可见显示区域与容器高度的差值兜底平移。
*/
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 container = FrameLayout(context).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
)
}
val lp = FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.BOTTOM,
)
content.layoutParams = lp
container.addView(content)
setContentView(container)
fullHeight = true
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
val threshold = (context.resources.displayMetrics.density * 80).toInt()
val listener = object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val frame = Rect()
root.getWindowVisibleDisplayFrame(frame)
val keyboard = screenHeight - frame.bottom
container.getWindowVisibleDisplayFrame(frame)
// resize 生效时容器已经收缩,差值为 0;未生效时差值就是键盘(含导航栏)高度
val keyboard = container.height - frame.bottom
content.translationY = if (keyboard > threshold) -keyboard.toFloat() else 0f
}
}
root.viewTreeObserver.addOnGlobalLayoutListener(listener)
container.viewTreeObserver.addOnGlobalLayoutListener(listener)
imeContainer = container
imeListener = listener
}
override fun dismiss() {
imeListener?.let { imeRoot?.viewTreeObserver?.removeOnGlobalLayoutListener(it) }
imeListener?.let { imeContainer?.viewTreeObserver?.removeOnGlobalLayoutListener(it) }
imeListener = null
imeRoot = null
imeContainer = null
super.dismiss()
}
override fun show() {
super.show()
window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
window?.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
if (fullHeight) ViewGroup.LayoutParams.MATCH_PARENT else ViewGroup.LayoutParams.WRAP_CONTENT,
)
}
}

View File

@@ -1,6 +1,7 @@
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
@@ -8,6 +9,7 @@ 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
@@ -81,11 +83,10 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
private val ticker = object : Runnable {
override fun run() {
// 兜底推进:万一前台服务被系统限流没起来,只要页面还在前台,
// 计时与到时结算仍然准确。
// 计时与到时结算仍然准确。续命统一交给 render() 末尾的 syncTicker(),
// 避免"点开始时轮询已经停了却没人重新拉起"导致圆环数字冻住。
if (TimerCore.running) TimerCore.tick()
render()
// 空闲且没拖动时 render 已经移除轮询,这里不再续命
if (TimerCore.running || dragging) handler.postDelayed(this, 250L)
}
}
@@ -154,10 +155,9 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
super.onResume()
cachedTaskId = Long.MIN_VALUE // 任务可能被改名/改时长,回到页面强制重读
TimerCore.addListener(onTimerChanged)
// show/hide 模式下所有片段都处于 resumed,隐藏的页面不该继续跑 250ms 刷新;
// render() 末尾会按当前 running 与 isHidden 自行决定是否挂上轮询。
render()
handler.removeCallbacks(ticker)
// show/hide 模式下所有片段都处于 resumed,隐藏的页面不该继续跑 250ms 刷新
if (!isHidden) handler.post(ticker)
}
override fun onPause() {
@@ -176,8 +176,6 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
// 任务可能在别的页面被改名/改时长,重新显示时强制重读
cachedTaskId = Long.MIN_VALUE
render()
handler.removeCallbacks(ticker)
handler.post(ticker)
}
}
@@ -238,8 +236,19 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
renderDots(accent)
renderTask()
applyKeepScreen(TimerCore.running && Prefs.keepScreenOn)
// 空闲且没在拖动时不必每 250ms 重绘一次,省电也避免无谓 GC
if (!TimerCore.running && !dragging) handler.removeCallbacks(ticker)
syncTicker()
}
/**
* 轮询必须与 TimerCore.running 同步:点"开始"时先前空闲状态已经移除了回调,
* 若只靠 onResume 挂一次,圆环数字就会一直停在点击那一刻。
* 每次 render 都先撤后挂,保证任何时刻至多只有一个待执行的 ticker。
*/
private fun syncTicker() {
handler.removeCallbacks(ticker)
if (TimerCore.running && !isHidden && view != null) {
handler.postDelayed(ticker, 250L)
}
}
/** 通用的原因输入弹层:输入法弹出时整体上移,收起后归位 */
@@ -260,15 +269,25 @@ class FocusFragment : Fragment(R.layout.fragment_focus) {
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.setContentView(root)
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)
}
}
}
/** 提前完成:按真实起止结束,并把"原计划 / 原因 / 提前多久"写进任务备注 */