100 lines
3.4 KiB
Kotlin
100 lines
3.4 KiB
Kotlin
|
|
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 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 ?: "")
|
||
|
|
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
|
||
|
|
}
|
||
|
|
if (existing == null) {
|
||
|
|
Store.addProject(name, color)
|
||
|
|
} else {
|
||
|
|
Store.updateProject(existing.id, name, color, existing.archived)
|
||
|
|
}
|
||
|
|
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)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|