kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
Add basic quick chat action settings
rodzic
483eca94e9
commit
8c2d3a4041
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
<vector android:height="24dp" android:tint="?attr/colorControlNormal"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="@android:color/white" android:pathData="M20,9H4v2h16V9zM4,15h16v-2H4V15z"/>
|
||||
</vector>
|
|
@ -0,0 +1,5 @@
|
|||
<vector android:height="24dp" android:tint="?attr/colorControlNormal"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="@android:color/white" android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/>
|
||||
</vector>
|
|
@ -0,0 +1,62 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
style="@style/Widget.App.CardView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/quickChatActionName"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:textSize="20sp"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/quickChatActionValue"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/quickChatActionName" />
|
||||
|
||||
<ImageButton
|
||||
android:id="@+id/quickChatActionEdit"
|
||||
android:layout_width="48dp"
|
||||
android:layout_height="48dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@id/quickChatActionDrag"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:srcCompat="@drawable/ic_baseline_edit_24" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/quickChatActionDrag"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="8dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:srcCompat="@drawable/ic_baseline_drag_handle_24" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
|
||||
</LinearLayout>
|
|
@ -0,0 +1,35 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_margin="16dp">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/addQuickChatMessage"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp"
|
||||
android:ems="10"
|
||||
android:hint="@string/message"
|
||||
android:inputType="textShortMessage"
|
||||
android:minHeight="48dp" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/addQuickChatName"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp"
|
||||
android:ems="10"
|
||||
android:hint="@string/name"
|
||||
android:inputType="textShortMessage"
|
||||
android:minHeight="48dp" />
|
||||
|
||||
<com.google.android.material.switchmaterial.SwitchMaterial
|
||||
android:id="@+id/addQuickChatMode"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp"
|
||||
android:text="@string/mode_append" />
|
||||
|
||||
</LinearLayout>
|
|
@ -1,6 +1,48 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/colorAdvancedBackground">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/quickChatSettingsTitle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:text="Quick chat options"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/quickChatSettingsView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/quickChatSettingsTitle"
|
||||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
|
||||
|
||||
|
||||
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
android:id="@+id/quickChatSettingsCreateButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="16dp"
|
||||
android:clickable="true"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:srcCompat="@drawable/ic_twotone_add_24" />
|
||||
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
@ -144,4 +144,7 @@
|
|||
<string name="resend">Resend</string>
|
||||
<string name="shutdown">Shutdown</string>
|
||||
<string name="reboot">Reboot</string>
|
||||
<string name="message">Message</string>
|
||||
<string name="mode_append">Append to message</string>
|
||||
<string name="mode_instant">Instantly send</string>
|
||||
</resources>
|
||||
|
|
Ładowanie…
Reference in New Issue