package com.focusbox.app.ui.settings import android.graphics.Typeface import android.os.Bundle import android.view.Gravity import android.view.View import android.widget.LinearLayout import android.widget.TextView import androidx.appcompat.widget.SwitchCompat import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import com.focusbox.app.App import com.focusbox.app.R import com.focusbox.app.data.Store import com.focusbox.app.service.TimerCore import com.focusbox.app.ui.MainActivity import com.focusbox.app.util.Dates import com.focusbox.app.util.Prefs import com.focusbox.app.util.col import com.focusbox.app.util.dpi import com.focusbox.app.util.roundBg import com.focusbox.app.util.tint /** * 设置页全部用代码搭。这里都是同构的行(标题 + 值/开关/步进器), * 写进 XML 会得到五个几乎一样的布局文件,改一处字号要同步五遍。 */ class SettingsFragment : Fragment(R.layout.fragment_settings) { private lateinit var container: LinearLayout override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) container = view.findViewById(R.id.container) build() } override fun onResume() { super.onResume() if (view != null) build() } private fun build() { container.removeAllViews() section(R.string.section_duration) card( stepper( getString(R.string.duration_focus), getString(R.string.value_minutes, Prefs.focusMinutes), ) { delta -> Prefs.focusMinutes += delta * 5 if (!TimerCore.running && TimerCore.mode == TimerCore.MODE_FOCUS) TimerCore.applyDefaultDuration() build() }, stepper( getString(R.string.duration_short), getString(R.string.value_minutes, Prefs.shortBreakMinutes), ) { delta -> Prefs.shortBreakMinutes += delta if (!TimerCore.running && TimerCore.mode == TimerCore.MODE_SHORT) TimerCore.applyDefaultDuration() build() }, stepper( getString(R.string.duration_long), getString(R.string.value_minutes, Prefs.longBreakMinutes), ) { delta -> Prefs.longBreakMinutes += delta * 5 if (!TimerCore.running && TimerCore.mode == TimerCore.MODE_LONG) TimerCore.applyDefaultDuration() build() }, stepper( getString(R.string.long_every), getString(R.string.value_rounds, Prefs.longBreakEvery), ) { delta -> Prefs.longBreakEvery += delta build() }, ) section(R.string.section_auto) card( toggle(getString(R.string.auto_start_break), Prefs.autoStartBreak) { Prefs.autoStartBreak = it }, toggle(getString(R.string.auto_start_focus), Prefs.autoStartFocus) { Prefs.autoStartFocus = it }, ) section(R.string.section_alert) card( toggle(getString(R.string.sound), Prefs.soundOn) { Prefs.soundOn = it }, toggle(getString(R.string.vibrate), Prefs.vibrateOn) { Prefs.vibrateOn = it }, toggle(getString(R.string.keep_screen_on), Prefs.keepScreenOn) { Prefs.keepScreenOn = it }, toggle(getString(R.string.lock_fullscreen_title), Prefs.lockFullscreen) { Prefs.lockFullscreen = it }, // 悬浮窗权限:锁屏倒计时主链路(鸿蒙拦 Activity 后台启动,悬浮窗不受限)。 // 已授权时既没有右侧箭头也不可点——权限本身没有应用内开关,免得误以为能点进去设置 run { val overlay = com.focusbox.app.service.LockOverlay.canShow(requireContext()) if (overlay) { action(getString(R.string.overlay_perm_title), getString(R.string.perm_granted), showArrow = false) } else { action(getString(R.string.overlay_perm_title), getString(R.string.go_set)) { openOverlayPerm() } } // 锁屏显示权限(华为「应用启动管理」→ 后台弹出界面/锁屏显示)没有查询接口, // 只能反证:锁屏倒计时页只要真的显示过一次,LockScreenActivity.onStart 会记下时间戳。 // 悬浮窗已授权时走的是悬浮窗链路,压根不经过启动管理,同样算已生效。 if (overlay || Prefs.lockShownAt > 0L) { action(getString(R.string.lock_perm_title), getString(R.string.perm_granted), showArrow = false) } else { action(getString(R.string.lock_perm_title), getString(R.string.lock_perm_value)) { openLockPerm() } } }, ) section(R.string.section_goal) card( stepper( getString(R.string.daily_goal), getString(R.string.value_minutes, Prefs.dailyGoalMinutes), // 数值支持直接点击输入;步进用于微调。Prefs 里没有上限 onEdit = { inputDailyGoal() }, ) { delta -> Prefs.dailyGoalMinutes += delta * 15 build() }, ) section(R.string.section_types) card( // 类型只是给项目打的标签:值列表可改,改完项目弹层与统计页的类型分布立刻跟着变 action( getString(R.string.type_settings), Prefs.projectTypes.joinToString("、"), ) { inputProjectTypes() }, ) section(R.string.section_appearance) card( action( getString(R.string.theme), getString( when (Prefs.themeMode) { 1 -> R.string.theme_light 2 -> R.string.theme_dark else -> R.string.theme_follow } ), ) { pickTheme() }, ) section(R.string.section_data) card( // 清理旧版本删除任务后残留的孤儿记录,排在「清空所有数据」前面: // 它只动查不到任务的记录,比全清安全一档 action(getString(R.string.purge_orphan), "", danger = true) { confirmPurgeOrphan() }, action(getString(R.string.clear_data), "", danger = true) { confirmClear() }, ) container.addView(TextView(requireContext()).apply { text = getString(R.string.about_version, VERSION) textSize = 11f gravity = Gravity.CENTER setTextColor(col(R.color.text_tertiary)) setPadding(0, dpi(26f), 0, dpi(10f)) }) } // ------------------------------------------------------------ 行构造 private fun section(titleRes: Int) { container.addView(TextView(requireContext()).apply { setText(titleRes) textSize = 12f typeface = Typeface.DEFAULT_BOLD letterSpacing = 0.06f setTextColor(col(R.color.text_secondary)) setPadding(dpi(4f), dpi(20f), 0, dpi(6f)) }) } private fun card(vararg rows: View) { val ctx = requireContext() val card = LinearLayout(ctx).apply { orientation = LinearLayout.VERTICAL background = ContextCompat.getDrawable(ctx, R.drawable.bg_card) setPadding(dpi(16f), 0, dpi(16f), 0) } rows.forEachIndexed { index, row -> if (index > 0) card.addView(divider()) card.addView(row) } container.addView(card, LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, )) } private fun divider(): View = View(requireContext()).apply { layoutParams = LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, 1, ) setBackgroundColor(col(R.color.divider)) } /** onEdit 非空时点击数值可直接输入(目前只有每日目标用);onChange 为加减步进 */ private fun stepper(title: String, value: String, onEdit: (() -> Unit)? = null, onChange: (Int) -> Unit): View { val ctx = requireContext() val row = LinearLayout(ctx).apply { orientation = LinearLayout.HORIZONTAL gravity = Gravity.CENTER_VERTICAL minimumHeight = dpi(54f) } row.addView(TextView(ctx).apply { text = title textSize = 15f setTextColor(col(R.color.text)) layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f) }) row.addView(stepButton("−") { onChange(-1) }) row.addView(TextView(ctx).apply { text = value textSize = 14f typeface = Typeface.DEFAULT_BOLD gravity = Gravity.CENTER setTextColor(col(R.color.title)) layoutParams = LinearLayout.LayoutParams(dpi(74f), LinearLayout.LayoutParams.WRAP_CONTENT) if (onEdit != null) setOnClickListener { onEdit() } }) row.addView(stepButton("+") { onChange(1) }) return row } private fun stepButton(text: String, onClick: () -> Unit): TextView { val ctx = requireContext() return TextView(ctx).apply { this.text = text textSize = 16f gravity = Gravity.CENTER setTextColor(col(R.color.text)) background = ctx.getDrawable(R.drawable.bg_chip_off) layoutParams = LinearLayout.LayoutParams(dpi(30f), dpi(30f)) setOnClickListener { onClick() } } } private fun toggle(title: String, checked: Boolean, onChange: (Boolean) -> Unit): View { val ctx = requireContext() val row = LinearLayout(ctx).apply { orientation = LinearLayout.HORIZONTAL gravity = Gravity.CENTER_VERTICAL minimumHeight = dpi(54f) setPadding(0, dpi(4f), 0, dpi(4f)) } val label = TextView(ctx).apply { text = title textSize = 15f setTextColor(col(R.color.text)) layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f) } val sw = SwitchCompat(ctx).apply { isChecked = checked setOnCheckedChangeListener { _, value -> onChange(value) } } row.addView(label) row.addView(sw) return row } private fun action( title: String, value: String, danger: Boolean = false, showArrow: Boolean = true, onClick: (() -> Unit)? = null, ): View { val ctx = requireContext() val row = LinearLayout(ctx).apply { orientation = LinearLayout.HORIZONTAL gravity = Gravity.CENTER_VERTICAL minimumHeight = dpi(54f) if (onClick != null) setOnClickListener { onClick() } } // 标题固定单行、按内容取宽。原来的写法是标题吃 weight = 1, // 值一长(「工作、学习、娱乐、提升」)就把标题挤到只剩几十像素, //「类型设置」被迫折成两行,右边再跟一个被省略号截断的值,很难看。 row.addView(TextView(ctx).apply { text = title textSize = 15f setTextColor(col(if (danger) R.color.danger else R.color.text)) isSingleLine = true layoutParams = LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, ) }) if (value.isNotEmpty()) { row.addView(TextView(ctx).apply { text = value textSize = 13f gravity = Gravity.END or Gravity.CENTER_VERTICAL setTextColor(col(R.color.text_secondary)) setPadding(dpi(12f), 0, dpi(6f), 0) // 值只吃剩余宽度:单行、末尾省略;完整内容点整行在弹窗里看 maxLines = 1 ellipsize = android.text.TextUtils.TruncateAt.END layoutParams = LinearLayout.LayoutParams( 0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f, ) }) } else { // 没有值的行补一个弹性占位,否则箭头会紧贴标题 row.addView(View(ctx).apply { layoutParams = LinearLayout.LayoutParams(0, 1, 1f) }) } if (showArrow) { row.addView(android.widget.ImageView(ctx).apply { setImageResource(R.drawable.ic_chevron_right) tint(col(R.color.text_tertiary)) layoutParams = LinearLayout.LayoutParams(dpi(15f), dpi(15f)) }) } return row } // ------------------------------------------------------------ 交互 private fun pickTheme() { val ctx = requireContext() val labels = intArrayOf( R.string.theme_follow, R.string.theme_light, R.string.theme_dark, ) val dialog = android.app.Dialog(ctx) dialog.window?.setBackgroundDrawableResource(android.R.color.transparent) val panel = LinearLayout(ctx).apply { orientation = LinearLayout.VERTICAL // 与全局卡片一致:圆角 16dp、surface 底色随深浅色自动切换 background = ctx.roundBg(ctx.col(R.color.surface), 16f, ctx.col(R.color.card_stroke), 1f) setPadding(dpi(16f), dpi(14f), dpi(16f), dpi(14f)) } labels.forEachIndexed { index, res -> val selected = Prefs.themeMode == index val row = TextView(ctx).apply { text = getString(res) textSize = 15f gravity = Gravity.CENTER setTextColor(col(if (selected) R.color.brand else R.color.text)) typeface = if (selected) Typeface.DEFAULT_BOLD else Typeface.DEFAULT background = if (selected) { ctx.roundBg(ctx.col(R.color.brand_soft), 12f) } else { ctx.roundBg(ctx.col(R.color.surface), 12f) } setPadding(0, dpi(14f), 0, dpi(14f)) setOnClickListener { dialog.dismiss() if (Prefs.themeMode != index) { Prefs.themeMode = index (requireActivity().application as App).applyTheme() requireActivity().recreate() } } } panel.addView(row, LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, ).apply { topMargin = dpi(6f) }) } dialog.setContentView(panel) dialog.show() } /** 每日目标:弹小窗直接输入分钟数(无上限,下限 15 由 Prefs 兜底) */ private fun inputDailyGoal() { val ctx = requireContext() val dialog = android.app.Dialog(ctx) dialog.window?.setBackgroundDrawableResource(android.R.color.transparent) val panel = LinearLayout(ctx).apply { orientation = LinearLayout.VERTICAL background = ctx.roundBg(ctx.col(R.color.surface), 16f, ctx.col(R.color.card_stroke), 1f) setPadding(dpi(20f), dpi(20f), dpi(20f), dpi(14f)) } panel.addView(TextView(ctx).apply { text = getString(R.string.daily_goal) textSize = 16f typeface = Typeface.DEFAULT_BOLD setTextColor(col(R.color.text)) }) val et = android.widget.EditText(ctx).apply { inputType = android.text.InputType.TYPE_CLASS_NUMBER setText(Prefs.dailyGoalMinutes.toString()) textSize = 15f setTextColor(col(R.color.text)) background = ctx.roundBg(ctx.col(R.color.surface_alt), 12f) setPadding(dpi(14f), dpi(12f), dpi(14f), dpi(12f)) layoutParams = LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, ).apply { topMargin = dpi(14f) } } panel.addView(et) et.requestFocus() val btnRow = LinearLayout(ctx).apply { orientation = LinearLayout.HORIZONTAL gravity = Gravity.END setPadding(0, dpi(12f), 0, 0) } fun btn(labelRes: Int, onClick: () -> Unit) = TextView(ctx).apply { text = getString(labelRes) textSize = 14f typeface = Typeface.DEFAULT_BOLD gravity = Gravity.CENTER setTextColor(col(R.color.brand)) background = ctx.roundBg(ctx.col(R.color.surface_alt), 12f) setPadding(dpi(22f), dpi(10f), dpi(22f), dpi(10f)) setOnClickListener { onClick() } } btnRow.addView(btn(R.string.cancel) { dialog.dismiss() }) btnRow.addView(btn(R.string.save) { val input = et.text.toString().trim().toIntOrNull() if (input != null) { Prefs.dailyGoalMinutes = input dialog.dismiss() build() } else { et.error = " " } }, LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT).apply { marginStart = dpi(10f) }) panel.addView(btnRow) dialog.setContentView(panel) dialog.show() // 弹窗不自动抢输入法焦点,手动拉起软键盘 et.postDelayed({ val imm = ctx.getSystemService(android.content.Context.INPUT_METHOD_SERVICE) as? android.view.inputmethod.InputMethodManager imm?.showSoftInput(et, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT) }, 100L) } /** * 项目类型列表:一行英文逗号分隔的文本。 * * 用纯文本而不是逐条增删的列表,是因为这个字段就是"几个词", * 一次粘贴「工作,学习,娱乐,提升」比点四次添加更快; * 解析时统一 trim / 去空项 / 去重,所以中文逗号、多余空格不会存进去 * (中文逗号会被当成普通字符,所以在说明里明确写了要用英文逗号)。 */ private fun inputProjectTypes() { val ctx = requireContext() val dialog = android.app.Dialog(ctx) dialog.window?.setBackgroundDrawableResource(android.R.color.transparent) val panel = LinearLayout(ctx).apply { orientation = LinearLayout.VERTICAL background = ctx.roundBg(ctx.col(R.color.surface), 16f, ctx.col(R.color.card_stroke), 1f) setPadding(dpi(20f), dpi(20f), dpi(20f), dpi(14f)) } panel.addView(TextView(ctx).apply { text = getString(R.string.type_settings) textSize = 16f typeface = Typeface.DEFAULT_BOLD setTextColor(col(R.color.text)) }) panel.addView(TextView(ctx).apply { text = getString(R.string.type_settings_hint) textSize = 12f setTextColor(col(R.color.text_tertiary)) setPadding(0, dpi(6f), 0, 0) }) val et = android.widget.EditText(ctx).apply { inputType = android.text.InputType.TYPE_CLASS_TEXT setText(Prefs.projectTypes.joinToString(",")) textSize = 15f setTextColor(col(R.color.text)) setSingleLine(true) background = ctx.roundBg(ctx.col(R.color.surface_alt), 12f) setPadding(dpi(14f), dpi(12f), dpi(14f), dpi(12f)) layoutParams = LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, ).apply { topMargin = dpi(12f) } } panel.addView(et) et.requestFocus() et.setSelection(et.text.length) val btnRow = LinearLayout(ctx).apply { orientation = LinearLayout.HORIZONTAL gravity = Gravity.END setPadding(0, dpi(12f), 0, 0) } fun btn(labelRes: Int, onClick: () -> Unit) = TextView(ctx).apply { text = getString(labelRes) textSize = 14f typeface = Typeface.DEFAULT_BOLD gravity = Gravity.CENTER setTextColor(col(R.color.brand)) background = ctx.roundBg(ctx.col(R.color.surface_alt), 12f) setPadding(dpi(22f), dpi(10f), dpi(22f), dpi(10f)) setOnClickListener { onClick() } } btnRow.addView(btn(R.string.cancel) { dialog.dismiss() }) btnRow.addView(btn(R.string.save) { val parsed = et.text.toString() .split(',') .map { it.trim() } .filter { it.isNotEmpty() } .distinct() if (parsed.isEmpty()) { // 全空等于把"类型"变成死字段,直接拦住 et.error = getString(R.string.type_settings_empty) } else { Prefs.projectTypes = parsed dialog.dismiss() build() } }, LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, ).apply { marginStart = dpi(10f) }) panel.addView(btnRow) dialog.setContentView(panel) dialog.show() et.postDelayed({ val imm = ctx.getSystemService(android.content.Context.INPUT_METHOD_SERVICE) as? android.view.inputmethod.InputMethodManager imm?.showSoftInput(et, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT) }, 100L) } /** 悬浮窗权限:标准 Android 特殊权限,有公开检测 API 和授权页,一键直达本应用 */ private fun openOverlayPerm() { val ctx = requireContext() if (com.focusbox.app.service.LockOverlay.canShow(ctx)) return runCatching { startActivity( android.content.Intent( android.provider.Settings.ACTION_MANAGE_OVERLAY_PERMISSION, android.net.Uri.parse("package:${ctx.packageName}"), ).addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK) ) } } /** 锁屏显示权限:弹说明后直达华为「应用启动管理」,非华为/直达失败退回应用详情页 */ private fun openLockPerm() { val ctx = requireContext() val dialog = android.app.Dialog(ctx) dialog.window?.setBackgroundDrawableResource(android.R.color.transparent) val panel = LinearLayout(ctx).apply { orientation = LinearLayout.VERTICAL background = ctx.roundBg(ctx.col(R.color.surface), 16f, ctx.col(R.color.card_stroke), 1f) setPadding(dpi(20f), dpi(20f), dpi(20f), dpi(14f)) } panel.addView(TextView(ctx).apply { text = getString(R.string.lock_perm_title) textSize = 16f typeface = Typeface.DEFAULT_BOLD setTextColor(col(R.color.text)) }) panel.addView(TextView(ctx).apply { text = getString(R.string.lock_perm_hint) textSize = 14f setTextColor(col(R.color.text_secondary)) setPadding(0, dpi(10f), 0, dpi(6f)) }) val btnRow = LinearLayout(ctx).apply { orientation = LinearLayout.HORIZONTAL gravity = Gravity.END setPadding(0, dpi(12f), 0, 0) } fun btn(labelRes: Int, onClick: () -> Unit) = TextView(ctx).apply { text = getString(labelRes) textSize = 14f typeface = Typeface.DEFAULT_BOLD gravity = Gravity.CENTER setTextColor(col(R.color.brand)) background = ctx.roundBg(ctx.col(R.color.surface_alt), 12f) setPadding(dpi(22f), dpi(10f), dpi(22f), dpi(10f)) setOnClickListener { onClick() } } btnRow.addView(btn(R.string.cancel) { dialog.dismiss() }) btnRow.addView(btn(R.string.go_set) { dialog.dismiss() if (!com.focusbox.app.util.HuaweiPerms.openStartupManager(ctx)) { com.focusbox.app.util.HuaweiPerms.openAppDetails(ctx) } }, LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT).apply { marginStart = dpi(10f) }) panel.addView(btnRow) dialog.setContentView(panel) dialog.show() } /** * 清空未关联数据:弹窗结构与「清空所有数据」完全一致,只是正文会说清要删多少条。 * * 删的是「找不到任务」的专注记录(旧版本删任务时把 task_id 清零留下的残骸、 * 以及没选任务直接开始的那几轮)。它们只能堆成饼图里的「未关联任务」, * 占一块地方又查不到是谁的,所以单独给一个清理入口。 */ private fun confirmPurgeOrphan() { val ctx = requireContext() val stat = Store.orphanSessions() val dialog = android.app.Dialog(ctx) dialog.window?.setBackgroundDrawableResource(android.R.color.transparent) val panel = LinearLayout(ctx).apply { orientation = LinearLayout.VERTICAL background = ctx.roundBg(ctx.col(R.color.surface), 16f, ctx.col(R.color.card_stroke), 1f) setPadding(dpi(20f), dpi(20f), dpi(20f), dpi(14f)) } panel.addView(TextView(ctx).apply { text = getString(R.string.purge_orphan) textSize = 16f typeface = Typeface.DEFAULT_BOLD setTextColor(col(R.color.text)) }) panel.addView(TextView(ctx).apply { text = if (stat.count > 0) { getString(R.string.purge_confirm, stat.count, Dates.duration(stat.seconds)) } else { getString(R.string.purge_none) } textSize = 14f setTextColor(col(R.color.text_secondary)) setPadding(0, dpi(10f), 0, dpi(6f)) }) val btnRow = LinearLayout(ctx).apply { orientation = LinearLayout.HORIZONTAL gravity = Gravity.END setPadding(0, dpi(12f), 0, 0) } fun btn(labelRes: Int, danger: Boolean, onClick: () -> Unit) = TextView(ctx).apply { text = getString(labelRes) textSize = 14f typeface = Typeface.DEFAULT_BOLD gravity = Gravity.CENTER setTextColor(col(if (danger) R.color.danger else R.color.brand)) background = ctx.roundBg(ctx.col(R.color.surface_alt), 12f) setPadding(dpi(22f), dpi(10f), dpi(22f), dpi(10f)) setOnClickListener { onClick() } } btnRow.addView(btn(R.string.cancel, false) { dialog.dismiss() }) btnRow.addView(btn(R.string.delete, true) { dialog.dismiss() if (stat.count > 0) Store.purgeOrphanSessions() (activity as? MainActivity)?.refreshAll() build() }, LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT).apply { marginStart = dpi(10f) }) panel.addView(btnRow) dialog.setContentView(panel) dialog.show() } private fun confirmClear() { val ctx = requireContext() val dialog = android.app.Dialog(ctx) dialog.window?.setBackgroundDrawableResource(android.R.color.transparent) val panel = LinearLayout(ctx).apply { orientation = LinearLayout.VERTICAL background = ctx.roundBg(ctx.col(R.color.surface), 16f, ctx.col(R.color.card_stroke), 1f) setPadding(dpi(20f), dpi(20f), dpi(20f), dpi(14f)) } panel.addView(TextView(ctx).apply { text = getString(R.string.clear_data) textSize = 16f typeface = Typeface.DEFAULT_BOLD setTextColor(col(R.color.text)) }) panel.addView(TextView(ctx).apply { text = getString(R.string.clear_confirm) textSize = 14f setTextColor(col(R.color.text_secondary)) setPadding(0, dpi(10f), 0, dpi(6f)) }) val btnRow = LinearLayout(ctx).apply { orientation = LinearLayout.HORIZONTAL gravity = Gravity.END setPadding(0, dpi(12f), 0, 0) } fun btn(labelRes: Int, danger: Boolean, onClick: () -> Unit) = TextView(ctx).apply { text = getString(labelRes) textSize = 14f typeface = Typeface.DEFAULT_BOLD gravity = Gravity.CENTER setTextColor(col(if (danger) R.color.danger else R.color.brand)) background = ctx.roundBg(ctx.col(R.color.surface_alt), 12f) setPadding(dpi(22f), dpi(10f), dpi(22f), dpi(10f)) setOnClickListener { onClick() } } btnRow.addView(btn(R.string.cancel, false) { dialog.dismiss() }) btnRow.addView(btn(R.string.delete, true) { dialog.dismiss() TimerCore.reset() Store.clearAll() Prefs.clearTimer() // 内存里的绑定也要解掉:只清磁盘的话,下一次开始专注仍带着旧 taskId 落库, // 会立刻写出一条指向已删除任务行的「未关联记录」 TimerCore.setTask(0, 0) Prefs.timerRound = 0 TimerCore.resetRound() (activity as? MainActivity)?.refreshAll() build() }, LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT).apply { marginStart = dpi(10f) }) panel.addView(btnRow) dialog.setContentView(panel) dialog.show() } companion object { const val VERSION = "3.18" } }