Files
FocusBox/src/main/java/com/focusbox/app/ui/projects/ProjectSheet.kt

117 lines
4.5 KiB
Kotlin
Raw Normal View History

package com.focusbox.app.ui.projects
import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.focusbox.app.R
import com.focusbox.app.data.Store
import com.focusbox.app.model.Project
import com.focusbox.app.ui.SheetDialog
import com.focusbox.app.util.Palette
import com.focusbox.app.util.circleBg
import com.focusbox.app.util.dpi
import com.focusbox.app.util.roundBg
import com.focusbox.app.util.visible
class ProjectSheet(
context: Context,
private val existing: Project?,
private val onSaved: () -> Unit,
) : SheetDialog(context) {
private val root = LayoutInflater.from(context).inflate(R.layout.sheet_project, null)
private val etName: EditText = root.findViewById(R.id.etName)
private val etSortIndex: EditText = root.findViewById(R.id.etSortIndex)
private val swPinned: androidx.appcompat.widget.SwitchCompat = root.findViewById(R.id.swPinned)
private val colorRow: LinearLayout = root.findViewById(R.id.colorRow)
private var color: Int = existing?.color ?: Palette.colorAt(0)
init {
setContentView(root)
root.findViewById<TextView>(R.id.sheetTitle).setText(
if (existing == null) R.string.add_project else R.string.edit
)
etName.setText(existing?.name ?: "")
// 编辑已有序号才回填;未设置过序号显示空,保存时按 0 处理(同级按创建先后排)
if (existing != null && existing.sortIndex > 0) {
etSortIndex.setText(existing.sortIndex.toString())
}
swPinned.isChecked = (existing?.pinnedAt ?: 0L) > 0L
renderColors()
val btnDelete = root.findViewById<TextView>(R.id.btnDelete)
val deletable = existing != null && existing.id != Store.INBOX_ID
btnDelete.visible(deletable)
btnDelete.setOnClickListener {
existing ?: return@setOnClickListener
AlertDialog.Builder(context)
.setMessage(context.getString(R.string.delete_project_confirm, existing.name))
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.delete) { _, _ ->
Store.deleteProject(existing.id)
onSaved()
dismiss()
}
.show()
}
root.findViewById<View>(R.id.btnSave).setOnClickListener {
val name = etName.text.toString().trim()
if (name.isEmpty()) {
etName.error = context.getString(R.string.project_name_hint)
return@setOnClickListener
}
val sortIndex = etSortIndex.text.toString().trim().toIntOrNull() ?: 0
val pinned = swPinned.isChecked
if (existing == null) {
// 新建置顶:置顶时间取当下,排进置顶组
Store.addProject(name, color, sortIndex, pinned)
} else {
// 已置顶且保持置顶 → 保留原置顶时间(重复保存不刷新排序);
// 新勾置顶 → 以保存这一刻为置顶时间;取消置顶 → Store 里归 0
val pinnedAt = when {
!pinned -> 0L
existing.pinnedAt > 0L -> existing.pinnedAt
else -> System.currentTimeMillis()
}
Store.updateProject(existing.id, name, color, existing.archived, sortIndex, pinned, pinnedAt)
}
onSaved()
dismiss()
}
}
private fun renderColors() {
val ctx = context
colorRow.removeAllViews()
Palette.colors.forEach { c ->
val wrap = FrameLayout(ctx)
val lp = LinearLayout.LayoutParams(dpi(40f), dpi(40f))
lp.marginEnd = dpi(2f)
wrap.layoutParams = lp
wrap.background = if (c == color) ctx.roundBg(Color.TRANSPARENT, 100f, c, 2f) else null
val dot = View(ctx)
val dlp = FrameLayout.LayoutParams(dpi(26f), dpi(26f))
dlp.gravity = Gravity.CENTER
dot.layoutParams = dlp
dot.background = circleBg(c)
wrap.addView(dot)
wrap.setOnClickListener {
color = c
renderColors()
}
colorRow.addView(wrap)
}
}
}