2026-11-28 00:25:22 +08:00
|
|
|
|
package com.focusbox.app.ui.projects
|
|
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
|
import android.view.View
|
2026-11-28 08:29:47 +08:00
|
|
|
|
import android.widget.LinearLayout
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import android.widget.TextView
|
|
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
|
|
import com.focusbox.app.R
|
|
|
|
|
|
import com.focusbox.app.data.Store
|
|
|
|
|
|
import com.focusbox.app.model.Project
|
2026-12-01 11:24:03 +08:00
|
|
|
|
import com.focusbox.app.model.ProjectTaskStat
|
|
|
|
|
|
import com.focusbox.app.model.Task
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import com.focusbox.app.ui.MainActivity
|
2026-11-28 08:29:47 +08:00
|
|
|
|
import com.focusbox.app.ui.SheetDialog
|
|
|
|
|
|
import com.focusbox.app.util.col
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import com.focusbox.app.util.visible
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectsFragment : Fragment(R.layout.fragment_projects) {
|
|
|
|
|
|
|
|
|
|
|
|
private lateinit var recycler: RecyclerView
|
|
|
|
|
|
private lateinit var tvSummary: TextView
|
|
|
|
|
|
private lateinit var tvEmpty: TextView
|
|
|
|
|
|
private lateinit var btnArchived: TextView
|
|
|
|
|
|
private lateinit var adapter: ProjectAdapter
|
|
|
|
|
|
|
|
|
|
|
|
private var showArchived = false
|
|
|
|
|
|
|
2026-12-01 11:24:03 +08:00
|
|
|
|
/** 当前展开的项目 id;-1 表示全部收起。只展开一个,避免列表过长 */
|
|
|
|
|
|
private var expandedProjectId = -1L
|
|
|
|
|
|
private var taskStats: Map<Long, List<ProjectTaskStat>> = emptyMap()
|
|
|
|
|
|
|
2026-11-28 00:25:22 +08:00
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
recycler = view.findViewById(R.id.recycler)
|
|
|
|
|
|
tvSummary = view.findViewById(R.id.tvSummary)
|
|
|
|
|
|
tvEmpty = view.findViewById(R.id.tvEmpty)
|
|
|
|
|
|
btnArchived = view.findViewById(R.id.btnArchived)
|
|
|
|
|
|
|
|
|
|
|
|
adapter = ProjectAdapter(
|
2026-12-01 11:24:03 +08:00
|
|
|
|
onToggleExpand = { p ->
|
|
|
|
|
|
expandedProjectId = if (expandedProjectId == p.id) -1L else p.id
|
|
|
|
|
|
reload()
|
|
|
|
|
|
},
|
2026-11-28 00:25:22 +08:00
|
|
|
|
onLongClick = { p -> showActions(p) },
|
|
|
|
|
|
onMore = { p -> showActions(p) },
|
2026-12-01 11:24:03 +08:00
|
|
|
|
onStart = { t -> (activity as? MainActivity)?.startFocusOn(t) },
|
2026-11-28 00:25:22 +08:00
|
|
|
|
)
|
|
|
|
|
|
recycler.layoutManager = LinearLayoutManager(requireContext())
|
|
|
|
|
|
recycler.adapter = adapter
|
|
|
|
|
|
recycler.itemAnimator = null
|
|
|
|
|
|
|
|
|
|
|
|
view.findViewById<View>(R.id.btnAdd).setOnClickListener {
|
|
|
|
|
|
ProjectSheet(requireContext(), null) { reload() }.show()
|
|
|
|
|
|
}
|
|
|
|
|
|
btnArchived.setOnClickListener {
|
|
|
|
|
|
showArchived = !showArchived
|
|
|
|
|
|
reload()
|
|
|
|
|
|
}
|
|
|
|
|
|
reload()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
|
|
super.onResume()
|
|
|
|
|
|
if (view != null) reload()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onHiddenChanged(hidden: Boolean) {
|
|
|
|
|
|
super.onHiddenChanged(hidden)
|
|
|
|
|
|
if (!hidden && view != null) reload()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun reload() {
|
|
|
|
|
|
if (view == null) return
|
|
|
|
|
|
val all = Store.projects(includeArchived = true)
|
|
|
|
|
|
val active = all.filter { !it.archived }
|
|
|
|
|
|
val archived = all.filter { it.archived }
|
|
|
|
|
|
|
|
|
|
|
|
val shown = if (showArchived) all else active
|
2026-12-01 11:24:03 +08:00
|
|
|
|
// 展开态只在可见项目上加载统计;项目被删/归档后自动收起,不留悬空 id
|
|
|
|
|
|
if (expandedProjectId != -1L && shown.none { it.id == expandedProjectId }) {
|
|
|
|
|
|
expandedProjectId = -1L
|
|
|
|
|
|
}
|
|
|
|
|
|
taskStats = shown.associate { it.id to Store.projectTaskRows(it.id) }
|
|
|
|
|
|
adapter.submit(shown, expandedProjectId, taskStats)
|
2026-11-28 00:25:22 +08:00
|
|
|
|
|
|
|
|
|
|
tvSummary.text = getString(
|
|
|
|
|
|
R.string.project_summary, active.size, active.sumOf { it.openTasks }
|
|
|
|
|
|
)
|
|
|
|
|
|
tvEmpty.visible(shown.isEmpty())
|
|
|
|
|
|
|
|
|
|
|
|
if (archived.isEmpty()) {
|
|
|
|
|
|
btnArchived.visible(false)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
btnArchived.visible(true)
|
|
|
|
|
|
btnArchived.text = if (showArchived) "隐藏已归档" else "已归档 ${archived.size}"
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-28 08:29:47 +08:00
|
|
|
|
/** 与新增/编辑项目同款圆角底部弹层,替代系统 AlertDialog */
|
|
|
|
|
|
private fun actionSheet(
|
|
|
|
|
|
title: String,
|
|
|
|
|
|
message: String? = null,
|
|
|
|
|
|
actions: List<Pair<String, Boolean>>,
|
|
|
|
|
|
onPick: (Int) -> Unit,
|
|
|
|
|
|
) {
|
|
|
|
|
|
val ctx = requireContext()
|
|
|
|
|
|
val sheet = SheetDialog(ctx)
|
|
|
|
|
|
val root = layoutInflater.inflate(R.layout.sheet_project_actions, null)
|
|
|
|
|
|
root.findViewById<TextView>(R.id.actionsTitle).text = title
|
|
|
|
|
|
if (message != null) {
|
|
|
|
|
|
root.findViewById<TextView>(R.id.actionsMessage).apply {
|
|
|
|
|
|
text = message
|
|
|
|
|
|
visibility = View.VISIBLE
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
val list = root.findViewById<LinearLayout>(R.id.actionsList)
|
|
|
|
|
|
actions.forEachIndexed { index, (label, destructive) ->
|
|
|
|
|
|
val row = layoutInflater.inflate(R.layout.item_action_row, list, false)
|
|
|
|
|
|
val tv = row.findViewById<TextView>(R.id.actionLabel)
|
|
|
|
|
|
tv.text = label
|
|
|
|
|
|
if (destructive) tv.setTextColor(col(R.color.danger))
|
|
|
|
|
|
row.setOnClickListener {
|
|
|
|
|
|
sheet.dismiss()
|
|
|
|
|
|
onPick(index)
|
|
|
|
|
|
}
|
|
|
|
|
|
list.addView(row)
|
|
|
|
|
|
}
|
|
|
|
|
|
sheet.setContentView(root)
|
|
|
|
|
|
sheet.show()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-28 00:25:22 +08:00
|
|
|
|
private fun showActions(p: Project) {
|
|
|
|
|
|
val isInbox = p.id == Store.INBOX_ID
|
2026-11-28 08:29:47 +08:00
|
|
|
|
val actions = ArrayList<Pair<String, Boolean>>()
|
|
|
|
|
|
actions.add(getString(R.string.edit) to false)
|
2026-11-28 00:25:22 +08:00
|
|
|
|
if (!isInbox) {
|
2026-11-28 08:29:47 +08:00
|
|
|
|
actions.add(getString(if (p.archived) R.string.unarchive else R.string.archive) to false)
|
|
|
|
|
|
actions.add(getString(R.string.delete) to true)
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
2026-11-28 08:29:47 +08:00
|
|
|
|
actionSheet(p.name, actions = actions) { which ->
|
|
|
|
|
|
when {
|
|
|
|
|
|
which == 0 -> ProjectSheet(requireContext(), p) { reload() }.show()
|
|
|
|
|
|
which == 1 && !isInbox -> {
|
2026-11-29 14:20:09 +08:00
|
|
|
|
// 归档/取消归档只切 archived,序号与置顶状态原样保留
|
|
|
|
|
|
Store.updateProject(
|
|
|
|
|
|
p.id, p.name, p.color, !p.archived,
|
|
|
|
|
|
p.sortIndex, p.pinnedAt > 0, p.pinnedAt,
|
|
|
|
|
|
)
|
2026-11-28 08:29:47 +08:00
|
|
|
|
reload()
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
2026-11-28 08:29:47 +08:00
|
|
|
|
which == 2 && !isInbox -> confirmDelete(p)
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
2026-11-28 08:29:47 +08:00
|
|
|
|
}
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun confirmDelete(p: Project) {
|
2026-11-28 08:29:47 +08:00
|
|
|
|
actionSheet(
|
|
|
|
|
|
title = getString(R.string.delete),
|
|
|
|
|
|
message = getString(R.string.delete_project_confirm, p.name),
|
|
|
|
|
|
actions = listOf(getString(R.string.cancel) to false, getString(R.string.delete) to true),
|
|
|
|
|
|
) { which ->
|
|
|
|
|
|
if (which == 1) {
|
2026-11-28 00:25:22 +08:00
|
|
|
|
Store.deleteProject(p.id)
|
|
|
|
|
|
reload()
|
|
|
|
|
|
}
|
2026-11-28 08:29:47 +08:00
|
|
|
|
}
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|