This commit is contained in:
Trey t
2025-11-05 10:38:46 -06:00
parent 025fcf677a
commit 2be3a5a3a8
23 changed files with 2837 additions and 124 deletions

View File

@@ -0,0 +1,400 @@
import SwiftUI
import ComposeApp
struct ResidenceDetailView: View {
let residenceId: Int32
@StateObject private var viewModel = ResidenceViewModel()
@State private var residenceWithTasks: ResidenceWithTasks?
@State private var isLoadingTasks = false
@State private var tasksError: String?
@State private var showAddTask = false
var body: some View {
ZStack {
Color(.systemGroupedBackground)
.ignoresSafeArea()
if viewModel.isLoading {
ProgressView()
} else if let error = viewModel.errorMessage {
ErrorView(message: error) {
loadResidenceData()
}
} else if let residence = viewModel.selectedResidence {
ScrollView {
VStack(spacing: 16) {
// Property Header Card
PropertyHeaderCard(residence: residence)
.padding(.horizontal)
.padding(.top)
// Tasks Section
if let residenceWithTasks = residenceWithTasks {
TasksSection(residenceWithTasks: residenceWithTasks)
.padding(.horizontal)
} else if isLoadingTasks {
ProgressView("Loading tasks...")
} else if let tasksError = tasksError {
Text("Error loading tasks: \(tasksError)")
.foregroundColor(.red)
.padding()
}
}
.padding(.bottom)
}
}
}
.navigationTitle("Property Details")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
showAddTask = true
}) {
Image(systemName: "plus")
}
}
}
.sheet(isPresented: $showAddTask) {
AddTaskView(residenceId: residenceId, isPresented: $showAddTask)
}
.onChange(of: showAddTask) { isShowing in
if !isShowing {
// Refresh tasks when sheet is dismissed
loadResidenceWithTasks()
}
}
.onAppear {
loadResidenceData()
}
}
private func loadResidenceData() {
viewModel.getResidence(id: residenceId)
loadResidenceWithTasks()
}
private func loadResidenceWithTasks() {
guard let token = TokenStorage().getToken() else { return }
isLoadingTasks = true
tasksError = nil
let residenceApi = ResidenceApi(client: ApiClient_iosKt.createHttpClient())
residenceApi.getMyResidences(token: token) { result, error in
if let successResult = result as? ApiResultSuccess<MyResidencesResponse> {
if let residence = successResult.data?.residences.first(where: { $0.id == residenceId }) {
self.residenceWithTasks = residence
}
self.isLoadingTasks = false
} else if let errorResult = result as? ApiResultError {
self.tasksError = errorResult.message
self.isLoadingTasks = false
} else if let error = error {
self.tasksError = error.localizedDescription
self.isLoadingTasks = false
}
}
}
}
struct PropertyHeaderCard: View {
let residence: Residence
var body: some View {
VStack(alignment: .leading, spacing: 16) {
HStack {
Image(systemName: "house.fill")
.font(.title2)
.foregroundColor(.blue)
VStack(alignment: .leading, spacing: 4) {
Text(residence.name)
.font(.title2)
.fontWeight(.bold)
Text(residence.propertyType)
.font(.caption)
.foregroundColor(.secondary)
}
Spacer()
}
Divider()
// Address
VStack(alignment: .leading, spacing: 4) {
Label(residence.streetAddress, systemImage: "mappin.circle.fill")
.font(.subheadline)
Text("\(residence.city), \(residence.stateProvince) \(residence.postalCode)")
.font(.subheadline)
.foregroundColor(.secondary)
if !residence.country.isEmpty {
Text(residence.country)
.font(.caption)
.foregroundColor(.secondary)
}
}
// Property Details
if let bedrooms = residence.bedrooms,
let bathrooms = residence.bathrooms {
Divider()
HStack(spacing: 24) {
PropertyDetailItem(icon: "bed.double.fill", value: "\(bedrooms)", label: "Beds")
PropertyDetailItem(icon: "shower.fill", value: String(format: "%.1f", bathrooms), label: "Baths")
if let sqft = residence.squareFootage {
PropertyDetailItem(icon: "square.fill", value: "\(sqft)", label: "Sq Ft")
}
}
}
// if !residence.description.isEmpty {
// Divider()
//
// Text(residence.)
// .font(.body)
// .foregroundColor(.secondary)
// }
}
.padding(20)
.background(Color.blue.opacity(0.1))
.cornerRadius(16)
}
}
struct PropertyDetailItem: View {
let icon: String
let value: String
let label: String
var body: some View {
VStack(spacing: 4) {
Image(systemName: icon)
.font(.caption)
.foregroundColor(.blue)
Text(value)
.font(.subheadline)
.fontWeight(.semibold)
Text(label)
.font(.caption2)
.foregroundColor(.secondary)
}
}
}
struct TasksSection: View {
let residenceWithTasks: ResidenceWithTasks
var body: some View {
VStack(alignment: .leading, spacing: 12) {
HStack {
Text("Tasks")
.font(.title2)
.fontWeight(.bold)
Spacer()
// Task Summary Pills
HStack(spacing: 8) {
TaskPill(count: residenceWithTasks.taskSummary.total, label: "Total", color: .blue)
TaskPill(count: residenceWithTasks.taskSummary.pending, label: "Pending", color: .orange)
TaskPill(count: residenceWithTasks.taskSummary.completed, label: "Done", color: .green)
}
}
if residenceWithTasks.tasks.isEmpty {
EmptyTasksView()
} else {
ForEach(residenceWithTasks.tasks, id: \.id) { task in
TaskCard(task: task)
}
}
}
}
}
struct TaskPill: View {
let count: Int32
let label: String
let color: Color
var body: some View {
HStack(spacing: 4) {
Text("\(count)")
.font(.caption)
.fontWeight(.bold)
Text(label)
.font(.caption2)
}
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(color.opacity(0.2))
.foregroundColor(color)
.cornerRadius(8)
}
}
struct TaskCard: View {
let task: TaskDetail
var body: some View {
VStack(alignment: .leading, spacing: 12) {
HStack {
VStack(alignment: .leading, spacing: 4) {
Text(task.title)
.font(.headline)
.foregroundColor(.primary)
if let status = task.status {
StatusBadge(status: status.name)
}
}
Spacer()
PriorityBadge(priority: task.priority.name)
}
if let description = task.description_, !description.isEmpty {
Text(description)
.font(.subheadline)
.foregroundColor(.secondary)
.lineLimit(2)
}
HStack {
Label(task.frequency.displayName, systemImage: "repeat")
.font(.caption)
.foregroundColor(.secondary)
Spacer()
Label(formatDate(task.dueDate), systemImage: "calendar")
.font(.caption)
.foregroundColor(.secondary)
}
// Completion count
if task.completions.count > 0 {
Divider()
HStack {
Image(systemName: "checkmark.circle")
.foregroundColor(.green)
Text("Completed \(task.completions.count) time\(task.completions.count == 1 ? "" : "s")")
.font(.caption)
.foregroundColor(.secondary)
}
}
}
.padding(16)
.background(Color(.systemBackground))
.cornerRadius(12)
.shadow(color: Color.black.opacity(0.05), radius: 3, x: 0, y: 2)
}
private func formatDate(_ dateString: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
if let date = formatter.date(from: dateString) {
formatter.dateStyle = .medium
return formatter.string(from: date)
}
return dateString
}
}
struct StatusBadge: View {
let status: String
var body: some View {
Text(formatStatus(status))
.font(.caption)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(statusColor.opacity(0.2))
.foregroundColor(statusColor)
.cornerRadius(6)
}
private func formatStatus(_ status: String) -> String {
switch status {
case "in_progress": return "In Progress"
default: return status.capitalized
}
}
private var statusColor: Color {
switch status {
case "completed": return .green
case "in_progress": return .blue
case "pending": return .orange
case "cancelled": return .red
default: return .gray
}
}
}
struct PriorityBadge: View {
let priority: String
var body: some View {
HStack(spacing: 4) {
Image(systemName: "exclamationmark.circle.fill")
.font(.caption2)
Text(priority.capitalized)
.font(.caption)
.fontWeight(.medium)
}
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(priorityColor.opacity(0.2))
.foregroundColor(priorityColor)
.cornerRadius(6)
}
private var priorityColor: Color {
switch priority.lowercased() {
case "high": return .red
case "medium": return .orange
case "low": return .green
default: return .gray
}
}
}
struct EmptyTasksView: View {
var body: some View {
VStack(spacing: 12) {
Image(systemName: "checkmark.circle")
.font(.system(size: 48))
.foregroundColor(.gray.opacity(0.5))
Text("No tasks yet")
.font(.subheadline)
.foregroundColor(.secondary)
}
.frame(maxWidth: .infinity)
.padding(32)
.background(Color(.systemBackground))
.cornerRadius(12)
}
}
#Preview {
NavigationView {
ResidenceDetailView(residenceId: 1)
}
}