Repository: Automattic/pocket-casts-ios
File: podcasts/PodcastListViewController.swift (Swift)

Code before:
            guard let self = self else { return }

            self.updateNavigationButtons()
        }
    }

    private func loadBannerAd() {
        if FeatureFlag.bannerAdPodcasts.enabled && !SubscriptionHelper.hasActiveSubscription() {
            bannerTask?.cancel()
            DiscoverServerHandler.shared.blazePromotion(for: .podcastList) { [weak self] promotion, shouldAnimate in
                guard let self = self else { return }

                if shouldAnimate {
                    self.bannerTask = Task { [weak self] in
                        try? await Task.sleep(for: .seconds(2))
                        await MainActor.run {
                            self?.setupBannerAd(promotion: promotion, shouldAnimate: true)
                        }
                    }
                } else {
                    self.setupBannerAd(promotion: promotion, shouldAnimate: false)
                }
            }
        } else {
            if bannerAdModel != nil {
                bannerAdModel = nil
                UIView.animate(withDuration: 0.25, delay: 0, options: [.curveEaseOut]) {
                    self.isAnimatingBannerAd = false
                } completion: { _ in
                    self.podcastsCollectionView.performBatchUpdates({
                        self.podcastsCollectionView.collectionViewLayout.invalidateLayout()
                    })
                }
            }
        }
    }

    private func makeBadge(size: CGFloat) -> UIView {
        let badgeView = CircleView()
        badgeView.borderColor = ThemeColor.secondaryUi01()
        badgeView.centerColor = ThemeColor.primaryInteractive01()
        badgeView.backgroundColor = .clear
        badgeView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            badgeView.widthAnchor.constraint(equalToConstant: size),
            badgeView.heightAnchor.constraint(equalToConstant: size),
        ])
        return badgeView
    }

    private func makeProfileButton(email: String?) -> UIBarButtonItem {

Review comment (refactor):
The only thing I wonder if whether `bannerTask?.cancel()` should be added here or moved outside of the `if FeatureFlag.bannerAdPodcasts.enabled && !SubscriptionHelper.hasActiveSubscription() {` block.

I'm not sure it really matters much but I could see a theoretical case where the task is running and flag or subscription changes and then the task comes back.

Code after:
            guard let self = self else { return }

            self.updateNavigationButtons()
        }
    }

    private func loadBannerAd() {
        bannerTask?.cancel()

        if FeatureFlag.bannerAdPodcasts.enabled && !SubscriptionHelper.hasActiveSubscription() {
            DiscoverServerHandler.shared.blazePromotion(for: .podcastList) { [weak self] promotion, shouldAnimate in
                guard let self = self else { return }

                if shouldAnimate {
                    self.bannerTask = Task { [weak self] in
                        try? await Task.sleep(for: .seconds(2))
                        await MainActor.run {
                            self?.setupBannerAd(promotion: promotion, shouldAnimate: true)
                        }
                    }
                } else {
                    self.setupBannerAd(promotion: promotion, shouldAnimate: false)
                }
            }
        } else {
            if bannerAdModel != nil {
                bannerAdModel = nil
                UIView.animate(withDuration: 0.25, delay: 0, options: [.curveEaseOut]) {
                    self.isAnimatingBannerAd = false
                } completion: { _ in
                    self.podcastsCollectionView.performBatchUpdates({
                        self.podcastsCollectionView.collectionViewLayout.invalidateLayout()
                    })
                }
            }
        }
    }

    private func makeBadge(size: CGFloat) -> UIView {
        let badgeView = CircleView()
        badgeView.borderColor = ThemeColor.secondaryUi01()
        badgeView.centerColor = ThemeColor.primaryInteractive01()
        badgeView.backgroundColor = .clear
        badgeView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            badgeView.widthAnchor.constraint(equalToConstant: size),
            badgeView.heightAnchor.constraint(equalToConstant: size),
        ])
        return badgeView
    }