From 46fe59c92016b835bdf5544033adb5d6c4613e0d Mon Sep 17 00:00:00 2001 From: sunxiaojian Date: Thu, 22 Apr 2021 10:11:19 +0800 Subject: [PATCH] chore: add debounce for refresh --- .../Scene/HomeTimeline/HomeTimelineViewModel.swift | 14 +++++++++----- .../SuggestionAccountViewModel.swift | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift index c154b0508..a81056d0f 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel.swift @@ -34,6 +34,7 @@ final class HomeTimelineViewModel: NSObject { weak var tableView: UITableView? weak var timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate? + let homeTimelineNeedRefresh = PassthroughSubject() // output // top loader private(set) lazy var loadLatestStateMachine: GKStateMachine = { @@ -122,6 +123,13 @@ final class HomeTimelineViewModel: NSObject { } .store(in: &disposeBag) + homeTimelineNeedRefresh + .debounce(for: 0.3, scheduler: DispatchQueue.main) + .sink { [weak self] _ in + self?.loadLatestStateMachine.enter(LoadLatestState.Loading.self) + } + .store(in: &disposeBag) + } deinit { @@ -130,8 +138,4 @@ final class HomeTimelineViewModel: NSObject { } -extension HomeTimelineViewModel: SuggestionAccountViewModelDelegate { - func homeTimelineNeedRefresh() { - loadLatestStateMachine.enter(LoadLatestState.Loading.self) - } -} +extension HomeTimelineViewModel: SuggestionAccountViewModelDelegate { } diff --git a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift index ec52120dc..494b00293 100644 --- a/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift +++ b/Mastodon/Scene/SuggestionAccount/SuggestionAccountViewModel.swift @@ -14,7 +14,7 @@ import os.log import UIKit protocol SuggestionAccountViewModelDelegate: AnyObject { - func homeTimelineNeedRefresh() + var homeTimelineNeedRefresh: PassthroughSubject { get } } final class SuggestionAccountViewModel: NSObject { var disposeBag = Set() @@ -141,7 +141,7 @@ final class SuggestionAccountViewModel: NSObject { case .failure(let error): os_log("%{public}s[%{public}ld], %{public}s: follow failed. %s", (#file as NSString).lastPathComponent, #line, #function, error.localizedDescription) case .finished: - self.delegate?.homeTimelineNeedRefresh() + self.delegate?.homeTimelineNeedRefresh.send() break } } receiveValue: { _ in