Repository Public
1import SwiftUI2import QuickLook34/// Bookmarks, reading list, history and downloads in one sheet (iPhone) — the same content the iPad sidebar pushes.5struct LibrarySheet: View {6 @Bindable var model: BrowserModel7 let environment: MobileEnvironment8 @State var section: LibrarySection9 @Environment(\.dismiss) private var dismiss1011 init(model: BrowserModel, environment: MobileEnvironment, initial: LibrarySection) {12 self.model = model13 self.environment = environment14 _section = State(initialValue: initial)15 }1617 var body: some View {18 NavigationStack {19 LibraryContent(section: section, model: model, environment: environment)20 .navigationTitle(section.title)21 .navigationBarTitleDisplayMode(.inline)22 .toolbar {23 ToolbarItem(placement: .topBarTrailing) { Button(L("Done")) { dismiss() } }24 ToolbarItem(placement: .bottomBar) {25 Picker(L("Library"), selection: $section) {26 ForEach(LibrarySection.allCases) { Image(systemName: $0.symbol).accessibilityLabel($0.title).tag($0) }27 }28 .pickerStyle(.segmented)29 .accessibilityIdentifier("library-picker")30 }31 }32 }33 .presentationDetents([.large])34 }35}3637struct LibraryContent: View {38 let section: LibrarySection39 @Bindable var model: BrowserModel40 let environment: MobileEnvironment4142 var body: some View {43 switch section {44 case .bookmarks: BookmarkList(parentID: nil, model: model, environment: environment)45 case .readingList: ReadingListView(model: model, environment: environment)46 case .history: HistoryList(model: model, environment: environment)47 case .downloads: DownloadList(model: model, environment: environment)48 }49 }50}5152// MARK: Bookmarks5354struct BookmarkList: View {55 let parentID: String?56 @Bindable var model: BrowserModel57 let environment: MobileEnvironment58 @State private var query = ""59 @State private var newFolder = false60 @State private var folderName = ""6162 var body: some View {63 let _ = environment.dataRevision64 let items = query.isEmpty ? environment.bookmarks.children(of: parentID) : environment.bookmarks.search(query, limit: 100)65 List {66 ForEach(items) { bookmark in67 if bookmark.isFolder {68 NavigationLink { BookmarkList(parentID: bookmark.id, model: model, environment: environment).navigationTitle(bookmark.title) } label: {69 Label(bookmark.title, systemImage: "folder")70 }71 .swipeActions { Button(role: .destructive) { delete(bookmark) } label: { Label(L("Delete"), systemImage: "trash") } }72 } else {73 Button { if let url = bookmark.url.flatMap(URL.init(string:)) { model.open(url) } } label: {74 VStack(alignment: .leading, spacing: 2) {75 Text(bookmark.title.isEmpty ? (bookmark.url ?? "") : bookmark.title).foregroundStyle(.primary).lineLimit(1)76 Text(bookmark.url ?? "").font(.caption).foregroundStyle(.secondary).lineLimit(1)77 }78 }79 .swipeActions {80 Button(role: .destructive) { delete(bookmark) } label: { Label(L("Delete"), systemImage: "trash") }81 Button { model.bookmarkDraft = .init(existing: bookmark, title: bookmark.title, url: bookmark.url ?? "", parentID: bookmark.parentID) } label: { Label(L("Edit"), systemImage: "pencil") }.tint(.blue)82 }83 .contextMenu {84 Button { if let url = bookmark.url.flatMap(URL.init(string:)) { model.open(url, inNewTab: true) } } label: { Label(L("Open in New Tab"), systemImage: "plus.square") }85 Button { PlatformActions.copy(bookmark.url ?? "") } label: { Label(L("Copy Link"), systemImage: "doc.on.doc") }86 }87 }88 }89 }90 .overlay { if items.isEmpty { ContentUnavailableView(L("No bookmarks"), systemImage: "bookmark", description: Text(L("Add the page you are reading from the share menu."))) } }91 .searchable(text: $query, prompt: L("Search bookmarks"))92 .toolbar {93 ToolbarItem(placement: .topBarLeading) { Button { folderName = ""; newFolder = true } label: { Image(systemName: "folder.badge.plus") }.accessibilityLabel(L("New Folder")) }94 }95 .alert(L("New Folder"), isPresented: $newFolder) {96 TextField(L("Name"), text: $folderName)97 Button(L("Create")) { if !folderName.trimmingCharacters(in: .whitespaces).isEmpty { environment.bookmarks.addFolder(folderName, parentID: parentID); environment.dataChanged() } }98 Button(L("Cancel"), role: .cancel) {}99 }100 }101102 private func delete(_ bookmark: Bookmark) {103 environment.bookmarks.delete(id: bookmark.id)104 environment.dataChanged()105 }106}107108struct BookmarkEditor: View {109 @State var draft: BrowserModel.BookmarkDraft110 let model: BrowserModel111 let environment: MobileEnvironment112 @Environment(\.dismiss) private var dismiss113114 var body: some View {115 NavigationStack {116 Form {117 TextField(L("Title"), text: $draft.title).accessibilityIdentifier("bookmark-title")118 TextField(L("Address"), text: $draft.url).textInputAutocapitalization(.never).autocorrectionDisabled().keyboardType(.URL)119 Picker(L("Folder"), selection: $draft.parentID) {120 Text(L("Bookmarks")).tag(String?.none)121 ForEach(environment.bookmarks.folders()) { Text($0.title).tag(String?.some($0.id)) }122 }123 if let existing = draft.existing {124 Button(L("Delete Bookmark"), role: .destructive) { environment.bookmarks.delete(id: existing.id); environment.dataChanged(); dismiss() }125 }126 }127 .navigationTitle(draft.existing == nil ? L("Add Bookmark") : L("Edit Bookmark"))128 .navigationBarTitleDisplayMode(.inline)129 .toolbar {130 ToolbarItem(placement: .cancellationAction) { Button(L("Cancel")) { dismiss() } }131 ToolbarItem(placement: .confirmationAction) { Button(L("Save")) { save() }.disabled(draft.url.trimmingCharacters(in: .whitespaces).isEmpty).accessibilityIdentifier("bookmark-save") }132 }133 }134 .presentationDetents([.medium, .large])135 }136137 private func save() {138 if var existing = draft.existing {139 existing.title = draft.title; existing.url = draft.url140 environment.bookmarks.update(existing)141 if existing.parentID != draft.parentID { _ = environment.bookmarks.move(id: existing.id, toParent: draft.parentID) }142 } else {143 environment.bookmarks.add(title: draft.title, url: draft.url, parentID: draft.parentID)144 }145 environment.dataChanged()146 model.toast = L("Bookmark saved")147 dismiss()148 }149}150151// MARK: Reading list152153struct ReadingListView: View {154 @Bindable var model: BrowserModel155 let environment: MobileEnvironment156157 var body: some View {158 let _ = (environment.services.libraryRevision, environment.dataRevision)159 let library = environment.services.library160 let items = library.readingList()161 List {162 ForEach(items) { item in163 Button {164 library.setRead(item, true); environment.dataChanged()165 if let url = URL(string: item.url) { model.open(url) }166 } label: {167 HStack(spacing: 10) {168 Circle().fill(item.isRead ? Color.clear : SPTheme.accent).frame(width: 8, height: 8).accessibilityHidden(true)169 VStack(alignment: .leading, spacing: 2) {170 Text(item.title.isEmpty ? item.url : item.title).foregroundStyle(.primary).lineLimit(2)171 Text(URL(string: item.url)?.host ?? item.url).font(.caption).foregroundStyle(.secondary).lineLimit(1)172 }173 }174 }175 .accessibilityValue(item.isRead ? L("Read") : L("Unread"))176 .swipeActions {177 Button(role: .destructive) { library.removeFromReadingList(url: item.url); environment.dataChanged() } label: { Label(L("Delete"), systemImage: "trash") }178 Button { library.setRead(item, !item.isRead); environment.dataChanged() } label: { Label(item.isRead ? L("Mark Unread") : L("Mark Read"), systemImage: item.isRead ? "circle.fill" : "checkmark") }.tint(.blue)179 }180 }181 }182 .overlay { if items.isEmpty { ContentUnavailableView(L("Reading list is empty"), systemImage: "eyeglasses", description: Text(L("Save pages to read later; they follow you to your other devices."))) } }183 }184}185186// MARK: History187188struct HistoryList: View {189 @Bindable var model: BrowserModel190 let environment: MobileEnvironment191 @State private var query = ""192 @State private var showsClear = false193194 var body: some View {195 let _ = environment.dataRevision196 let entries = environment.history.search(query, limit: 300)197 let groups = Dictionary(grouping: entries) { Calendar.current.startOfDay(for: $0.visitedAt) }198 List {199 ForEach(groups.keys.sorted(by: >), id: \.self) { day in200 Section(day.formatted(.dateTime.weekday(.wide).day().month(.wide))) {201 ForEach(groups[day] ?? []) { entry in202 Button { if let url = URL(string: entry.url) { model.open(url) } } label: {203 VStack(alignment: .leading, spacing: 2) {204 Text(entry.title.isEmpty ? entry.url : entry.title).foregroundStyle(.primary).lineLimit(1)205 Text("\(entry.visitedAt.formatted(date: .omitted, time: .shortened)) · \(URL(string: entry.url)?.host ?? entry.url)").font(.caption).foregroundStyle(.secondary).lineLimit(1)206 }207 }208 .swipeActions { Button(role: .destructive) { environment.history.delete(id: entry.id); environment.dataChanged() } label: { Label(L("Delete"), systemImage: "trash") } }209 .contextMenu {210 Button { if let url = URL(string: entry.url) { model.open(url, inNewTab: true) } } label: { Label(L("Open in New Tab"), systemImage: "plus.square") }211 Button { PlatformActions.copy(entry.url) } label: { Label(L("Copy Link"), systemImage: "doc.on.doc") }212 if let host = URL(string: entry.url)?.host {213 Button(role: .destructive) { environment.history.delete(host: host); environment.dataChanged() } label: { Label(L("Delete All Visits to This Site"), systemImage: "trash") }214 }215 }216 }217 }218 }219 }220 .overlay { if entries.isEmpty { ContentUnavailableView(query.isEmpty ? L("No history") : L("No match"), systemImage: "clock") } }221 .searchable(text: $query, prompt: L("Search history"))222 .toolbar { ToolbarItem(placement: .topBarLeading) { Button(L("Clear…")) { showsClear = true }.accessibilityIdentifier("history-clear") } }223 .confirmationDialog(L("Clear history"), isPresented: $showsClear, titleVisibility: .visible) {224 Button(L("The Last Hour"), role: .destructive) { environment.clearHistory(since: Date().addingTimeInterval(-3600)) }225 Button(L("Today"), role: .destructive) { environment.clearHistory(since: Calendar.current.startOfDay(for: Date())) }226 Button(L("All History"), role: .destructive) { environment.clearHistory(since: nil) }227 Button(L("Cancel"), role: .cancel) {}228 } message: { Text(L("Cleared history is also removed from your other devices. Clearing everything also clears your search history.")) }229 }230}231232// MARK: Downloads233234struct DownloadList: View {235 @Bindable var model: BrowserModel236 let environment: MobileEnvironment237 @State private var preview: URL?238239 var body: some View {240 let downloads = environment.downloads241 List {242 ForEach(downloads.items) { item in243 HStack(spacing: 12) {244 Image(systemName: "doc").font(.title3).foregroundStyle(.secondary).frame(width: 28)245 VStack(alignment: .leading, spacing: 3) {246 Text(item.name).lineLimit(1)247 switch item.state {248 case .running: ProgressView(value: item.fraction).accessibilityLabel(L("Downloading"))249 case .finished: Text(item.source.isEmpty ? L("Saved in Downloads") : item.source).font(.caption).foregroundStyle(.secondary)250 case .failed(let message): Text(message).font(.caption).foregroundStyle(.orange).lineLimit(1)251 }252 }253 Spacer(minLength: 0)254 if item.state == .finished { ShareLink(item: item.fileURL) { Image(systemName: "square.and.arrow.up") }.buttonStyle(.borderless).accessibilityLabel(L("Share…")) }255 if item.state == .running { Button { downloads.cancel(item) } label: { Image(systemName: "xmark.circle") }.buttonStyle(.borderless).accessibilityLabel(L("Cancel")) }256 }257 .contentShape(Rectangle())258 .onTapGesture { if item.state == .finished { preview = item.fileURL } }259 .swipeActions { Button(role: .destructive) { downloads.remove(item, deleteFile: true) } label: { Label(L("Delete"), systemImage: "trash") } }260 }261 }262 .overlay { if downloads.items.isEmpty { ContentUnavailableView(L("No downloads"), systemImage: "arrow.down.circle", description: Text(L("Files you download are saved in SP Browser's folder in the Files app."))) } }263 .quickLookPreview($preview)264 }265}266