SPB Git

Repository Public

ItemViews.swift 7.4 KB 126 lines swift

1// SP Drive iOS2// File: Features/MyDrive/ItemViews.swift3// Description: A file or folder as a list row and as a grid cell: name, type, size, date, sync state, offline availability, sharing mark.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI89extension DriveItem {10    /// One line describing the item for assistive technologies and the details header: family, size or content, date.11    @MainActor func summary(model: AppModel, showLocation: Bool = false) -> String {12        var parts: [String] = []13        if showLocation, let parent = parentID.flatMap(model.store.item) { parts.append(parent.parentID == nil ? String(localized: "Mon Drive") : parent.name) }14        if isFolder {15            _ = model.itemsVersion16            let n = model.store.childCount(of: id)17            parts.append(n == 0 ? String(localized: "Vide") : n == 1 ? String(localized: "1 élément") : String(localized: "\(n) éléments"))18        } else {19            parts.append(size.byteString)20        }21        parts.append(updatedAt.shortRelative)22        return parts.joined(separator: " · ")23    }24}2526/// A file or folder as a list row. Thumb-friendly: the whole row is the tap target, the « … » button opens the actions sheet,27/// and at accessibility text sizes the row reflows instead of truncating the name.28struct ItemRowView: View {29    @Environment(AppModel.self) private var model30    @Environment(\.dynamicTypeSize) private var typeSize31    let item: DriveItem32    var showLocation = false33    /// When set, a « more » button is shown at the end of the row.34    var onMore: (() -> Void)?3536    var body: some View {37        let badge = model.badge(item)38        HStack(spacing: SPDTheme.Space.md) {39            HStack(spacing: SPDTheme.Space.md) {40                ItemThumbnail(item: item, size: typeSize.isAccessibilitySize ? 56 : 46).zoomSource(item.id)41                VStack(alignment: .leading, spacing: 3) {42                    HStack(alignment: .firstTextBaseline, spacing: 5) {43                        // the name is the primary content: it wraps at large text sizes instead of being cut44                        Text(item.name).font(SPDTheme.Typo.body).lineLimit(typeSize.isAccessibilitySize ? nil : typeSize >= .xLarge ? 3 : 1).truncationMode(.middle)45                            .fixedSize(horizontal: false, vertical: true)46                        if item.starred { Image(systemName: "star.fill").font(.caption2).foregroundStyle(.yellow).accessibilityHidden(true) }47                    }48                    Text(item.summary(model: model, showLocation: showLocation)).font(SPDTheme.Typo.caption).foregroundStyle(.secondary)49                        .lineLimit(typeSize.isAccessibilitySize ? 3 : 1)50                    if typeSize.isAccessibilitySize, badge != .none { Label { Text(badge.spokenLabel) } icon: { SyncBadgeView(badge: badge) }.font(SPDTheme.Typo.caption).foregroundStyle(.secondary) }51                }52                Spacer(minLength: 4)53                if item.isInfected { Image(systemName: "exclamationmark.shield.fill").foregroundStyle(SPDTheme.Palette.critical).accessibilityHidden(true) }54                if !typeSize.isAccessibilitySize { SyncBadgeView(badge: badge).accessibilityHidden(true) }55            }56            .contentShape(Rectangle())57            .accessibilityElement(children: .ignore)58            .accessibilityLabel(item.name)59            .accessibilityValue(accessibilityValue(badge))60            .accessibilityAddTraits(.isButton)61            .accessibilityIdentifier("item.\(item.name)")62            if let onMore {63                Button(action: onMore) {64                    Image(systemName: "ellipsis").font(.body.weight(.semibold)).foregroundStyle(.secondary).frame(width: 36, height: 44).contentShape(Rectangle())65                }66                .buttonStyle(.borderless)67                .hoverEffect(.highlight)68                .accessibilityLabel(Text("Actions pour \(item.name)"))69                .accessibilityIdentifier("more.\(item.name)")70            }71        }72        .padding(.vertical, 3)73    }7475    private func accessibilityValue(_ badge: SyncBadge) -> String {76        var parts = [item.kind.label, item.summary(model: model, showLocation: showLocation)]77        if item.starred { parts.append(String(localized: "Favori")) }78        if item.isInfected { parts.append(String(localized: "Signalé par l’antivirus")) }79        parts.append(badge.spokenLabel)80        return parts.joined(separator: ", ")81    }82}8384/// A file or folder as a card: a real preview when there is one, the name on two lines, the state in a corner.85struct ItemGridCell: View {86    @Environment(AppModel.self) private var model87    @Environment(\.dynamicTypeSize) private var typeSize88    let item: DriveItem89    var selected = false90    var selecting = false9192    var body: some View {93        let badge = model.badge(item)94        VStack(alignment: .leading, spacing: 0) {95            ItemPreviewTile(item: item)96                .aspectRatio(4 / 3, contentMode: .fit)97                .overlay(alignment: .topLeading) { if item.starred { Image(systemName: "star.fill").font(.caption).foregroundStyle(.yellow).padding(6).background(.ultraThinMaterial, in: Circle()).padding(6).accessibilityHidden(true) } }98                .overlay(alignment: .topTrailing) {99                    if selecting {100                        Image(systemName: selected ? "checkmark.circle.fill" : "circle").font(.title3).symbolRenderingMode(.palette)101                            .foregroundStyle(selected ? Color.white : Color.secondary, selected ? SPDTheme.Palette.brand : Color.clear).padding(6).accessibilityHidden(true)102                    }103                }104                .overlay(alignment: .bottomTrailing) { if badge != .none { SyncBadgeView(badge: badge).padding(5).background(.regularMaterial, in: Circle()).padding(6).accessibilityHidden(true) } }105            VStack(alignment: .leading, spacing: 2) {106                Text(item.name).font(.footnote.weight(.medium)).lineLimit(typeSize.isAccessibilitySize ? nil : typeSize >= .xLarge ? 4 : 3).multilineTextAlignment(.leading).fixedSize(horizontal: false, vertical: true)107                Text(item.isFolder ? item.summary(model: model) : "\(item.size.byteString) · \(item.updatedAt.shortRelative)").font(SPDTheme.Typo.eyebrow).foregroundStyle(.secondary).lineLimit(typeSize.isAccessibilitySize ? nil : typeSize >= .xLarge ? 2 : 1).fixedSize(horizontal: false, vertical: true)108            }109            .padding(.horizontal, SPDTheme.Space.sm + 2).padding(.vertical, SPDTheme.Space.sm)110            .frame(maxWidth: .infinity, alignment: .leading)111        }112        .spdCard(radius: SPDTheme.Radius.lg, elevation: .e1)113        .overlay(RoundedRectangle(cornerRadius: SPDTheme.Radius.lg, style: .continuous).strokeBorder(selected ? SPDTheme.Palette.brand : .clear, lineWidth: 2.5))114        .clipShape(RoundedRectangle(cornerRadius: SPDTheme.Radius.lg, style: .continuous))115        .contentShape(.rect(cornerRadius: SPDTheme.Radius.lg))116        .contentShape(.hoverEffect, .rect(cornerRadius: SPDTheme.Radius.lg))117        .hoverEffect(.lift)118        .zoomSource(item.id)119        .accessibilityElement(children: .ignore)120        .accessibilityLabel(item.name)121        .accessibilityValue([item.kind.label, item.summary(model: model), badge.spokenLabel].joined(separator: ", "))122        .accessibilityAddTraits(selected ? [.isButton, .isSelected] : .isButton)123        .accessibilityIdentifier("item.\(item.name)")124    }125}126
Files main
Menu

Theme