Skip to content

Commit

Permalink
Prevent duplication of adjacent items.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattDavo committed Jun 13, 2021
1 parent 0d735b5 commit 6e01126
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
30 changes: 22 additions & 8 deletions Yippy/Sources/Models/History/History.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class History {
private var subscribers = [SubscribeHandler]()

private let bundleIdDenylist = [String]()
/// If a pasteboard item types contains any of these, it will not be saved.
/// If a pasteboard item's types contains any of these, it will not be saved.
private let pasteboardTypeDenylist: Set = [
"org.nspasteboard.TransientType",
"org.nspasteboard.ConcealedType",
Expand All @@ -73,6 +73,10 @@ class History {
"Pasteboard generator type",
"net.antelle.keeweb",
]
/// These pasteboard item types will not be saved.
private let pasteboardTypeIgnoreList = Set([
"dyn.ah62d4rv4gu8zg55zsmv0nvperf4g86varvu0635zqfx0nkdsqf00nkduqf31k3pcr7u1e3basv61a3k",
].map({NSPasteboard.PasteboardType(rawValue: $0)}));

init(historyFM: HistoryFileManager = .default, cache: HistoryCache, items: [HistoryItem], maxItems: Int = Constants.system.maxHistoryItems) {
self.historyFM = historyFM
Expand Down Expand Up @@ -181,20 +185,30 @@ extension History: PasteboardMonitorDelegate {
}

for item in items {
if !item.types.isEmpty && Set(item.types.map({ $0.rawValue })).isDisjoint(with: pasteboardTypeDenylist) {
let filteredTypes = Set(item.types).subtracting(self.pasteboardTypeIgnoreList)
let hasTypes = !filteredTypes.isEmpty
let hasNoDeniedTypes = Set(filteredTypes.map({ $0.rawValue })).isDisjoint(with: pasteboardTypeDenylist)

if hasTypes && hasNoDeniedTypes {
var data = [NSPasteboard.PasteboardType: Data]()
for type in item.types {
for type in filteredTypes {
if let d = item.data(forType: type) {
data[type] = d
let firstData = self.items.first?.data(forType: type)
let isNewData = firstData == nil || firstData?.hashValue != d.hashValue
if isNewData {
data[type] = d
}
}
else {
print("Warning: new pasteboard data nil for type '\(type.rawValue)'")
}
}
let historyItem = HistoryItem(unsavedData: data, cache: cache)
insertItem(historyItem, at: 0)
let selected = (_selected.value ?? -1) + 1
setSelected(selected)
if !data.isEmpty {
let historyItem = HistoryItem(unsavedData: data, cache: cache)
insertItem(historyItem, at: 0)
let selected = (_selected.value ?? -1) + 1
setSelected(selected)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Yippy/Sources/Models/History/HistoryItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class HistoryItem: NSObject {
/// Returns the data for given type.
///
/// If the data is available in `unsavedData` it will be returned.
/// Otherwise if the item is not being cached, the data will be load from disk using the cache, but it will not be cached.
/// Otherwise if the item is not being cached, the data will be loaded from disk using the cache, but it will not be cached.
/// Otherwise it will use the cache to get the data.
///
/// - Parameter type: The type of pasteboard data to get.
Expand Down

0 comments on commit 6e01126

Please sign in to comment.