107 lines
3.5 KiB
Swift
107 lines
3.5 KiB
Swift
import SwiftUI
|
|
import ComposeApp
|
|
|
|
struct JoinResidenceView: View {
|
|
@Environment(\.dismiss) private var dismiss
|
|
let onJoined: () -> Void
|
|
|
|
@State private var shareCode: String = ""
|
|
@State private var isJoining = false
|
|
@State private var errorMessage: String?
|
|
|
|
private let residenceApi = ResidenceApi(client: ApiClient_iosKt.createHttpClient())
|
|
|
|
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()
|
|
errorMessage = nil
|
|
}
|
|
.disabled(isJoining)
|
|
} header: {
|
|
Text("Enter Share Code")
|
|
} footer: {
|
|
Text("Enter the 6-character code shared with you to join a residence")
|
|
.foregroundColor(.secondary)
|
|
}
|
|
|
|
if let error = errorMessage {
|
|
Section {
|
|
Text(error)
|
|
.foregroundColor(.red)
|
|
}
|
|
}
|
|
|
|
Section {
|
|
Button(action: joinResidence) {
|
|
HStack {
|
|
Spacer()
|
|
if isJoining {
|
|
ProgressView()
|
|
.progressViewStyle(CircularProgressViewStyle())
|
|
} else {
|
|
Text("Join Residence")
|
|
.fontWeight(.semibold)
|
|
}
|
|
Spacer()
|
|
}
|
|
}
|
|
.disabled(shareCode.count != 6 || isJoining)
|
|
}
|
|
}
|
|
.navigationTitle("Join Residence")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .navigationBarLeading) {
|
|
Button("Cancel") {
|
|
dismiss()
|
|
}
|
|
.disabled(isJoining)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func joinResidence() {
|
|
guard shareCode.count == 6 else {
|
|
errorMessage = "Share code must be 6 characters"
|
|
return
|
|
}
|
|
|
|
guard let token = TokenStorage.shared.getToken() else {
|
|
errorMessage = "Not authenticated"
|
|
return
|
|
}
|
|
|
|
isJoining = true
|
|
errorMessage = nil
|
|
|
|
residenceApi.joinWithCode(token: token, code: shareCode) { result, error in
|
|
if result is ApiResultSuccess<JoinResidenceResponse> {
|
|
self.isJoining = false
|
|
self.onJoined()
|
|
self.dismiss()
|
|
} else if let errorResult = result as? ApiResultError {
|
|
self.errorMessage = errorResult.message
|
|
self.isJoining = false
|
|
} else if let error = error {
|
|
self.errorMessage = error.localizedDescription
|
|
self.isJoining = false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
JoinResidenceView(onJoined: {})
|
|
}
|