Skip to content

Commit

Permalink
add manual resend button
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Hibbe <[email protected]>
  • Loading branch information
mahibi committed Dec 27, 2024
1 parent e11e8ce commit fc05012
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ interface ChatMessageRepository : LifecycleAwareManager {
referenceId: String
): Flow<Result<ChatMessage?>>

suspend fun resendChatMessage(
credentials: String,
url: String,
message: String,
displayName: String,
replyTo: Int,
sendWithoutNotification: Boolean,
referenceId: String
): Flow<Result<ChatMessage?>>

suspend fun addTemporaryMessage(
message: CharSequence,
displayName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ class OfflineFirstChatRepository @Inject constructor(

override fun loadInitialMessages(withNetworkParams: Bundle): Job =
scope.launch {
sendTempChatMessages(credentials, urlForChatting)

Log.d(TAG, "---- loadInitialMessages ------------")
newXChatLastCommonRead = conversationModel.lastCommonReadMessage

Expand Down Expand Up @@ -197,6 +195,8 @@ class OfflineFirstChatRepository @Inject constructor(
)
}

sendTempChatMessages(credentials, urlForChatting)

// delay is a dirty workaround to make sure messages are added to adapter on initial load before dealing
// with them (otherwise there is a race condition).
delay(DELAY_TO_ENSURE_MESSAGES_ARE_ADDED)
Expand Down Expand Up @@ -843,6 +843,37 @@ class OfflineFirstChatRepository @Inject constructor(
emit(Result.failure(e))
}

override suspend fun resendChatMessage(
credentials: String,
url: String,
message: String,
displayName: String,
replyTo: Int,
sendWithoutNotification: Boolean,
referenceId: String
): Flow<Result<ChatMessage?>> {
val messageToResend = chatDao.getTempMessageForConversation(internalConversationId, referenceId).first()
messageToResend.sendingFailed = false
chatDao.updateChatMessage(messageToResend)

val messageToResendModel = messageToResend.asModel()
_removeMessageFlow.emit(messageToResendModel)

val tripleChatMessages = Triple(true, false, listOf(messageToResendModel))
_messageFlow.emit(tripleChatMessages)

return sendChatMessage(
credentials,
url,
message,
displayName,
replyTo,
sendWithoutNotification,
referenceId
)
}


override suspend fun editChatMessage(
credentials: String,
url: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.nextcloud.talk.chat.data.model.ChatMessage
import com.nextcloud.talk.chat.data.network.ChatNetworkDataSource
import com.nextcloud.talk.conversationlist.data.OfflineConversationsRepository
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.extensions.toIntOrZero
import com.nextcloud.talk.jobs.UploadAndShareFilesWorker
import com.nextcloud.talk.models.domain.ConversationModel
import com.nextcloud.talk.models.domain.ReactionAddedModel
Expand Down Expand Up @@ -794,6 +795,29 @@ class ChatViewModel @Inject constructor(
}
}

fun resendMessage(
credentials: String,
urlForChat: String,
message: ChatMessage) {
viewModelScope.launch {
chatRepository.resendChatMessage(
credentials,
urlForChat,
message.message.orEmpty(),
message.actorDisplayName.orEmpty(),
message.parentMessageId?.toIntOrZero() ?: 0,
false,
message.referenceId.orEmpty()
).collect { result ->
if (result.isSuccess) {
Log.d(TAG, "resend successful")
} else {
Log.e(TAG, "resend failed")
}
}
}
}

companion object {
private val TAG = ChatViewModel::class.simpleName
const val JOIN_ROOM_RETRY_COUNT: Long = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class TempMessageActionsDialog(

private fun initMenuItems() {
this.lifecycleScope.launch {
initResendMessage(true)
initMenuEditMessage(true)
initMenuDeleteMessage(true)
}
Expand All @@ -80,6 +81,27 @@ class TempMessageActionsDialog(
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
}

private fun initResendMessage(visible: Boolean) {
if (visible) {
binding.menuResendMessage.setOnClickListener {


chatActivity.chatViewModel.resendMessage(
chatActivity.conversationUser!!.getCredentials(),
ApiUtils.getUrlForChat(
chatActivity.chatApiVersion,
chatActivity.conversationUser!!.baseUrl!!,
chatActivity.roomToken
),
message
)

dismiss()
}
}
binding.menuResendMessage.visibility = getVisibility(visible)
}

private fun initMenuDeleteMessage(visible: Boolean) {
if (visible) {
binding.menuDeleteMessage.setOnClickListener {
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/res/layout/dialog_temp_message_actions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,39 @@
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:id="@+id/menu_resend_message"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_sheet_item_height"
android:background="?android:attr/selectableItemBackground"
android:gravity="center_vertical"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">

<ImageView
android:id="@+id/menu_icon_resend_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:paddingStart="@dimen/standard_padding"
android:paddingEnd="@dimen/zero"
android:src="@drawable/ic_send"
app:tint="@color/high_emphasis_menu_icon" />

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/menu_text_resend_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:paddingStart="@dimen/standard_double_padding"
android:paddingEnd="@dimen/standard_padding"
android:text="@string/resend_message"
android:textAlignment="viewStart"
android:textColor="@color/high_emphasis_text"
android:textSize="@dimen/bottom_sheet_text_size" />

</LinearLayout>

<LinearLayout
android:id="@+id/menu_copy_message"
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -843,4 +843,5 @@ How to translate with transifex:
<string name="user_absence">%1$s is out of office and might not respond</string>
<string name="user_absence_for_one_day">%1$s is out of office today</string>
<string name="user_absence_replacement">Replacement: </string>
<string name="resend_message">Resend</string>
</resources>

0 comments on commit fc05012

Please sign in to comment.