NetNewsWire/Mac/MainWindow/Sidebar/SidebarStatusBarView.swift

143 lines
3.4 KiB
Swift
Raw Normal View History

2017-12-17 01:36:35 +01:00
//
// SidebarStatusBarView.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
2017-12-17 01:36:35 +01:00
//
// Created by Brent Simmons on 9/17/16.
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
//
import AppKit
2017-12-17 01:36:35 +01:00
import RSCore
import Articles
2017-12-17 01:36:35 +01:00
import RSWeb
import Account
final class SidebarStatusBarView: NSView {
@IBOutlet var progressIndicator: NSProgressIndicator!
@IBOutlet var progressLabel: NSTextField!
@IBOutlet var bottomConstraint: NSLayoutConstraint!
@IBOutlet var heightConstraint: NSLayoutConstraint!
private var isAnimatingProgress = false
private var progress: CombinedRefreshProgress? = nil {
didSet {
CoalescingQueue.standard.add(self, #selector(updateUI))
}
}
2017-12-17 01:36:35 +01:00
override var isFlipped: Bool {
return true
2017-12-17 01:36:35 +01:00
}
2017-12-17 01:36:35 +01:00
override func awakeFromNib() {
progressIndicator.isHidden = true
progressLabel.isHidden = true
let progressLabelFontSize = progressLabel.font?.pointSize ?? 13.0
progressLabel.font = NSFont.monospacedDigitSystemFont(ofSize: progressLabelFontSize, weight: NSFont.Weight.regular)
progressLabel.stringValue = ""
NotificationCenter.default.addObserver(self, selector: #selector(progressDidChange(_:)), name: .AccountRefreshProgressDidChange, object: nil)
}
@objc func updateUI() {
2017-12-17 01:36:35 +01:00
guard let progress = progress else {
stopProgressIfNeeded()
return
}
2017-12-17 01:36:35 +01:00
updateProgressIndicator(progress)
updateProgressLabel(progress)
}
// MARK: Notifications
2017-12-17 01:36:35 +01:00
@objc dynamic func progressDidChange(_ notification: Notification) {
progress = AccountManager.shared.combinedRefreshProgress
}
2017-12-17 01:36:35 +01:00
}
private extension SidebarStatusBarView {
// MARK: Progress
static let animationDuration = 0.2
2017-12-17 01:36:35 +01:00
func stopProgressIfNeeded() {
if !isAnimatingProgress {
return
}
isAnimatingProgress = false
self.progressIndicator.stopAnimation(self)
progressIndicator.isHidden = true
progressLabel.isHidden = true
superview?.layoutSubtreeIfNeeded()
NSAnimationContext.runAnimationGroup{ (context) in
context.duration = SidebarStatusBarView.animationDuration
context.allowsImplicitAnimation = true
bottomConstraint.constant = -(heightConstraint.constant)
superview?.layoutSubtreeIfNeeded()
}
2017-12-17 01:36:35 +01:00
}
func startProgressIfNeeded() {
if isAnimatingProgress {
return
}
isAnimatingProgress = true
progressIndicator.isHidden = false
progressLabel.isHidden = false
2017-12-17 01:36:35 +01:00
progressIndicator.startAnimation(self)
superview?.layoutSubtreeIfNeeded()
NSAnimationContext.runAnimationGroup{ (context) in
context.duration = SidebarStatusBarView.animationDuration
context.allowsImplicitAnimation = true
bottomConstraint.constant = 0
superview?.layoutSubtreeIfNeeded()
}
2017-12-17 01:36:35 +01:00
}
func updateProgressIndicator(_ progress: CombinedRefreshProgress) {
if progress.isComplete {
stopProgressIfNeeded()
return
}
startProgressIfNeeded()
let maxValue = Double(progress.numberOfTasks)
if progressIndicator.maxValue != maxValue {
progressIndicator.maxValue = maxValue
}
let doubleValue = Double(progress.numberCompleted)
if progressIndicator.doubleValue != doubleValue {
progressIndicator.doubleValue = doubleValue
}
}
func updateProgressLabel(_ progress: CombinedRefreshProgress) {
if progress.isComplete {
progressLabel.stringValue = ""
return
}
let formatString = NSLocalizedString("%@ of %@", comment: "Status bar progress")
let s = NSString(format: formatString as NSString, NSNumber(value: progress.numberCompleted), NSNumber(value: progress.numberOfTasks))
2017-12-17 01:36:35 +01:00
progressLabel.stringValue = s as String
}
}