This commit is contained in:
Trey t
2025-11-08 10:52:28 -06:00
parent 364f98a303
commit 97eed0eee9
6 changed files with 381 additions and 1 deletions

View File

@@ -0,0 +1,106 @@
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: {})
}

View File

@@ -4,6 +4,7 @@ import ComposeApp
struct ResidencesListView: View {
@StateObject private var viewModel = ResidenceViewModel()
@State private var showingAddResidence = false
@State private var showingJoinResidence = false
var body: some View {
ZStack {
@@ -54,7 +55,13 @@ struct ResidencesListView: View {
.navigationTitle("My Properties")
.navigationBarTitleDisplayMode(.large)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
ToolbarItemGroup(placement: .navigationBarTrailing) {
Button(action: {
showingJoinResidence = true
}) {
Image(systemName: "person.badge.plus")
}
Button(action: {
showingAddResidence = true
}) {
@@ -65,6 +72,11 @@ struct ResidencesListView: View {
.sheet(isPresented: $showingAddResidence) {
AddResidenceView(isPresented: $showingAddResidence)
}
.sheet(isPresented: $showingJoinResidence) {
JoinResidenceView(onJoined: {
viewModel.loadMyResidences()
})
}
.onAppear {
viewModel.loadMyResidences()
}