Browse Source

1.修改完成任务后收到IM消息没有弹窗提示bug

王鹏鹏 2 years ago
parent
commit
7a9cd87980

+ 2 - 0
baselib/src/main/java/com/yingyangfly/baselib/dialog/TaskFragment.kt

@@ -18,6 +18,7 @@ import com.yingyangfly.baselib.R
 import com.yingyangfly.baselib.ext.setOnSingleClickListener
 import com.yingyangfly.baselib.utils.LiveEventBusUtil
 import com.yingyangfly.baselib.utils.RxBusCodes
+import com.yingyangfly.baselib.utils.User
 import com.yingyangfly.baselib.utils.ViewTool
 
 /**
@@ -77,6 +78,7 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
         tvContent = rootView.findViewById(R.id.tvContent)
         confirmBtn = rootView.findViewById(R.id.confirmBtn)
         confirmBtn?.setOnSingleClickListener {
+            User.saveMessageBean("")
             onDialogClickListener?.invoke(id)
             dismiss()
         }

+ 1 - 0
baselib/src/main/java/com/yingyangfly/baselib/utils/CommonParam.kt

@@ -18,4 +18,5 @@ object CommonParam {
     val PADNO = "padNo"
     val SAVEGAMESOUNDDATE = "saveGameSoundDate"
     val TRAINREPORT_URL = "trainReportUrl"
+    val MESSAGEBEAN = "MessageBean"
 }

+ 18 - 0
baselib/src/main/java/com/yingyangfly/baselib/utils/User.kt

@@ -264,4 +264,22 @@ object User {
         }
     }
 
+    /**
+     * 保存理疗报告IM消息内容
+     */
+    fun saveMessageBean(messageBean: String) {
+        if (TextUtils.isEmpty(messageBean).not()) {
+            Preferences.put(CommonParam.MESSAGEBEAN, messageBean)
+        } else {
+            Preferences.put(CommonParam.MESSAGEBEAN, "")
+        }
+    }
+
+    fun getMessageBean(): String {
+        return if (TextUtils.isEmpty(Preferences.getString(CommonParam.MESSAGEBEAN))) {
+            ""
+        } else {
+            Preferences.getString(CommonParam.MESSAGEBEAN)!!
+        }
+    }
 }

+ 25 - 64
home/src/main/java/com/yingyangfly/home/activity/HomeActivity.kt

@@ -1,7 +1,9 @@
 package com.yingyangfly.home.activity
 
 import android.annotation.SuppressLint
-import android.os.CountDownTimer
+import android.app.Activity
+import android.app.ActivityManager
+import android.content.Context
 import android.text.TextUtils
 import android.util.Log
 import android.view.MotionEvent
@@ -69,8 +71,6 @@ class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(), Vie
     //推荐游戏分页数据
     private var pageList = mutableListOf<Record>()
     private val gameAdapter by lazy { GameAdapter() }
-    private var aCountDownTimer: CountDownTimer? = null
-    private var aMessageList = mutableMapOf<Long, MessageBean>()
 
     override fun initViews() {
         voicePlayer = VoicePlayer.getInstance(mContext)
@@ -160,6 +160,11 @@ class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(), Vie
             }
             LiveEventBusUtil.send(RxBusCodes.FINISHGAME, "")
             reportLog("跳转home页")
+            if (TextUtils.isEmpty(User.getMessageBean()).not()) {
+                val messageBean =
+                    GsonUtil.GsonToBean(User.getMessageBean(), MessageBean::class.java)
+                showTaskDialog(messageBean)
+            }
         }
     }
 
@@ -682,70 +687,12 @@ class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(), Vie
         }
     }
 
-    /**
-     * 移除消息监听
-     */
-    private fun removeAdvancedMsgListener() {
-        runOnUiThread {
-            //消息接受监听
-            V2TIMManager.getMessageManager().removeAdvancedMsgListener(simpleMsgListener)
-        }
-    }
-
     /**
      * 记录消息
      */
-    private fun addAMessageData(msg: MessageBean) {
+    private fun addAMessageData(bean: MessageBean) {
         runOnUiThread {
-            if (aMessageList.isNotEmpty() && aMessageList.containsKey(msg.timestamp).not()) {
-                aMessageList[msg.timestamp] = msg
-            } else {
-                aMessageList[msg.timestamp] = msg
-            }
-            if (aCountDownTimer != null) {
-                aCountDownTimer?.cancel()
-                aCountDownTimer?.start()
-            } else {
-                aCountDownTimer = object : CountDownTimer(1500, 500) {
-                    override fun onTick(millisUntilFinished: Long) {
-                    }
-
-                    override fun onFinish() {
-                        sortMessageDataByTimestamp(ArrayList(aMessageList.values))
-                    }
-                }
-                aCountDownTimer?.start()
-            }
-        }
-    }
-
-    /**
-     * 消息分类
-     */
-    private fun sortMessageDataByTimestamp(conversationList: List<MessageBean>) {
-        runOnUiThread {
-            if (conversationList.isNullOrEmpty().not()) {
-                if (conversationList.isNotEmpty()) {
-                    binding.headLayout.postDelayed({
-                        showMessageDialog(conversationList)
-                    }, 3000)
-                }
-            }
-            aMessageList.clear()
-        }
-    }
-
-    /**
-     * 展示消息弹框
-     */
-    private fun showMessageDialog(messageBeans: List<MessageBean>) {
-        val lengthComparator =
-            Comparator { o1: MessageBean, o2: MessageBean -> o2.timestamp.toInt() - o1.timestamp.toInt() }
-        val list = messageBeans.sortedWith(lengthComparator)
-        if (list.isNullOrEmpty().not()) {
-            LiveEventBusUtil.send(
-                RxBusCodes.SHOWTASKDIALOG, list[0]
-            )
+            User.saveMessageBean(GsonUtil.GsonString(bean))
         }
     }
 
@@ -831,7 +778,11 @@ class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(), Vie
                             }
 
                             "C" -> {
-                                addAMessageData(messageBean)
+                                if (isTopActivity(this)) {
+                                    showTaskDialog(messageBean)
+                                } else {
+                                    addAMessageData(messageBean)
+                                }
                             }
 
                             "end" -> {
@@ -982,4 +933,14 @@ class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(), Vie
             }
         }
     }
+
+    /**
+     * 判断当前activity是否位于栈顶
+     */
+    private fun isTopActivity(activity: Activity): Boolean {
+        val am = activity.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
+        val cn = am.getRunningTasks(1)[0].topActivity
+        return cn!!.className == activity.javaClass.name
+    }
+
 }