2019-10-25 20:34:59 +02:00
|
|
|
//
|
|
|
|
// RefeshProgressView.swift
|
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 10/24/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Account
|
|
|
|
|
|
|
|
class RefreshProgressView: UIView {
|
|
|
|
|
|
|
|
@IBOutlet weak var progressView: UIProgressView!
|
|
|
|
@IBOutlet weak var label: UILabel!
|
2020-02-01 21:19:39 +01:00
|
|
|
private lazy var progressWidthConstraint = progressView.widthAnchor.constraint(equalToConstant: 100.0)
|
2019-10-25 20:34:59 +02:00
|
|
|
|
2020-01-03 22:23:37 +01:00
|
|
|
override func awakeFromNib() {
|
2019-10-25 20:34:59 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(progressDidChange(_:)), name: .AccountRefreshProgressDidChange, object: nil)
|
2020-01-17 22:33:06 +01:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(contentSizeCategoryDidChange(_:)), name: UIContentSizeCategory.didChangeNotification, object: nil)
|
|
|
|
|
2020-01-03 22:23:37 +01:00
|
|
|
if !AccountManager.shared.combinedRefreshProgress.isComplete {
|
|
|
|
progressChanged()
|
|
|
|
} else {
|
|
|
|
updateRefreshLabel()
|
|
|
|
}
|
2020-01-09 02:07:58 +01:00
|
|
|
|
|
|
|
scheduleUpdateRefreshLabel()
|
2019-10-25 20:34:59 +02:00
|
|
|
}
|
|
|
|
|
2020-02-01 21:19:39 +01:00
|
|
|
override func didMoveToSuperview() {
|
|
|
|
progressChanged()
|
|
|
|
}
|
|
|
|
|
2019-10-25 20:34:59 +02:00
|
|
|
func updateRefreshLabel() {
|
2019-12-10 02:34:26 +01:00
|
|
|
if let accountLastArticleFetchEndTime = AccountManager.shared.lastArticleFetchEndTime {
|
2020-01-03 22:23:37 +01:00
|
|
|
|
2020-01-07 02:43:23 +01:00
|
|
|
if Date() > accountLastArticleFetchEndTime.addingTimeInterval(60) {
|
2020-01-03 22:23:37 +01:00
|
|
|
|
2019-10-25 23:27:10 +02:00
|
|
|
let relativeDateTimeFormatter = RelativeDateTimeFormatter()
|
|
|
|
relativeDateTimeFormatter.dateTimeStyle = .named
|
2019-12-10 02:34:26 +01:00
|
|
|
let refreshed = relativeDateTimeFormatter.localizedString(for: accountLastArticleFetchEndTime, relativeTo: Date())
|
2019-10-25 23:27:10 +02:00
|
|
|
let localizedRefreshText = NSLocalizedString("Updated %@", comment: "Updated")
|
|
|
|
let refreshText = NSString.localizedStringWithFormat(localizedRefreshText as NSString, refreshed) as String
|
|
|
|
label.text = refreshText
|
2020-01-03 22:23:37 +01:00
|
|
|
|
2019-10-25 23:27:10 +02:00
|
|
|
} else {
|
2020-01-07 02:43:23 +01:00
|
|
|
label.text = NSLocalizedString("Updated Just Now", comment: "Updated Just Now")
|
2019-10-25 23:27:10 +02:00
|
|
|
}
|
2020-01-03 22:23:37 +01:00
|
|
|
|
2019-12-10 02:34:26 +01:00
|
|
|
} else {
|
|
|
|
label.text = ""
|
2019-10-25 20:34:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-10-25 23:27:10 +02:00
|
|
|
|
2019-10-25 20:34:59 +02:00
|
|
|
@objc func progressDidChange(_ note: Notification) {
|
2020-01-03 22:23:37 +01:00
|
|
|
progressChanged()
|
|
|
|
}
|
|
|
|
|
2020-01-17 22:33:06 +01:00
|
|
|
@objc func contentSizeCategoryDidChange(_ note: Notification) {
|
|
|
|
// This hack is probably necessary because custom views in the toolbar don't get
|
|
|
|
// notifications that the content size changed.
|
|
|
|
label.font = UIFont.preferredFont(forTextStyle: .footnote)
|
|
|
|
}
|
|
|
|
|
2020-01-03 22:23:37 +01:00
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private extension RefreshProgressView {
|
|
|
|
|
|
|
|
func progressChanged() {
|
2020-02-01 21:19:39 +01:00
|
|
|
// Layout may crash if not in the view hierarchy.
|
|
|
|
// https://github.com/Ranchero-Software/NetNewsWire/issues/1764
|
|
|
|
let isInViewHierarchy = self.superview != nil
|
|
|
|
|
2019-10-25 20:34:59 +02:00
|
|
|
let progress = AccountManager.shared.combinedRefreshProgress
|
|
|
|
|
|
|
|
if progress.isComplete {
|
2020-02-01 21:19:39 +01:00
|
|
|
if isInViewHierarchy {
|
|
|
|
progressView.setProgress(1, animated: true)
|
|
|
|
}
|
2019-10-25 20:34:59 +02:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
|
|
self.updateRefreshLabel()
|
|
|
|
self.label.isHidden = false
|
|
|
|
self.progressView.isHidden = true
|
2020-02-01 21:19:39 +01:00
|
|
|
self.progressWidthConstraint.isActive = false
|
|
|
|
if isInViewHierarchy {
|
|
|
|
self.progressView.setProgress(0, animated: true)
|
|
|
|
}
|
2019-10-25 20:34:59 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
label.isHidden = true
|
|
|
|
progressView.isHidden = false
|
2020-02-01 21:19:39 +01:00
|
|
|
progressWidthConstraint.isActive = true
|
|
|
|
if isInViewHierarchy {
|
|
|
|
progressView.setNeedsLayout()
|
|
|
|
progressView.layoutIfNeeded()
|
|
|
|
let percent = Float(progress.numberCompleted) / Float(progress.numberOfTasks)
|
|
|
|
|
|
|
|
// Don't let the progress bar go backwards unless we need to go back more than 25%
|
|
|
|
if percent > progressView.progress || progressView.progress - percent > 0.25 {
|
|
|
|
progressView.setProgress(percent, animated: true)
|
|
|
|
}
|
2020-01-07 03:09:46 +01:00
|
|
|
}
|
2019-10-25 20:34:59 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-09 02:07:58 +01:00
|
|
|
|
|
|
|
func scheduleUpdateRefreshLabel() {
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 60) { [weak self] in
|
|
|
|
self?.updateRefreshLabel()
|
|
|
|
self?.scheduleUpdateRefreshLabel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 20:34:59 +02:00
|
|
|
}
|