From 527d2500245123e183a7b823de454f42b9ea1486 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Sun, 7 Apr 2024 13:53:13 +0200 Subject: [PATCH] Show "New Post :pill:" if new posts (IOS-234) --- .../HomeTimelineViewController.swift | 22 ++++++++++++++++--- ...omeTimelineViewModel+LoadLatestState.swift | 6 ++++- .../HomeTimeline/HomeTimelineViewModel.swift | 1 + .../HomeTimeline/TimelineStatusPill.swift | 1 - 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift index 605f016be..0ab223fd9 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift @@ -318,6 +318,20 @@ extension HomeTimelineViewController { self.timelinePillCenterXAnchor = timelinePillCenterXAnchor self.timelinePillVisibleTopAnchor = timelinePillVisibleTopAnchor self.timelinePillHiddenTopAnchor = timelinePillHiddenTopAnchor + + viewModel?.hasNewPosts + .receive(on: DispatchQueue.main) + .sink(receiveValue: { [weak self] hasNewPosts in + guard let self else { return } + + if hasNewPosts { + self.timelinePill.update(with: .newPosts) + self.showTimelinePill() + } else { + self.hideTimelinePill() + } + }) + .store(in: &disposeBag) } override func viewWillAppear(_ animated: Bool) { @@ -471,16 +485,18 @@ extension HomeTimelineViewController { @objc private func timelinePillPressed(_ sender: TimelineStatusPill) { guard let reason = sender.reason else { return } + switch reason { case .newPosts: - print("Bring me to the new posts and disappear") + scrollToTop(animated: true) + viewModel?.hasNewPosts.value = false case .postSent: print("Bring me to my post and disappear") case .offline: - print("Just disappear") + hideTimelinePill() } - hideTimelinePill() + } private func showTimelinePill() { diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift index 97093660d..fa7045f46 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift @@ -159,7 +159,11 @@ extension HomeTimelineViewModel.LoadLatestState { await UIImpactFeedbackGenerator(style: .light) .impactOccurred() } - + + if newStatuses.isNotEmpty { + viewModel.hasNewPosts.value = true + } + } catch { await enter(state: Idle.self) viewModel.didLoadLatest.send() diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift index e934fe1d0..7daabe67e 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift @@ -33,6 +33,7 @@ final class HomeTimelineViewModel: NSObject { @Published var scrollPositionRecord: ScrollPositionRecord? = nil @Published var displaySettingBarButtonItem = true @Published var hasPendingStatusEditReload = false + var hasNewPosts = CurrentValueSubject(false) var timelineContext: MastodonFeed.Kind.TimelineContext = .home weak var tableView: UITableView? diff --git a/Mastodon/Scene/HomeTimeline/TimelineStatusPill.swift b/Mastodon/Scene/HomeTimeline/TimelineStatusPill.swift index 1d24fe674..7846b8006 100644 --- a/Mastodon/Scene/HomeTimeline/TimelineStatusPill.swift +++ b/Mastodon/Scene/HomeTimeline/TimelineStatusPill.swift @@ -71,5 +71,4 @@ class TimelineStatusPill: UIButton { } } } - }