HistoryView: Compact UI for phones
This commit is contained in:
@@ -86,7 +86,7 @@ class BrowserHistory
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func viewModel(forFilterString filterString: String? = nil, limit: Int? = nil) -> ViewModel {
|
public func viewModel(forFilterString filterString: String? = nil, limit: Int = 500) -> ViewModel {
|
||||||
let resultsController = fetchRequestController(forQuery: filterString, limit: limit)
|
let resultsController = fetchRequestController(forQuery: filterString, limit: limit)
|
||||||
return ViewModel(fetchedResultsController: resultsController, historyController: self)
|
return ViewModel(fetchedResultsController: resultsController, historyController: self)
|
||||||
}
|
}
|
||||||
@@ -107,7 +107,7 @@ class BrowserHistory
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func allHistory(filteredBy filterString: String? = nil, limit: Int? = nil) -> [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)
|
||||||
let entities: [HistoryItemEntity] = (try? dataContext.fetch(fetchRequest)) ?? []
|
let entities: [HistoryItemEntity] = (try? dataContext.fetch(fetchRequest)) ?? []
|
||||||
@@ -160,24 +160,21 @@ class BrowserHistory
|
|||||||
|
|
||||||
extension BrowserHistory
|
extension BrowserHistory
|
||||||
{
|
{
|
||||||
fileprivate func fetchRequestController(forQuery query: String? = nil, limit: Int? = nil) -> NSFetchedResultsController<HistoryItemEntity> {
|
fileprivate func fetchRequestController(forQuery query: String? = nil, limit: Int = 500) -> NSFetchedResultsController<HistoryItemEntity> {
|
||||||
let fetchRequest = fetchRequest(forStringContaining: query, limit: limit)
|
let fetchRequest = fetchRequest(forStringContaining: query, limit: limit)
|
||||||
return NSFetchedResultsController(fetchRequest: fetchRequest,
|
return NSFetchedResultsController(fetchRequest: fetchRequest,
|
||||||
managedObjectContext: persistentContainer.viewContext,
|
managedObjectContext: persistentContainer.viewContext,
|
||||||
sectionNameKeyPath: nil, cacheName: nil)
|
sectionNameKeyPath: nil, cacheName: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate func fetchRequest(forStringContaining filterString: String? = nil, limit: Int? = nil) -> NSFetchRequest<HistoryItemEntity> {
|
fileprivate func fetchRequest(forStringContaining filterString: String? = nil, limit: Int = 500) -> NSFetchRequest<HistoryItemEntity> {
|
||||||
let fetchRequest: NSFetchRequest<HistoryItemEntity> = HistoryItemEntity.fetchRequest()
|
let fetchRequest: NSFetchRequest<HistoryItemEntity> = HistoryItemEntity.fetchRequest()
|
||||||
|
fetchRequest.fetchLimit = limit
|
||||||
fetchRequest.sortDescriptors = [
|
fetchRequest.sortDescriptors = [
|
||||||
// Sort by date
|
// Sort by date
|
||||||
NSSortDescriptor(keyPath: \HistoryItemEntity.lastVisited, ascending: false)
|
NSSortDescriptor(keyPath: \HistoryItemEntity.lastVisited, ascending: false)
|
||||||
]
|
]
|
||||||
|
|
||||||
if let limit {
|
|
||||||
fetchRequest.fetchLimit = limit
|
|
||||||
}
|
|
||||||
|
|
||||||
if let filterString, filterString.count > 0 {
|
if let filterString, filterString.count > 0 {
|
||||||
fetchRequest.predicate = NSPredicate(format: """
|
fetchRequest.predicate = NSPredicate(format: """
|
||||||
host CONTAINS[cd] %@
|
host CONTAINS[cd] %@
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
// Created by James Magahern on 8/14/20.
|
// Created by James Magahern on 8/14/20.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
import CoreData
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct HistoryItem: Hashable, Identifiable
|
struct HistoryItem: Hashable, Identifiable
|
||||||
|
|||||||
@@ -473,6 +473,7 @@ class BrowserViewController: UIViewController
|
|||||||
let historyViewController = HistoryBrowserViewController()
|
let historyViewController = HistoryBrowserViewController()
|
||||||
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.
|
||||||
historyViewController.dismiss(animated: true)
|
historyViewController.dismiss(animated: true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -19,16 +19,25 @@ struct HistoryView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Environment(\.dismiss) private var dismissAction
|
@Environment(\.dismiss) private var dismissAction
|
||||||
|
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Table(viewModel.historyItems, selection: $selectedItems) {
|
Table(viewModel.historyItems, selection: $selectedItems) {
|
||||||
TableColumn("Title", value: \.title)
|
TableColumn("Title") { item in
|
||||||
|
VStack(alignment: .leading) {
|
||||||
|
Text(item.title.count > 0 ? item.title : item.url.absoluteString)
|
||||||
|
.lineLimit(1)
|
||||||
|
|
||||||
TableColumn("URL") { item in
|
if horizontalSizeClass == .compact {
|
||||||
Text(item.url.absoluteString)
|
Text(item.url.shortString()).font(.caption).lineLimit(1)
|
||||||
|
Text(dateFormatter.string(from: item.lastVisited))
|
||||||
|
.font(.caption)
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
TableColumn("URL", value: \.url.absoluteString)
|
||||||
TableColumn("Last Visited") { item in
|
TableColumn("Last Visited") { item in
|
||||||
Text(dateFormatter.string(from: item.lastVisited))
|
Text(dateFormatter.string(from: item.lastVisited))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,3 +70,9 @@ struct HistoryViewPreviewProvider: PreviewProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension URL {
|
||||||
|
public func shortString() -> String {
|
||||||
|
return String(absoluteString.trimmingPrefix(try! Regex("(.+)://")))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user