SPB Git

Repository Public

ShareView.swift 8.4 KB 169 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/ShareExtension/ShareView.swift3// Description: The share extension's interface — a compact native compose surface (sender, recipient, subject, note, attachments) with4// « Envoyer » and « Enregistrer le brouillon » — and the view controllers that host it on iOS / iPadOS and macOS. No web view.5import SPMailCore6import SPMailSystem7import SwiftUI8#if os(iOS)9import UIKit10#else11import AppKit12#endif1314struct ShareView: View {15    @Bindable var model: ShareModel16    let cancel: () -> Void17    let finish: () -> Void18    @Environment(\.colorScheme) private var scheme19    @FocusState private var focus: Field?20    private enum Field { case to, subject, note }2122    var body: some View {23        VStack(spacing: 0) {24            bar25            Divider()26            content27        }28        .tint(ExtColor.accent(scheme))29        #if os(macOS)30        .frame(width: 440, height: 460)31        #endif32    }3334    private var bar: some View {35        HStack {36            Button("Annuler", action: cancel).keyboardShortcut(.cancelAction)37            Spacer()38            Label("SP Mail", systemImage: "envelope.fill").font(.headline).labelStyle(.titleAndIcon)39            Spacer()40            Button("Envoyer") { Task { await model.send() } }41                .fontWeight(.semibold).disabled(!model.canSend).keyboardShortcut(.defaultAction)42        }43        .padding(.horizontal, 16).padding(.vertical, 12)44    }4546    @ViewBuilder private var content: some View {47        switch model.phase {48        case .loading: ProgressView().frame(maxWidth: .infinity, maxHeight: .infinity)49        case .working(let label): VStack(spacing: 12) { ProgressView(); Text(label).font(.callout).foregroundStyle(.secondary) }.frame(maxWidth: .infinity, maxHeight: .infinity)50        case .done(let message): outcome(symbol: "checkmark.circle.fill", message: message, color: ExtColor.accent(scheme)) { Button("OK", action: finish).buttonStyle(.borderedProminent) }51        case .failed(let message): outcome(symbol: "exclamationmark.triangle.fill", message: message, color: .orange) { Button("Revenir au brouillon") { model.retry() }.buttonStyle(.bordered) }52        case .editing:53            if model.isSignedIn { editor } else {54                outcome(symbol: "person.crop.circle.badge.exclamationmark", message: String(localized: "Ouvrez SP Mail et connectez-vous, puis partagez de nouveau."), color: .secondary) { Button("Fermer", action: cancel).buttonStyle(.bordered) }55            }56        }57    }5859    private func outcome<Actions: View>(symbol: String, message: String, color: Color, @ViewBuilder actions: () -> Actions) -> some View {60        VStack(spacing: 14) {61            Image(systemName: symbol).font(.system(size: 40)).foregroundStyle(color).accessibilityHidden(true)62            Text(message).multilineTextAlignment(.center).font(.callout)63            actions()64        }65        .padding(24).frame(maxWidth: .infinity, maxHeight: .infinity)66    }6768    private var editor: some View {69        ScrollView {70            VStack(alignment: .leading, spacing: 0) {71                if model.mailboxes.count > 1 {72                    row("De") {73                        Picker("De", selection: Binding(get: { model.mailbox?.id ?? "" }, set: { model.mailboxId = $0 })) {74                            ForEach(model.mailboxes) { Text($0.address).tag($0.id) }75                        }76                        .labelsHidden().pickerStyle(.menu)77                    }78                } else if let box = model.mailbox {79                    row("De") { Text(box.address).foregroundStyle(.secondary).lineLimit(1).truncationMode(.middle) }80                }81                row("À") {82                    TextField("adresse@exemple.com", text: $model.to).focused($focus, equals: .to).autocorrectionDisabled().textFieldStyle(.plain)83                        #if os(iOS)84                        .textInputAutocapitalization(.never).keyboardType(.emailAddress).textContentType(.emailAddress)85                        #endif86                }87                row("Objet") { TextField("Objet", text: $model.subject).focused($focus, equals: .subject).textFieldStyle(.plain) }88                TextEditor(text: $model.note).focused($focus, equals: .note).font(.body).frame(minHeight: 110).scrollContentBackground(.hidden).padding(.horizontal, 12).padding(.top, 6)89                    .accessibilityLabel(Text("Note"))90                if !model.items.isEmpty {91                    Divider().padding(.top, 4)92                    ForEach(model.items) { item in93                        HStack(spacing: 8) {94                            Image(systemName: "paperclip").foregroundStyle(.secondary).accessibilityHidden(true)95                            Text(item.name).lineLimit(1).truncationMode(.middle)96                            Spacer(minLength: 6)97                            Text(ByteCountFormatter.string(fromByteCount: Int64(item.size), countStyle: .file)).font(.footnote).foregroundStyle(.secondary)98                            Button { model.remove(item) } label: { Image(systemName: "xmark.circle.fill").foregroundStyle(.tertiary) }.buttonStyle(.plain).accessibilityLabel(Text("Retirer \(item.name)"))99                        }100                        .padding(.horizontal, 16).padding(.vertical, 7)101                    }102                }103                Divider().padding(.top, 4)104                VStack(alignment: .leading, spacing: 8) {105                    if model.tooLarge { Label("Les pièces jointes dépassent 25 Mo.", systemImage: "exclamationmark.triangle").font(.footnote).foregroundStyle(.orange) }106                    if model.appLocked { Label("SP Mail est verrouillé : le brouillon vous attendra dans l’app.", systemImage: "lock.fill").font(.footnote).foregroundStyle(.secondary) }107                    if !model.items.isEmpty, !model.store.shareQueue.acceptsFiles { Text("Avec des pièces jointes, le partage s’envoie directement d’ici.").font(.footnote).foregroundStyle(.secondary) }108                    Button { Task { await model.queue() } } label: { Label("Enregistrer le brouillon dans SP Mail", systemImage: "tray.and.arrow.down") }109                        .buttonStyle(.bordered).disabled(!model.canQueue)110                }111                .padding(16)112            }113        }114        .onAppear { if model.to.isEmpty { focus = .to } }115    }116117    private func row<Field: View>(_ label: LocalizedStringKey, @ViewBuilder field: () -> Field) -> some View {118        VStack(spacing: 0) {119            HStack(spacing: 10) {120                Text(label).foregroundStyle(.secondary).frame(width: 46, alignment: .leading)121                field()122                Spacer(minLength: 0)123            }124            .padding(.horizontal, 16).padding(.vertical, 9)125            Divider().padding(.leading, 16)126        }127    }128}129130#if os(iOS)131/// Principal class of the iOS / iPadOS share extension (NSExtensionPrincipalClass).132final class ShareViewController: UIViewController {133    private let model = ShareModel()134135    override func viewDidLoad() {136        super.viewDidLoad()137        let host = UIHostingController(rootView: ShareView(model: model, cancel: { [weak self] in self?.close(cancelled: true) }, finish: { [weak self] in self?.close(cancelled: false) }))138        addChild(host)139        host.view.frame = view.bounds140        host.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]141        view.addSubview(host.view)142        host.didMove(toParent: self)143        Task { await model.load(extensionContext) }144    }145    private func close(cancelled: Bool) {146        model.cleanUp()147        if cancelled { extensionContext?.cancelRequest(withError: CocoaError(.userCancelled)) } else { extensionContext?.completeRequest(returningItems: nil) }148    }149}150#else151/// Principal class of the macOS share extension (NSExtensionPrincipalClass).152final class ShareViewController: NSViewController {153    private let model = ShareModel()154155    override func loadView() {156        view = NSHostingView(rootView: ShareView(model: model, cancel: { [weak self] in self?.close(cancelled: true) }, finish: { [weak self] in self?.close(cancelled: false) }))157        preferredContentSize = NSSize(width: 440, height: 460)158    }159    override func viewDidLoad() {160        super.viewDidLoad()161        Task { await model.load(extensionContext) }162    }163    private func close(cancelled: Bool) {164        model.cleanUp()165        if cancelled { extensionContext?.cancelRequest(withError: CocoaError(.userCancelled)) } else { extensionContext?.completeRequest(returningItems: nil) }166    }167}168#endif169
Files main
Menu

Theme