73 lines
2.2 KiB
Swift
73 lines
2.2 KiB
Swift
|
|
//
|
||
|
|
// HistoryView.swift
|
||
|
|
// App
|
||
|
|
//
|
||
|
|
// Created by James Magahern on 1/20/23.
|
||
|
|
//
|
||
|
|
|
||
|
|
import SwiftUI
|
||
|
|
import UniformTypeIdentifiers
|
||
|
|
|
||
|
|
struct HistoryView: View {
|
||
|
|
var historyItems: [HistoryItem]
|
||
|
|
|
||
|
|
private let dateFormatter: DateFormatter
|
||
|
|
@State public var selectedItems = Set<HistoryItem.ID>()
|
||
|
|
@Environment(\.dismiss) private var dismissAction
|
||
|
|
|
||
|
|
init(historyItems: [HistoryItem]) {
|
||
|
|
self.historyItems = historyItems
|
||
|
|
|
||
|
|
let formatter = DateFormatter()
|
||
|
|
formatter.locale = Locale.current
|
||
|
|
formatter.dateStyle = .medium
|
||
|
|
formatter.timeStyle = .short
|
||
|
|
self.dateFormatter = formatter
|
||
|
|
}
|
||
|
|
|
||
|
|
var body: some View {
|
||
|
|
Table(historyItems, selection: $selectedItems) {
|
||
|
|
TableColumn("Title", value: \.title)
|
||
|
|
|
||
|
|
TableColumn("URL") { item in
|
||
|
|
Text(item.url.absoluteString)
|
||
|
|
}
|
||
|
|
|
||
|
|
TableColumn("Last Visited") { item in
|
||
|
|
Text(dateFormatter.string(from: item.lastVisited))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
.contextMenu(forSelectionType: HistoryItem.ID.self, menu: { items in
|
||
|
|
if let firstItem: HistoryItem.ID = items.first,
|
||
|
|
let historyItem = historyItems.first { $0.id == firstItem }
|
||
|
|
{
|
||
|
|
Button("Copy") {
|
||
|
|
UIPasteboard.general.addItems([
|
||
|
|
[ UTType.url.identifier : historyItem.url ]
|
||
|
|
])
|
||
|
|
}
|
||
|
|
|
||
|
|
// TODO: Delete?
|
||
|
|
}
|
||
|
|
}, primaryAction: { items in
|
||
|
|
if let firstItem: HistoryItem.ID = items.first,
|
||
|
|
let historyItem = historyItems.first(where: { $0.id == firstItem })
|
||
|
|
{
|
||
|
|
UIApplication.shared.open(historyItem.url)
|
||
|
|
dismissAction()
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
struct HistoryViewPreviewProvider: PreviewProvider {
|
||
|
|
static var previews: some View {
|
||
|
|
HistoryView(historyItems: [
|
||
|
|
HistoryItem(url: URL(string: "https://apple.com")!, title: "Apple", lastVisited: Date.now),
|
||
|
|
HistoryItem(url: URL(string: "https://google.com")!, title: "Google", lastVisited: Date.now)
|
||
|
|
])
|
||
|
|
.previewLayout(.fixed(width: 480.0, height: 800.0))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|