Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Maccy/Observables/History.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ class History: ItemsContainer { // swiftlint:disable:this type_body_length
var removedItemIndex: Int?
if let existingHistoryItem = findSimilarItem(item) {
if isModified(item) == nil {
for content in item.contents {
Storage.shared.context.delete(content)
}
item.contents = existingHistoryItem.contents
existingHistoryItem.contents = []
}
item.firstCopiedAt = existingHistoryItem.firstCopiedAt
item.numberOfCopies += existingHistoryItem.numberOfCopies
Expand Down
18 changes: 18 additions & 0 deletions MaccyTests/HistoryTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import XCTest
import Defaults
import SwiftData
@testable import Maccy

@MainActor
Expand Down Expand Up @@ -123,6 +124,23 @@ class HistoryTests: XCTestCase {
XCTAssertEqual(Set(history.items[0].item.contents), Set(firstContents))
}

func testDuplicateDoesNotOrphanContents() {
let item = historyItem("foo")
history.add(item)

let descriptor1 = FetchDescriptor<HistoryItemContent>()
let contents1 = try? Storage.shared.context.fetch(descriptor1)
XCTAssertEqual(contents1?.count, 1)

let duplicate = historyItem("foo")
history.add(duplicate)

let descriptor2 = FetchDescriptor<HistoryItemContent>()
let contents2 = try? Storage.shared.context.fetch(descriptor2)
XCTAssertEqual(contents2?.count, 1)
XCTAssertNotNil(contents2?.first?.item)
}

func testAddingItemFromMaccy() {
let firstContents = [
HistoryItemContent(
Expand Down