Przeglądaj źródła

优化微聊发送失败重发机制

carlos 2 miesięcy temu
rodzic
commit
42621d583e

+ 106 - 73
app/src/main/java/com/xplora/commonservice/modules/ChatMsgManager.kt

@@ -7,7 +7,7 @@ import android.os.PowerManager
 import com.google.gson.Gson
 import com.xplora.commonservice.BaseApplication.Companion.globalContext
 import com.xplora.commonservice.R
-import com.xplora.commonservice.model.*
+import com.xplora.commonservice.model.ChatFileType
 import com.xplora.commonservice.model.ChatMsgState.MESSAGE_STATE_READ
 import com.xplora.commonservice.model.ChatMsgState.MESSAGE_STATE_SEND_FAIL
 import com.xplora.commonservice.model.ChatMsgState.MESSAGE_STATE_SEND_ING
@@ -19,10 +19,26 @@ import com.xplora.commonservice.model.ChatMsgType.PHOTO
 import com.xplora.commonservice.model.ChatMsgType.TEXT
 import com.xplora.commonservice.model.ChatMsgType.VIDEO
 import com.xplora.commonservice.model.ChatMsgType.VOICE
+import com.xplora.commonservice.model.CkError
+import com.xplora.commonservice.model.HttpApi
+import com.xplora.commonservice.model.MusicDownloadStatus
+import com.xplora.commonservice.model.NetworkType
 import com.xplora.commonservice.model.database.ChatDbDbEntity
 import com.xplora.commonservice.model.database.MusicDbDbEntity
 import com.xplora.commonservice.model.http.BaseResponse
-import com.xplora.commonservice.model.http.chat.*
+import com.xplora.commonservice.model.http.chat.GetMsgStateRepo
+import com.xplora.commonservice.model.http.chat.GetMsgStateReq
+import com.xplora.commonservice.model.http.chat.MsgListRepo
+import com.xplora.commonservice.model.http.chat.MsgListReq
+import com.xplora.commonservice.model.http.chat.MsgRepo
+import com.xplora.commonservice.model.http.chat.MsgReq
+import com.xplora.commonservice.model.http.chat.SendEmojiReq
+import com.xplora.commonservice.model.http.chat.SendMsgRepo
+import com.xplora.commonservice.model.http.chat.SendPhotoReq
+import com.xplora.commonservice.model.http.chat.SendTextReq
+import com.xplora.commonservice.model.http.chat.SendVideoReq
+import com.xplora.commonservice.model.http.chat.SendVoiceReq
+import com.xplora.commonservice.model.http.chat.SetMsgReadReq
 import com.xplora.commonservice.model.http.file.UploadRepo
 import com.xplora.commonservice.model.mqtt.BaseMessage
 import com.xplora.commonservice.modules.NotificationManager.cancelMusicDownloadingNotification
@@ -48,9 +64,19 @@ import com.xplora.commonservice.ui.activity.EmptyActivity
 import com.xplora.commonservice.utils.FileUtil
 import com.xplora.commonservice.utils.Logger
 import com.xplora.commonservice.utils.NetworkUtil
-import kotlinx.coroutines.*
+import kotlinx.coroutines.CoroutineName
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Deferred
+import kotlinx.coroutines.DelicateCoroutinesApi
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.async
+import kotlinx.coroutines.cancel
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
 import okhttp3.HttpUrl.Companion.toHttpUrl
-import java.util.*
 
 @OptIn(DelicateCoroutinesApi::class)
 object ChatMsgManager {
@@ -67,9 +93,9 @@ object ChatMsgManager {
             wl.acquire(10 * 1000)
             Logger.d(TAG, "updateMsg wl.acquire")
             val isExist = queryChatMsgByMsgId(pushMsg.data!!.msgId!!)
-            if(isExist != null) {
+            if (isExist != null) {
                 Logger.w(TAG, "msgId already exist, donot download again!!!")
-                if(wl.isHeld) wl.release()
+                if (wl.isHeld) wl.release()
                 return@launch
             }
             val pushId = pushMsg.data!!.xpPushId
@@ -131,7 +157,7 @@ object ChatMsgManager {
                     }
                 }
             }
-            if(wl.isHeld) wl.release()
+            if (wl.isHeld) wl.release()
             Logger.d(TAG, "updateMsg after wl.release")
         }
     }
@@ -253,6 +279,7 @@ object ChatMsgManager {
                         )
                     }
                 }
+
                 TEXT -> {
                     block = this.async {
                         val request = SendTextReq(
@@ -266,78 +293,81 @@ object ChatMsgManager {
                         )
                     }
                 }
+
                 PHOTO -> {
-                    if (entity.fid != null) {
-                        block = this.async {
-                            val request = SendPhotoReq(
-                                entity.fid!!,
-                                entity.receiverId!!
-                            )
-                            XPHttpServiceExecutor.execute(
-                                null,
-                                request,
-                                getFlowable(HttpApi.Chat.PHOTO, request, null)
-                            )
-                        }
-                    } else {
-                        block = this.async {
-                            val r: BaseResponse<*>
-                            FileUploader.execute(ChatFileType.PHOTO.ordinal, entity.filePath!!)
-                                .let {
-                                    r = if (it.ckError == CkError.SUCCESS) {
-                                        (it as UploadRepo).run {
-                                            entity.fid = arrayOf(this.fid).toString()
-                                            val request = SendPhotoReq(
-                                                this.fid,
-                                                entity.receiverId!!
-                                            )
-                                            XPHttpServiceExecutor.execute(
-                                                null,
-                                                request,
-                                                getFlowable(HttpApi.Chat.PHOTO, request, null)
-                                            )
-                                        }
-                                    } else it
-                                }
-                            r
-                        }
+                    /*                    if (entity.fid != null) {
+                                            block = this.async {
+                                                val request = SendPhotoReq(
+                                                    entity.fid!!,
+                                                    entity.receiverId!!
+                                                )
+                                                XPHttpServiceExecutor.execute(
+                                                    null,
+                                                    request,
+                                                    getFlowable(HttpApi.Chat.PHOTO, request, null)
+                                                )
+                                            }
+                                        } else {*/
+                    block = this.async {
+                        val r: BaseResponse<*>
+                        FileUploader.execute(ChatFileType.PHOTO.ordinal, entity.filePath!!)
+                            .let {
+                                r = if (it.ckError == CkError.SUCCESS) {
+                                    (it as UploadRepo).run {
+                                        entity.fid = arrayOf(this.fid).toString()
+                                        val request = SendPhotoReq(
+                                            this.fid,
+                                            entity.receiverId!!
+                                        )
+                                        XPHttpServiceExecutor.execute(
+                                            null,
+                                            request,
+                                            getFlowable(HttpApi.Chat.PHOTO, request, null)
+                                        )
+                                    }
+                                } else it
+                            }
+                        r
                     }
+                    // }
                 }
+
                 VOICE -> {
                     Logger.d(TAG, "sendChatMsg: VOICE")
-                    if (entity.fid != null) {
-                        block = this.async {
-                            val request = SendVoiceReq(entity.fid!!,entity.dur!!.toInt(), entity.receiverId!!)
-                            XPHttpServiceExecutor.execute(
-                                null,
-                                request,
-                                getFlowable(HttpApi.Chat.VOICE, request, null)
-                            )
-                        }
-                    } else {
-                        block = this.async {
-                            val r: BaseResponse<*>
-                            Logger.d(TAG, "sendChatMsg: VOICE new")
-                            FileUploader.execute(ChatFileType.VOICE.ordinal, entity.filePath!!)
-                                .let {
-                                    r = if (it.ckError == CkError.SUCCESS) {
-                                        (it as UploadRepo).run {
-                                            entity.fid = arrayOf(this.fid).toString()
-                                            val request = SendVoiceReq(
-                                                this.fid, entity.dur!!.toInt(), entity.receiverId!!
-                                            )
-                                            XPHttpServiceExecutor.execute(
-                                                null,
-                                                request,
-                                                getFlowable(HttpApi.Chat.VOICE, request, null)
-                                            )
-                                        }
-                                    } else it
-                                }
-                            r
-                        }
+                    /*                    if (entity.fid != null) {
+                                            block = this.async {
+                                                val request = SendVoiceReq(entity.fid!!,entity.dur!!.toInt(), entity.receiverId!!)
+                                                XPHttpServiceExecutor.execute(
+                                                    null,
+                                                    request,
+                                                    getFlowable(HttpApi.Chat.VOICE, request, null)
+                                                )
+                                            }
+                                        } else {*/
+                    block = this.async {
+                        val r: BaseResponse<*>
+                        Logger.d(TAG, "sendChatMsg: VOICE new")
+                        FileUploader.execute(ChatFileType.VOICE.ordinal, entity.filePath!!)
+                            .let {
+                                r = if (it.ckError == CkError.SUCCESS) {
+                                    (it as UploadRepo).run {
+                                        entity.fid = arrayOf(this.fid).toString()
+                                        val request = SendVoiceReq(
+                                            this.fid, entity.dur!!.toInt(), entity.receiverId!!
+                                        )
+                                        XPHttpServiceExecutor.execute(
+                                            null,
+                                            request,
+                                            getFlowable(HttpApi.Chat.VOICE, request, null)
+                                        )
+                                    }
+                                } else it
+                            }
+                        r
                     }
+                    // }
                 }
+
                 VIDEO -> {
                     val coverRepo: BaseResponse<*>?
                     val videoRepo: BaseResponse<*>?
@@ -401,7 +431,7 @@ object ChatMsgManager {
                     }
                 } ?: run { entity.localState = MESSAGE_STATE_SEND_FAIL.toString() }
             } ?: run { entity.localState = MESSAGE_STATE_SEND_FAIL.toString() }
-            if(entity.localState != MESSAGE_STATE_SEND_SUCCESS.toString()) {
+            if (entity.localState != MESSAGE_STATE_SEND_SUCCESS.toString()) {
                 showChatFailNotification(entity)
             }
             updateChatMsg(entity)
@@ -413,12 +443,15 @@ object ChatMsgManager {
             PHOTO -> {
                 downloadPhoto(entity)
             }
+
             VOICE -> {
                 downloadVoice(entity)
             }
+
             VIDEO -> {
                 downloadVideo(entity)
             }
+
             MP3 -> {
                 downloadMP3(entity)
             }