导入 v0.1.0 / v0.2.0 源码:五模块首版、交互重构、重复任务、任务分布
- 源码镜像 src/main(81 个文件):首页双模式、任务、项目、统计、设置 - 补齐 README / LICENSE / CHANGELOG / CONTRIBUTING / .gitignore - 不含构建配置、签名材料、编译产物与备份数据
This commit is contained in:
44
src/main/java/com/focusbox/app/util/Chips.kt
Normal file
44
src/main/java/com/focusbox/app/util/Chips.kt
Normal file
@@ -0,0 +1,44 @@
|
||||
package com.focusbox.app.util
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.graphics.drawable.GradientDrawable
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.focusbox.app.R
|
||||
|
||||
/** 小圆点,用作项目标记 */
|
||||
fun Context.dotDrawable(color: Int, sizeDp: Float = 8f): Drawable = GradientDrawable().apply {
|
||||
shape = GradientDrawable.OVAL
|
||||
setColor(color)
|
||||
setSize(dpi(sizeDp), dpi(sizeDp))
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一的筛选胶囊。选中态用浅品牌底 + 品牌描边,
|
||||
* 未选中是白底细描边——比 Material Chip 少一层阴影,在浅底上更干净。
|
||||
*/
|
||||
fun Context.makeChip(text: String, selected: Boolean, dotColor: Int? = null): TextView =
|
||||
TextView(this).apply {
|
||||
this.text = text
|
||||
textSize = 12.5f
|
||||
gravity = android.view.Gravity.CENTER
|
||||
setTextColor(col(if (selected) R.color.brand else R.color.text_secondary))
|
||||
background = ContextCompat.getDrawable(
|
||||
this@makeChip,
|
||||
if (selected) R.drawable.bg_chip_on else R.drawable.bg_chip_off,
|
||||
)
|
||||
setPadding(dpi(12f), dpi(7f), dpi(12f), dpi(7f))
|
||||
if (dotColor != null) {
|
||||
val d = this@makeChip.dotDrawable(dotColor)
|
||||
setCompoundDrawablesRelativeWithIntrinsicBounds(d, null, null, null)
|
||||
compoundDrawablePadding = dpi(5f)
|
||||
}
|
||||
val lp = LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.WRAP_CONTENT,
|
||||
LinearLayout.LayoutParams.WRAP_CONTENT,
|
||||
)
|
||||
lp.marginEnd = dpi(8f)
|
||||
layoutParams = lp
|
||||
}
|
||||
125
src/main/java/com/focusbox/app/util/Dates.kt
Normal file
125
src/main/java/com/focusbox/app/util/Dates.kt
Normal file
@@ -0,0 +1,125 @@
|
||||
package com.focusbox.app.util
|
||||
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
/** 时间格式化与"自然日"边界计算,全局只此一份,避免各处口径不一。 */
|
||||
object Dates {
|
||||
|
||||
private val keyFmt = SimpleDateFormat("yyyy-MM-dd", Locale.US)
|
||||
private val mdFmt = SimpleDateFormat("M/d", Locale.CHINA)
|
||||
private val mdFmt2 = SimpleDateFormat("M月d日", Locale.CHINA)
|
||||
private val hmFmt = SimpleDateFormat("HH:mm", Locale.CHINA)
|
||||
private val hmsFmt = SimpleDateFormat("HH:mm:ss", Locale.CHINA)
|
||||
|
||||
fun dayKey(ms: Long): String = keyFmt.format(Date(ms))
|
||||
|
||||
fun dayStart(ms: Long): Long {
|
||||
val c = Calendar.getInstance()
|
||||
c.timeInMillis = ms
|
||||
c.set(Calendar.HOUR_OF_DAY, 0)
|
||||
c.set(Calendar.MINUTE, 0)
|
||||
c.set(Calendar.SECOND, 0)
|
||||
c.set(Calendar.MILLISECOND, 0)
|
||||
return c.timeInMillis
|
||||
}
|
||||
|
||||
fun dayStartOfKey(key: String): Long = keyFmt.parse(key)?.time ?: 0L
|
||||
|
||||
fun todayStart(): Long = dayStart(System.currentTimeMillis())
|
||||
|
||||
/** 本周一 00:00(周一为一周起点,符合国内习惯) */
|
||||
fun weekStart(): Long {
|
||||
val c = Calendar.getInstance()
|
||||
c.timeInMillis = todayStart()
|
||||
val dow = c.get(Calendar.DAY_OF_WEEK) // 1=周日
|
||||
val back = if (dow == Calendar.SUNDAY) 6 else dow - 2
|
||||
c.add(Calendar.DAY_OF_MONTH, -back)
|
||||
return c.timeInMillis
|
||||
}
|
||||
|
||||
fun monthStart(): Long {
|
||||
val c = Calendar.getInstance()
|
||||
c.timeInMillis = todayStart()
|
||||
c.set(Calendar.DAY_OF_MONTH, 1)
|
||||
return c.timeInMillis
|
||||
}
|
||||
|
||||
fun addDays(ms: Long, days: Int): Long {
|
||||
val c = Calendar.getInstance()
|
||||
c.timeInMillis = ms
|
||||
c.add(Calendar.DAY_OF_MONTH, days)
|
||||
return c.timeInMillis
|
||||
}
|
||||
|
||||
/** 月末会按当月最后一天收敛(1月31日 + 1月 = 2月28/29日),不会溢出到下个月 */
|
||||
fun addMonths(ms: Long, months: Int): Long {
|
||||
val c = Calendar.getInstance()
|
||||
c.timeInMillis = ms
|
||||
c.add(Calendar.MONTH, months)
|
||||
return c.timeInMillis
|
||||
}
|
||||
|
||||
/** 12:30 这种时钟显示;不足 10 分钟显示 09:30 两位分 */
|
||||
fun clock(ms: Long): String {
|
||||
val total = (ms / 1000).coerceAtLeast(0)
|
||||
val m = total / 60
|
||||
val s = total % 60
|
||||
return String.format(Locale.US, "%02d:%02d", m, s)
|
||||
}
|
||||
|
||||
/** 45m / 2h10m / 12.5h,统计里统一用这个口径 */
|
||||
fun duration(seconds: Int): String {
|
||||
if (seconds < 60) return "${seconds}秒"
|
||||
val minutes = seconds / 60
|
||||
if (minutes < 60) return "${minutes}分钟"
|
||||
val h = minutes / 60
|
||||
val m = minutes % 60
|
||||
return if (m == 0) "${h}小时" else "${h}小时${m}分"
|
||||
}
|
||||
|
||||
/** 图表坐标轴用的短格式:2h / 45m */
|
||||
fun shortDuration(seconds: Int): String {
|
||||
if (seconds <= 0) return "0"
|
||||
val minutes = seconds / 60
|
||||
if (minutes < 60) return "${minutes}m"
|
||||
val h = minutes / 60f
|
||||
return if (h >= 10) "${h.toInt()}h" else String.format(Locale.US, "%.1fh", h)
|
||||
}
|
||||
|
||||
fun md(ms: Long): String = mdFmt.format(Date(ms))
|
||||
fun mdLong(ms: Long): String = mdFmt2.format(Date(ms))
|
||||
fun hm(ms: Long): String = hmFmt.format(Date(ms))
|
||||
fun hms(ms: Long): String = hmsFmt.format(Date(ms))
|
||||
|
||||
/** 周五 / 周一 */
|
||||
fun weekday(ms: Long): String {
|
||||
val c = Calendar.getInstance()
|
||||
c.timeInMillis = ms
|
||||
return arrayOf("周日", "周一", "周二", "周三", "周四", "周五", "周六")[
|
||||
(c.get(Calendar.DAY_OF_WEEK) - 1).coerceIn(0, 6)
|
||||
]
|
||||
}
|
||||
|
||||
/** 11月27日 周五 */
|
||||
fun dateWithWeek(ms: Long): String = "${mdLong(ms)} ${weekday(ms)}"
|
||||
|
||||
/** 今天 / 明天 / 已逾期 / 11月30日 */
|
||||
fun dueLabel(due: Long): String {
|
||||
if (due <= 0) return ""
|
||||
val today = todayStart()
|
||||
val d = dayStart(due)
|
||||
return when {
|
||||
d == today -> "今天"
|
||||
d == addDays(today, 1) -> "明天"
|
||||
d == addDays(today, -1) -> "昨天"
|
||||
d < today -> md(d)
|
||||
else -> md(d)
|
||||
}
|
||||
}
|
||||
|
||||
fun weekdayShort(index: Int): String =
|
||||
arrayOf("一", "二", "三", "四", "五", "六", "日")[index.coerceIn(0, 6)]
|
||||
}
|
||||
21
src/main/java/com/focusbox/app/util/Palette.kt
Normal file
21
src/main/java/com/focusbox/app/util/Palette.kt
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.focusbox.app.util
|
||||
|
||||
/** 项目配色。八色一组,两两之间在色相上有足够间距,圆点上也能分辨。 */
|
||||
object Palette {
|
||||
|
||||
val colors = intArrayOf(
|
||||
0xFFC75B45.toInt(), // 陶土红
|
||||
0xFFD98324.toInt(), // 琥珀
|
||||
0xFFC9A227.toInt(), // 芥黄
|
||||
0xFF4C8C4A.toInt(), // 苔绿
|
||||
0xFF3F7F6E.toInt(), // 松石
|
||||
0xFF3B7DD8.toInt(), // 靛蓝
|
||||
0xFF6C5CE7.toInt(), // 紫
|
||||
0xFFB7548E.toInt(), // 玫红
|
||||
)
|
||||
|
||||
/** 收件箱用中性灰,避免和自建项目争注意力 */
|
||||
const val INBOX = 0xFF7A756C.toInt()
|
||||
|
||||
fun colorAt(index: Int): Int = colors[index.mod(colors.size)]
|
||||
}
|
||||
125
src/main/java/com/focusbox/app/util/Prefs.kt
Normal file
125
src/main/java/com/focusbox/app/util/Prefs.kt
Normal file
@@ -0,0 +1,125 @@
|
||||
package com.focusbox.app.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
|
||||
/**
|
||||
* 全部可调项与计时状态的落盘位置。
|
||||
*
|
||||
* 计时状态必须持久化:息屏被杀进程后重新打开,仍要能接着走同一个番茄,
|
||||
* 否则用户会看到一个"自己重新开始"的计时器。
|
||||
*/
|
||||
object Prefs {
|
||||
|
||||
private lateinit var sp: SharedPreferences
|
||||
|
||||
fun init(context: Context) {
|
||||
sp = context.getSharedPreferences("focusbox", Context.MODE_PRIVATE)
|
||||
}
|
||||
|
||||
// ---- 时长 ----
|
||||
var focusMinutes: Int
|
||||
get() = sp.getInt("focus_minutes", 25)
|
||||
set(v) = sp.edit().putInt("focus_minutes", v.coerceIn(5, 180)).apply()
|
||||
|
||||
var shortBreakMinutes: Int
|
||||
get() = sp.getInt("short_break", 5)
|
||||
set(v) = sp.edit().putInt("short_break", v.coerceIn(1, 60)).apply()
|
||||
|
||||
var longBreakMinutes: Int
|
||||
get() = sp.getInt("long_break", 15)
|
||||
set(v) = sp.edit().putInt("long_break", v.coerceIn(5, 90)).apply()
|
||||
|
||||
var longBreakEvery: Int
|
||||
get() = sp.getInt("long_every", 4)
|
||||
set(v) = sp.edit().putInt("long_every", v.coerceIn(2, 8)).apply()
|
||||
|
||||
// ---- 自动衔接 ----
|
||||
var autoStartBreak: Boolean
|
||||
get() = sp.getBoolean("auto_break", true)
|
||||
set(v) = sp.edit().putBoolean("auto_break", v).apply()
|
||||
|
||||
var autoStartFocus: Boolean
|
||||
get() = sp.getBoolean("auto_focus", false)
|
||||
set(v) = sp.edit().putBoolean("auto_focus", v).apply()
|
||||
|
||||
// ---- 提醒 ----
|
||||
var soundOn: Boolean
|
||||
get() = sp.getBoolean("sound", true)
|
||||
set(v) = sp.edit().putBoolean("sound", v).apply()
|
||||
|
||||
var vibrateOn: Boolean
|
||||
get() = sp.getBoolean("vibrate", true)
|
||||
set(v) = sp.edit().putBoolean("vibrate", v).apply()
|
||||
|
||||
var keepScreenOn: Boolean
|
||||
get() = sp.getBoolean("keep_screen", true)
|
||||
set(v) = sp.edit().putBoolean("keep_screen", v).apply()
|
||||
|
||||
// ---- 目标 ----
|
||||
var dailyGoalMinutes: Int
|
||||
get() = sp.getInt("daily_goal", 120)
|
||||
set(v) = sp.edit().putInt("daily_goal", v.coerceIn(15, 720)).apply()
|
||||
|
||||
/** 0 跟随系统 / 1 浅色 / 2 深色 */
|
||||
var themeMode: Int
|
||||
get() = sp.getInt("theme_mode", 0)
|
||||
set(v) = sp.edit().putInt("theme_mode", v).apply()
|
||||
|
||||
// ---- 计时状态 ----
|
||||
var timerMode: Int
|
||||
get() = sp.getInt("t_mode", 0)
|
||||
set(v) = sp.edit().putInt("t_mode", v).apply()
|
||||
|
||||
var timerRunning: Boolean
|
||||
get() = sp.getBoolean("t_running", false)
|
||||
set(v) = sp.edit().putBoolean("t_running", v).apply()
|
||||
|
||||
var timerTotalMs: Long
|
||||
get() = sp.getLong("t_total", 0L)
|
||||
set(v) = sp.edit().putLong("t_total", v).apply()
|
||||
|
||||
var timerRemainingMs: Long
|
||||
get() = sp.getLong("t_remaining", 0L)
|
||||
set(v) = sp.edit().putLong("t_remaining", v).apply()
|
||||
|
||||
var timerEndAt: Long
|
||||
get() = sp.getLong("t_end", 0L)
|
||||
set(v) = sp.edit().putLong("t_end", v).apply()
|
||||
|
||||
var timerTaskId: Long
|
||||
get() = sp.getLong("t_task", 0L)
|
||||
set(v) = sp.edit().putLong("t_task", v).apply()
|
||||
|
||||
var timerProjectId: Long
|
||||
get() = sp.getLong("t_project", 0L)
|
||||
set(v) = sp.edit().putLong("t_project", v).apply()
|
||||
|
||||
var timerRound: Int
|
||||
get() = sp.getInt("t_round", 0)
|
||||
set(v) = sp.edit().putInt("t_round", v).apply()
|
||||
|
||||
var timerStartedAt: Long
|
||||
get() = sp.getLong("t_started", 0L)
|
||||
set(v) = sp.edit().putLong("t_started", v).apply()
|
||||
|
||||
/** 是否处于正计时(秒表)页 */
|
||||
var timerCountUp: Boolean
|
||||
get() = sp.getBoolean("t_countup", false)
|
||||
set(v) = sp.edit().putBoolean("t_countup", v).apply()
|
||||
|
||||
/** 正计时的累计毫秒(暂停时保存的值) */
|
||||
var timerAccumMs: Long
|
||||
get() = sp.getLong("t_accum", 0L)
|
||||
set(v) = sp.edit().putLong("t_accum", v).apply()
|
||||
|
||||
fun clearTimer() {
|
||||
sp.edit()
|
||||
.putBoolean("t_running", false)
|
||||
.putLong("t_remaining", 0L)
|
||||
.putLong("t_end", 0L)
|
||||
.putLong("t_started", 0L)
|
||||
.putLong("t_accum", 0L)
|
||||
.apply()
|
||||
}
|
||||
}
|
||||
97
src/main/java/com/focusbox/app/util/Ui.kt
Normal file
97
src/main/java/com/focusbox/app/util/Ui.kt
Normal file
@@ -0,0 +1,97 @@
|
||||
package com.focusbox.app.util
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.drawable.GradientDrawable
|
||||
import android.util.TypedValue
|
||||
import android.view.View
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.annotation.ColorRes
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.graphics.drawable.DrawableCompat
|
||||
|
||||
fun Context.dpf(v: Float): Float = v * resources.displayMetrics.density
|
||||
|
||||
fun Context.dpi(v: Float): Int = (v * resources.displayMetrics.density).toInt()
|
||||
|
||||
fun Context.col(@ColorRes id: Int): Int = ContextCompat.getColor(this, id)
|
||||
|
||||
fun Context.spf(v: Float): Float = TypedValue.applyDimension(
|
||||
TypedValue.COMPLEX_UNIT_SP, v, resources.displayMetrics
|
||||
)
|
||||
|
||||
/*
|
||||
* 下面几组重载不是冗余:布局代码大量写在 `LinearLayout(ctx).apply { ... }` 里,
|
||||
* 此时隐式接收者是 View 而不是 Context,只留 Context 版本会导致 dpi/col 全部解析失败。
|
||||
* Fragment 版本覆盖 `LinearLayout.LayoutParams(...).apply { marginStart = dpi(4f) }`
|
||||
* 这类接收者既不是 View 也不是 Context 的场景。
|
||||
*/
|
||||
|
||||
fun View.dpi(v: Float): Int = context.dpi(v)
|
||||
|
||||
fun View.col(@ColorRes id: Int): Int = context.col(id)
|
||||
|
||||
fun androidx.fragment.app.Fragment.dpi(v: Float): Int = requireContext().dpi(v)
|
||||
|
||||
fun androidx.fragment.app.Fragment.col(@ColorRes id: Int): Int = requireContext().col(id)
|
||||
|
||||
fun android.app.Dialog.dpi(v: Float): Int = context.dpi(v)
|
||||
|
||||
fun android.app.Dialog.col(@ColorRes id: Int): Int = context.col(id)
|
||||
|
||||
/** 圆角面板 / 药丸 / 圆点,全部用代码生成,避免为每种尺寸再建一个 drawable */
|
||||
fun Context.roundBg(color: Int, radiusDp: Float, strokeColor: Int? = null, strokeDp: Float = 1f): GradientDrawable =
|
||||
GradientDrawable().apply {
|
||||
shape = GradientDrawable.RECTANGLE
|
||||
cornerRadius = dpf(radiusDp)
|
||||
setColor(color)
|
||||
if (strokeColor != null) setStroke(dpi(strokeDp).coerceAtLeast(1), strokeColor)
|
||||
}
|
||||
|
||||
fun View.roundBg(color: Int, radiusDp: Float, strokeColor: Int? = null, strokeDp: Float = 1f): GradientDrawable =
|
||||
context.roundBg(color, radiusDp, strokeColor, strokeDp)
|
||||
|
||||
fun androidx.fragment.app.Fragment.roundBg(
|
||||
color: Int,
|
||||
radiusDp: Float,
|
||||
strokeColor: Int? = null,
|
||||
strokeDp: Float = 1f,
|
||||
): GradientDrawable = requireContext().roundBg(color, radiusDp, strokeColor, strokeDp)
|
||||
|
||||
fun android.app.Dialog.roundBg(
|
||||
color: Int,
|
||||
radiusDp: Float,
|
||||
strokeColor: Int? = null,
|
||||
strokeDp: Float = 1f,
|
||||
): GradientDrawable = context.roundBg(color, radiusDp, strokeColor, strokeDp)
|
||||
|
||||
fun Context.circleBg(color: Int): GradientDrawable = GradientDrawable().apply {
|
||||
shape = GradientDrawable.OVAL
|
||||
setColor(color)
|
||||
}
|
||||
|
||||
fun View.circleBg(color: Int): GradientDrawable = context.circleBg(color)
|
||||
|
||||
fun androidx.fragment.app.Fragment.circleBg(color: Int): GradientDrawable = requireContext().circleBg(color)
|
||||
|
||||
fun android.app.Dialog.circleBg(color: Int): GradientDrawable = context.circleBg(color)
|
||||
|
||||
fun ImageView.tint(color: Int) {
|
||||
val d = drawable ?: return
|
||||
val wrapped = DrawableCompat.wrap(d.mutate())
|
||||
DrawableCompat.setTint(wrapped, color)
|
||||
setImageDrawable(wrapped)
|
||||
}
|
||||
|
||||
fun View.visible(show: Boolean) {
|
||||
visibility = if (show) View.VISIBLE else View.GONE
|
||||
}
|
||||
|
||||
/** 统一的行内边距换算,保证不同布局的 16dp 是同一个 16dp */
|
||||
fun View.pad(h: Float, v: Float) {
|
||||
setPadding(context.dpi(h), context.dpi(v), context.dpi(h), context.dpi(v))
|
||||
}
|
||||
|
||||
fun TextView.setAlphaCompat(a: Float) {
|
||||
alpha = a
|
||||
}
|
||||
Reference in New Issue
Block a user