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-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-05-10 17:00:04 +02:00
|
|
|
update()
|
|
|
|
scheduleUpdateRefreshLabel()
|
|
|
|
}
|
|
|
|
|
|
|
|
func update() {
|
2020-01-03 22:23:37 +01:00
|
|
|
if !AccountManager.shared.combinedRefreshProgress.isComplete {
|
2020-04-29 21:29:21 +02:00
|
|
|
progressChanged(animated: false)
|
2020-01-03 22:23:37 +01:00
|
|
|
} else {
|
|
|
|
updateRefreshLabel()
|
|
|
|
}
|
2019-10-25 20:34:59 +02:00
|
|
|
}
|
2020-03-02 22:33:59 +01:00
|
|
|
|
2020-02-01 21:19:39 +01:00
|
|
|
override func didMoveToSuperview() {
|
2020-04-29 21:29:21 +02:00
|
|
|
progressChanged(animated: false)
|
2020-02-01 21:19:39 +01:00
|
|
|
}
|
2020-03-02 22:33:59 +01:00
|
|
|
|
2019-10-25 20:34:59 +02:00
|
|
|
@objc func progressDidChange(_ note: Notification) {
|
2020-04-29 21:29:21 +02:00
|
|
|
progressChanged(animated: true)
|
2020-01-03 22:23:37 +01:00
|
|
|
}
|
2020-03-02 22:33:59 +01:00
|
|
|
|
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-03-02 22:33:59 +01:00
|
|
|
|
2020-01-03 22:23:37 +01:00
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private extension RefreshProgressView {
|
2020-03-02 22:33:59 +01:00
|
|
|
|
2020-04-29 21:29:21 +02:00
|
|
|
func progressChanged(animated: Bool) {
|
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
|
2020-03-02 22:33:59 +01:00
|
|
|
|
2019-10-25 20:34:59 +02:00
|
|
|
let progress = AccountManager.shared.combinedRefreshProgress
|
2020-03-02 22:33:59 +01:00
|
|
|
|
2019-10-25 20:34:59 +02:00
|
|
|
if progress.isComplete {
|
2020-02-01 21:19:39 +01:00
|
|
|
if isInViewHierarchy {
|
2020-04-29 21:29:21 +02:00
|
|
|
progressView.setProgress(1, animated: animated)
|
2020-02-01 21:19:39 +01:00
|
|
|
}
|
2019-10-25 20:34:59 +02:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
2020-03-02 22:25:57 +01:00
|
|
|
// Check that there are no pending downloads.
|
|
|
|
if (AccountManager.shared.combinedRefreshProgress.isComplete) {
|
|
|
|
self.updateRefreshLabel()
|
|
|
|
self.label.isHidden = false
|
|
|
|
self.progressView.isHidden = true
|
|
|
|
if self.superview != nil {
|
2020-04-29 21:29:21 +02:00
|
|
|
self.progressView.setProgress(0, animated: animated)
|
2020-03-02 22:25:57 +01:00
|
|
|
}
|
2020-02-01 21:19:39 +01:00
|
|
|
}
|
2019-10-25 20:34:59 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
label.isHidden = true
|
|
|
|
progressView.isHidden = false
|
2020-02-01 21:19:39 +01:00
|
|
|
if isInViewHierarchy {
|
|
|
|
let percent = Float(progress.numberCompleted) / Float(progress.numberOfTasks)
|
2020-03-02 22:33:59 +01:00
|
|
|
|
2020-02-01 21:19:39 +01:00
|
|
|
// 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 {
|
2020-04-29 21:29:21 +02:00
|
|
|
progressView.setProgress(percent, animated: animated)
|
2020-02-01 21:19:39 +01:00
|
|
|
}
|
2020-01-07 03:09:46 +01:00
|
|
|
}
|
2019-10-25 20:34:59 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-09 02:07:58 +01:00
|
|
|
|
2020-05-10 17:00:04 +02:00
|
|
|
func updateRefreshLabel() {
|
|
|
|
if let accountLastArticleFetchEndTime = AccountManager.shared.lastArticleFetchEndTime {
|
|
|
|
|
|
|
|
if Date() > accountLastArticleFetchEndTime.addingTimeInterval(60) {
|
|
|
|
|
|
|
|
let relativeDateTimeFormatter = RelativeDateTimeFormatter()
|
|
|
|
relativeDateTimeFormatter.dateTimeStyle = .named
|
|
|
|
let refreshed = relativeDateTimeFormatter.localizedString(for: accountLastArticleFetchEndTime, relativeTo: Date())
|
|
|
|
let localizedRefreshText = NSLocalizedString("Updated %@", comment: "Updated")
|
|
|
|
let refreshText = NSString.localizedStringWithFormat(localizedRefreshText as NSString, refreshed) as String
|
|
|
|
label.text = refreshText
|
|
|
|
|
|
|
|
} else {
|
|
|
|
label.text = NSLocalizedString("Updated Just Now", comment: "Updated Just Now")
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
label.text = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|