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.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.view.ViewCompat
|
import androidx.core.view.ViewCompat
|
||||||
import androidx.core.view.WindowInsetsCompat
|
import androidx.core.view.WindowInsetsCompat
|
||||||
|
import androidx.lifecycle.Lifecycle
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import androidx.lifecycle.repeatOnLifecycle
|
||||||
import com.yuanxuan.rokid.databinding.ActivityMainBinding
|
import com.yuanxuan.rokid.databinding.ActivityMainBinding
|
||||||
import com.yuanxuan.rokid.extension.fadeIn
|
import com.yuanxuan.rokid.extension.fadeIn
|
||||||
import com.yuanxuan.rokid.extension.fadeOut
|
import com.yuanxuan.rokid.extension.fadeOut
|
||||||
|
|
@ -55,6 +57,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private fun observer() {
|
private fun observer() {
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
|
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||||
viewModel.uiState.collect { uiState ->
|
viewModel.uiState.collect { uiState ->
|
||||||
binding.batteryLevel.text =
|
binding.batteryLevel.text =
|
||||||
resources.getString(R.string.status_bar_battery, uiState.batteryLevel)
|
resources.getString(R.string.status_bar_battery, uiState.batteryLevel)
|
||||||
|
|
@ -70,4 +73,5 @@ class MainActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.asSharedFlow
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
class MainViewModel : ViewModel() {
|
class MainViewModel : ViewModel() {
|
||||||
|
|
||||||
|
|
@ -33,7 +34,7 @@ class MainViewModel : ViewModel() {
|
||||||
wifiLevel = 0,
|
wifiLevel = 0,
|
||||||
isVoiceInputVisible = false
|
isVoiceInputVisible = false
|
||||||
),
|
),
|
||||||
started = SharingStarted.Eagerly,
|
started = SharingStarted.WhileSubscribed(5.seconds.inWholeMilliseconds),
|
||||||
)
|
)
|
||||||
|
|
||||||
fun quitInstructReceived(){
|
fun quitInstructReceived(){
|
||||||
|
|
|
||||||
|
|
@ -36,21 +36,6 @@ class OkHttpWebSocketConnection() : WebSocketListener(), WebSocketConnection {
|
||||||
client = okHttpClient.newWebSocket(request, this)
|
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) {
|
override fun onMessage(webSocket: WebSocket, text: String) {
|
||||||
Timber.d(text)
|
Timber.d(text)
|
||||||
}
|
}
|
||||||
|
|
@ -61,7 +46,7 @@ class OkHttpWebSocketConnection() : WebSocketListener(), WebSocketConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
|
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
|
||||||
Timber.e("websocket断开连接 ${t}")
|
Timber.e("websocket断开连接 $t")
|
||||||
_webSocketConnectionStateFlow.update { WebSocketConnectionState.FAILED }
|
_webSocketConnectionStateFlow.update { WebSocketConnectionState.FAILED }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,17 +11,17 @@ interface WebSocketConnection {
|
||||||
|
|
||||||
fun connect(sn: String)
|
fun connect(sn: String)
|
||||||
|
|
||||||
fun isDead(): Boolean
|
// fun isDead(): Boolean
|
||||||
|
|
||||||
fun disconnect()
|
// fun disconnect()
|
||||||
|
|
||||||
fun sendRequest(request: WebSocketRequestMessage) {
|
// fun sendRequest(request: WebSocketRequestMessage) {
|
||||||
return sendRequest(request, DEFAULT_SEND_TIMEOUT.inWholeSeconds)
|
// return sendRequest(request, DEFAULT_SEND_TIMEOUT.inWholeSeconds)
|
||||||
}
|
// }
|
||||||
|
|
||||||
fun sendRequest(
|
// fun sendRequest(
|
||||||
request: WebSocketRequestMessage,
|
// request: WebSocketRequestMessage,
|
||||||
timeoutSeconds: Long
|
// 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
|
package com.yuanxuan.rokid.network.websocket
|
||||||
|
|
||||||
data class WebSocketResponseMessage(
|
import com.google.gson.annotations.SerializedName
|
||||||
val requestId: String
|
|
||||||
|
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.SharingStarted
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.shareIn
|
import kotlinx.coroutines.flow.shareIn
|
||||||
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
class HomeViewModel : ViewModel() {
|
class HomeViewModel : ViewModel() {
|
||||||
val homeMenuBeans =
|
val homeMenuBeans =
|
||||||
|
|
@ -33,7 +34,7 @@ class HomeViewModel : ViewModel() {
|
||||||
)
|
)
|
||||||
}.shareIn(
|
}.shareIn(
|
||||||
scope = viewModelScope,
|
scope = viewModelScope,
|
||||||
started = SharingStarted.Eagerly,
|
started = SharingStarted.WhileSubscribed(5.seconds.inWholeMilliseconds),
|
||||||
replay = 1
|
replay = 1
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.SharedFlow
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.flow.shareIn
|
import kotlinx.coroutines.flow.shareIn
|
||||||
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
class SettingViewModel : ViewModel() {
|
class SettingViewModel : ViewModel() {
|
||||||
|
|
||||||
|
|
@ -25,7 +26,7 @@ class SettingViewModel : ViewModel() {
|
||||||
}
|
}
|
||||||
}.shareIn(
|
}.shareIn(
|
||||||
scope = viewModelScope,
|
scope = viewModelScope,
|
||||||
started = SharingStarted.Eagerly,
|
started = SharingStarted.WhileSubscribed(5.seconds.inWholeMilliseconds),
|
||||||
replay = 1
|
replay = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -43,26 +44,26 @@ class SettingViewModel : ViewModel() {
|
||||||
}
|
}
|
||||||
}.shareIn(
|
}.shareIn(
|
||||||
scope = viewModelScope,
|
scope = viewModelScope,
|
||||||
started = SharingStarted.Eagerly,
|
started = SharingStarted.WhileSubscribed(5.seconds.inWholeMilliseconds),
|
||||||
replay = 1
|
replay = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
fun brightnessAdd(){
|
fun brightnessAdd() {
|
||||||
AppDependencies.deviceServiceManager.brightnessAdd()
|
AppDependencies.deviceServiceManager.brightnessAdd()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun brightnessSubtract(){
|
fun brightnessSubtract() {
|
||||||
AppDependencies.deviceServiceManager.brightnessSubtract()
|
AppDependencies.deviceServiceManager.brightnessSubtract()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun volumeAdd(){
|
fun volumeAdd() {
|
||||||
AppDependencies.deviceServiceManager.volumeAdd()
|
AppDependencies.deviceServiceManager.volumeAdd()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun volumeSubtract(){
|
fun volumeSubtract() {
|
||||||
AppDependencies.deviceServiceManager.volumeSubtract()
|
AppDependencies.deviceServiceManager.volumeSubtract()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deviceSn()= AppDependencies.deviceServiceManager.sn
|
fun deviceSn() = AppDependencies.deviceServiceManager.sn
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue