message sending kinda works again

1.2-legacy
geeksville 2020-04-08 17:50:23 -07:00
rodzic 29f0435f18
commit 749103cd09
4 zmienionych plików z 54 dodań i 5 usunięć

Wyświetl plik

@ -4,6 +4,8 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
@ -15,6 +17,17 @@ import com.geeksville.mesh.model.UIViewModel
import kotlinx.android.synthetic.main.adapter_message_layout.view.*
import kotlinx.android.synthetic.main.messages_fragment.*
// Allows usage like email.on(EditorInfo.IME_ACTION_NEXT, { confirm() })
fun EditText.on(actionId: Int, func: () -> Unit) {
setOnEditorActionListener { _, receivedActionId, _ ->
if (actionId == receivedActionId) {
func()
}
true
}
}
class MessagesFragment : ScreenFragment("Messages"), Logging {
@ -116,6 +129,9 @@ class MessagesFragment : ScreenFragment("Messages"), Logging {
fun onMessagesChanged(nodesIn: Collection<TextMessage>) {
messages = nodesIn.toTypedArray()
notifyDataSetChanged() // FIXME, this is super expensive and redraws all messages
// scroll to the last line
messageListView.scrollToPosition(this.itemCount - 1)
}
}
@ -129,8 +145,18 @@ class MessagesFragment : ScreenFragment("Messages"), Logging {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
messageInputText.on(EditorInfo.IME_ACTION_DONE) {
debug("did IME action")
val str = messageInputText.text.toString()
model.messagesState.sendMessage(str)
messageInputText.setText("") // blow away the string the user just entered
}
messageListView.adapter = messagesAdapter
messageListView.layoutManager = LinearLayoutManager(requireContext())
val layoutManager = LinearLayoutManager(requireContext())
layoutManager.stackFromEnd = true // We want the last rows to always be shown
messageListView.layoutManager = layoutManager
model.messagesState.messages.observe(viewLifecycleOwner, Observer { it ->
messagesAdapter.onMessagesChanged(it)

Wyświetl plik

@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_margin="4dp"
android:clipToPadding="false">
<androidx.constraintlayout.widget.ConstraintLayout
@ -32,6 +32,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/username"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>

Wyświetl plik

@ -6,14 +6,35 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/messageListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/textInputLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:hint="@string/send_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/messageInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
android:text="" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Wyświetl plik

@ -13,4 +13,5 @@
<string name="sample_distance">2.13 km</string>
<string name="sample_message">hey I found the cache, it is over here next to the big tiger. I\'m kinda scared.</string>
<string name="some_username">Some Username</string>
<string name="send_text">Send Text</string>
</resources>