2017-05-23 22:14:30 +02:00
|
|
|
//
|
|
|
|
// UnreadCountProtocol.swift
|
2017-05-27 19:43:27 +02:00
|
|
|
// Evergreen
|
2017-05-23 22:14:30 +02:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 4/8/16.
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
public protocol UnreadCountProvider {
|
|
|
|
|
2017-09-17 00:25:38 +02:00
|
|
|
var unreadCount: Int { get }
|
2017-05-23 22:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public func calculateUnreadCount<T: Collection>(_ children: T) -> Int {
|
|
|
|
|
2017-07-03 19:29:44 +02:00
|
|
|
let updatedUnreadCount = children.reduce(0) { (result, oneChild) -> Int in
|
2017-05-23 22:14:30 +02:00
|
|
|
if let oneUnreadCountProvider = oneChild as? UnreadCountProvider {
|
2017-07-03 19:29:44 +02:00
|
|
|
return result + oneUnreadCountProvider.unreadCount
|
2017-05-23 22:14:30 +02:00
|
|
|
}
|
2017-07-03 19:29:44 +02:00
|
|
|
return result
|
2017-05-23 22:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return updatedUnreadCount
|
|
|
|
}
|
|
|
|
|
|
|
|
public extension UnreadCountProvider {
|
|
|
|
|
|
|
|
public func postUnreadCountDidChangeNotification() {
|
|
|
|
|
|
|
|
NotificationCenter.default.post(name: .UnreadCountDidChange, object: self, userInfo: [unreadCountKey: unreadCount])
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|