From a8ae559c9d55eb2edeca321f19bfdd3d957a47c0 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 5 Sep 2019 19:53:08 -0400 Subject: [PATCH] Add timestamp to follow notifications --- ...ActionNotificationGroupTableViewCell.swift | 2 +- ...FollowNotificationGroupTableViewCell.swift | 40 ++++++++++++++++++- .../FollowNotificationGroupTableViewCell.xib | 27 ++++++++++--- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift b/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift index 1febc504af..e32e9cdf37 100644 --- a/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift +++ b/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift @@ -94,7 +94,7 @@ class ActionNotificationGroupTableViewCell: UITableViewCell { func updateTimestamp() { guard let id = group.notificationIDs.first, let notification = MastodonCache.notification(for: id) else { - fatalError("Missing cached status") + fatalError("Missing cached notification") } timestampLabel.text = notification.createdAt.timeAgoString() diff --git a/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.swift b/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.swift index eb90024f43..ab07991193 100644 --- a/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.swift +++ b/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.swift @@ -14,10 +14,13 @@ class FollowNotificationGroupTableViewCell: UITableViewCell { var delegate: TuskerNavigationDelegate? @IBOutlet weak var avatarStackView: UIStackView! + @IBOutlet weak var timestampLabel: UILabel! @IBOutlet weak var actionLabel: UILabel! var group: NotificationGroup! + var updateTimestampWorkItem: DispatchWorkItem? + override func awakeFromNib() { super.awakeFromNib() @@ -39,6 +42,7 @@ class FollowNotificationGroupTableViewCell: UITableViewCell { let people = group.notificationIDs.compactMap(MastodonCache.notification(for:)).map { $0.account } updateActionLabel(people: people) + updateTimestamp() avatarStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } for account in people { @@ -74,5 +78,39 @@ class FollowNotificationGroupTableViewCell: UITableViewCell { } actionLabel.text = "Followed by \(peopleStr)" } - + + func updateTimestamp() { + guard let id = group.notificationIDs.first, + let notification = MastodonCache.notification(for: id) else { + fatalError("Missing cached notification") + } + + timestampLabel.text = notification.createdAt.timeAgoString() + + let delay: DispatchTimeInterval? + switch notification.createdAt.timeAgo().1 { + case .second: + delay = .seconds(10) + case .minute: + delay = .seconds(60) + default: + delay = nil + } + if let delay = delay { + updateTimestampWorkItem = DispatchWorkItem { + self.updateTimestamp() + } + DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!) + } else { + updateTimestampWorkItem = nil + } + } + + override func prepareForReuse() { + super.prepareForReuse() + + updateTimestampWorkItem?.cancel() + updateTimestampWorkItem = nil + } + } diff --git a/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.xib b/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.xib index c853d3e847..654c1f5f51 100644 --- a/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.xib +++ b/Tusker/Views/Notifications/FollowNotificationGroupTableViewCell.xib @@ -16,17 +16,31 @@ - + - + - - - + + + + + + + + + + + +