From 1f590ddf091884551b7e35f53832de6a59da2d25 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Tue, 26 Jan 2021 18:31:00 -0800 Subject: [PATCH] Notification timestamps --- .../Content/ContentDatabase+Migration.swift | 1 + .../DB/Content/NotificationRecord.swift | 3 +++ .../MastodonNotification+Extensions.swift | 1 + .../Entities/MastodonNotification.swift | 8 +++++++- .../View Models/NotificationViewModel.swift | 2 ++ Views/NotificationView.swift | 20 ++++++++++++++++--- 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/DB/Sources/DB/Content/ContentDatabase+Migration.swift b/DB/Sources/DB/Content/ContentDatabase+Migration.swift index c16bb05..7d0a0d1 100644 --- a/DB/Sources/DB/Content/ContentDatabase+Migration.swift +++ b/DB/Sources/DB/Content/ContentDatabase+Migration.swift @@ -198,6 +198,7 @@ extension ContentDatabase { t.column("id", .text).primaryKey(onConflict: .replace) t.column("type", .text).notNull() t.column("accountId", .text).notNull().references("accountRecord", onDelete: .cascade) + t.column("createdAt", .datetime).notNull() t.column("statusId").references("statusRecord", onDelete: .cascade) } diff --git a/DB/Sources/DB/Content/NotificationRecord.swift b/DB/Sources/DB/Content/NotificationRecord.swift index ec4855b..c408034 100644 --- a/DB/Sources/DB/Content/NotificationRecord.swift +++ b/DB/Sources/DB/Content/NotificationRecord.swift @@ -8,6 +8,7 @@ struct NotificationRecord: ContentDatabaseRecord, Hashable { let id: String let type: MastodonNotification.NotificationType let accountId: Account.Id + let createdAt: Date let statusId: Status.Id? } @@ -16,6 +17,7 @@ extension NotificationRecord { static let id = Column(CodingKeys.id) static let type = Column(CodingKeys.type) static let accountId = Column(CodingKeys.accountId) + static let createdAt = Column(CodingKeys.createdAt) static let statusId = Column(CodingKeys.statusId) } @@ -26,6 +28,7 @@ extension NotificationRecord { id = notification.id type = notification.type accountId = notification.account.id + createdAt = notification.createdAt statusId = notification.status?.id } } diff --git a/DB/Sources/DB/Extensions/MastodonNotification+Extensions.swift b/DB/Sources/DB/Extensions/MastodonNotification+Extensions.swift index dbadccf..c8df134 100644 --- a/DB/Sources/DB/Extensions/MastodonNotification+Extensions.swift +++ b/DB/Sources/DB/Extensions/MastodonNotification+Extensions.swift @@ -24,6 +24,7 @@ extension MastodonNotification { id: info.record.id, type: info.record.type, account: .init(info: info.accountInfo), + createdAt: info.record.createdAt, status: status) } } diff --git a/Mastodon/Sources/Mastodon/Entities/MastodonNotification.swift b/Mastodon/Sources/Mastodon/Entities/MastodonNotification.swift index 60cd759..ced870e 100644 --- a/Mastodon/Sources/Mastodon/Entities/MastodonNotification.swift +++ b/Mastodon/Sources/Mastodon/Entities/MastodonNotification.swift @@ -6,12 +6,18 @@ public struct MastodonNotification: Codable, Hashable { public let id: Id public let type: NotificationType public let account: Account + public let createdAt: Date public let status: Status? - public init(id: String, type: MastodonNotification.NotificationType, account: Account, status: Status?) { + public init(id: String, + type: MastodonNotification.NotificationType, + account: Account, + createdAt: Date, + status: Status?) { self.id = id self.type = type self.account = account + self.createdAt = createdAt self.status = status } } diff --git a/ViewModels/Sources/ViewModels/View Models/NotificationViewModel.swift b/ViewModels/Sources/ViewModels/View Models/NotificationViewModel.swift index ca044f7..bc2dcb2 100644 --- a/ViewModels/Sources/ViewModels/View Models/NotificationViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/NotificationViewModel.swift @@ -39,6 +39,8 @@ public extension NotificationViewModel { notificationService.notification.type } + var time: String? { notificationService.notification.createdAt.timeAgo } + func accountSelected() { eventsSubject.send( Just(.navigation( diff --git a/Views/NotificationView.swift b/Views/NotificationView.swift index 20a15c8..c49a3d7 100644 --- a/Views/NotificationView.swift +++ b/Views/NotificationView.swift @@ -10,6 +10,7 @@ final class NotificationView: UIView { private let avatarImageView = AnimatedImageView() private let avatarButton = UIButton() private let typeLabel = UILabel() + private let timeLabel = UILabel() private let displayNameLabel = UILabel() private let accountLabel = UILabel() private let statusBodyView = StatusBodyView() @@ -75,24 +76,29 @@ private extension NotificationView { func initialSetup() { let containerStackView = UIStackView() let sideStackView = UIStackView() + let typeTimeStackView = UIStackView() let mainStackView = UIStackView() addSubview(containerStackView) containerStackView.translatesAutoresizingMaskIntoConstraints = false containerStackView.spacing = .defaultSpacing + containerStackView.alignment = .top sideStackView.axis = .vertical sideStackView.alignment = .trailing sideStackView.spacing = .compactSpacing sideStackView.addArrangedSubview(iconImageView) sideStackView.addArrangedSubview(avatarImageView) - sideStackView.addArrangedSubview(UIView()) containerStackView.addArrangedSubview(sideStackView) + typeTimeStackView.spacing = .compactSpacing + typeTimeStackView.alignment = .top + mainStackView.axis = .vertical mainStackView.spacing = .compactSpacing - mainStackView.addArrangedSubview(typeLabel) - mainStackView.addSubview(UIView()) + typeTimeStackView.addArrangedSubview(typeLabel) + typeTimeStackView.addArrangedSubview(timeLabel) + mainStackView.addArrangedSubview(typeTimeStackView) mainStackView.addArrangedSubview(statusBodyView) mainStackView.addArrangedSubview(displayNameLabel) mainStackView.addArrangedSubview(accountLabel) @@ -121,6 +127,12 @@ private extension NotificationView { typeLabel.adjustsFontForContentSizeCategory = true typeLabel.numberOfLines = 0 + timeLabel.font = .preferredFont(forTextStyle: .subheadline) + timeLabel.adjustsFontForContentSizeCategory = true + timeLabel.textColor = .secondaryLabel + timeLabel.setContentCompressionResistancePriority(.required, for: .horizontal) + timeLabel.setContentHuggingPriority(.required, for: .horizontal) + statusBodyView.alpha = 0.5 statusBodyView.isUserInteractionEnabled = false @@ -205,6 +217,8 @@ private extension NotificationView { accountLabel.isHidden = true } + timeLabel.text = viewModel.time + iconImageView.image = UIImage( systemName: viewModel.type.systemImageName, withConfiguration: UIImage.SymbolConfiguration(scale: .medium))