diff --git a/.gitignore b/.gitignore index e86d61cc..0f040ac1 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ local.properties \$PWD/ $PWD/ +ktlint diff --git a/app/src/main/java/com/m57/hermescontrol/ui/connect/ConnectViewModel.kt b/app/src/main/java/com/m57/hermescontrol/ui/connect/ConnectViewModel.kt index 0c20ea89..1888bb83 100644 --- a/app/src/main/java/com/m57/hermescontrol/ui/connect/ConnectViewModel.kt +++ b/app/src/main/java/com/m57/hermescontrol/ui/connect/ConnectViewModel.kt @@ -165,12 +165,37 @@ class ConnectViewModel : ViewModel() { connect() return } + // Decoded valid JSON but missing required fields + _uiState.update { + it.copy( + errorMessage = "Malformed pairing string — missing host, port, or token", + ) + } + return } + // Decoded to valid string but not JSON shape — not a pairing string + _uiState.update { + it.copy( + errorMessage = "Malformed pairing string — expected URL or Base64-encoded JSON", + ) + } + return + } catch (e: IllegalArgumentException) { + // Not valid Base64 — check if input looks like a raw token + if (trimmed.length >= 32 && trimmed.matches(Regex("[A-Za-z0-9_\\-]+"))) { + _uiState.update { it.copy(token = trimmed, errorMessage = null) } + } else { + _uiState.update { + it.copy( + errorMessage = "Malformed pairing string — expected URL or Base64-encoded JSON", + ) + } + } + return } catch (e: Exception) { - // Ignore base64 decode failures, fallback to setting raw token + _uiState.update { it.copy(errorMessage = "Failed to parse pairing string: ${e.message}") } + return } - - _uiState.update { it.copy(token = trimmed, errorMessage = null) } } catch (e: Exception) { _uiState.update { it.copy(errorMessage = "Failed to parse pairing string: ${e.message}") } }