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)
|
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
commonInit()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
commonInit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func commonInit() {
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(progressDidChange(_:)), name: .AccountRefreshProgressDidChange, object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateRefreshLabel() {
|
2019-12-10 02:34:26 +01:00
|
|
|
if let accountLastArticleFetchEndTime = AccountManager.shared.lastArticleFetchEndTime {
|
|
|
|
if Date() > accountLastArticleFetchEndTime.addingTimeInterval(1) {
|
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
|
|
|
|
} else {
|
|
|
|
label.text = NSLocalizedString("Updated just now", comment: "Updated Just Now")
|
|
|
|
}
|
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) {
|
|
|
|
|
|
|
|
let progress = AccountManager.shared.combinedRefreshProgress
|
|
|
|
|
|
|
|
if progress.isComplete {
|
|
|
|
progressView.progress = 1
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
|
|
self.updateRefreshLabel()
|
|
|
|
self.label.isHidden = false
|
|
|
|
self.progressView.isHidden = true
|
|
|
|
self.progressWidth.isActive = false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
label.isHidden = true
|
|
|
|
progressView.isHidden = false
|
|
|
|
self.progressWidth.isActive = true
|
|
|
|
let percent = Float(progress.numberCompleted) / Float(progress.numberOfTasks)
|
|
|
|
progressView.progress = percent
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|