SPB Git

Repository Public

ReportsView.swift 11.7 KB 242 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Immo-Ka Admin iOS9// File             : ImmoKaAdmin/Features/ReportsView.swift10// Description      : signalements de la messagerie — liste (ouverts, ou tous),11//                    qui a signalé et pourquoi, fil des derniers messages, «12//                    Classer » ou « Bloquer la conversation », avec13//                    confirmation. Les messages sont du texte brut.14// Module           : ImmoKaAdmin15// Repository       : https://git.spboucher.ai/immo-ka-admin-ios16// Project Website  : https://www.immo-ka.com17// Created          : 2026-11-3018// Last Modified    : 2026-11-3019//20// =============================================================================21// AUTHOR & PROFESSIONAL AFFILIATION22// =============================================================================23//24// Author           : Simon-Pierre Boucher25// Position         : Professor of Finance26// Institution      : Université du Québec en Outaouais (UQO)27// Department       : Département des sciences administratives28// Organization     : SP Boucher29//30// Website          : https://spboucher.ai31// Git              : https://git.spboucher.ai32//33// =============================================================================34// OFFICIAL CONTACT DIRECTORY35// =============================================================================36//37// General          : info@spboucher.ai38// Professional     : contact@spboucher.ai39// Direct           : simon@spboucher.ai40// Research         : research@spboucher.ai41// Development      : dev@spboucher.ai42// Support          : support@spboucher.ai43// Security/Admin   : admin@spboucher.ai44// Notifications    : notifications@spboucher.ai45//46// Preferred Contact: support@spboucher.ai47//48// =============================================================================49// COPYRIGHT & INTELLECTUAL PROPERTY50// =============================================================================51//52// Copyright (c) 202653// Simon-Pierre Boucher54//55// All rights reserved where applicable.56//57// This file is governed by the applicable project license,58// copyright notices, institutional agreements and relevant law.59//60// Unless expressly authorized by the applicable license or61// rights holder, copying, redistribution, sublicensing,62// publication and commercial exploitation are not permitted.63//64// Existing third-party rights and notices remain unaffected.65//66// =============================================================================67// CONFIDENTIALITY & SECURITY68// =============================================================================69//70// Classification   : Public71// Distribution     : Public repository; use is governed by the project license72// Security Contact : admin@spboucher.ai73//74// Access to this source does not independently grant permission75// to redistribute or disclose confidential materials.76//77// Do not embed credentials, private keys, access tokens,78// certificates or confidential personal information.79//80// =============================================================================81// LICENSE & MAINTENANCE82// =============================================================================83//84// License          : None specified (no license file in this repository)85// Maintainer       : Simon-Pierre Boucher86// Technical Contact: dev@spboucher.ai87//88// =============================================================================89// END OF SP BOUCHER SOURCE HEADER90// =============================================================================9192import SwiftUI9394struct ReportsView: View {95    @Environment(AdminSession.self) private var session96    @State private var all = false97    @State private var items: [Report]?98    @State private var failure: String?99100    var body: some View {101        VStack(spacing: 0) {102            Picker("Signalements affichés", selection: $all) { Text("Ouverts").tag(false); Text("Tous").tag(true) }103                .pickerStyle(.segmented)104                .padding(.horizontal, 16).padding(.vertical, 8)105                .accessibilityIdentifier("reports.filter")106            Divider().overlay(Theme.line)107            Loadable(value: items, failure: failure, retry: { Task { await load() } }) { items in108                List {109                    if items.isEmpty { EmptyState(symbol: "checkmark.circle", title: all ? "Aucun signalement" : "Aucun signalement ouvert").listRowBackground(Color.clear) }110                    ForEach(items) { r in111                        NavigationLink(value: r) { ReportRow(report: r) }.accessibilityIdentifier("reports.row")112                    }113                }114                .listStyle(.plain)115                .refreshable { await load() }116            }117        }118        .background(Theme.bg)119        .navigationTitle("Signalements")120        .navigationBarTitleDisplayMode(.inline)121        .navigationDestination(for: Report.self) { r in122            ReportDetailView(report: r) { Task { await load() } }123        }124        .task(id: all) { items = nil; await load() }125    }126127    private func load() async {128        do {129            items = try await session.call { try await $0.reports(all: all) }130            failure = nil131            try? await session.load()132        } catch {133            if items == nil { failure = AdminError.message(error) }134        }135    }136}137138private struct ReportRow: View {139    let report: Report140    var body: some View {141        VStack(alignment: .leading, spacing: 5) {142            HStack(alignment: .firstTextBaseline) {143                Text(verbatim: report.reason.isEmpty ? "Sans motif" : report.reason).font(.subheadline.weight(.semibold)).foregroundStyle(Theme.ink).lineLimit(2)144                Spacer(minLength: 8)145                if report.isOpen { StatusTag(label: "Ouvert", symbol: "flag", tone: .warn) }146                else { StatusTag(label: report.decisionLabel ?? "Traité", symbol: "checkmark", tone: .neutral) }147            }148            Text(verbatim: "Signalé par \(report.reporter)\(report.brokerName.map { " · \($0)" } ?? "")").font(.footnote).foregroundStyle(Theme.ink2).lineLimit(2)149            Text(verbatim: "nº \(report.id) · \(Fmt.dateTime(report.createdAt))\(report.conversationGone ? " · conversation effacée" : "")").font(.caption).foregroundStyle(Theme.ink3)150        }151        .padding(.vertical, 3)152        .accessibilityElement(children: .combine)153    }154}155156struct ReportDetailView: View {157    let report: Report158    var changed: () -> Void159    @Environment(AdminSession.self) private var session160    @Environment(Navigator.self) private var nav161    @Environment(\.dismiss) private var dismiss162    @State private var confirming: String?163    @State private var failure: String?164    @State private var busy = false165166    var body: some View {167        Form {168            if let failure { Section { ErrorBanner(text: failure).listRowInsets(EdgeInsets()) } }169            Section("Signalement") {170                VStack(alignment: .leading, spacing: 3) {171                    Text("Motif").font(.caption).foregroundStyle(Theme.ink3)172                    PlainText(text: report.reason.isEmpty ? "Sans motif" : report.reason, color: Theme.ink)173                }174                InfoRow(label: "Signalé par", value: report.reporter)175                InfoRow(label: "Le", value: Fmt.dateTime(report.createdAt))176                if !report.isOpen { InfoRow(label: "Traité", value: "\(report.decisionLabel ?? "oui") · \(Fmt.dateTime(report.handledAt))\(report.handledBy.map { "\npar \($0)" } ?? "")") }177            }178            Section("Conversation") {179                if let m = report.brokerName { InfoRow(label: "Courtier", value: m) }180                if let t = report.tenantName { InfoRow(label: "Acheteur", value: t) }181                if let e = report.tenantEmail { InfoRow(label: "Courriel de l'acheteur", value: e) }182                if let s = report.conversationStatus { InfoRow(label: "État", value: s == "ouverte" ? "ouverte" : s == "fermee" ? "fermée" : s == "bloquee" ? "bloquée" : s) }183                if report.conversationGone {184                    Label("La conversation a été effacée par l'acheteur : il ne reste que le motif du signalement.", systemImage: "trash").font(.subheadline).foregroundStyle(Theme.ink2)185                }186            }187            if !report.messages.isEmpty {188                Section("Derniers messages") {189                    ForEach(report.messages) { m in190                        VStack(alignment: .leading, spacing: 3) {191                            HStack(alignment: .firstTextBaseline) {192                                Text(verbatim: "\(role(m.role)) · \(m.author)").font(.caption.weight(.semibold)).foregroundStyle(Theme.ink2)193                                if m.id == report.messageID { StatusTag(label: "Message signalé", symbol: "flag.fill", tone: .warn) }194                                Spacer(minLength: 6)195                                Text(verbatim: Fmt.dateTime(m.createdAt / 1000)).font(.caption2).monospacedDigit().foregroundStyle(Theme.ink3)196                            }197                            PlainText(text: m.text, color: Theme.ink)198                        }199                        .padding(.vertical, 2)200                        .accessibilityElement(children: .combine)201                    }202                }203            }204            if report.isOpen {205                Section {206                    Button { confirming = "classer" } label: { Label("Classer le signalement", systemImage: "archivebox") }207                        .disabled(busy).accessibilityIdentifier("report.dismiss")208                    Button(role: .destructive) { confirming = "bloquer" } label: { Label("Bloquer la conversation", systemImage: "hand.raised") }209                        .disabled(busy).accessibilityIdentifier("report.block")210                } header: {211                    Text("Décision")212                } footer: {213                    Text("Classer : rien ne change pour les participants. Bloquer : plus personne ne peut écrire dans cette conversation ; les deux le voient.")214                }215            }216        }217        .navigationTitle("Signalement nº \(report.id)")218        .navigationBarTitleDisplayMode(.inline)219        .alert(confirming == "bloquer" ? "Bloquer cette conversation ?" : "Classer ce signalement ?", isPresented: Binding(get: { confirming != nil }, set: { if !$0 { confirming = nil } }), presenting: confirming) { decision in220            Button(decision == "bloquer" ? "Bloquer" : "Classer", role: decision == "bloquer" ? .destructive : nil) { decide(decision) }221            Button("Annuler", role: .cancel) {}222        } message: { decision in223            Text(decision == "bloquer" ? "Plus personne ne pourra y écrire. La décision est consignée au journal." : "Le signalement sort de la liste des signalements ouverts. La décision est consignée au journal.")224        }225    }226227    private func role(_ r: String) -> String { r == "acheteur" ? "Acheteur" : r == "courtier" ? "Courtier" : r == "systeme" ? "Réponse automatique" : r }228229    private func decide(_ decision: String) {230        busy = true; failure = nil231        Task {232            defer { busy = false }233            do {234                try await session.call { try await $0.decideReport(report.id, decision: decision) }235                nav.show(decision == "bloquer" ? "Conversation bloquée" : "Signalement classé")236                changed()237                dismiss()238            } catch { failure = AdminError.message(error) }239        }240    }241}242
Files main
Menu

Theme