Meshtastic-Android/app/src/main/java/com/geeksville/mesh/ui/QuickChatSettingsFragment.kt

173 lines
6.7 KiB
Kotlin
Raw Normal View History

2022-08-09 15:26:52 +01:00
package com.geeksville.mesh.ui
2022-08-11 16:43:26 +01:00
import android.content.Context
2022-08-09 15:26:52 +01:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2022-08-11 16:43:26 +01:00
import android.widget.EditText
import android.widget.ImageView
2022-08-11 16:43:26 +01:00
import androidx.core.widget.addTextChangedListener
2022-08-09 15:26:52 +01:00
import androidx.fragment.app.activityViewModels
2022-08-11 16:43:26 +01:00
import androidx.lifecycle.asLiveData
import androidx.recyclerview.widget.ItemTouchHelper
2022-08-11 16:43:26 +01:00
import androidx.recyclerview.widget.LinearLayoutManager
2022-09-04 22:52:40 -03:00
import com.geeksville.mesh.android.Logging
2022-08-11 16:43:26 +01:00
import com.geeksville.mesh.R
import com.geeksville.mesh.database.entity.QuickChatAction
import com.geeksville.mesh.databinding.QuickChatSettingsFragmentBinding
2022-08-09 15:26:52 +01:00
import com.geeksville.mesh.model.UIViewModel
2022-08-11 16:43:26 +01:00
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.switchmaterial.SwitchMaterial
2022-08-09 15:26:52 +01:00
import dagger.hilt.android.AndroidEntryPoint
import java.util.*
2022-08-09 15:26:52 +01:00
@AndroidEntryPoint
2022-12-28 17:37:25 -03:00
class QuickChatSettingsFragment : ScreenFragment("Quick Chat Settings"), Logging {
2022-08-09 15:26:52 +01:00
private var _binding: QuickChatSettingsFragmentBinding? = null
private val binding get() = _binding!!
private val model: UIViewModel by activityViewModels()
private lateinit var actions: List<QuickChatAction>
2022-08-09 15:26:52 +01:00
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = QuickChatSettingsFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2022-08-10 17:34:18 +01:00
binding.quickChatSettingsCreateButton.setOnClickListener {
2022-12-28 17:37:25 -03:00
val builder = createEditDialog(requireContext(), getString(R.string.quick_chat_new))
2022-08-10 17:34:18 +01:00
2022-12-28 17:37:25 -03:00
builder.builder.setPositiveButton(R.string.add) { _, _ ->
2022-08-10 17:34:18 +01:00
val name = builder.nameInput.text.toString().trim()
val message = builder.messageInput.text.toString()
2022-08-11 16:43:26 +01:00
if (builder.isNotEmpty())
2022-08-10 17:34:18 +01:00
model.addQuickChatAction(
name, message,
if (builder.modeSwitch.isChecked) QuickChatAction.Mode.Instant else QuickChatAction.Mode.Append
)
}
val dialog = builder.builder.create()
dialog.show()
}
val quickChatActionAdapter =
QuickChatActionAdapter(requireContext(), { action: QuickChatAction ->
2022-12-28 17:37:25 -03:00
val builder = createEditDialog(requireContext(), getString(R.string.quick_chat_edit))
2022-08-10 17:34:18 +01:00
builder.nameInput.setText(action.name)
builder.messageInput.setText(action.message)
val isInstant = action.mode == QuickChatAction.Mode.Instant
builder.modeSwitch.isChecked = isInstant
builder.instantImage.visibility = if (isInstant) View.VISIBLE else View.INVISIBLE
2022-08-10 17:34:18 +01:00
2022-08-11 16:43:26 +01:00
builder.builder.setNegativeButton(R.string.delete) { _, _ ->
model.deleteQuickChatAction(action)
}
2022-12-28 17:37:25 -03:00
builder.builder.setPositiveButton(R.string.save) { _, _ ->
2022-08-11 16:43:26 +01:00
if (builder.isNotEmpty()) {
model.updateQuickChatAction(
action,
builder.nameInput.text.toString(),
builder.messageInput.text.toString(),
if (builder.modeSwitch.isChecked) QuickChatAction.Mode.Instant else QuickChatAction.Mode.Append
)
}
2022-08-10 17:34:18 +01:00
}
val dialog = builder.builder.create()
dialog.show()
}, { fromPos, toPos ->
Collections.swap(actions, fromPos, toPos)
}, {
model.updateActionPositions(actions)
})
val dragCallback =
DragManageAdapter(quickChatActionAdapter, ItemTouchHelper.UP or ItemTouchHelper.DOWN, 0)
val helper = ItemTouchHelper(dragCallback)
2022-08-10 17:34:18 +01:00
binding.quickChatSettingsView.apply {
this.layoutManager = LinearLayoutManager(requireContext())
this.adapter = quickChatActionAdapter
helper.attachToRecyclerView(this)
2022-08-10 17:34:18 +01:00
}
2022-08-11 16:43:26 +01:00
model.quickChatActions.asLiveData().observe(viewLifecycleOwner) { actions ->
actions?.let {
quickChatActionAdapter.setActions(actions)
this.actions = actions
}
2022-08-11 16:43:26 +01:00
}
2022-08-10 17:34:18 +01:00
}
data class DialogBuilder(
val builder: MaterialAlertDialogBuilder,
val nameInput: EditText,
val messageInput: EditText,
val modeSwitch: SwitchMaterial,
val instantImage: ImageView
2022-08-11 16:43:26 +01:00
) {
fun isNotEmpty(): Boolean = nameInput.text.isNotEmpty() and messageInput.text.isNotEmpty()
}
2022-08-10 17:34:18 +01:00
private fun getMessageName(message: String): String {
return if (message.length <= 3) {
message.uppercase()
} else {
buildString {
append(message.first().uppercase())
append(message[message.length / 2].uppercase())
append(message.last().uppercase())
}
}
}
private fun createEditDialog(context: Context, title: String): DialogBuilder {
val builder = MaterialAlertDialogBuilder(context)
builder.setTitle(title)
val layout =
LayoutInflater.from(requireContext()).inflate(R.layout.dialog_add_quick_chat, null)
val nameInput: EditText = layout.findViewById(R.id.addQuickChatName)
val messageInput: EditText = layout.findViewById(R.id.addQuickChatMessage)
val modeSwitch: SwitchMaterial = layout.findViewById(R.id.addQuickChatMode)
val instantImage: ImageView = layout.findViewById(R.id.addQuickChatInsant)
instantImage.visibility = if (modeSwitch.isChecked) View.VISIBLE else View.INVISIBLE
2022-08-10 17:34:18 +01:00
var nameHasChanged = false
modeSwitch.setOnCheckedChangeListener { _, _ ->
if (modeSwitch.isChecked) {
2022-12-28 17:37:25 -03:00
modeSwitch.setText(R.string.quick_chat_instant)
instantImage.visibility = View.VISIBLE
} else {
2022-12-28 17:37:25 -03:00
modeSwitch.setText(R.string.quick_chat_append)
instantImage.visibility = View.INVISIBLE
}
2022-08-10 17:34:18 +01:00
}
messageInput.addTextChangedListener { text ->
if (!nameHasChanged) {
nameInput.setText(getMessageName(text.toString()))
}
}
nameInput.addTextChangedListener {
if (nameInput.isFocused) nameHasChanged = true
}
builder.setView(layout)
return DialogBuilder(builder, nameInput, messageInput, modeSwitch, instantImage)
2022-08-09 15:26:52 +01:00
}
}