feat: 连接状态
This commit is contained in:
parent
9c29fee24e
commit
e1ddc14553
|
|
@ -18,6 +18,7 @@ import com.yuanxuan.rokid.databinding.ActivityMainBinding
|
|||
import com.yuanxuan.rokid.extension.fadeIn
|
||||
import com.yuanxuan.rokid.extension.fadeOut
|
||||
import com.yuanxuan.rokid.keeplive.KeepLiveService
|
||||
import com.yuanxuan.rokid.network.websocket.WebSocketConnectionState
|
||||
import com.yuanxuan.rokid.network.websocket.WebSocketEvent
|
||||
import com.yuanxuan.rokid.ui.NoticeFragment
|
||||
import kotlinx.coroutines.delay
|
||||
|
|
@ -47,8 +48,6 @@ class MainActivity : AppCompatActivity() {
|
|||
* 拦截返回键事件,防止返回到桌面
|
||||
*/
|
||||
onBackPressedDispatcher.addCallback {
|
||||
viewModel.wakeUp()
|
||||
navigationToast("TestMessage")
|
||||
}
|
||||
/**
|
||||
* 这个服务保证心跳包准时发
|
||||
|
|
@ -119,6 +118,13 @@ class MainActivity : AppCompatActivity() {
|
|||
binding.lottieVoiceInput.cancelAnimation()
|
||||
binding.lottieVoiceInput.fadeOut(300)
|
||||
}
|
||||
if (uiState.connectStatus == WebSocketConnectionState.CONNECTED) {
|
||||
binding.socketStatus.text =
|
||||
resources.getString(R.string.socket_status_connected)
|
||||
} else {
|
||||
binding.socketStatus.text =
|
||||
resources.getString(R.string.socket_status_connecting)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,10 @@ package com.yuanxuan.rokid
|
|||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.yuanxuan.rokid.dependencies.AppDependencies
|
||||
import com.yuanxuan.rokid.dependencies.AppDependencies.webSocketManager
|
||||
import com.yuanxuan.rokid.dependencies.AppDependencies.deviceServiceManager
|
||||
import com.yuanxuan.rokid.device.DeviceServiceManager
|
||||
import com.yuanxuan.rokid.network.websocket.WebSocketConnectionState
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
|
|
@ -21,24 +22,27 @@ class MainViewModel : ViewModel() {
|
|||
val uiState = combine(
|
||||
deviceServiceManager.batteryPercentage,
|
||||
deviceServiceManager.wifiState,
|
||||
deviceServiceManager.instructState
|
||||
) { battery, wifi, instruct ->
|
||||
deviceServiceManager.instructState,
|
||||
webSocketManager.connectFlow
|
||||
) { battery, wifi, instruct, connect ->
|
||||
MainUiState(
|
||||
batteryLevel = battery,
|
||||
wifiLevel = if (wifi is DeviceServiceManager.WifiState.Connected) wifi.level else 0,
|
||||
isVoiceInputVisible = instruct == DeviceServiceManager.InstructState.WaitingInstructState
|
||||
isVoiceInputVisible = instruct == DeviceServiceManager.InstructState.WaitingInstructState,
|
||||
connectStatus = connect
|
||||
)
|
||||
}.stateIn(
|
||||
scope = viewModelScope,
|
||||
initialValue = MainUiState(
|
||||
batteryLevel = 0,
|
||||
wifiLevel = 0,
|
||||
isVoiceInputVisible = false
|
||||
isVoiceInputVisible = false,
|
||||
connectStatus = WebSocketConnectionState.DISCONNECTED
|
||||
),
|
||||
started = SharingStarted.WhileSubscribed(5.seconds.inWholeMilliseconds),
|
||||
)
|
||||
|
||||
val socketEvent = AppDependencies.webSocketManager.socketEventFlow
|
||||
val socketEvent = webSocketManager.socketEventFlow
|
||||
|
||||
fun quitInstructReceived() {
|
||||
deviceServiceManager.quitInstructReceived()
|
||||
|
|
@ -68,6 +72,7 @@ class MainViewModel : ViewModel() {
|
|||
val batteryLevel: Int,
|
||||
val wifiLevel: Int,
|
||||
val isVoiceInputVisible: Boolean,
|
||||
val connectStatus: WebSocketConnectionState,
|
||||
)
|
||||
|
||||
}
|
||||
|
|
@ -6,8 +6,8 @@ import java.net.NetworkInterface
|
|||
|
||||
object NetUtils {
|
||||
|
||||
fun getBaseUrl() = "http://${getLocalIPV4address()}.70:7070"
|
||||
// fun getBaseUrl() = "http://${getLocalIPV4address()}.52:8765"
|
||||
// fun getBaseUrl() = "http://${getLocalIPV4address()}.70:7070"
|
||||
fun getBaseUrl() = "http://${getLocalIPV4address()}.13:8765"
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ class WebSocketManager(context: Context, scope: CoroutineScope) {
|
|||
|
||||
val socketEventFlow = webSocketConnection.eventFlow
|
||||
|
||||
val connectFlow = webSocketConnection.webSocketConnectionStateFlow
|
||||
|
||||
init {
|
||||
scope.launch {
|
||||
webSocketConnection.webSocketConnectionStateFlow.collectLatest { state ->
|
||||
|
|
|
|||
|
|
@ -12,20 +12,20 @@
|
|||
android:name="androidx.navigation.fragment.NavHostFragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintBottom_toTopOf="@id/lottie_voice_input"
|
||||
app:defaultNavHost="true"
|
||||
app:layout_constraintBottom_toTopOf="@id/lottie_voice_input"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:navGraph="@navigation/app_navigation" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/battery_level"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Hello World!"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="@dimen/status_bar_text_size"
|
||||
app:layout_constraintBottom_toTopOf="@id/bottom"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
tools:text="50%" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/wifi_iv"
|
||||
|
|
@ -50,6 +50,7 @@
|
|||
app:layout_constraintTop_toTopOf="@id/battery_level" />
|
||||
|
||||
<TextClock
|
||||
android:id="@+id/time"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="@color/white"
|
||||
|
|
@ -57,6 +58,17 @@
|
|||
app:layout_constraintBottom_toTopOf="@id/bottom"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/socket_status"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="5dp"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="@dimen/status_bar_text_size"
|
||||
app:layout_constraintBottom_toTopOf="@id/bottom"
|
||||
app:layout_constraintStart_toEndOf="@id/time"
|
||||
tools:text="已连接" />
|
||||
|
||||
<com.airbnb.lottie.LottieAnimationView
|
||||
android:id="@+id/lottie_voice_input"
|
||||
android:layout_width="150dp"
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
android:paddingStart="30.dp"
|
||||
android:paddingEnd="30.dp"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="16sp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
|
|
|
|||
|
|
@ -11,4 +11,6 @@
|
|||
<string name="status_bar_battery">%d%%</string>
|
||||
<string name="device_info_sn">设备SN:%s</string>
|
||||
<string name="device_info_app_version">APP版本:%s</string>
|
||||
<string name="socket_status_connected">已连接</string>
|
||||
<string name="socket_status_connecting">连接中</string>
|
||||
</resources>
|
||||
Loading…
Reference in New Issue