Files
honeyDueKMP/iosApp/iosApp/Residence/JoinResidenceView.swift
Trey t 2730c94e4d Add comprehensive error message parsing to prevent raw JSON display
- Created ErrorMessageParser utility for both iOS (Swift) and Android (Kotlin)
- Parser detects JSON-formatted error messages and extracts user-friendly text
- Identifies when data objects (not errors) are returned and provides generic messages
- Updated all API error handling to pass raw error bodies instead of concatenating
- Applied ErrorMessageParser across all ViewModels and screens on both platforms
- Fixed ContractorApi and DocumentApi to not concatenate error bodies with messages
- Updated ApiResultHandler to automatically parse all error messages
- Error messages now show "Request failed. Please check your input and try again." instead of raw JSON

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-14 22:59:42 -06:00

105 lines
3.6 KiB
Swift

import SwiftUI
import ComposeApp
struct JoinResidenceView: View {
@Environment(\.dismiss) private var dismiss
@StateObject private var viewModel = ResidenceViewModel()
let onJoined: () -> Void
@State private var shareCode: String = ""
var body: some View {
NavigationView {
Form {
Section {
TextField("Share Code", text: $shareCode)
.textInputAutocapitalization(.characters)
.autocorrectionDisabled()
.onChange(of: shareCode) { newValue in
// Limit to 6 characters and uppercase
if newValue.count > 6 {
shareCode = String(newValue.prefix(6))
}
shareCode = shareCode.uppercased()
viewModel.clearError()
}
.disabled(viewModel.isLoading)
} header: {
Text("Enter Share Code")
} footer: {
Text("Enter the 6-character code shared with you to join a residence")
.foregroundColor(.secondary)
}
if let error = viewModel.errorMessage {
Section {
Text(error)
.foregroundColor(.red)
}
}
Section {
Button(action: joinResidence) {
HStack {
Spacer()
if viewModel.isLoading {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
} else {
Text("Join Residence")
.fontWeight(.semibold)
}
Spacer()
}
}
.disabled(shareCode.count != 6 || viewModel.isLoading)
}
}
.navigationTitle("Join Residence")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("Cancel") {
dismiss()
}
.disabled(viewModel.isLoading)
}
}
}
}
private func joinResidence() {
guard shareCode.count == 6 else {
viewModel.errorMessage = "Share code must be 6 characters"
return
}
Task {
// Call the shared ViewModel which uses APILayer
await viewModel.sharedViewModel.joinWithCode(code: shareCode)
// Observe the result
for await state in viewModel.sharedViewModel.joinResidenceState {
if state is ApiResultSuccess<JoinResidenceResponse> {
await MainActor.run {
viewModel.sharedViewModel.resetJoinResidenceState()
onJoined()
dismiss()
}
break
} else if let error = state as? ApiResultError {
await MainActor.run {
viewModel.errorMessage = ErrorMessageParser.parse(error.message)
viewModel.sharedViewModel.resetJoinResidenceState()
}
break
}
}
}
}
}
#Preview {
JoinResidenceView(onJoined: {})
}