184 lines
6.9 KiB
Kotlin
184 lines
6.9 KiB
Kotlin
package com.focusbox.app.ui.projects
|
||
|
||
import android.os.Bundle
|
||
import android.view.View
|
||
import android.widget.LinearLayout
|
||
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
|
||
import com.focusbox.app.model.ProjectCompletion
|
||
import com.focusbox.app.model.ProjectTaskStat
|
||
import com.focusbox.app.model.Task
|
||
import com.focusbox.app.ui.MainActivity
|
||
import com.focusbox.app.ui.SheetDialog
|
||
import com.focusbox.app.ui.tasks.TaskSheet
|
||
import com.focusbox.app.util.col
|
||
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
|
||
|
||
/** 当前展开的项目 id;-1 表示全部收起。只展开一个,避免列表过长 */
|
||
private var expandedProjectId = -1L
|
||
private var taskStats: Map<Long, List<ProjectTaskStat>> = emptyMap()
|
||
private var completions: Map<Long, List<ProjectCompletion>> = emptyMap()
|
||
/** 项目任务下方的完成记录默认折叠,避免历史记录越积越长 */
|
||
private var showDoneInProject = false
|
||
|
||
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(
|
||
onToggleExpand = { p ->
|
||
val opening = expandedProjectId != p.id
|
||
expandedProjectId = if (opening) p.id else -1L
|
||
if (opening) showDoneInProject = false
|
||
reload()
|
||
},
|
||
onLongClick = { p -> showActions(p) },
|
||
onMore = { p -> showActions(p) },
|
||
onStart = { t -> (activity as? MainActivity)?.startFocusOn(t) },
|
||
onEdit = { t -> TaskSheet(requireContext(), t, 0L) { reload() }.show() },
|
||
onToggleDone = {
|
||
showDoneInProject = !showDoneInProject
|
||
reload()
|
||
},
|
||
)
|
||
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
|
||
// 展开态只在可见项目上加载统计;项目被删/归档后自动收起,不留悬空 id
|
||
if (expandedProjectId != -1L && shown.none { it.id == expandedProjectId }) {
|
||
expandedProjectId = -1L
|
||
}
|
||
taskStats = shown.associate { it.id to Store.projectTaskRows(it.id) }
|
||
completions = shown.associate { it.id to Store.projectCompletions(it.id) }
|
||
adapter.submit(shown, expandedProjectId, taskStats, completions, showDoneInProject)
|
||
|
||
tvSummary.text = getString(
|
||
R.string.project_summary, active.size, active.sumOf { it.totalTasks }
|
||
)
|
||
tvEmpty.visible(shown.isEmpty())
|
||
|
||
if (archived.isEmpty()) {
|
||
btnArchived.visible(false)
|
||
} else {
|
||
btnArchived.visible(true)
|
||
btnArchived.text = if (showArchived) "隐藏已归档" else "已归档${archived.size}"
|
||
}
|
||
}
|
||
|
||
/** 与新增/编辑项目同款圆角底部弹层,替代系统 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()
|
||
}
|
||
|
||
private fun showActions(p: Project) {
|
||
val isInbox = p.id == Store.INBOX_ID
|
||
val actions = ArrayList<Pair<String, Boolean>>()
|
||
actions.add(getString(R.string.edit) to false)
|
||
if (!isInbox) {
|
||
actions.add(getString(if (p.archived) R.string.unarchive else R.string.archive) to false)
|
||
actions.add(getString(R.string.delete) to true)
|
||
}
|
||
actionSheet(p.name, actions = actions) { which ->
|
||
when {
|
||
which == 0 -> ProjectSheet(requireContext(), p) { reload() }.show()
|
||
which == 1 && !isInbox -> {
|
||
// 归档/取消归档只切 archived,序号与置顶状态原样保留
|
||
Store.updateProject(
|
||
p.id, p.name, p.color, !p.archived,
|
||
p.sortIndex, p.pinnedAt > 0, p.pinnedAt,
|
||
)
|
||
reload()
|
||
}
|
||
which == 2 && !isInbox -> confirmDelete(p)
|
||
}
|
||
}
|
||
}
|
||
|
||
private fun confirmDelete(p: Project) {
|
||
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) {
|
||
Store.deleteProject(p.id)
|
||
reload()
|
||
}
|
||
}
|
||
}
|
||
}
|