mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-03 21:53:55 -04:00
Add basic quick chat action settings
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package com.geeksville.mesh.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.TextView
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.geeksville.mesh.R
|
||||
import com.geeksville.mesh.model.QuickChatAction
|
||||
|
||||
class QuickChatActionAdapter internal constructor(
|
||||
context: Context,
|
||||
private val onEdit: (action: QuickChatAction) -> Unit
|
||||
) : RecyclerView.Adapter<QuickChatActionAdapter.ActionViewHolder>() {
|
||||
|
||||
private val inflater: LayoutInflater = LayoutInflater.from(context)
|
||||
private var actions = emptyList<QuickChatAction>()
|
||||
private val TAG = "QuickChatAdapter"
|
||||
|
||||
inner class ActionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
val actionName: TextView = itemView.findViewById(R.id.quickChatActionName)
|
||||
val actionValue: TextView = itemView.findViewById(R.id.quickChatActionValue)
|
||||
val actionEdit: View = itemView.findViewById(R.id.quickChatActionEdit)
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ActionViewHolder {
|
||||
val itemView = inflater.inflate(R.layout.adapter_quick_chat_action_layout, parent, false)
|
||||
Log.d(TAG, "Created view holder")
|
||||
return ActionViewHolder(itemView)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ActionViewHolder, position: Int) {
|
||||
val current = actions[position]
|
||||
holder.actionName.text = current.name
|
||||
holder.actionValue.text = current.message
|
||||
holder.actionEdit.setOnClickListener{
|
||||
onEdit(current)
|
||||
}
|
||||
Log.d(TAG, "Bound actions")
|
||||
}
|
||||
|
||||
|
||||
internal fun setActions(actions: List<QuickChatAction>) {
|
||||
this.actions = actions
|
||||
notifyDataSetChanged()
|
||||
Log.d(TAG, String.format("setActions(size=%d, count=%d)", actions.size, itemCount))
|
||||
}
|
||||
|
||||
override fun getItemCount() = actions.size
|
||||
|
||||
}
|
||||
@@ -30,6 +30,107 @@ class QuickChatSettingsFragment : ScreenFragment("Quick Chat settings"), Logging
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
// TODO
|
||||
Log.d(TAG, "viewCreated")
|
||||
|
||||
binding.quickChatSettingsCreateButton.setOnClickListener {
|
||||
Log.d(TAG, "Create quick chat")
|
||||
|
||||
val builder = createEditDialog(requireContext(), "New quick chat")
|
||||
|
||||
builder.builder.setPositiveButton("Add") { view, x ->
|
||||
|
||||
val name = builder.nameInput.text.toString().trim()
|
||||
val message = builder.messageInput.text.toString()
|
||||
if (name.isNotEmpty() and message.isNotEmpty())
|
||||
model.addQuickChatAction(
|
||||
name, message,
|
||||
if (builder.modeSwitch.isChecked) QuickChatAction.Mode.Instant else QuickChatAction.Mode.Append
|
||||
)
|
||||
// TODO
|
||||
}
|
||||
builder.builder.setNegativeButton("Cancel") { _, _ ->
|
||||
// TODO
|
||||
}
|
||||
|
||||
val dialog = builder.builder.create()
|
||||
dialog.getButton(0).isEnabled = false
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
model.addQuickChatAction("TST", "Test", QuickChatAction.Mode.Append)
|
||||
val quickChatActionAdapter =
|
||||
QuickChatActionAdapter(requireContext()) { action: QuickChatAction ->
|
||||
val builder = createEditDialog(requireContext(), "Edit quick chat")
|
||||
builder.nameInput.setText(action.name)
|
||||
builder.messageInput.setText(action.message)
|
||||
builder.modeSwitch.isChecked = action.mode == QuickChatAction.Mode.Instant
|
||||
|
||||
builder.builder.setNegativeButton(R.string.cancel) { _, _ -> }
|
||||
builder.builder.setPositiveButton(R.string.save_btn) { _, _ ->
|
||||
// TODO
|
||||
}
|
||||
val dialog = builder.builder.create()
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
binding.quickChatSettingsView.apply {
|
||||
this.layoutManager = LinearLayoutManager(requireContext())
|
||||
this.adapter = quickChatActionAdapter
|
||||
}
|
||||
|
||||
quickChatActionAdapter.setActions(model.quickChatActions)
|
||||
Log.d(TAG, "viewCreation done")
|
||||
}
|
||||
|
||||
data class DialogBuilder(
|
||||
val builder: MaterialAlertDialogBuilder,
|
||||
val nameInput: EditText,
|
||||
val messageInput: EditText,
|
||||
val modeSwitch: SwitchMaterial
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
var nameHasChanged = false
|
||||
|
||||
modeSwitch.setOnCheckedChangeListener { _, _ ->
|
||||
modeSwitch.setText(if (modeSwitch.isChecked) R.string.mode_instant else R.string.mode_append)
|
||||
}
|
||||
|
||||
messageInput.addTextChangedListener { text ->
|
||||
if (!nameHasChanged) {
|
||||
nameInput.setText(getMessageName(text.toString()))
|
||||
}
|
||||
}
|
||||
|
||||
nameInput.addTextChangedListener {
|
||||
if (nameInput.isFocused) nameHasChanged = true
|
||||
}
|
||||
|
||||
// TODO: Don't enable positive button until there is name and message
|
||||
builder.setView(layout)
|
||||
|
||||
return DialogBuilder(builder, nameInput, messageInput, modeSwitch)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user