Fix connection state flapping and bump version to 1.1
Add consecutive failure threshold (3 polls) before marking as disconnected, preventing UI flapping caused by intermittent ESP32 errors. Bump versionCode to 2 and versionName to 1.1.
This commit is contained in:
parent
1eda5614ac
commit
fe98d3d379
@ -32,8 +32,8 @@ android {
|
|||||||
applicationId = "cz.bugsy.karemote"
|
applicationId = "cz.bugsy.karemote"
|
||||||
minSdk = 33
|
minSdk = 33
|
||||||
targetSdk = 36
|
targetSdk = 36
|
||||||
versionCode = 1
|
versionCode = 2
|
||||||
versionName = "1.0"
|
versionName = "1.1"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
vectorDrawables {
|
vectorDrawables {
|
||||||
|
|||||||
@ -65,6 +65,7 @@ class MainViewModel @Inject constructor(
|
|||||||
private var pollingJob: Job? = null
|
private var pollingJob: Job? = null
|
||||||
private var lastStationName: String? = null
|
private var lastStationName: String? = null
|
||||||
private var connectionFailedSince: Long? = null
|
private var connectionFailedSince: Long? = null
|
||||||
|
private var consecutiveFailures: Int = 0
|
||||||
private var volumeJob: Job? = null
|
private var volumeJob: Job? = null
|
||||||
private var lastVolumeSentTime: Long = 0
|
private var lastVolumeSentTime: Long = 0
|
||||||
private var pendingVolume: Int? = null
|
private var pendingVolume: Int? = null
|
||||||
@ -72,6 +73,7 @@ class MainViewModel @Inject constructor(
|
|||||||
companion object {
|
companion object {
|
||||||
private const val ERROR_DISPLAY_DELAY_MS = 10_000L // 10 seconds
|
private const val ERROR_DISPLAY_DELAY_MS = 10_000L // 10 seconds
|
||||||
private const val VOLUME_THROTTLE_MS = 300L // Throttle volume commands
|
private const val VOLUME_THROTTLE_MS = 300L // Throttle volume commands
|
||||||
|
private const val DISCONNECT_THRESHOLD = 3 // Consecutive failures before marking disconnected
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@ -81,6 +83,7 @@ class MainViewModel @Inject constructor(
|
|||||||
if (settings.serverAddress.isNotBlank()) {
|
if (settings.serverAddress.isNotBlank()) {
|
||||||
// Reset failure tracking when server address changes
|
// Reset failure tracking when server address changes
|
||||||
connectionFailedSince = null
|
connectionFailedSince = null
|
||||||
|
consecutiveFailures = 0
|
||||||
_uiState.update { it.copy(isConnecting = true, errorMessage = null) }
|
_uiState.update { it.copy(isConnecting = true, errorMessage = null) }
|
||||||
startPolling()
|
startPolling()
|
||||||
} else {
|
} else {
|
||||||
@ -115,6 +118,7 @@ class MainViewModel @Inject constructor(
|
|||||||
.onSuccess { status ->
|
.onSuccess { status ->
|
||||||
// Connection successful - reset failure tracking
|
// Connection successful - reset failure tracking
|
||||||
connectionFailedSince = null
|
connectionFailedSince = null
|
||||||
|
consecutiveFailures = 0
|
||||||
_uiState.update {
|
_uiState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
isConnected = true,
|
isConnected = true,
|
||||||
@ -131,6 +135,7 @@ class MainViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onFailure { error ->
|
.onFailure { error ->
|
||||||
|
consecutiveFailures++
|
||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
|
|
||||||
// Start tracking failure time if this is the first failure
|
// Start tracking failure time if this is the first failure
|
||||||
@ -141,14 +146,18 @@ class MainViewModel @Inject constructor(
|
|||||||
val failureDuration = now - (connectionFailedSince ?: now)
|
val failureDuration = now - (connectionFailedSince ?: now)
|
||||||
val shouldShowError = failureDuration >= ERROR_DISPLAY_DELAY_MS
|
val shouldShowError = failureDuration >= ERROR_DISPLAY_DELAY_MS
|
||||||
|
|
||||||
_uiState.update {
|
// Only mark as disconnected after multiple consecutive failures
|
||||||
it.copy(
|
// to avoid flapping when ESP32 has intermittent errors
|
||||||
isConnected = false,
|
if (consecutiveFailures >= DISCONNECT_THRESHOLD) {
|
||||||
isConnecting = !shouldShowError,
|
_uiState.update {
|
||||||
errorMessage = if (shouldShowError) {
|
it.copy(
|
||||||
"Connection failed: ${error.localizedMessage}"
|
isConnected = false,
|
||||||
} else null
|
isConnecting = !shouldShowError,
|
||||||
)
|
errorMessage = if (shouldShowError) {
|
||||||
|
"Connection failed: ${error.localizedMessage}"
|
||||||
|
} else null
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user