2026-11-28 00:25:22 +08:00
|
|
|
|
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
|
2026-11-28 17:40:00 +08:00
|
|
|
|
import com.focusbox.app.util.roundBg
|
2026-11-28 00:25:22 +08:00
|
|
|
|
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 },
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
section(R.string.section_goal)
|
|
|
|
|
|
card(
|
|
|
|
|
|
stepper(
|
|
|
|
|
|
getString(R.string.daily_goal),
|
|
|
|
|
|
getString(R.string.value_minutes, Prefs.dailyGoalMinutes),
|
|
|
|
|
|
) { delta ->
|
|
|
|
|
|
Prefs.dailyGoalMinutes += delta * 15
|
|
|
|
|
|
build()
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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.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))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun stepper(title: String, value: String, 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)
|
|
|
|
|
|
})
|
|
|
|
|
|
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, onClick: () -> Unit): View {
|
|
|
|
|
|
val ctx = requireContext()
|
|
|
|
|
|
val row = LinearLayout(ctx).apply {
|
|
|
|
|
|
orientation = LinearLayout.HORIZONTAL
|
|
|
|
|
|
gravity = Gravity.CENTER_VERTICAL
|
|
|
|
|
|
minimumHeight = dpi(54f)
|
|
|
|
|
|
setOnClickListener { onClick() }
|
|
|
|
|
|
}
|
|
|
|
|
|
row.addView(TextView(ctx).apply {
|
|
|
|
|
|
text = title
|
|
|
|
|
|
textSize = 15f
|
|
|
|
|
|
setTextColor(col(if (danger) R.color.danger else R.color.text))
|
|
|
|
|
|
layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
|
|
|
|
|
|
})
|
|
|
|
|
|
if (value.isNotEmpty()) {
|
|
|
|
|
|
row.addView(TextView(ctx).apply {
|
|
|
|
|
|
text = value
|
|
|
|
|
|
textSize = 13f
|
|
|
|
|
|
setTextColor(col(R.color.text_secondary))
|
|
|
|
|
|
setPadding(0, 0, dpi(6f), 0)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
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() {
|
2026-11-28 17:40:00 +08:00
|
|
|
|
val ctx = requireContext()
|
|
|
|
|
|
val labels = intArrayOf(
|
|
|
|
|
|
R.string.theme_follow,
|
|
|
|
|
|
R.string.theme_light,
|
|
|
|
|
|
R.string.theme_dark,
|
2026-11-28 00:25:22 +08:00
|
|
|
|
)
|
2026-11-28 17:40:00 +08:00
|
|
|
|
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()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
2026-11-28 17:40:00 +08:00
|
|
|
|
panel.addView(row, LinearLayout.LayoutParams(
|
|
|
|
|
|
LinearLayout.LayoutParams.MATCH_PARENT,
|
|
|
|
|
|
LinearLayout.LayoutParams.WRAP_CONTENT,
|
|
|
|
|
|
).apply { topMargin = dpi(6f) })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dialog.setContentView(panel)
|
|
|
|
|
|
dialog.show()
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun confirmClear() {
|
2026-11-28 17:40:00 +08:00
|
|
|
|
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()
|
|
|
|
|
|
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()
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
companion object {
|
2026-11-29 11:30:59 +08:00
|
|
|
|
const val VERSION = "0.14"
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|