正倒计时布局统一消除晃动;原因弹层适配输入法;已完成按钮位置固定;原因文案与暂停原因;科技蓝主色
This commit is contained in:
@@ -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)) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user