From 11f5a3a91437252915c77efcf1eb328d8fd0f9a8 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Fri, 5 Mar 2021 21:48:11 -0800 Subject: [PATCH] Only refresh or alert if visible --- Extensions/UIVIewController+Extensions.swift | 2 ++ View Controllers/TableViewController.swift | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Extensions/UIVIewController+Extensions.swift b/Extensions/UIVIewController+Extensions.swift index f4786a4..803d4c1 100644 --- a/Extensions/UIVIewController+Extensions.swift +++ b/Extensions/UIVIewController+Extensions.swift @@ -5,6 +5,8 @@ import UIKit import ViewModels extension UIViewController { + var isVisible: Bool { isViewLoaded && view.window != nil && presentedViewController == nil } + func present(alertItem: AlertItem) { let alertController = UIAlertController( title: nil, diff --git a/View Controllers/TableViewController.swift b/View Controllers/TableViewController.swift index 3c11ec5..7e9a06a 100644 --- a/View Controllers/TableViewController.swift +++ b/View Controllers/TableViewController.swift @@ -422,7 +422,11 @@ private extension TableViewController { viewModel.alertItems .compactMap { $0 } - .sink { [weak self] in self?.present(alertItem: $0) } + .sink { [weak self] in + guard let self = self, self.isVisible else { return } + + self.present(alertItem: $0) + } .store(in: &cancellables) tableView.publisher(for: \.contentOffset) @@ -444,7 +448,11 @@ private extension TableViewController { NotificationCenter.default.publisher(for: UIScene.willEnterForegroundNotification) .merge(with: NotificationCenter.default.publisher(for: NewStatusViewController.newStatusPostedNotification)) - .sink { [weak self] _ in self?.refreshIfAble() } + .sink { [weak self] _ in + guard let self = self, self.isVisible else { return } + + self.refreshIfAble() + } .store(in: &cancellables) NotificationCenter.default.publisher(for: LoadMoreView.accessibilityCustomAction)