Adds history browser view
This commit is contained in:
20
App/History UI/HistoryBrowserViewController.swift
Normal file
20
App/History UI/HistoryBrowserViewController.swift
Normal file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// HistoryBrowserViewController.swift
|
||||
// App
|
||||
//
|
||||
// Created by James Magahern on 1/20/23.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import UIKit
|
||||
|
||||
@MainActor
|
||||
class HistoryBrowserViewController: UIHostingController<HistoryView> {
|
||||
public init() {
|
||||
super.init(rootView: HistoryView(historyItems: BrowserHistory.shared.allHistory(limit: 500)))
|
||||
}
|
||||
|
||||
required dynamic init?(coder aDecoder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
}
|
||||
72
App/History UI/HistoryView.swift
Normal file
72
App/History UI/HistoryView.swift
Normal file
@@ -0,0 +1,72 @@
|
||||
//
|
||||
// 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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user