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.
|
|
|
|
//
|
|
|
|
|
2018-02-03 07:51:32 +01:00
|
|
|
import AppKit
|
2017-12-17 01:36:35 +01:00
|
|
|
import RSCore
|
2018-07-24 03:29:08 +02:00
|
|
|
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!
|
2019-02-02 05:53:02 +01:00
|
|
|
@IBOutlet var bottomConstraint: NSLayoutConstraint!
|
2019-02-02 05:26:40 +01:00
|
|
|
@IBOutlet var heightConstraint: NSLayoutConstraint!
|
2019-02-02 05:53:02 +01:00
|
|
|
|
|
|
|
private var isAnimatingProgress = false
|
2018-02-18 03:10:00 +01:00
|
|
|
|
|
|
|
private var progress: CombinedRefreshProgress? = nil {
|
|
|
|
didSet {
|
|
|
|
CoalescingQueue.standard.add(self, #selector(updateUI))
|
|
|
|
}
|
|
|
|
}
|
2017-12-17 01:36:35 +01:00
|
|
|
override var isFlipped: Bool {
|
2018-02-14 22:14:25 +01:00
|
|
|
return true
|
2017-12-17 01:36:35 +01:00
|
|
|
}
|
2018-02-19 07:13:20 +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)
|
|
|
|
}
|
|
|
|
|
2018-02-18 03:10:00 +01:00
|
|
|
@objc func updateUI() {
|
2017-12-17 01:36:35 +01:00
|
|
|
|
2018-02-18 03:10:00 +01:00
|
|
|
guard let progress = progress else {
|
|
|
|
stopProgressIfNeeded()
|
|
|
|
return
|
|
|
|
}
|
2017-12-17 01:36:35 +01:00
|
|
|
|
|
|
|
updateProgressIndicator(progress)
|
|
|
|
updateProgressLabel(progress)
|
|
|
|
}
|
|
|
|
|
2018-02-18 03:10:00 +01:00
|
|
|
// MARK: Notifications
|
2017-12-17 01:36:35 +01:00
|
|
|
|
2018-02-18 03:10:00 +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
|
2019-02-02 05:53:02 +01:00
|
|
|
|
|
|
|
static let animationDuration = 0.2
|
|
|
|
|
2017-12-17 01:36:35 +01:00
|
|
|
func stopProgressIfNeeded() {
|
|
|
|
|
|
|
|
if !isAnimatingProgress {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isAnimatingProgress = false
|
2019-02-02 05:53:02 +01:00
|
|
|
self.progressIndicator.stopAnimation(self)
|
|
|
|
progressIndicator.isHidden = true
|
|
|
|
progressLabel.isHidden = true
|
2019-02-02 05:26:40 +01:00
|
|
|
|
|
|
|
superview?.layoutSubtreeIfNeeded()
|
|
|
|
|
2019-02-02 05:53:02 +01:00
|
|
|
NSAnimationContext.runAnimationGroup{ (context) in
|
|
|
|
context.duration = SidebarStatusBarView.animationDuration
|
2019-02-02 05:26:40 +01:00
|
|
|
context.allowsImplicitAnimation = true
|
2019-02-02 05:53:02 +01:00
|
|
|
bottomConstraint.constant = -(heightConstraint.constant)
|
2019-02-02 05:26:40 +01:00
|
|
|
superview?.layoutSubtreeIfNeeded()
|
|
|
|
}
|
2017-12-17 01:36:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func startProgressIfNeeded() {
|
|
|
|
|
|
|
|
if isAnimatingProgress {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isAnimatingProgress = true
|
2019-02-02 05:53:02 +01:00
|
|
|
progressIndicator.isHidden = false
|
|
|
|
progressLabel.isHidden = false
|
2017-12-17 01:36:35 +01:00
|
|
|
progressIndicator.startAnimation(self)
|
2019-02-02 05:53:02 +01:00
|
|
|
|
|
|
|
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")
|
2019-02-02 05:53:02 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|