feat: 优化flow订阅策略

This commit is contained in:
yangxisong 2025-11-27 17:40:43 +08:00
parent 0c122b9352
commit 84961a367c
8 changed files with 44 additions and 52 deletions

View File

@ -8,7 +8,9 @@ import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.yuanxuan.rokid.databinding.ActivityMainBinding
import com.yuanxuan.rokid.extension.fadeIn
import com.yuanxuan.rokid.extension.fadeOut
@ -55,17 +57,19 @@ class MainActivity : AppCompatActivity() {
private fun observer() {
lifecycleScope.launch {
viewModel.uiState.collect { uiState ->
binding.batteryLevel.text =
resources.getString(R.string.status_bar_battery, uiState.batteryLevel)
binding.batteryLevelIv.setImageLevel(uiState.batteryLevel)
binding.wifiIv.setImageLevel(uiState.wifiLevel)
if (uiState.isVoiceInputVisible) {
binding.lottieVoiceInput.playAnimation()
binding.lottieVoiceInput.fadeIn(300)
} else {
binding.lottieVoiceInput.cancelAnimation()
binding.lottieVoiceInput.fadeOut(300)
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect { uiState ->
binding.batteryLevel.text =
resources.getString(R.string.status_bar_battery, uiState.batteryLevel)
binding.batteryLevelIv.setImageLevel(uiState.batteryLevel)
binding.wifiIv.setImageLevel(uiState.wifiLevel)
if (uiState.isVoiceInputVisible) {
binding.lottieVoiceInput.playAnimation()
binding.lottieVoiceInput.fadeIn(300)
} else {
binding.lottieVoiceInput.cancelAnimation()
binding.lottieVoiceInput.fadeOut(300)
}
}
}
}

View File

@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlin.time.Duration.Companion.seconds
class MainViewModel : ViewModel() {
@ -33,7 +34,7 @@ class MainViewModel : ViewModel() {
wifiLevel = 0,
isVoiceInputVisible = false
),
started = SharingStarted.Eagerly,
started = SharingStarted.WhileSubscribed(5.seconds.inWholeMilliseconds),
)
fun quitInstructReceived(){

View File

@ -36,21 +36,6 @@ class OkHttpWebSocketConnection() : WebSocketListener(), WebSocketConnection {
client = okHttpClient.newWebSocket(request, this)
}
override fun isDead(): Boolean {
TODO("Not yet implemented")
}
override fun disconnect() {
TODO("Not yet implemented")
}
override fun sendRequest(
request: WebSocketRequestMessage,
timeoutSeconds: Long
) {
client?.send(request.requestId)
}
override fun onMessage(webSocket: WebSocket, text: String) {
Timber.d(text)
}
@ -61,7 +46,7 @@ class OkHttpWebSocketConnection() : WebSocketListener(), WebSocketConnection {
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
Timber.e("websocket断开连接 ${t}")
Timber.e("websocket断开连接 $t")
_webSocketConnectionStateFlow.update { WebSocketConnectionState.FAILED }
}

View File

@ -11,17 +11,17 @@ interface WebSocketConnection {
fun connect(sn: String)
fun isDead(): Boolean
// fun isDead(): Boolean
fun disconnect()
// fun disconnect()
fun sendRequest(request: WebSocketRequestMessage) {
return sendRequest(request, DEFAULT_SEND_TIMEOUT.inWholeSeconds)
}
// fun sendRequest(request: WebSocketRequestMessage) {
// return sendRequest(request, DEFAULT_SEND_TIMEOUT.inWholeSeconds)
// }
fun sendRequest(
request: WebSocketRequestMessage,
timeoutSeconds: Long
)
// fun sendRequest(
// request: WebSocketRequestMessage,
// timeoutSeconds: Long
// )
}

View File

@ -1,5 +0,0 @@
package com.yuanxuan.rokid.network.websocket
data class WebSocketRequestMessage(
val requestId: String
)

View File

@ -1,5 +1,10 @@
package com.yuanxuan.rokid.network.websocket
data class WebSocketResponseMessage(
val requestId: String
import com.google.gson.annotations.SerializedName
data class WebSocketResponseMessage<T>(
@SerializedName("MsgType")
val msgType: Int,
@SerializedName("Msg")
val msg: T
)

View File

@ -7,6 +7,7 @@ import com.yuanxuan.rokid.dependencies.AppDependencies
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.shareIn
import kotlin.time.Duration.Companion.seconds
class HomeViewModel : ViewModel() {
val homeMenuBeans =
@ -33,7 +34,7 @@ class HomeViewModel : ViewModel() {
)
}.shareIn(
scope = viewModelScope,
started = SharingStarted.Eagerly,
started = SharingStarted.WhileSubscribed(5.seconds.inWholeMilliseconds),
replay = 1
)
}

View File

@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.shareIn
import kotlin.time.Duration.Companion.seconds
class SettingViewModel : ViewModel() {
@ -25,7 +26,7 @@ class SettingViewModel : ViewModel() {
}
}.shareIn(
scope = viewModelScope,
started = SharingStarted.Eagerly,
started = SharingStarted.WhileSubscribed(5.seconds.inWholeMilliseconds),
replay = 1
)
@ -43,26 +44,26 @@ class SettingViewModel : ViewModel() {
}
}.shareIn(
scope = viewModelScope,
started = SharingStarted.Eagerly,
started = SharingStarted.WhileSubscribed(5.seconds.inWholeMilliseconds),
replay = 1
)
fun brightnessAdd(){
fun brightnessAdd() {
AppDependencies.deviceServiceManager.brightnessAdd()
}
fun brightnessSubtract(){
fun brightnessSubtract() {
AppDependencies.deviceServiceManager.brightnessSubtract()
}
fun volumeAdd(){
fun volumeAdd() {
AppDependencies.deviceServiceManager.volumeAdd()
}
fun volumeSubtract(){
fun volumeSubtract() {
AppDependencies.deviceServiceManager.volumeSubtract()
}
fun deviceSn()= AppDependencies.deviceServiceManager.sn
fun deviceSn() = AppDependencies.deviceServiceManager.sn
}