Skip to content

Commit

Permalink
[] -- Messages Async
Browse files Browse the repository at this point in the history
  • Loading branch information
Isanz committed May 6, 2024
1 parent 146fbb4 commit 98ab4f3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,17 @@ class ChatFragment : Fragment() {
@RequiresApi(Build.VERSION_CODES.O)
private fun setUpButtons() {
mBinding.fabSendMessage.setOnClickListener {
sendMessage()
lifecycleScope.launch {
sendMessage()
}
}
mBinding.ibBack.setOnClickListener {
navigateBack()
}
}

@RequiresApi(Build.VERSION_CODES.O)
private fun sendMessage() {
private suspend fun sendMessage() {
val text = mBinding.tieMessage.text.toString()
chatViewModel.sendMessage(text, idChat, recipientId)
mBinding.tieMessage.text?.clear()
Expand Down
15 changes: 7 additions & 8 deletions app/src/main/java/com/isanz/inmomarket/ui/chat/ChatViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.coroutines.resume
Expand All @@ -35,17 +36,15 @@ class ChatViewModel : ViewModel() {
val messageList: LiveData<List<Message>> get() = _messageList

@RequiresApi(Build.VERSION_CODES.O)
fun sendMessage(text: String, chatId: String, senderId: String) {
suspend fun sendMessage(text: String, chatId: String, senderId: String) {
val message = createMessage(text, senderId)
try {
database.getReference("chatMessages").child(chatId).push().setValue(message)
} catch (e: Exception) {
Log.e(TAG, "saveMessageToChatMessage:failure", e)
}
try {
database.getReference("chats").child(chatId).child("lastMessage").setValue(message)
withContext(Dispatchers.IO) {
database.getReference("chatMessages").child(chatId).push().setValue(message).await()
database.getReference("chats").child(chatId).child("lastMessage").setValue(message).await()
}
} catch (e: Exception) {
Log.e(TAG, "saveMessageToChats:failure", e)
Log.e(TAG, "sendMessage:failure", e)
}
}

Expand Down
4 changes: 1 addition & 3 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@
<string name="prompt_to">A</string>
<string name="action_sign_in_github">Accedi con Github</string>
<string name="clear_filters">Cancella filtri</string>
<string name="no_user_loged">Al momento non c\'è nessun utente connesso. Effettua prima l'accesso.</string>
<string name="no_user_loged">Al momento non c\'è nessun utente connesso. Effettua prima l\'accesso.</string>
<string name="login_use_biometric">Accedi utilizzando le tue credenziali biometriche</string>
<string name="biometric_login">Accesso biometrico</string>
<string name="use_account_password">Utilizza la password dell\'account</string>


</resources>

0 comments on commit 98ab4f3

Please sign in to comment.