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

152 lines
5.2 KiB
Kotlin
Raw Normal View History

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.ui.MainActivity
import com.focusbox.app.ui.SheetDialog
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
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(
onClick = { p -> (activity as? MainActivity)?.openProjectTasks(p.id) },
onLongClick = { p -> showActions(p) },
onMore = { p -> showActions(p) },
)
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
adapter.submit(shown)
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}"
}
}
/** 与新增/编辑项目同款圆角底部弹层,替代系统 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 -> {
Store.updateProject(p.id, p.name, p.color, !p.archived)
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()
}
}
}
}