diff --git a/App/Backend/History/BrowserHistory.swift b/App/Backend/History/BrowserHistory.swift index e4d7557..08da701 100644 --- a/App/Backend/History/BrowserHistory.swift +++ b/App/Backend/History/BrowserHistory.swift @@ -107,6 +107,15 @@ class BrowserHistory } } + public func clearAllHistory() { + let dataContext = persistentContainer.viewContext + let fetchRequest: NSFetchRequest = HistoryItemEntity.fetchRequest() + let request = NSBatchDeleteRequest(fetchRequest: fetchRequest) + + do { try dataContext.execute(request) } + catch { print("Error clearing history: \(error.localizedDescription)") } + } + public func allHistory(filteredBy filterString: String? = nil, limit: Int = 500) -> [HistoryItem] { let dataContext = persistentContainer.viewContext let fetchRequest = fetchRequest(forStringContaining: filterString, limit: limit) diff --git a/App/Browser View/BrowserViewController.swift b/App/Browser View/BrowserViewController.swift index 693b68e..0853a05 100644 --- a/App/Browser View/BrowserViewController.swift +++ b/App/Browser View/BrowserViewController.swift @@ -470,13 +470,34 @@ class BrowserViewController: UIViewController } internal func showHistoryWindow() { - let historyViewController = HistoryBrowserViewController() + let historyViewController = HistoryBrowserViewController { [unowned self] url in + if tab.url == nil { + tab.beginLoadingURL(url) + } else { + createNewTab(withURL: url) + } + + presentedViewController?.dismiss(animated: true) + } + historyViewController.title = "History" historyViewController.navigationItem.rightBarButtonItem = UIBarButtonItem(systemItem: .done, primaryAction: UIAction { _ in // xxx: This is not the SwiftUI-y way to do this. historyViewController.dismiss(animated: true) }) + historyViewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Clear", primaryAction: UIAction { [unowned self] action in + let alertController = UIAlertController(title: "Clear History", message: "Are you sure you want to clear all history?", preferredStyle: .actionSheet) + alertController.addAction(UIAlertAction(title: "Clear", style: .destructive, handler: { [unowned self] _ in + BrowserHistory.shared.clearAllHistory() + presentedViewController?.dismiss(animated: true) + })) + alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel)) + alertController.popoverPresentationController?.sourceItem = action.presentationSourceItem + + presentedViewController?.present(alertController, animated: true) + }) + let navigationController = UINavigationController(rootViewController: historyViewController) present(navigationController, animated: true) } diff --git a/App/History UI/HistoryBrowserViewController.swift b/App/History UI/HistoryBrowserViewController.swift index 3862a56..95aba62 100644 --- a/App/History UI/HistoryBrowserViewController.swift +++ b/App/History UI/HistoryBrowserViewController.swift @@ -10,8 +10,8 @@ import UIKit @MainActor class HistoryBrowserViewController: UIHostingController { - public init() { - super.init(rootView: HistoryView(viewModel: BrowserHistory.shared.viewModel(limit: 500))) + public init(onSelectItem: @escaping (URL) -> Void) { + super.init(rootView: HistoryView(viewModel: BrowserHistory.shared.viewModel(limit: 500), onSelectItem: onSelectItem)) } required dynamic init?(coder aDecoder: NSCoder) { diff --git a/App/History UI/HistoryView.swift b/App/History UI/HistoryView.swift index 1f46110..42e8022 100644 --- a/App/History UI/HistoryView.swift +++ b/App/History UI/HistoryView.swift @@ -11,6 +11,7 @@ import UniformTypeIdentifiers struct HistoryView: View { @StateObject public var viewModel: BrowserHistory.ViewModel @State public var selectedItems = Set() + public var onSelectItem: (URL) -> Void private let dateFormatter = DateFormatter() .. { $0.locale = Locale.current @@ -55,8 +56,7 @@ struct HistoryView: View { } }, primaryAction: { items in items.compactMap({ viewModel.item(forIdentifier: $0) }).forEach { item in - UIApplication.shared.open(item.url) - dismissAction() + onSelectItem(item.url) } }) .searchable(text: $viewModel.searchQuery) @@ -65,7 +65,7 @@ struct HistoryView: View { struct HistoryViewPreviewProvider: PreviewProvider { static var previews: some View { - HistoryView(viewModel: BrowserHistory.shared.viewModel()) + HistoryView(viewModel: BrowserHistory.shared.viewModel(), onSelectItem: { _ in }) .previewLayout(.fixed(width: 480.0, height: 800.0)) } }