SPB Git

Repository Public License

SharedViews.swift 9.2 KB 224 lines swift

1// ORIX Scan2// File: App/Sources/Core/SharedViews.swift3// Description: Views used across features: page thumbnails, document cards and rows, activity overlay, toast.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import ORIXDesign8import ORIXKit9import SwiftUI1011/// A rendered page, loaded asynchronously and cached. Keeps the page's aspect ratio while loading.12struct PageThumbnail: View {13    @Environment(AppModel.self) private var model14    let documentID: UUID15    let page: PageRecord16    var maxDimension = 48017    @State private var image: UIImage?1819    private var aspect: CGFloat {20        let turned = page.edit.quarterTurns % 2 != 021        var w = page.width, h = page.height22        if let quad = page.edit.quad { w *= Double(quad.bounds.width); h *= Double(quad.bounds.height) }23        guard w > 0, h > 0 else { return 0.707 }24        return CGFloat(turned ? h / w : w / h)25    }2627    var body: some View {28        ZStack {29            if let image {30                Image(uiImage: image).resizable().aspectRatio(contentMode: .fit).transition(.opacity)31            } else {32                Rectangle().fill(Color.white).aspectRatio(aspect, contentMode: .fit)33                    .overlay(ProgressView().controlSize(.small))34            }35        }36        .orixPage()37        .task(id: "\(page.id)-\(page.edit.cacheKey)-\(maxDimension)") {38            let loaded = await model.thumbnails.image(for: page, in: documentID, maxDimension: maxDimension)39            withAnimation(ORIX.Motion.quick) { image = loaded }40        }41        .accessibilityHidden(true)42    }43}4445/// First page of a document, for library cards.46struct DocumentThumbnail: View {47    @Environment(AppModel.self) private var model48    let id: UUID49    var maxDimension = 4805051    var body: some View {52        Group {53            if let page = model.manifest(id)?.pages.first {54                PageThumbnail(documentID: id, page: page, maxDimension: maxDimension)55            } else {56                Rectangle().fill(Color.white).aspectRatio(0.707, contentMode: .fit).orixPage()57            }58        }59        .orixScanBeam(active: model.isRecognizing(id))60        .task(id: id) { if model.manifest(id) == nil { await model.load(id) } }61    }62}6364extension DocumentRecord {65    var pagesText: String { pageCount == 1 ? String(localized: "1 page") : String(localized: "\(pageCount) pages") }6667    var subtitle: String {68        "\(modifiedAt.formatted(.relative(presentation: .named))) · \(pagesText)"69    }7071    var accessibilitySummary: String {72        var parts = [title, pagesText, modifiedAt.formatted(date: .abbreviated, time: .shortened)]73        if isFavorite { parts.append(String(localized: "Favourite")) }74        if hasText { parts.append(String(localized: "Text recognized")) }75        return parts.joined(separator: ", ")76    }77}7879/// Grid card: thumbnail on the desk surface, title and metadata underneath.80struct DocumentCard: View {81    @Environment(AppModel.self) private var model82    @Environment(\.orixAccent) private var accent83    let record: DocumentRecord84    var selected: Bool?8586    var body: some View {87        VStack(alignment: .leading, spacing: ORIX.Space.xs) {88            ZStack(alignment: .topTrailing) {89                DocumentThumbnail(id: record.id)90                    .padding(ORIX.Space.sm)91                    .frame(maxWidth: .infinity)92                    .frame(height: 190)93                    .background(ORIX.Palette.desk, in: RoundedRectangle(cornerRadius: ORIX.Radius.control, style: .continuous))94                if let selected {95                    Image(systemName: selected ? "checkmark.circle.fill" : "circle")96                        .font(.title2)97                        .symbolRenderingMode(.palette)98                        .foregroundStyle(selected ? Color.white : ORIX.Palette.textSecondary, selected ? accent.primary : Color.clear)99                        .padding(ORIX.Space.xs)100                } else if record.isFavorite {101                    Image(systemName: "star.fill").font(.caption).foregroundStyle(ORIX.Palette.warning).padding(ORIX.Space.xs)102                }103            }104            VStack(alignment: .leading, spacing: 2) {105                Text(record.title).font(.orixHeadline).foregroundStyle(ORIX.Palette.textPrimary).lineLimit(2)106                HStack(spacing: 4) {107                    if model.isRecognizing(record.id) {108                        Image(systemName: "text.viewfinder").foregroundStyle(accent.secondary)109                    } else if record.hasText {110                        Image(systemName: "text.magnifyingglass").foregroundStyle(accent.primary)111                    }112                    Text(record.subtitle).lineLimit(1)113                }114                .font(.orixCaption)115                .foregroundStyle(ORIX.Palette.textSecondary)116            }117            .padding(.horizontal, 2)118        }119        .contentShape(Rectangle())120        .accessibilityElement(children: .ignore)121        .accessibilityLabel(record.accessibilitySummary)122        .accessibilityAddTraits(selected == true ? [.isButton, .isSelected] : .isButton)123    }124}125126/// List row: small thumbnail, title, snippet.127struct DocumentRow: View {128    @Environment(AppModel.self) private var model129    @Environment(\.orixAccent) private var accent130    let record: DocumentRecord131    var selected: Bool?132133    var body: some View {134        HStack(spacing: ORIX.Space.sm) {135            if let selected {136                Image(systemName: selected ? "checkmark.circle.fill" : "circle")137                    .font(.title3).foregroundStyle(selected ? accent.primary : ORIX.Palette.textTertiary)138            }139            DocumentThumbnail(id: record.id, maxDimension: 240)140                .frame(width: 46, height: 60)141            VStack(alignment: .leading, spacing: 3) {142                HStack(spacing: 4) {143                    Text(record.title).font(.orixHeadline).foregroundStyle(ORIX.Palette.textPrimary).lineLimit(1)144                    if record.isFavorite { Image(systemName: "star.fill").font(.caption2).foregroundStyle(ORIX.Palette.warning) }145                }146                Text(record.subtitle).font(.orixCaption).foregroundStyle(ORIX.Palette.textSecondary).lineLimit(1)147                if !record.snippet.isEmpty {148                    Text(record.snippet).font(.caption).foregroundStyle(ORIX.Palette.textTertiary).lineLimit(1)149                }150            }151            Spacer(minLength: 0)152        }153        .padding(.vertical, 4)154        .contentShape(Rectangle())155        .accessibilityElement(children: .ignore)156        .accessibilityLabel(record.accessibilitySummary)157    }158}159160/// Blocking progress for operations the user started and is waiting on.161struct ActivityOverlay: View {162    let activity: Activity163164    var body: some View {165        ZStack {166            Color.black.opacity(0.35).ignoresSafeArea()167            VStack(spacing: ORIX.Space.md) {168                if let fraction = activity.fraction {169                    ORIXProgressBar(value: fraction).frame(width: 220)170                } else {171                    ProgressView().controlSize(.large)172                }173                VStack(spacing: 4) {174                    Text(activity.title).font(.orixHeadline).foregroundStyle(ORIX.Palette.textPrimary)175                    if !activity.detail.isEmpty { Text(activity.detail).font(.orixCaption).foregroundStyle(ORIX.Palette.textSecondary) }176                }177                if let task = activity.task {178                    Button("Cancel", role: .cancel) { task.cancel() }179                        .buttonStyle(.bordered)180                        .accessibilityIdentifier("activity.cancel")181                }182            }183            .padding(ORIX.Space.xl)184            .background(.regularMaterial, in: RoundedRectangle(cornerRadius: ORIX.Radius.card, style: .continuous))185            .accessibilityElement(children: .contain)186        }187        .transition(.opacity)188    }189}190191struct ToastView: View {192    let text: String193    var body: some View {194        Text(text)195            .font(.orixCallout.weight(.medium))196            .foregroundStyle(.white)197            .padding(.horizontal, ORIX.Space.md)198            .padding(.vertical, ORIX.Space.sm)199            .background(Color.black.opacity(0.82), in: Capsule())200            .padding(.bottom, ORIX.Space.xl)201            .transition(.move(edge: .bottom).combined(with: .opacity))202            .accessibilityAddTraits(.isStaticText)203    }204}205206/// Document context menu shared by cards, rows and the document screen.207struct DocumentMenuItems: View {208    @Environment(AppModel.self) private var model209    let record: DocumentRecord210    var onRename: () -> Void211    var onMove: () -> Void212213    var body: some View {214        Button { model.index.setFavorite([record.id], !record.isFavorite) } label: {215            Label(record.isFavorite ? "Remove from Favourites" : "Add to Favourites", systemImage: record.isFavorite ? "star.slash" : "star")216        }217        Button(action: onRename) { Label("Rename", systemImage: "pencil") }218        Button(action: onMove) { Label("Move to Folder", systemImage: "folder") }219        Button { Task { await model.duplicate(record.id) } } label: { Label("Duplicate", systemImage: "plus.square.on.square") }220        Divider()221        Button(role: .destructive) { model.moveToTrash([record.id]) } label: { Label("Delete", systemImage: "trash") }222    }223}224
Files main
Menu

Theme