Files
FocusBox/src/main/java/com/focusbox/app/ui/settings/SettingsFragment.kt

460 lines
17 KiB
Kotlin
Raw Normal View History

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 后台启动,悬浮窗不受限)
action(
getString(R.string.overlay_perm_title),
if (com.focusbox.app.service.LockOverlay.canShow(requireContext())) {
getString(R.string.perm_granted)
} else {
getString(R.string.go_set)
},
) { openOverlayPerm() },
// 华为/鸿蒙默认拦截后台拉起页面,无悬浮窗权限时的 Activity 兜底链路也要这两项权限;
// 放在开关正下方,用户开了开关不生效时第一眼就能看到入口
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),
) { 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() {
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()
}
/** 悬浮窗权限:标准 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()
}
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()
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 = "1.4"
}
}