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
17 changes: 17 additions & 0 deletions Maccy/Observables/HistoryItemDecorator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ class HistoryItemDecorator: Identifiable, Hashable, HasVisibility {

static var previewImageSize: NSSize { NSScreen.forPopup?.visibleFrame.size ?? NSSize(width: 2048, height: 1536) }
static var thumbnailImageSize: NSSize { NSSize(width: 340, height: Defaults[.imageMaxHeight]) }
private static func relativeCopyTimeFormatter(locale: Locale) -> RelativeDateTimeFormatter {
let formatter = RelativeDateTimeFormatter()
formatter.locale = locale
formatter.unitsStyle = .abbreviated
formatter.dateTimeStyle = .named
return formatter
}

static func formatRelativeCopyTime(
for date: Date,
relativeTo referenceDate: Date = .now,
locale: Locale = .autoupdatingCurrent
) -> String {
return relativeCopyTimeFormatter(locale: locale)
.localizedString(for: date, relativeTo: referenceDate)
}

let id = UUID()

Expand Down Expand Up @@ -49,6 +65,7 @@ class HistoryItemDecorator: Identifiable, Hashable, HasVisibility {

// 10k characters seems to be more than enough on large displays
var text: String { item.previewableText.shortened(to: 10_000) }
var relativeLastCopiedAt: String { Self.formatRelativeCopyTime(for: item.lastCopiedAt) }

var isPinned: Bool { item.pin != nil }
var isUnpinned: Bool { item.pin == nil }
Expand Down
1 change: 1 addition & 0 deletions Maccy/Views/HistoryItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct HistoryItemView: View {
accessoryImage: item.thumbnailImage != nil ? nil : ColorImage.from(item.title),
attributedTitle: item.attributedTitle,
shortcuts: item.shortcuts,
detailText: item.relativeLastCopiedAt,
isSelected: item.isSelected,
selectionIndex: visualIndex,
selectionAppearance: selectionAppearance
Expand Down
10 changes: 10 additions & 0 deletions Maccy/Views/ListItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct ListItemView<Title: View, ID: Hashable>: View {
var accessoryImage: NSImage?
var attributedTitle: AttributedString?
var shortcuts: [KeyShortcut]
var detailText: String? = nil
var isSelected: Bool
var selectionIndex: Int?
var help: LocalizedStringKey?
Expand Down Expand Up @@ -81,6 +82,15 @@ struct ListItemView<Title: View, ID: Hashable>: View {
Spacer()

HStack(spacing: 5) {
if let detailText, !detailText.isEmpty {
Text(detailText)
.font(.caption2)
.monospacedDigit()
.foregroundStyle(isSelected ? Color.white.opacity(0.8) : .secondary)
.lineLimit(1)
.fixedSize(horizontal: true, vertical: false)
}

if let index = selectionIndex {
Text("\(index + 1)")
.font(.caption)
Expand Down
1 change: 1 addition & 0 deletions Maccy/Views/PasteStackItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct PasteStackItemView: View {
accessoryImage: item.thumbnailImage != nil ? nil : ColorImage.from(item.title),
attributedTitle: item.attributedTitle,
shortcuts: [],
detailText: item.relativeLastCopiedAt,
isSelected: isSelected,
selectionIndex: index,
selectionAppearance: .none
Expand Down
13 changes: 13 additions & 0 deletions MaccyTests/HistoryDecoratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ class HistoryItemDecoratorTests: XCTestCase {
XCTAssertEqual(itemDecorator.attributedTitle, nil)
}

func testRelativeCopyTimeFormatting() {
let date = Date(timeIntervalSince1970: -3 * 60 * 60)
let referenceDate = Date(timeIntervalSince1970: 0)

let relative = HistoryItemDecorator.formatRelativeCopyTime(
for: date,
relativeTo: referenceDate,
locale: Locale(identifier: "en_US")
)

XCTAssertEqual(relative, "3h ago")
}

private func historyItemDecorator(
_ value: String?,
application: String? = "com.apple.finder"
Expand Down