Ver Fonte

feat: 未读消息

tongmengxiao há 5 meses atrás
pai
commit
c6632dc866

+ 16 - 0
baseswago/src/main/java/com/swago/baseswago/model/UnreadMessageVm.kt

@@ -0,0 +1,16 @@
+package com.swago.baseswago.model
+
+import android.app.Application
+import androidx.lifecycle.MutableLiveData
+import com.swago.baseswago.util.BaseViewModel
+
+class UnreadMessageVm (application: Application) : BaseViewModel(application){
+
+    val unreadMessageData by lazy {
+        MutableLiveData<Long>()
+    }
+
+    fun setTotalUnreadCount(count: Long) {
+        unreadMessageData.value = count
+    }
+}

+ 16 - 0
baseswago/src/main/java/com/swago/baseswago/model/ViewModelProviderUtil.kt

@@ -0,0 +1,16 @@
+package com.swago.baseswago.model
+
+import android.app.Application
+import androidx.lifecycle.ViewModelProvider
+
+object ViewModelProviderUtil {
+    private var unreadMessageViewModel: UnreadMessageVm? = null
+
+    fun getUnreadMessageViewModel(application: Application): UnreadMessageVm {
+        if (unreadMessageViewModel == null) {
+            unreadMessageViewModel = ViewModelProvider.AndroidViewModelFactory(application)
+                .create(UnreadMessageVm::class.java)
+        }
+        return unreadMessageViewModel!!
+    }
+}

+ 21 - 0
baseswago/src/main/java/com/swago/baseswago/util/IMUtil.kt

@@ -85,4 +85,25 @@ object IMUtil {
             loginCallback?.invoke()
         }
     }
+
+    /**
+     * 未读消息数
+     * */
+    fun updateUnreadMessageCount(call:(total:Long)->Unit){
+        V2TIMManager.getConversationManager().getTotalUnreadMessageCount(object :V2TIMValueCallback<Long>{
+            override fun onSuccess(totalUnreadCount: Long) {
+                call.invoke(totalUnreadCount)
+            }
+
+            override fun onError(code: Int, desc: String?) {
+
+            }
+        })
+        V2TIMManager.getConversationManager().addConversationListener(object :V2TIMConversationListener(){
+            override fun onTotalUnreadMessageCountChanged(totalUnreadCount: Long) {
+                super.onTotalUnreadMessageCountChanged(totalUnreadCount)
+                call.invoke(totalUnreadCount)
+            }
+        })
+    }
 }

+ 17 - 1
home/src/main/java/com/swago/home/HomeActivity.kt

@@ -1,6 +1,7 @@
 package com.swago.home
 
 import android.text.TextUtils
+import android.view.View
 import android.widget.Toast
 import androidx.activity.viewModels
 import androidx.fragment.app.FragmentActivity
@@ -22,6 +23,8 @@ import com.swago.baseswago.im.IMMessageManager
 import com.swago.baseswago.inter.ApiManager
 import com.swago.baseswago.libpay.IPayCallback
 import com.swago.baseswago.libpay.PayManager
+import com.swago.baseswago.model.UnreadMessageVm
+import com.swago.baseswago.model.ViewModelProviderUtil
 import com.swago.baseswago.model.pay.PayModel
 import com.swago.baseswago.util.*
 import com.swago.home.databinding.ActivityHomeBinding
@@ -46,7 +49,7 @@ class HomeActivity : BaseXActivity<ActivityHomeBinding>(), IPayCallback {
     private val officialVm by viewModels<OfficialVm>()
     private val homeVm by viewModels<HomeVm>()
     private val payVm by viewModels<PayVm>()
-    private val userVm by viewModels<UserVm>()
+    private lateinit var unreadMessageVm: UnreadMessageVm
 
     private val listFragment =
         arrayListOf(HomeFragment(), MessageFragment(), MineFragment())
@@ -56,6 +59,7 @@ class HomeActivity : BaseXActivity<ActivityHomeBinding>(), IPayCallback {
     }
 
     override fun initOther() {
+        unreadMessageVm = ViewModelProviderUtil.getUnreadMessageViewModel(application)
         TUIKit.init(this, UrlConstant.getImId(), TUIKitConfigs.getConfigs())
         PayManager.init()
         IMMessageManager.groupMessageParser.add(GroupMsgParser())
@@ -117,6 +121,15 @@ class HomeActivity : BaseXActivity<ActivityHomeBinding>(), IPayCallback {
         })
 
         binding.viewPager.adapter = SwagoAdapter(listFragment, supportFragmentManager)
+        unreadMessageVm.unreadMessageData.observe(this){
+            if (it > 0){
+                binding.tvMessageNum.visibility = View.VISIBLE
+                binding.tvMessageNum.text = "$it"
+            } else {
+                binding.tvMessageNum.visibility = View.GONE
+            }
+
+        }
     }
 
     /**
@@ -288,6 +301,9 @@ class HomeActivity : BaseXActivity<ActivityHomeBinding>(), IPayCallback {
                 IUIKitCallBack {
                 override fun onSuccess(data: Any?) {
                     IMUtil.joinGroup(null)
+                    IMUtil.updateUnreadMessageCount {unread->
+                        unreadMessageVm.setTotalUnreadCount(unread)
+                    }
                 }
 
                 override fun onError(module: String?, errCode: Int, errMsg: String?) {

+ 3 - 2
home/src/main/res/layout/activity_home.xml

@@ -59,12 +59,13 @@
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
-        android:layout_marginBottom="30dp"
-        android:layout_marginStart="20dp"
+        android:layout_marginBottom="28dp"
+        android:layout_marginStart="18dp"
         android:textColor="#fff"
         tools:text="1"
         android:textSize="8sp"
         android:gravity="center"
+        android:visibility="gone"
         android:background="@drawable/shape_d54941_circle"/>
 
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 4 - 0
room/src/main/java/com/swago/room/anchor/AnchorRoomFragment.kt

@@ -156,6 +156,10 @@ class AnchorRoomFragment : BaseComFragment<FragmentBaseComBinding>() {
         lianMaiVm.showLianMaiDotFun = {
             (iFooter as AnchorFooterView).showLianMaiDot()
         }
+
+        unreadMessageVm.unreadMessageData.observe(this) {
+            unReadNum = it
+        }
     }
 
     private fun openLianMaiListDialog() {

+ 6 - 1
room/src/main/java/com/swago/room/base/BaseComFragment.kt

@@ -36,6 +36,8 @@ import com.swago.baseswago.dialog.ChoiceDialogFragment
 import com.swago.baseswago.dialog.ReportDialog
 import com.swago.baseswago.fragment.BaseXFragment
 import com.swago.baseswago.im.IRoomChat
+import com.swago.baseswago.model.UnreadMessageVm
+import com.swago.baseswago.model.ViewModelProviderUtil
 import com.swago.baseswago.model.im.GamePrize
 import com.swago.baseswago.model.im.RoomSystemMsgBean
 import com.swago.baseswago.model.live.RoomConfig
@@ -114,6 +116,7 @@ abstract class BaseComFragment<T : FragmentBaseComBinding> : BaseXFragment<T>(),
     val roomOtherVm by activityViewModels<RoomOtherVm>()
     val msgVm by activityViewModels<MsgVm>()
     val userVm by activityViewModels<UserVm>()
+    lateinit var unreadMessageVm: UnreadMessageVm
 
     /**
      * 麦位信息
@@ -122,6 +125,7 @@ abstract class BaseComFragment<T : FragmentBaseComBinding> : BaseXFragment<T>(),
         CopyOnWriteArrayList<PositionBean>()
     }
     var count = 2
+    var unReadNum:Long = 0
 
     //直播间观众列表
     var roomUserList = CopyOnWriteArrayList<RoomUserModel.ListBean>()
@@ -176,6 +180,7 @@ abstract class BaseComFragment<T : FragmentBaseComBinding> : BaseXFragment<T>(),
     }
 
     override fun initOther() {
+        unreadMessageVm = ViewModelProviderUtil.getUnreadMessageViewModel(AppContext.getContext())
         RoomTimer.timeTickList.add(this)
         PKStateManager.addPKListener(this)
         SwagoRoomManager.addListener(this)
@@ -731,7 +736,7 @@ abstract class BaseComFragment<T : FragmentBaseComBinding> : BaseXFragment<T>(),
     }
 
     fun openMoreDialog(){
-        FooterMoreDialog.newInstance().apply {
+        FooterMoreDialog.newInstance(unReadNum).apply {
             openMessageFun = {
                 openMessageListDialog()
             }

+ 11 - 1
room/src/main/java/com/swago/room/dialog/FooterMoreDialog.kt

@@ -27,8 +27,9 @@ class FooterMoreDialog : BaseXDFragment<DialogFooterMoreBinding>() {
     }
 
     companion object{
-        fun newInstance(): FooterMoreDialog {
+        fun newInstance(unReadNum:Long): FooterMoreDialog {
             val args = Bundle()
+            args.putLong("unReadNum", unReadNum)
             val fragment = FooterMoreDialog()
             fragment.arguments = args
             return fragment
@@ -40,6 +41,15 @@ class FooterMoreDialog : BaseXDFragment<DialogFooterMoreBinding>() {
     }
 
     override fun initLiveData() {
+        arguments?.let {
+            val unReadNum = it.getLong("unReadNum",0)
+            if (unReadNum > 0){
+                binding.tvNum.visibility = View.VISIBLE
+                binding.tvNum.text = "$unReadNum"
+            } else {
+                binding.tvNum.visibility = View.GONE
+            }
+        }
         binding.ivMessage.setOnClickListener {
             openMessageFun?.invoke()
             dismissAllowingStateLoss()

+ 4 - 0
room/src/main/java/com/swago/room/user/UserRoomFragment.kt

@@ -124,6 +124,10 @@ class UserRoomFragment : BaseComFragment<FragmentBaseComBinding>() {
             openLianMaiListDialog()
         }
 
+        unreadMessageVm.unreadMessageData.observe(this){
+            footerView.setUnReadCount(it)
+        }
+
         msgVm.forbidSpeakOrCancel = {
             isForbid = it
         }

+ 9 - 0
room/src/main/java/com/swago/room/widget/UserFooterView.kt

@@ -123,4 +123,13 @@ class UserFooterView : ConstraintLayout, IFooter,IRoomActiveListener {
     override fun closeRoomed() {
         SwagoRoomManager.removeListener(this)
     }
+
+    fun setUnReadCount(num:Long){
+        if (num > 0){
+            binding.tvNum.visibility = View.VISIBLE
+            binding.tvNum.text = "$num"
+        } else {
+            binding.tvNum.visibility = View.GONE
+        }
+    }
 }

+ 32 - 11
room/src/main/res/layout/dialog_footer_more.xml

@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
 
@@ -11,23 +12,43 @@
         android:layout_width="match_parent"
         android:layout_height="0dp">
 
-        <ImageView
-        android:id="@+id/ivMessage"
-        android:layout_marginStart="10dp"
-        android:background="@drawable/shape_80000000_20"
-        app:layout_constraintTop_toTopOf="parent"
-        app:layout_constraintStart_toStartOf="parent"
-        android:src="@mipmap/live_message"
-        android:layout_marginTop="@dimen/dp_10"
-        android:layout_width="40dp"
-        android:layout_height="40dp"/>
+        <androidx.constraintlayout.widget.ConstraintLayout
+            android:id="@+id/clMessage"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            app:layout_constraintTop_toTopOf="parent"
+            app:layout_constraintStart_toStartOf="parent"
+            android:layout_marginTop="@dimen/dp_10">
+            <ImageView
+                android:id="@+id/ivMessage"
+                android:layout_marginStart="10dp"
+                android:background="@drawable/shape_80000000_20"
+                app:layout_constraintTop_toTopOf="parent"
+                app:layout_constraintStart_toStartOf="parent"
+                android:src="@mipmap/live_message"
+                android:layout_width="40dp"
+                android:layout_height="40dp"/>
+            <TextView
+                android:id="@+id/tvNum"
+                android:layout_width="12dp"
+                android:layout_height="12dp"
+                app:layout_constraintEnd_toEndOf="parent"
+                app:layout_constraintTop_toTopOf="parent"
+                android:textColor="#fff"
+                tools:text="1"
+                android:textSize="8sp"
+                android:gravity="center"
+                android:visibility="gone"
+                android:background="@drawable/shape_d54941_circle"/>
+        </androidx.constraintlayout.widget.ConstraintLayout>
+
 
         <ImageView
         android:id="@+id/ivShare"
         android:layout_marginStart="16dp"
         android:background="@drawable/shape_80000000_20"
         app:layout_constraintTop_toTopOf="parent"
-        app:layout_constraintStart_toEndOf="@+id/ivMessage"
+        app:layout_constraintStart_toEndOf="@+id/clMessage"
         android:src="@mipmap/live_share"
         android:padding="6dp"
         android:layout_marginTop="@dimen/dp_10"

+ 29 - 7
room/src/main/res/layout/layout_user_footer_view.xml

@@ -63,14 +63,36 @@
         android:layout_width="40dp"
         android:layout_height="40dp"/>
 
-    <ImageView
-        android:id="@+id/ivMessage"
-        android:background="@drawable/shape_80000000_20"
+    <androidx.constraintlayout.widget.ConstraintLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
         app:layout_constraintTop_toTopOf="parent"
         app:layout_constraintStart_toEndOf="@+id/ivShare"
-        android:layout_marginStart="10dp"
-        android:src="@mipmap/live_message"
-        android:layout_width="40dp"
-        android:layout_height="40dp"/>
+        android:layout_marginStart="10dp">
+
+        <ImageView
+            android:id="@+id/ivMessage"
+            android:background="@drawable/shape_80000000_20"
+            android:src="@mipmap/live_message"
+            app:layout_constraintTop_toTopOf="parent"
+            app:layout_constraintStart_toStartOf="parent"
+            android:layout_width="40dp"
+            android:layout_height="40dp"/>
+
+        <TextView
+            android:id="@+id/tvNum"
+            android:layout_width="12dp"
+            android:layout_height="12dp"
+            app:layout_constraintEnd_toEndOf="parent"
+            app:layout_constraintTop_toTopOf="parent"
+            android:textColor="#fff"
+            tools:text="1"
+            android:textSize="8sp"
+            android:gravity="center"
+            android:visibility="gone"
+            android:background="@drawable/shape_d54941_circle"/>
+    </androidx.constraintlayout.widget.ConstraintLayout>
+
+
 
 </androidx.constraintlayout.widget.ConstraintLayout>