From 6f8666aaa8931cd76883368721f791b9fed9966b Mon Sep 17 00:00:00 2001 From: CMK Date: Mon, 21 Jun 2021 00:26:23 +0800 Subject: [PATCH] feat: add timestamp updater --- .../Share/View/Node/Status/StatusNode.swift | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/Mastodon/Scene/Share/View/Node/Status/StatusNode.swift b/Mastodon/Scene/Share/View/Node/Status/StatusNode.swift index d4a3b3ba7..c4ec3e680 100644 --- a/Mastodon/Scene/Share/View/Node/Status/StatusNode.swift +++ b/Mastodon/Scene/Share/View/Node/Status/StatusNode.swift @@ -18,6 +18,8 @@ protocol StatusNodeDelegate: AnyObject { final class StatusNode: ASCellNode { var disposeBag = Set() + var timestamp: Date + var timestampSubscription: AnyCancellable? weak var delegate: StatusNodeDelegate? // needs assign on main queue static let avatarImageSize = CGSize(width: 42, height: 42) @@ -61,6 +63,7 @@ final class StatusNode: ASCellNode { }() init(status: Status) { + timestamp = (status.reblog ?? status).createdAt super.init() automaticallyManagesSubnodes = true @@ -78,23 +81,10 @@ final class StatusNode: ASCellNode { .font: UIFont.systemFont(ofSize: 13, weight: .regular) ]) // set date - let createdAt = (status.reblog ?? status).createdAt - dateTextNode.attributedText = NSAttributedString(string: createdAt.slowedTimeAgoSinceNow, attributes: [ + dateTextNode.attributedText = NSAttributedString(string: timestamp.slowedTimeAgoSinceNow, attributes: [ .foregroundColor: Asset.Colors.Label.secondary.color, .font: UIFont.systemFont(ofSize: 13, weight: .regular) ]) -// RunLoop.main.perform { [weak self] in -// guard let self = self else { return } -// AppContext.shared.timestampUpdatePublisher -// .sink { [weak self] _ in -// guard let self = self else { return } -// self.dateTextNode.attributedText = NSAttributedString(string: createdAt.slowedTimeAgoSinceNow, attributes: [ -// .foregroundColor: Asset.Colors.Label.secondary.color, -// .font: UIFont.systemFont(ofSize: 13, weight: .regular) -// ]) -// } -// .store(in: &self.disposeBag) -// } usernameTextNode.attributedText = NSAttributedString(string: "@" + status.author.acct, attributes: [ .foregroundColor: Asset.Colors.Label.secondary.color, @@ -113,6 +103,15 @@ final class StatusNode: ASCellNode { override func didEnterDisplayState() { super.didEnterDisplayState() + timestampSubscription = AppContext.shared.timestampUpdatePublisher + .sink { [weak self] _ in + guard let self = self else { return } + self.dateTextNode.attributedText = NSAttributedString(string: self.timestamp.slowedTimeAgoSinceNow, attributes: [ + .foregroundColor: Asset.Colors.Label.secondary.color, + .font: UIFont.systemFont(ofSize: 13, weight: .regular) + ]) + } + statusContentTextNode.textView.isEditable = false statusContentTextNode.textView.textDragInteraction?.isEnabled = false statusContentTextNode.textView.linkTextAttributes = [ @@ -120,6 +119,11 @@ final class StatusNode: ASCellNode { ] } + override func didExitVisibleState() { + super.didExitVisibleState() + timestampSubscription = nil + } + override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { let headerStack = ASStackLayoutSpec.horizontal() headerStack.alignItems = .center