feat: 优化flow订阅策略
This commit is contained in:
parent
0c122b9352
commit
84961a367c
|
|
@ -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,6 +57,7 @@ class MainActivity : AppCompatActivity() {
|
|||
|
||||
private fun observer() {
|
||||
lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
viewModel.uiState.collect { uiState ->
|
||||
binding.batteryLevel.text =
|
||||
resources.getString(R.string.status_bar_battery, uiState.batteryLevel)
|
||||
|
|
@ -71,3 +74,4 @@ class MainActivity : AppCompatActivity() {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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(){
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
// )
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
package com.yuanxuan.rokid.network.websocket
|
||||
|
||||
data class WebSocketRequestMessage(
|
||||
val requestId: String
|
||||
)
|
||||
|
|
@ -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
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
)
|
||||
}
|
||||
|
|
@ -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,7 +44,7 @@ class SettingViewModel : ViewModel() {
|
|||
}
|
||||
}.shareIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.Eagerly,
|
||||
started = SharingStarted.WhileSubscribed(5.seconds.inWholeMilliseconds),
|
||||
replay = 1
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue