119 lines
4.0 KiB
Kotlin
119 lines
4.0 KiB
Kotlin
|
|
package com.focusbox.app.ui.projects
|
||
|
|
|
||
|
|
import android.os.Bundle
|
||
|
|
import android.view.View
|
||
|
|
import android.widget.TextView
|
||
|
|
import androidx.appcompat.app.AlertDialog
|
||
|
|
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.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}"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private fun showActions(p: Project) {
|
||
|
|
val isInbox = p.id == Store.INBOX_ID
|
||
|
|
val actions = ArrayList<String>()
|
||
|
|
actions.add(getString(R.string.edit))
|
||
|
|
if (!isInbox) {
|
||
|
|
actions.add(getString(if (p.archived) R.string.unarchive else R.string.archive))
|
||
|
|
actions.add(getString(R.string.delete))
|
||
|
|
}
|
||
|
|
|
||
|
|
AlertDialog.Builder(requireContext())
|
||
|
|
.setTitle(p.name)
|
||
|
|
.setItems(actions.toTypedArray()) { _, 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)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
.show()
|
||
|
|
}
|
||
|
|
|
||
|
|
private fun confirmDelete(p: Project) {
|
||
|
|
AlertDialog.Builder(requireContext())
|
||
|
|
.setMessage(getString(R.string.delete_project_confirm, p.name))
|
||
|
|
.setNegativeButton(R.string.cancel, null)
|
||
|
|
.setPositiveButton(R.string.delete) { _, _ ->
|
||
|
|
Store.deleteProject(p.id)
|
||
|
|
reload()
|
||
|
|
}
|
||
|
|
.show()
|
||
|
|
}
|
||
|
|
}
|