SPB Git

Repository Public

InboxScreen.swift 9.9 KB 195 lines swift

1// SP Notes iOS2// File: Features/Inbox/InboxScreen.swift3// Description: The inbox: the captures waiting to be sorted, each with its actions — file in a section, turn into a task,4//              archive, delete — and an undo after each. Says where the list comes from (server, last copy, this device) and5//              when an action waits for the network.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import SPNotesCore10import SPNotesDesign11import SwiftUI1213struct InboxReloadKey: Equatable { var lists: Int; var queue: Int; var cache: Int; var online: Bool; var session: SessionState }1415struct InboxScreen: View {16    @Environment(AppModel.self) private var app17    @Environment(ShellState.self) private var shell18    @State private var model: InboxModel?1920    var body: some View {21        Group {22            if let model { content(model) } else { ProgressView() }23        }24        .background(NVColor.bg)25        .navigationTitle("Boîte de réception")26        .navigationBarTitleDisplayMode(.inline)27        .task { if model == nil { model = InboxModel(app: app) } }28        .task(id: InboxReloadKey(lists: app.feed.lists, queue: app.feed.queue, cache: app.feed.cache, online: app.network.isOnline, session: app.session)) { await model?.load() }29    }3031    @ViewBuilder private func content(_ model: InboxModel) -> some View {32        List {33            Section {34                VStack(alignment: .leading, spacing: NVSpace.xs) {35                    Text("Boîte de réception").nvFont(.h1).foregroundStyle(NVColor.fg).accessibilityAddTraits(.isHeader)36                    Text(InboxText.summary(count: model.entries.count)).font(.subheadline).foregroundStyle(NVColor.fgMuted)37                    if let origin = InboxText.origin(model.origin, signedIn: app.session == .signedIn) {38                        Label(origin, systemImage: "clock.arrow.circlepath").font(.footnote).foregroundStyle(NVColor.fgMuted)39                            .accessibilityIdentifier("inbox-origin")40                    }41                }42                .frame(maxWidth: .infinity, alignment: .leading)43                .padding(.top, NVSpace.sm)44            }45            .listRowSeparator(.hidden)46            .listRowBackground(Color.clear)4748            if model.isLoaded, model.entries.isEmpty {49                Section {50                    NVEmptyState(systemImage: "tray", title: String(localized: "Boîte de réception vide"),51                                 message: String(localized: "Les captures rapides et ce que tu partages vers SP Notes arrivent ici, en attendant d’être classées."))52                        .frame(maxWidth: .infinity)53                        .accessibilityIdentifier("inbox-empty")54                }55                .listRowBackground(Color.clear)56                .listRowSeparator(.hidden)57            } else {58                Section {59                    ForEach(model.entries) { entry in row(entry, model: model) }60                }61            }62        }63        .listStyle(.plain)64        .listSectionSpacing(.compact)65        .environment(\.defaultMinListHeaderHeight, 0)66        .scrollContentBackground(.hidden)67        .refreshable { await app.syncAndWait(); await model.load() }68        .toolbar {69            ToolbarItem(placement: .topBarTrailing) {70                Menu {71                    // the 1.0 list of the inbox (multiple selection, sort, move, tags…) stays one tap away72                    Button { shell.open(source: .smart(.inbox)) } label: { Label("Afficher en liste de notes", systemImage: "list.bullet") }73                    Button { Task { await app.syncAndWait(); await model.load() } } label: { Label("Synchroniser maintenant", systemImage: "arrow.triangle.2.circlepath") }74                } label: { Label("Options de la boîte de réception", systemImage: "ellipsis.circle") }75                .accessibilityIdentifier("inbox-options")76            }77        }78        .onChange(of: model.notice) { _, notice in79            guard let notice else { return }80            shell.show(notice)81            model.notice = nil82        }83        .accessibilityIdentifier("inbox-screen")84    }8586    private func row(_ entry: InboxEntry, model: InboxModel) -> some View {87        HStack(alignment: .top, spacing: NVSpace.sm) {88            Button { if entry.isOnDevice { shell.open(note: entry.id) } } label: {89                VStack(alignment: .leading, spacing: NVSpace.xs) {90                    Text(NoteTitle.display(entry.title)).font(.headline).foregroundStyle(NVColor.fg).lineLimit(2).multilineTextAlignment(.leading)91                    if !entry.excerpt.isEmpty { Text(entry.excerpt).font(.subheadline).foregroundStyle(NVColor.fgMuted).lineLimit(2).multilineTextAlignment(.leading) }92                    ViewThatFits(in: .horizontal) {93                        HStack(spacing: NVSpace.sm) { meta(entry) }94                        VStack(alignment: .leading, spacing: NVSpace.xs) { meta(entry) }95                    }96                }97                .frame(maxWidth: .infinity, alignment: .leading)98                .padding(.vertical, NVSpace.xs)99                .contentShape(Rectangle())100            }101            .buttonStyle(.plain)102            .accessibilityElement(children: .combine)103            .accessibilityHint(entry.isOnDevice ? Text("Ouvre la capture") : Text("Disponible après la synchronisation"))104            .accessibilityIdentifier("inbox-row")105106            Menu { actions(entry, model: model) } label: {107                Image(systemName: "tray.and.arrow.down").font(.body.weight(.medium)).foregroundStyle(NVColor.accent)108                    .frame(width: 44, height: 44).contentShape(Rectangle())109            }110            .accessibilityLabel("Trier \(NoteTitle.display(entry.title))")111            .accessibilityIdentifier("inbox-triage")112        }113        .listRowBackground(NVColor.bg)114        .swipeActions(edge: .leading, allowsFullSwipe: true) {115            Button { run(.file(section: "notes"), entry, model) } label: { Label("Classer", systemImage: "tray.and.arrow.down") }.tint(NVColor.success)116            Button { run(.task, entry, model) } label: { Label("Tâche", systemImage: "checklist") }.tint(NVColor.accent)117        }118        .swipeActions(edge: .trailing, allowsFullSwipe: true) {119            Button(role: .destructive) { run(.trash, entry, model) } label: { Label("Supprimer", systemImage: "trash") }120            Button { run(.archive, entry, model) } label: { Label("Archiver", systemImage: "archivebox") }.tint(.gray)121        }122        .contextMenu {123            if entry.isOnDevice { Button { shell.open(note: entry.id) } label: { Label("Ouvrir", systemImage: "note.text") } }124            actions(entry, model: model)125        }126    }127128    @ViewBuilder private func meta(_ entry: InboxEntry) -> some View {129        if let date = entry.date { NVChip(date.listLabel, systemImage: "clock") }130        if let kind = entry.kind, let label = InboxText.kindLabel(kind) { NVChip(label.title, systemImage: label.symbol) }131        if entry.isDeviceOnly || !entry.isOnDevice {132            NVChip(entry.isOnDevice ? String(localized: "Sur cet appareil") : String(localized: "Sur le serveur"), systemImage: entry.isOnDevice ? "iphone" : "icloud")133        }134    }135136    @ViewBuilder private func actions(_ entry: InboxEntry, model: InboxModel) -> some View {137        Section("Classer dans") {138            ForEach(NoteSection.editable, id: \.self) { section in139                Button { run(.file(section: section), entry, model) } label: { Label(NoteSection.title(section), systemImage: NoteSection.symbol(section)) }140            }141        }142        Button { run(.task, entry, model) } label: { Label("Convertir en tâche", systemImage: "checklist") }143        Button { run(.archive, entry, model) } label: { Label("Archiver", systemImage: "archivebox") }144        Button(role: .destructive) { run(.trash, entry, model) } label: { Label("Supprimer", systemImage: "trash") }145    }146147    private func run(_ triage: InboxTriage, _ entry: InboxEntry, _ model: InboxModel) {148        Task {149            guard let outcome = await model.perform(triage, on: entry) else { return }150            Haptics.success()151            if triage == .trash || triage == .archive { shell.close(note: entry.id) }152            if let undo = outcome.undo {153                shell.show(outcome.message, actionTitle: String(localized: "Annuler")) { Task { await model.undo(undo) } }154            } else {155                shell.show(outcome.message)156            }157        }158    }159}160161/// Wording of the inbox (pure, tested).162enum InboxText {163    static func summary(count: Int) -> String {164        switch count {165        case 0: return String(localized: "Rien à trier.")166        case 1: return String(localized: "1 capture à trier")167        default: return String(localized: "\(count) captures à trier")168        }169    }170171    /// Says where the list comes from when it is not the server's current one.172    static func origin(_ origin: InboxOrigin, signedIn: Bool, now: Date = Date()) -> String? {173        switch origin {174        case .server: return nil175        case .cache(let date):176            let formatter = RelativeDateTimeFormatter()177            formatter.unitsStyle = .full178            return String(localized: "Liste du serveur mise à jour \(formatter.localizedString(for: date, relativeTo: now)), plus les captures de cet appareil")179        case .device: return signedIn ? String(localized: "Hors ligne : les captures de cet appareil") : String(localized: "Les captures de cet appareil")180        }181    }182183    static func kindLabel(_ kind: String) -> (title: String, symbol: String)? {184        switch kind {185        case "link": return (String(localized: "Lien"), "link")186        case "photo", "screenshot": return (String(localized: "Image"), "photo")187        case "file", "document": return (String(localized: "Fichier"), "doc")188        case "audio": return (String(localized: "Audio"), "waveform")189        case "tasks": return (String(localized: "Tâches"), "checklist")190        case "selection": return (String(localized: "Sélection"), "text.quote")191        default: return nil192        }193    }194}195
Files main
Menu

Theme