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!
|
|
|
|
private lazy var progressWidth = progressView.widthAnchor.constraint(equalToConstant: 100.0)
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private extension RefreshProgressView {
|
|
|
|
|
|
|
|
func progressChanged() {
|
2019-10-25 20:34:59 +02:00
|
|
|
let progress = AccountManager.shared.combinedRefreshProgress
|
|
|
|
|
|
|
|
if progress.isComplete {
|
2020-01-07 03:09:46 +01:00
|
|
|
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
|
|
|
|
self.progressWidth.isActive = false
|
2020-01-07 03:09:46 +01:00
|
|
|
self.progressView.setProgress(0, animated: true)
|
2019-10-25 20:34:59 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
label.isHidden = true
|
|
|
|
progressView.isHidden = false
|
|
|
|
self.progressWidth.isActive = true
|
2020-01-07 03:09:46 +01:00
|
|
|
self.progressView.setNeedsLayout()
|
|
|
|
self.progressView.layoutIfNeeded()
|
2019-10-25 20:34:59 +02:00
|
|
|
let percent = Float(progress.numberCompleted) / Float(progress.numberOfTasks)
|
2020-01-07 03:09:46 +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 {
|
|
|
|
progressView.setProgress(percent, animated: true)
|
|
|
|
}
|
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
|
|
|
}
|