SPB Git

Repository Public

NoteRow.swift 5.3 KB 125 lines swift

1// SP Notes iOS2// File: Components/NoteRow.swift3// Description: One note in a list: title, date and preview, the folder when the list mixes places, and discreet marks4//              (pinned, favourite, and the sync state only when the note is not synchronised).5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SPNotesCore9import SPNotesStore10import SwiftUI1112struct NoteRow: View {13    let note: PageSummary14    var folderName: String?15    /// Search results pass the title and excerpt with the matches marked.16    var highlightedTitle: String?17    var highlightedSnippet: String?18    /// The date shown; the trash shows the date of deletion, the inbox the date of capture.19    var date: Date?2021    var body: some View {22        VStack(alignment: .leading, spacing: 4) {23            HStack(alignment: .firstTextBaseline, spacing: 6) {24                titleText25                    .font(.headline)26                    .foregroundStyle(Color.spText)27                    .lineLimit(2)28                Spacer(minLength: 4)29                marks30            }31            (Text((date ?? note.updatedAt).listLabel).foregroundStyle(Color.spText.opacity(0.75)) + Text("  ") + previewText.foregroundStyle(Color.spTextSecondary))32                .font(.subheadline)33                .lineLimit(2)34            if folderName != nil || !note.tags.isEmpty {35                HStack(spacing: 10) {36                    if let folderName {37                        (Text(Image(systemName: "folder")) + Text(" ") + Text(folderName)).lineLimit(1)38                    }39                    if !note.tags.isEmpty {40                        Text(note.tags.prefix(3).map { "#\($0)" }.joined(separator: " ")).lineLimit(1)41                    }42                }43                .font(.footnote)44                .foregroundStyle(Color.spMuted)45            }46        }47        .padding(.vertical, 4)48        .frame(maxWidth: .infinity, alignment: .leading)49        .contentShape(Rectangle())50        .accessibilityElement(children: .ignore)51        .accessibilityLabel(accessibilityText)52    }5354    private var titleText: Text {55        if note.isFolder { return Text(Image(systemName: "folder")) + Text(" ") + Text(AppModel.displayTitle(note.title)) }56        if let highlightedTitle, !highlightedTitle.isEmpty { return HighlightedText.make(highlightedTitle, untitled: note.title) }57        return Text(AppModel.displayTitle(note.title))58    }5960    private var previewText: Text {61        if let highlightedSnippet, !highlightedSnippet.isEmpty { return HighlightedText.make(highlightedSnippet) }62        if note.isFolder { return Text("Dossier") }63        return Text(note.preview.isEmpty ? String(localized: "Aucun texte") : note.preview)64    }6566    @ViewBuilder private var marks: some View {67        HStack(spacing: 6) {68            if note.pinned { Image(systemName: "pin.fill").foregroundStyle(Color.accentColor) }69            if note.favorite { Image(systemName: "star.fill").foregroundStyle(Color.spWarning) }70            SyncBadge(state: note.syncState)71        }72        .font(.caption)73        .accessibilityHidden(true)74    }7576    private var accessibilityText: String {77        var parts = [AppModel.displayTitle(note.title)]78        if note.isFolder { parts.append(String(localized: "dossier")) }79        parts.append((date ?? note.updatedAt).listLabel)80        if !note.preview.isEmpty { parts.append(note.preview) }81        if let folderName { parts.append(String(localized: "dans \(folderName)")) }82        if note.pinned { parts.append(String(localized: "épinglée")) }83        if note.favorite { parts.append(String(localized: "favori")) }84        if let state = SyncBadge.label(note.syncState) { parts.append(state) }85        return parts.joined(separator: ", ")86    }87}8889/// A small mark shown only when a note is not synchronised: waiting, in conflict, or refused.90struct SyncBadge: View {91    let state: SyncState9293    var body: some View {94        switch state {95        case .synced: EmptyView()96        case .pending: Image(systemName: "arrow.triangle.2.circlepath").foregroundStyle(Color.spMuted)97        case .conflict: Image(systemName: "exclamationmark.triangle.fill").foregroundStyle(Color.spWarning)98        case .error: Image(systemName: "exclamationmark.icloud.fill").foregroundStyle(Color.spDanger)99        }100    }101102    static func label(_ state: SyncState) -> String? {103        switch state {104        case .synced: return nil105        case .pending: return String(localized: "enregistrée sur cet appareil, en attente de synchronisation")106        case .conflict: return String(localized: "conflit de synchronisation")107        case .error: return String(localized: "erreur de synchronisation")108        }109    }110}111112/// Text with the search matches emphasised (weight and tint, not colour alone).113@MainActor enum HighlightedText {114    static func make(_ marked: String, untitled: String? = nil) -> Text {115        if let untitled, SPNotesStore.SearchHit.plain(marked) == AppModel.untitled || untitled.isEmpty { return Text(AppModel.displayTitle(untitled)) }116        var text = Text("")117        for segment in SPNotesStore.SearchHit.segments(of: marked) {118            text = text + (segment.highlighted119                ? Text(segment.text).fontWeight(.bold).foregroundStyle(Color.accentColor)120                : Text(segment.text))121        }122        return text123    }124}125
Files main
Menu

Theme