Quellcode durchsuchen

feat: 未读消息

tongmengxiao vor 6 Monaten
Ursprung
Commit
814a6fd823

+ 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)
+            }
+        })
+    }
 }

+ 18 - 5
home/src/main/java/com/swago/home/HomeActivity.kt

@@ -1,30 +1,28 @@
 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
-import androidx.fragment.app.viewModels
 import androidx.lifecycle.lifecycleScope
 import androidx.viewpager.widget.ViewPager
 import com.alibaba.android.arouter.facade.annotation.Route
 import com.android.billingclient.api.Purchase
-import com.google.gson.Gson
 import com.swago.baseswago.PayVm
 import com.swago.baseswago.SwagoAdapter
-import com.swago.baseswago.UserVm
 import com.swago.baseswago.activity.BaseXActivity
 import com.swago.baseswago.constant.ARouteConstant
 import com.swago.baseswago.constant.UrlConstant
 import com.swago.baseswago.dialog.CloseAccountDialog
-import com.swago.baseswago.dialog.ForceOfflineDialog
-import com.swago.baseswago.dialog.InviteBindingDialog
 import com.swago.baseswago.dialog.OrderLostDialog
 import com.swago.baseswago.im.GroupMsgParser
 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
@@ -49,6 +47,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 lateinit var unreadMessageVm: UnreadMessageVm
 
     private val listFragment =
         arrayListOf(HomeFragment(), MessageFragment(), MineFragment())
@@ -58,6 +57,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())
@@ -119,6 +119,16 @@ 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
+            }
+
+        }
+
     }
 
     /**
@@ -271,6 +281,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

@@ -144,6 +144,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
@@ -113,6 +115,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
 
     /**
      * 麦位信息
@@ -121,6 +124,7 @@ abstract class BaseComFragment<T : FragmentBaseComBinding> : BaseXFragment<T>(),
         CopyOnWriteArrayList<PositionBean>()
     }
     var count = 2
+    var unReadNum:Long = 0
 
     //直播间观众列表
     var roomUserList = CopyOnWriteArrayList<RoomUserModel.ListBean>()
@@ -175,6 +179,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)
@@ -726,7 +731,7 @@ abstract class BaseComFragment<T : FragmentBaseComBinding> : BaseXFragment<T>(),
     }
 
     fun openMoreDialog(){
-        FooterMoreDialog.newInstance().apply {
+        FooterMoreDialog.newInstance(unReadNum).apply {
             openMessageFun = {
                 openMessageListDialog()
             }

+ 14 - 3
room/src/main/java/com/swago/room/dialog/FooterMoreDialog.kt

@@ -7,6 +7,9 @@ import android.view.View.OnClickListener
 import androidx.fragment.app.viewModels
 import com.swago.baseswago.baseroom.SwagoRoomManager
 import com.swago.baseswago.dialog.BaseXDFragment
+import com.swago.baseswago.model.UnreadMessageVm
+import com.swago.baseswago.model.ViewModelProviderUtil
+import com.swago.baseswago.util.AppContext
 import com.swago.baseswago.util.NoDoubleClickListener
 import com.swago.baseswago.util.SwagoLoading
 import com.swago.room.bean.UserRoomModel
@@ -18,7 +21,6 @@ class FooterMoreDialog : BaseXDFragment<DialogFooterMoreBinding>() {
 
     var openMessageFun:(()->Unit)? = null
     var openShareFun:(()->Unit)? = null
-
     init {
         setGravity(Gravity.BOTTOM)
         setDimAmount(0.5f)
@@ -26,8 +28,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
@@ -35,10 +38,18 @@ class FooterMoreDialog : BaseXDFragment<DialogFooterMoreBinding>() {
     }
 
     override fun initOther() {
-
     }
 
     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
+        }
+    }
 }

+ 2 - 0
room/src/main/res/layout/dialog_footer_more.xml

@@ -29,6 +29,7 @@
                 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"
@@ -37,6 +38,7 @@
                 tools:text="1"
                 android:textSize="8sp"
                 android:gravity="center"
+                android:visibility="gone"
                 android:background="@drawable/shape_d54941_circle"/>
         </androidx.constraintlayout.widget.ConstraintLayout>
 

+ 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>