2017-05-23 22:14:30 +02:00
|
|
|
//
|
|
|
|
// UnreadCountProtocol.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
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
|
|
|
|
|
2017-10-07 20:56:22 +02:00
|
|
|
public extension Notification.Name {
|
2017-05-23 22:14:30 +02:00
|
|
|
|
2019-02-12 16:04:18 +01:00
|
|
|
static let UnreadCountDidChange = Notification.Name(rawValue: "UnreadCountDidChange")
|
2017-05-23 22:14:30 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:22 +02:00
|
|
|
public protocol UnreadCountProvider {
|
2017-05-23 22:14:30 +02:00
|
|
|
|
2017-10-07 20:56:22 +02:00
|
|
|
var unreadCount: Int { get }
|
2017-05-23 22:14:30 +02:00
|
|
|
|
2017-10-07 20:56:22 +02:00
|
|
|
func postUnreadCountDidChangeNotification()
|
|
|
|
func calculateUnreadCount<T: Collection>(_ children: T) -> Int
|
2017-05-23 22:14:30 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:22 +02:00
|
|
|
|
2017-05-23 22:14:30 +02:00
|
|
|
public extension UnreadCountProvider {
|
|
|
|
|
2019-02-12 16:04:18 +01:00
|
|
|
func postUnreadCountDidChangeNotification() {
|
2017-05-23 22:14:30 +02:00
|
|
|
|
2017-10-07 20:56:22 +02:00
|
|
|
NotificationCenter.default.post(name: .UnreadCountDidChange, object: self, userInfo: nil)
|
|
|
|
}
|
|
|
|
|
2019-02-12 16:04:18 +01:00
|
|
|
func calculateUnreadCount<T: Collection>(_ children: T) -> Int {
|
2017-10-07 20:56:22 +02:00
|
|
|
|
|
|
|
let updatedUnreadCount = children.reduce(0) { (result, oneChild) -> Int in
|
|
|
|
if let oneUnreadCountProvider = oneChild as? UnreadCountProvider {
|
|
|
|
return result + oneUnreadCountProvider.unreadCount
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
return updatedUnreadCount
|
2017-05-23 22:14:30 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-07 20:56:22 +02:00
|
|
|
|
|
|
|
|