fix:修复切换同类型房间导致的Widget未更新Context问题
This commit is contained in:
@@ -194,11 +194,6 @@ class GameRoomFragment : BaseRoomFragment<IGameRoomView?, GameRoomPresenter?>(),
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
gameBinding.redPackageWidget.onStop()
|
||||
}
|
||||
|
||||
override fun initWidget() {
|
||||
super.initWidget()
|
||||
registerWidget(RedPackageWidget::class.java.simpleName, gameBinding.redPackageWidget)
|
||||
|
@@ -533,12 +533,6 @@ public class HomePartyRoomFragment extends BaseRoomFragment<IHomePartyView, Home
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
gameBinding.redPackageWidget.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
if (mDisposable != null) {
|
||||
|
@@ -267,9 +267,4 @@ class SingleRoomFragment : BaseRoomFragment<ISingleRoomView?, SingleRoomPresente
|
||||
super.initWidget()
|
||||
registerWidget(RedPackageWidget::class.java.simpleName, gameBinding.redPackageWidget)
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
gameBinding.redPackageWidget.onStop()
|
||||
}
|
||||
}
|
@@ -33,9 +33,15 @@ class RedPackageHandler : RoomHandler() {
|
||||
|
||||
override fun onStart(context: RoomContext) {
|
||||
super.onStart(context)
|
||||
logD("Handler onStart ${hashCode()}","MAAAX")
|
||||
registerSignaling()
|
||||
}
|
||||
|
||||
override fun onStop(context: RoomContext) {
|
||||
super.onStop(context)
|
||||
logD("Handler onStop ${hashCode()}","MAAAX")
|
||||
}
|
||||
|
||||
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
|
||||
super.onStateChanged(source, event)
|
||||
if (event == Lifecycle.Event.ON_RESUME) {
|
||||
@@ -87,6 +93,7 @@ class RedPackageHandler : RoomHandler() {
|
||||
.subscribe({ data ->
|
||||
handleRoomRedPackage(data, false)
|
||||
}, {
|
||||
logD("Handler requestLatestRoomRedPackage null","MAAAX")
|
||||
if (it.message == "No RedPackage") {
|
||||
updateIcon(null)
|
||||
} else {
|
||||
@@ -100,6 +107,7 @@ class RedPackageHandler : RoomHandler() {
|
||||
* @param isSignaling 是否来自信令?
|
||||
*/
|
||||
private fun handleRoomRedPackage(data: RedPackageNotifyInfo, isSignaling: Boolean) {
|
||||
logD("Handler handleRoomRedPackage isSignaling:$isSignaling","MAAAX")
|
||||
tryShowOpenDialog(data, isSignaling)
|
||||
updateIcon(data)
|
||||
}
|
||||
@@ -108,6 +116,7 @@ class RedPackageHandler : RoomHandler() {
|
||||
* 更新红包入口图标
|
||||
*/
|
||||
private fun updateIcon(data: RedPackageNotifyInfo?) {
|
||||
logD("Handler updateIcon","MAAAX")
|
||||
iconLiveData.postValue(data)
|
||||
}
|
||||
|
||||
|
@@ -4,17 +4,17 @@ import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.util.TypedValue
|
||||
import android.view.LayoutInflater
|
||||
import android.widget.TextView
|
||||
import androidx.constraintlayout.widget.ConstraintLayout
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import com.chuhai.utils.ServiceTime
|
||||
import com.chuhai.utils.ktx.singleClick
|
||||
import com.yizhuan.erban.R
|
||||
import com.yizhuan.erban.avroom.redpackage.open.RedPackageOpenDialog
|
||||
import com.yizhuan.erban.common.widget.dialog.DialogManager
|
||||
import com.yizhuan.erban.databinding.RedPackageWidgetBinding
|
||||
import com.yizhuan.xchat_android_core.redpackage.RedPackageModel
|
||||
import com.yizhuan.xchat_android_core.redpackage.RedPackageNotifyInfo
|
||||
import com.yizhuan.xchat_android_core.support.room.FrameLayoutRoomWidget
|
||||
import com.yizhuan.xchat_android_core.support.room.RoomContext
|
||||
import com.yizhuan.xchat_android_core.support.room.RoomView
|
||||
import com.yizhuan.xchat_android_core.support.room.RoomWidget
|
||||
@@ -32,10 +32,7 @@ import java.util.concurrent.TimeUnit
|
||||
* Created by Max on 2023/10/24 16:37
|
||||
* Desc:房间内的红包入口
|
||||
**/
|
||||
class RedPackageWidget : ConstraintLayout, RoomWidget {
|
||||
private var roomView: RoomView? = null
|
||||
private var textView: TextView? = null
|
||||
private var numView: TextView? = null
|
||||
class RedPackageWidget : FrameLayoutRoomWidget, RoomWidget {
|
||||
private var countDownDisposable: Disposable? = null
|
||||
private var data: RedPackageNotifyInfo? = null
|
||||
private val redPackageHandler: RedPackageHandler?
|
||||
@@ -52,18 +49,12 @@ class RedPackageWidget : ConstraintLayout, RoomWidget {
|
||||
}
|
||||
}
|
||||
|
||||
// 为了获取到RoomContext
|
||||
private val contextObserver = object : Observer<RoomContext?> {
|
||||
override fun onChanged(value: RoomContext?) {
|
||||
if (value != null) {
|
||||
RoomContext.contextLiveData.removeObserver(this)
|
||||
val view = this@RedPackageWidget.roomView
|
||||
if (view != null) {
|
||||
init(view)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private val binding: RedPackageWidgetBinding =
|
||||
DataBindingUtil.inflate(
|
||||
LayoutInflater.from(
|
||||
context
|
||||
), R.layout.red_package_widget, this, true
|
||||
)
|
||||
|
||||
private val compositeDisposable = CompositeDisposable()
|
||||
|
||||
@@ -85,10 +76,6 @@ class RedPackageWidget : ConstraintLayout, RoomWidget {
|
||||
init {
|
||||
// 默认不可见
|
||||
this.isVisible = false
|
||||
LayoutInflater.from(context)
|
||||
.inflate(R.layout.red_package_widget, this, true)
|
||||
textView = findViewById(R.id.tv_text)
|
||||
numView = findViewById(R.id.tv_num)
|
||||
this.setBackgroundResource(R.drawable.red_package_widget_bg)
|
||||
singleClick {
|
||||
data?.let {
|
||||
@@ -113,7 +100,7 @@ class RedPackageWidget : ConstraintLayout, RoomWidget {
|
||||
.doOnNext {
|
||||
val gap = time - ServiceTime.time
|
||||
if (gap >= 0) {
|
||||
textView?.text = mmssFormat.format(gap)
|
||||
binding.tvText.text = mmssFormat.format(gap)
|
||||
}
|
||||
}
|
||||
.doOnComplete {
|
||||
@@ -128,33 +115,30 @@ class RedPackageWidget : ConstraintLayout, RoomWidget {
|
||||
*/
|
||||
private fun switchUI(isCountDown: Boolean) {
|
||||
if (isCountDown) {
|
||||
textView?.setBackgroundResource(R.drawable.shape_99292929_8)
|
||||
textView?.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10f)
|
||||
binding.tvText.setBackgroundResource(R.drawable.shape_99292929_8)
|
||||
binding.tvText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10f)
|
||||
} else {
|
||||
textView?.setBackgroundResource(R.drawable.red_package_widget_bg_text)
|
||||
textView?.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 9f)
|
||||
textView?.setText(R.string.red_package_widget_get)
|
||||
binding.tvText.setBackgroundResource(R.drawable.red_package_widget_bg_text)
|
||||
binding.tvText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 9f)
|
||||
binding.tvText.setText(R.string.red_package_widget_get)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStart(roomView: RoomView) {
|
||||
this.roomView = roomView
|
||||
super.onStart(roomView)
|
||||
logD("Widget onStart", "MAAAX")
|
||||
roomView.getActivity()?.let {
|
||||
this.dialogManager = DialogManager(it)
|
||||
this.dialogManager?.setCanceledOnClickOutside(false)
|
||||
}
|
||||
// TODO #临时方案 因为目前View的生命周期比RoomContext还早,暂时这样拿到Context
|
||||
val context = RoomContext.contextLiveData.value
|
||||
if (context == null) {
|
||||
RoomContext.contextLiveData.observeForever(contextObserver)
|
||||
} else {
|
||||
init(roomView)
|
||||
}
|
||||
}
|
||||
|
||||
private fun init(roomView: RoomView) {
|
||||
override fun onInitialize(roomView: RoomView, roomContext: RoomContext) {
|
||||
super.onInitialize(roomView, roomContext)
|
||||
logD("Widget onInitialize", "MAAAX")
|
||||
val lifecycleOwner = roomView.getLifecycleOwner()
|
||||
redPackageHandler?.iconLiveData?.observe(lifecycleOwner) {
|
||||
logD("Widget iconLiveData $it", "MAAAX")
|
||||
loadData(it)
|
||||
}
|
||||
}
|
||||
@@ -169,13 +153,13 @@ class RedPackageWidget : ConstraintLayout, RoomWidget {
|
||||
val num = data.redEnvelopeNum
|
||||
if (num > 0) {
|
||||
if (num > 99) {
|
||||
numView?.text = "99+"
|
||||
binding.tvNum.text = "99+"
|
||||
} else {
|
||||
numView?.text = num.toString()
|
||||
binding.tvNum.text = num.toString()
|
||||
}
|
||||
numView?.isVisible = true
|
||||
binding.tvNum.isVisible = true
|
||||
} else {
|
||||
numView?.isVisible = false
|
||||
binding.tvNum.isVisible = false
|
||||
}
|
||||
if (data.validityType == 1) {
|
||||
// 限时生效
|
||||
@@ -226,14 +210,26 @@ class RedPackageWidget : ConstraintLayout, RoomWidget {
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
compositeDisposable.dispose()
|
||||
RoomContext.contextLiveData.removeObserver(contextObserver)
|
||||
stopCountDown()
|
||||
super.onStop()
|
||||
logD("Widget onStop", "MAAAX")
|
||||
this.dialogManager?.dismissDialog()
|
||||
this.dialogManager = null
|
||||
this.roomView = null
|
||||
}
|
||||
|
||||
override fun onUnbindContext() {
|
||||
super.onUnbindContext()
|
||||
logD("Widget onUnbindContext", "MAAAX")
|
||||
compositeDisposable.dispose()
|
||||
stopCountDown()
|
||||
loadData(null)
|
||||
}
|
||||
|
||||
override fun onBindContext(roomContext: RoomContext) {
|
||||
super.onBindContext(roomContext)
|
||||
logD("Widget onBindContext", "MAAAX")
|
||||
}
|
||||
|
||||
private fun stopCountDown() {
|
||||
if (countDownDisposable?.isDisposed == false) {
|
||||
countDownDisposable?.dispose()
|
||||
|
@@ -1,43 +1,46 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<merge 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"
|
||||
android:background="@drawable/red_package_widget_bg"
|
||||
tools:layout_height="58dp"
|
||||
tools:layout_width="58dp"
|
||||
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">
|
||||
<layout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_text"
|
||||
android:layout_width="36dp"
|
||||
android:layout_height="15dp"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:background="@drawable/red_package_widget_bg_text"
|
||||
android:gravity="center"
|
||||
android:includeFontPadding="false"
|
||||
android:text="@string/red_package_widget_get"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="9dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
<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"
|
||||
android:background="@drawable/red_package_widget_bg"
|
||||
tools:layout_height="58dp"
|
||||
tools:layout_width="58dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_num"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="12dp"
|
||||
android:layout_marginTop="4dp"
|
||||
android:layout_marginEnd="4dp"
|
||||
android:background="@drawable/red_package_bg_num"
|
||||
android:gravity="center"
|
||||
android:includeFontPadding="false"
|
||||
android:minWidth="12dp"
|
||||
android:paddingHorizontal="3dp"
|
||||
android:text="1"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="9dp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
</merge>
|
||||
<TextView
|
||||
android:id="@+id/tv_text"
|
||||
android:layout_width="36dp"
|
||||
android:layout_height="15dp"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:background="@drawable/red_package_widget_bg_text"
|
||||
android:gravity="center"
|
||||
android:includeFontPadding="false"
|
||||
android:text="@string/red_package_widget_get"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="9dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_num"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="12dp"
|
||||
android:layout_marginTop="4dp"
|
||||
android:layout_marginEnd="4dp"
|
||||
android:background="@drawable/red_package_bg_num"
|
||||
android:gravity="center"
|
||||
android:includeFontPadding="false"
|
||||
android:minWidth="12dp"
|
||||
android:paddingHorizontal="3dp"
|
||||
android:text="1"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="9dp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</layout>
|
@@ -0,0 +1,80 @@
|
||||
package com.yizhuan.xchat_android_core.support.room
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.widget.FrameLayout
|
||||
import androidx.annotation.CallSuper
|
||||
import androidx.lifecycle.Observer
|
||||
|
||||
/**
|
||||
* Created by Max on 2023/10/30 18:20
|
||||
* Desc:FrameLayout版本的房间组件
|
||||
* PS:由于目前房间架构支持同类型房间View层共用+目前的房间生命周期很不清晰!所以下面相关代码都在解决何时初始化、何时解绑等!!
|
||||
**/
|
||||
abstract class FrameLayoutRoomWidget : FrameLayout, RoomWidget {
|
||||
|
||||
protected var roomView: RoomView? = null
|
||||
|
||||
// 当前房间UID
|
||||
private var roomId: Long? = null
|
||||
|
||||
// 为了获取到RoomContext
|
||||
private val contextObserver =
|
||||
Observer<RoomContext?> { value ->
|
||||
if (roomId != null && (value == null || roomId != value.roomId)) {
|
||||
onUnbindContext()
|
||||
}
|
||||
roomId = value?.roomId
|
||||
if (value != null) {
|
||||
onBindContext(value)
|
||||
this.roomView?.let {
|
||||
onInitialize(it, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
constructor(context: Context) : super(context)
|
||||
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
|
||||
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
|
||||
context,
|
||||
attrs,
|
||||
defStyleAttr
|
||||
)
|
||||
|
||||
constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet?,
|
||||
defStyleAttr: Int,
|
||||
defStyleRes: Int
|
||||
) : super(context, attrs, defStyleAttr, defStyleRes)
|
||||
|
||||
@CallSuper
|
||||
override fun onStart(roomView: RoomView) {
|
||||
this.roomView = roomView
|
||||
RoomContext.contextLiveData.observeForever(contextObserver)
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
override fun onStop() {
|
||||
// 注销监听
|
||||
RoomContext.contextLiveData.removeObserver(contextObserver)
|
||||
// 解绑
|
||||
onUnbindContext()
|
||||
this.roomView = null
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定房间上下文:建立数据连接
|
||||
*/
|
||||
open fun onBindContext(roomContext: RoomContext) {}
|
||||
|
||||
/**
|
||||
* 解绑上下文:重置View状态
|
||||
*/
|
||||
open fun onUnbindContext() {}
|
||||
|
||||
/**
|
||||
* 初始化(View+Context)
|
||||
*/
|
||||
open fun onInitialize(roomView: RoomView, roomContext: RoomContext) {}
|
||||
}
|
@@ -9,12 +9,12 @@ import com.chuhai.utils.log.ILog
|
||||
interface RoomWidget : ILog {
|
||||
|
||||
/**
|
||||
* 组件激活
|
||||
* 开始(View层)
|
||||
*/
|
||||
fun onStart(roomView: RoomView)
|
||||
|
||||
/**
|
||||
* 组件停止
|
||||
* 结束(View层)
|
||||
*/
|
||||
fun onStop()
|
||||
}
|
Reference in New Issue
Block a user