SPB Git

Repository Public

SyncedPanels.swift 6.7 KB 125 lines swift

1import SwiftUI23/// Pages saved for later. Synchronized between the user's devices.4struct ReadingListPanel: View {5    @Bindable var state: BrowserWindowState6    let environment: AppEnvironment7    @State private var showsRead = false89    private var services: BrowserServices { environment.services }1011    var body: some View {12        let _ = services.libraryRevision13        let all = services.library.readingList()14        let items = showsRead ? all : all.filter { !$0.isRead }15        VStack(spacing: 0) {16            HStack {17                Picker("", selection: $showsRead) { Text(L("Unread")).tag(false); Text(L("All")).tag(true) }.pickerStyle(.segmented).labelsHidden()18                IconButton(symbol: "plus", label: CommandID.addToReadingList.title, size: 26) { state.perform(.addToReadingList) }19                    .disabled(!state.isEnabled(.addToReadingList))20            }21            .padding(10)22            if items.isEmpty {23                StateView(symbol: "eyeglasses", title: all.isEmpty ? L("Nothing saved for later") : L("Everything is read"),24                          message: L("Add the current page with ⌥⌘D. The list follows you to your other devices."), compact: true)25            } else {26                ScrollView {27                    LazyVStack(spacing: 2) {28                        ForEach(items) { item in29                            Button { if let url = URL(string: item.url) { state.open(url); services.library.setRead(item, true); services.localChange() } } label: {30                                HoverRow {31                                    HStack(spacing: 8) {32                                        Circle().fill(item.isRead ? Color.clear : Theme.accent).frame(width: 7, height: 7)33                                        VStack(alignment: .leading, spacing: 1) {34                                            Text(item.title.isEmpty ? item.url : item.title).font(.system(size: 12.5, weight: item.isRead ? .regular : .medium)).lineLimit(2)35                                            Text("\(URL(string: item.url)?.host ?? "") · \(item.addedAt.relativeShort)").font(.system(size: 10.5)).foregroundStyle(.secondary).lineLimit(1)36                                        }37                                        Spacer(minLength: 0)38                                    }39                                    .padding(.horizontal, 8).padding(.vertical, 6)40                                }41                            }42                            .buttonStyle(.plain)43                            .contextMenu {44                                Button(L("Open in New Tab")) { if let url = URL(string: item.url) { state.newTab(url: url, select: true) } }45                                Button(item.isRead ? L("Mark as Unread") : L("Mark as Read")) { services.library.setRead(item, !item.isRead); services.localChange() }46                                Button(L("Copy Link")) { PlatformActions.copy(item.url) }47                                Divider()48                                Button(L("Remove"), role: .destructive) { services.library.removeFromReadingList(url: item.url); services.localChange() }49                            }50                        }51                    }52                    .padding(.horizontal, 8)53                }54            }55        }56    }57}5859/// Tabs open (and recently closed) on the user's other Macs, iPhones and iPads.60struct OtherDevicesPanel: View {61    @Bindable var state: BrowserWindowState62    let environment: AppEnvironment6364    private var services: BrowserServices { environment.services }6566    var body: some View {67        let _ = services.libraryRevision68        if !services.account.isSignedIn {69            StateView(symbol: "laptopcomputer.and.iphone", title: L("Tabs from your other devices"),70                      message: L("Sign in to see what is open on your other Macs, iPhones and iPads."),71                      actionTitle: L("Account…"), action: { state.perform(.showAccount) }, compact: true)72        } else {73            let devices = services.otherDevices74            let closed = services.library.otherDevicesClosedTabs(excluding: services.account.currentDeviceID)75            if devices.isEmpty && closed.isEmpty {76                StateView(symbol: "laptopcomputer.and.iphone", title: L("No tabs from other devices yet"),77                          message: syncMessage, actionTitle: L("Synchronize Now"), action: { state.perform(.syncNow) }, compact: true)78            } else {79                ScrollView {80                    LazyVStack(alignment: .leading, spacing: 10) {81                        ForEach(devices) { device in section(device, closed: false) }82                        ForEach(closed) { device in section(device, closed: true) }83                    }84                    .padding(10)85                }86            }87        }88    }8990    private var syncMessage: String {91        switch services.sync.phase {92        case .off: return L("Synchronization is turned off on this Mac.")93        case .waitingForKey: return L("This Mac is waiting to be approved by one of your other devices.")94        case .offline: return L("You are offline. Tabs will appear when the connection is back.")95        default: return L("Open SP Browser on another device signed in to your account.")96        }97    }9899    private func section(_ device: DeviceTabs, closed: Bool) -> some View {100        VStack(alignment: .leading, spacing: 2) {101            HStack(spacing: 6) {102                Image(systemName: device.platform == "macos" ? "desktopcomputer" : (device.platform == "ipados" ? "ipad" : "iphone")).foregroundStyle(.secondary)103                Text(closed ? L("%@ — recently closed", device.deviceName) : device.deviceName).font(.system(size: 11.5, weight: .semibold))104                Spacer()105                Text(device.updatedAt.relativeShort).font(.system(size: 10.5)).foregroundStyle(.tertiary)106            }107            .padding(.horizontal, 6).padding(.bottom, 2)108            ForEach(Array(device.tabs.enumerated()), id: \.offset) { _, tab in109                Button { if let url = URL(string: tab.url) { state.newTab(url: url, select: true) } } label: {110                    HoverRow {111                        VStack(alignment: .leading, spacing: 1) {112                            Text(tab.title.isEmpty ? tab.url : tab.title).font(.system(size: 12.5)).lineLimit(1)113                            Text(URL(string: tab.url)?.host ?? tab.url).font(.system(size: 10.5)).foregroundStyle(.secondary).lineLimit(1)114                        }115                        .padding(.horizontal, 8).padding(.vertical, 5)116                    }117                }118                .buttonStyle(.plain)119                .help(tab.url)120                .contextMenu { Button(L("Copy Link")) { PlatformActions.copy(tab.url) } }121            }122        }123    }124}125
Files main
Menu

Theme