Adds history browser view
This commit is contained in:
@@ -37,10 +37,19 @@ class BrowserHistory
|
||||
}
|
||||
}
|
||||
|
||||
public func allHistory() -> [HistoryItem] {
|
||||
public func allHistory(limit: Int? = nil) -> [HistoryItem] {
|
||||
let dataContext = persistentContainer.viewContext
|
||||
|
||||
let fetchRequest: NSFetchRequest<HistoryItemEntity> = HistoryItemEntity.fetchRequest()
|
||||
fetchRequest.sortDescriptors = [
|
||||
// Sort by date
|
||||
NSSortDescriptor(keyPath: \HistoryItemEntity.lastVisited, ascending: false)
|
||||
]
|
||||
|
||||
if let limit {
|
||||
fetchRequest.fetchLimit = limit
|
||||
}
|
||||
|
||||
let entities: [HistoryItemEntity] = (try? dataContext.fetch(fetchRequest)) ?? []
|
||||
|
||||
return entities.map { (entity) -> HistoryItem in
|
||||
|
||||
@@ -7,15 +7,25 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
struct HistoryItem: Hashable
|
||||
struct HistoryItem: Hashable, Identifiable
|
||||
{
|
||||
var url: URL
|
||||
var title: String
|
||||
var lastVisited: Date
|
||||
var id: ObjectIdentifier
|
||||
|
||||
init(entity: HistoryItemEntity) {
|
||||
self.url = entity.url ?? URL(string: "about:blank")!
|
||||
self.lastVisited = entity.lastVisited ?? Date()
|
||||
self.title = entity.title ?? ""
|
||||
self.id = entity.id
|
||||
}
|
||||
|
||||
// For testing/previews
|
||||
public init(url: URL, title: String, lastVisited: Date) {
|
||||
self.url = url
|
||||
self.title = title
|
||||
self.lastVisited = lastVisited
|
||||
self.id = ObjectIdentifier(NSUUID())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user