gpt4 book ai didi

android - 状态值更改时屏幕不重组 - Jetpack Compose

转载 作者:行者123 更新时间:2023-12-05 02:25:49 25 4
gpt4 key购买 nike

是视频通话画面。它需要 token 和 channel 名称才能工作,需要将其传递给初始化调用引擎。我将这些存储在用作可变状态的数据类中。

屏幕状态数据类

@Keep
data class CallScreenState(
val callerId: Int? = null,
val recieverId: Int? = null,
val chatRoom: ChatRoom.Data? = null,
val rtcToken: AgoraTokenResponse.TokenData? = null
)

并在 View 模型中通过这段代码初始化状态:

var callScreenState by mutableStateOf(CallScreenState())

并且在聊天室和 token api 成功响应的 View 模型中,使用此代码更新状态。

callScreenState = callScreenState.copy(
chatRoom = chatRoom.data,//from response
rtcToken = token.data //from response
)

从这里开始,预计将使用 chatRoom 和 rtcToken 的新更新值重新组合屏幕。

并且是可组合的

val screenState = remember {
viewModel.callScreenState
}

此屏幕状态用于将值传递给初始化引擎

val mEngine = remember {
initEngine(
context,
object : IRtcEngineEventHandler() {
override fun onJoinChannelSuccess(channel: String?, uid: Int, elapsed: Int) {
Timber.e("hhp-CallScreen onJoinChannelSuccess channel:$channel,uid:$uid,elapsed:$elapsed")
}

override fun onUserJoined(uid: Int, elapsed: Int) {
Timber.e("hhp-CallScreen onUserJoined:$uid")
val desiredUserList = remoteUserMap.toMutableMap()
desiredUserList[uid] = null
remoteUserMap = desiredUserList.toMap() as HashMap<Int, TextureView?>
}

override fun onUserOffline(uid: Int, reason: Int) {
Timber.e("hhp-CallScreen onUserOffline:$uid")
val desiredUserList = remoteUserMap.toMutableMap()
desiredUserList.remove(uid)
remoteUserMap = desiredUserList.toMap() as HashMap<Int, TextureView?>
}

override fun onNetworkQuality(uid: Int, txQuality: Int, rxQuality: Int) {
Timber.e("hhp-CallScreen onNetworkQuality $uid $txQuality $rxQuality")

}
},
screenState.chatRoom?.channelName ?: "", //Not recomposing when value changes in viewmodel
viewModel.userRole,
token = screenState.rtcToken?.token ?: "" //Not recomposing when value changes in viewmodel
)
}

这是initEngine函数的创建

fun initEngine(
current: Context,
eventHandler: IRtcEngineEventHandler,
channelName: String,
userRole: String,
token: String
): RtcEngine =
RtcEngine.create(current, BuildConfig.AGORA_APPID, eventHandler).apply {
enableVideo()
setChannelProfile(1)
if (userRole == "Broadcaster") {
setClientRole(1)
} else {
setClientRole(0)
}
//Expected to be recomposed when screen state value updated with new values
joinChannel(token, channelName, "", 0)
}

我了解到,在调用 api 之前,屏幕状态中的 channel 名称和 token 在一开始是空的。一旦用于获取 token 和聊天室的 api 成功,屏幕状态就会从 viewmodel 更新,我希望 initEngine 乐趣再次被调用,因为它应该重新组合。但事实并非如此。我错过了什么吗? screen sctate 中的 channelname 值发生变化时,如何让它重新组合?

最佳答案

我无法理解你的整个用例,但你是否尝试过为你的remember指定一个key

How to make it recompose whenver the channelname value inside screensctate changes?

你可以尝试其中任何一个,虽然我不确定它们是否能解决你的问题,但是当你提供一个 keyremember 并且它改变时,它将重新计算,假设remember's之前计算使用的channelName在下一次re-composition.

这个,

val screenState = remember(key1 = channelname) {
viewModel.callScreenState
}

或者这个

val mEngine = remember(key1 = channelname) { ... }

关于android - 状态值更改时屏幕不重组 - Jetpack Compose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74274295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com