chore: add debounce for refresh

This commit is contained in:
sunxiaojian 2021-04-22 10:11:19 +08:00
parent 106a5cc71a
commit 46fe59c920
2 changed files with 11 additions and 7 deletions

View File

@ -34,6 +34,7 @@ final class HomeTimelineViewModel: NSObject {
weak var tableView: UITableView?
weak var timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate?
let homeTimelineNeedRefresh = PassthroughSubject<Void, Never>()
// 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 { }

View File

@ -14,7 +14,7 @@ import os.log
import UIKit
protocol SuggestionAccountViewModelDelegate: AnyObject {
func homeTimelineNeedRefresh()
var homeTimelineNeedRefresh: PassthroughSubject<Void, Never> { get }
}
final class SuggestionAccountViewModel: NSObject {
var disposeBag = Set<AnyCancellable>()
@ -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