History UI improvements
This commit is contained in:
@@ -107,6 +107,15 @@ class BrowserHistory
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func clearAllHistory() {
|
||||||
|
let dataContext = persistentContainer.viewContext
|
||||||
|
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = 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] {
|
public func allHistory(filteredBy filterString: String? = nil, limit: Int = 500) -> [HistoryItem] {
|
||||||
let dataContext = persistentContainer.viewContext
|
let dataContext = persistentContainer.viewContext
|
||||||
let fetchRequest = fetchRequest(forStringContaining: filterString, limit: limit)
|
let fetchRequest = fetchRequest(forStringContaining: filterString, limit: limit)
|
||||||
|
|||||||
@@ -470,13 +470,34 @@ class BrowserViewController: UIViewController
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal func showHistoryWindow() {
|
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.title = "History"
|
||||||
historyViewController.navigationItem.rightBarButtonItem = UIBarButtonItem(systemItem: .done, primaryAction: UIAction { _ in
|
historyViewController.navigationItem.rightBarButtonItem = UIBarButtonItem(systemItem: .done, primaryAction: UIAction { _ in
|
||||||
// xxx: This is not the SwiftUI-y way to do this.
|
// xxx: This is not the SwiftUI-y way to do this.
|
||||||
historyViewController.dismiss(animated: true)
|
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)
|
let navigationController = UINavigationController(rootViewController: historyViewController)
|
||||||
present(navigationController, animated: true)
|
present(navigationController, animated: true)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ import UIKit
|
|||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
class HistoryBrowserViewController: UIHostingController<HistoryView> {
|
class HistoryBrowserViewController: UIHostingController<HistoryView> {
|
||||||
public init() {
|
public init(onSelectItem: @escaping (URL) -> Void) {
|
||||||
super.init(rootView: HistoryView(viewModel: BrowserHistory.shared.viewModel(limit: 500)))
|
super.init(rootView: HistoryView(viewModel: BrowserHistory.shared.viewModel(limit: 500), onSelectItem: onSelectItem))
|
||||||
}
|
}
|
||||||
|
|
||||||
required dynamic init?(coder aDecoder: NSCoder) {
|
required dynamic init?(coder aDecoder: NSCoder) {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import UniformTypeIdentifiers
|
|||||||
struct HistoryView: View {
|
struct HistoryView: View {
|
||||||
@StateObject public var viewModel: BrowserHistory.ViewModel
|
@StateObject public var viewModel: BrowserHistory.ViewModel
|
||||||
@State public var selectedItems = Set<HistoryItem.ID>()
|
@State public var selectedItems = Set<HistoryItem.ID>()
|
||||||
|
public var onSelectItem: (URL) -> Void
|
||||||
|
|
||||||
private let dateFormatter = DateFormatter() .. {
|
private let dateFormatter = DateFormatter() .. {
|
||||||
$0.locale = Locale.current
|
$0.locale = Locale.current
|
||||||
@@ -55,8 +56,7 @@ struct HistoryView: View {
|
|||||||
}
|
}
|
||||||
}, primaryAction: { items in
|
}, primaryAction: { items in
|
||||||
items.compactMap({ viewModel.item(forIdentifier: $0) }).forEach { item in
|
items.compactMap({ viewModel.item(forIdentifier: $0) }).forEach { item in
|
||||||
UIApplication.shared.open(item.url)
|
onSelectItem(item.url)
|
||||||
dismissAction()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.searchable(text: $viewModel.searchQuery)
|
.searchable(text: $viewModel.searchQuery)
|
||||||
@@ -65,7 +65,7 @@ struct HistoryView: View {
|
|||||||
|
|
||||||
struct HistoryViewPreviewProvider: PreviewProvider {
|
struct HistoryViewPreviewProvider: PreviewProvider {
|
||||||
static var previews: some View {
|
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))
|
.previewLayout(.fixed(width: 480.0, height: 800.0))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user