2017-11-19 22:57:42 +01:00
|
|
|
//
|
2017-11-20 00:40:02 +01:00
|
|
|
// SmartFeed.swift
|
2017-11-19 22:57:42 +01:00
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/19/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2017-11-20 00:40:02 +01:00
|
|
|
import RSCore
|
2017-11-19 22:57:42 +01:00
|
|
|
import Data
|
|
|
|
import Account
|
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
protocol SmartFeedDelegate: DisplayNameProvider {
|
2017-11-19 22:57:42 +01:00
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
func fetchUnreadCount(for: Account, callback: @escaping (Int) -> Void)
|
|
|
|
}
|
|
|
|
|
|
|
|
final class SmartFeed: PseudoFeed {
|
|
|
|
|
|
|
|
var nameForDisplay: String {
|
|
|
|
get {
|
|
|
|
return delegate.nameForDisplay
|
|
|
|
}
|
|
|
|
}
|
2017-11-19 22:57:42 +01:00
|
|
|
|
|
|
|
var unreadCount = 0 {
|
|
|
|
didSet {
|
|
|
|
if unreadCount != oldValue {
|
|
|
|
postUnreadCountDidChangeNotification()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
private let delegate: SmartFeedDelegate
|
2017-11-19 22:57:42 +01:00
|
|
|
private var timer: Timer?
|
|
|
|
private var unreadCounts = [Account: Int]()
|
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
init(delegate: SmartFeedDelegate) {
|
2017-11-19 22:57:42 +01:00
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
self.delegate = delegate
|
2017-11-19 22:57:42 +01:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
|
|
|
startTimer() // Fetch unread count at startup
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func unreadCountDidChange(_ note: Notification) {
|
|
|
|
|
|
|
|
if note.object is Account {
|
|
|
|
startTimer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
private extension SmartFeed {
|
2017-11-19 22:57:42 +01:00
|
|
|
|
|
|
|
// MARK: - Unread Counts
|
|
|
|
|
|
|
|
private func fetchUnreadCount(for account: Account) {
|
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
delegate.fetchUnreadCount(for: account) { (accountUnreadCount) in
|
2017-11-19 22:57:42 +01:00
|
|
|
self.unreadCounts[account] = accountUnreadCount
|
|
|
|
self.updateUnreadCount()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func fetchUnreadCounts() {
|
|
|
|
|
|
|
|
AccountManager.shared.accounts.forEach { self.fetchUnreadCount(for: $0) }
|
|
|
|
}
|
|
|
|
|
|
|
|
private func updateUnreadCount() {
|
|
|
|
|
|
|
|
unreadCount = AccountManager.shared.accounts.reduce(0) { (result, account) -> Int in
|
|
|
|
if let oneUnreadCount = unreadCounts[account] {
|
|
|
|
return result + oneUnreadCount
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Timer
|
|
|
|
|
|
|
|
func stopTimer() {
|
|
|
|
|
|
|
|
if let timer = timer {
|
|
|
|
timer.rs_invalidateIfValid()
|
|
|
|
}
|
|
|
|
timer = nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 01:28:26 +01:00
|
|
|
private static let fetchCoalescingDelay: TimeInterval = 0.1
|
2017-11-19 22:57:42 +01:00
|
|
|
|
|
|
|
func startTimer() {
|
|
|
|
|
|
|
|
stopTimer()
|
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
timer = Timer.scheduledTimer(withTimeInterval: SmartFeed.fetchCoalescingDelay, repeats: false, block: { (timer) in
|
2017-11-19 22:57:42 +01:00
|
|
|
self.fetchUnreadCounts()
|
|
|
|
self.stopTimer()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|