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

306 lines
11 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.app.AlertDialog
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.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() {
val labels = arrayOf(
getString(R.string.theme_follow),
getString(R.string.theme_light),
getString(R.string.theme_dark),
)
AlertDialog.Builder(requireContext())
.setTitle(R.string.theme)
.setSingleChoiceItems(labels, Prefs.themeMode) { dialog, which ->
Prefs.themeMode = which
(requireActivity().application as App).applyTheme()
dialog.dismiss()
requireActivity().recreate()
}
.show()
}
private fun confirmClear() {
AlertDialog.Builder(requireContext())
.setTitle(R.string.clear_data)
.setMessage(R.string.clear_confirm)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.delete) { _, _ ->
TimerCore.reset()
Store.clearAll()
Prefs.clearTimer()
Prefs.timerRound = 0
TimerCore.resetRound()
(activity as? MainActivity)?.refreshAll()
build()
}
.show()
}
companion object {
const val VERSION = "0.2.0"
}
}