2019-04-23 14:26:35 +02:00
|
|
|
//
|
|
|
|
// UINavigationController+Progress.swift
|
|
|
|
// KYNavigationProgress
|
|
|
|
//
|
|
|
|
// Created by kyo__hei on 2015/12/29.
|
|
|
|
// Copyright (c) 2015 kyo__hei. All rights reserved.
|
|
|
|
//
|
|
|
|
// Original project: https://github.com/ykyouhei/KYNavigationProgress
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Account
|
|
|
|
|
|
|
|
private let constraintIdentifier = "progressHeightConstraint"
|
|
|
|
|
|
|
|
public extension UINavigationController {
|
|
|
|
|
|
|
|
/* ====================================================================== */
|
|
|
|
// MARK: - Properties
|
|
|
|
/* ====================================================================== */
|
|
|
|
|
|
|
|
/**
|
|
|
|
Default is 2.0
|
|
|
|
*/
|
|
|
|
var progressHeight: CGFloat {
|
|
|
|
get { return progressView.frame.height }
|
|
|
|
set {
|
|
|
|
progressView.frame.origin.y = navigationBar.frame.height - newValue
|
|
|
|
progressView.frame.size.height = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
The color shown for the portion of the progress bar that is not filled.
|
|
|
|
default is clear color.
|
|
|
|
*/
|
|
|
|
var trackTintColor: UIColor? {
|
|
|
|
get { return progressView.trackTintColor }
|
|
|
|
set { progressView.trackTintColor = newValue }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
The color shown for the portion of the progress bar that is filled.
|
|
|
|
default is (r: 0, g: 122, b: 225, a: 255.
|
|
|
|
*/
|
|
|
|
var progressTintColor: UIColor? {
|
|
|
|
get { return progressView.progressTintColor }
|
|
|
|
set { progressView.progressTintColor = newValue }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
The current progress is represented by a floating-point value between 0.0 and 1.0,
|
|
|
|
inclusive, where 1.0 indicates the completion of the task. The default value is 0.0.
|
|
|
|
*/
|
|
|
|
var progress: Float {
|
|
|
|
get { return progressView.progress }
|
|
|
|
set { progressView.progress = newValue }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private var progressView: NavigationProgressView {
|
|
|
|
|
|
|
|
for subview in navigationBar.subviews {
|
|
|
|
if let progressView = subview as? NavigationProgressView {
|
|
|
|
return progressView
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let defaultHeight = CGFloat(2)
|
|
|
|
let frame = CGRect(
|
|
|
|
x: 0,
|
|
|
|
y: navigationBar.frame.height - defaultHeight,
|
|
|
|
width: navigationBar.frame.width,
|
|
|
|
height: defaultHeight
|
|
|
|
)
|
|
|
|
let progressView = NavigationProgressView(frame: frame)
|
|
|
|
|
|
|
|
navigationBar.addSubview(progressView)
|
|
|
|
|
|
|
|
progressView.autoresizingMask = [
|
|
|
|
.flexibleWidth, .flexibleTopMargin
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
return progressView
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ====================================================================== */
|
|
|
|
// MARK: - Public Method
|
|
|
|
/* ====================================================================== */
|
|
|
|
|
|
|
|
/**
|
|
|
|
Adjusts the current progress shown by the receiver, optionally animating the change.
|
|
|
|
|
|
|
|
- parameter progress: The new progress value.
|
|
|
|
- parameter animated: true if the change should be animated, false if the change should happen immediately.
|
|
|
|
*/
|
2019-09-15 00:53:37 +02:00
|
|
|
func setProgress(_ progress: Float, animated: Bool, completion: @escaping () -> Void) {
|
2019-04-23 14:26:35 +02:00
|
|
|
progressView.bar.alpha = 1
|
2019-09-15 00:53:37 +02:00
|
|
|
progressView.setProgress(progress, animated: animated, completion: completion)
|
2019-04-23 14:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
While progress is changed to 1.0, the bar will fade out. After that, progress will be 0.0.
|
|
|
|
*/
|
|
|
|
func finishProgress() {
|
|
|
|
progressView.bar.alpha = 1
|
2019-09-15 00:53:37 +02:00
|
|
|
progressView.setProgress(1, animated: true) {
|
|
|
|
UIView.animate(withDuration: 0.5, animations: { self.progressView.bar.alpha = 0 }) { finished in
|
|
|
|
self.progressView.progress = 0
|
|
|
|
}
|
2019-04-23 14:26:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
While progress is changed to 0.0, the bar will fade out.
|
|
|
|
*/
|
|
|
|
func cancelProgress() {
|
2019-09-15 00:53:37 +02:00
|
|
|
progressView.setProgress(0, animated: true) {
|
|
|
|
UIView.animate(withDuration: 0.5, animations: {
|
|
|
|
self.progressView.bar.alpha = 0
|
|
|
|
})
|
|
|
|
}
|
2019-04-23 14:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateAccountRefreshProgressIndicator() {
|
|
|
|
|
|
|
|
let progress = AccountManager.shared.combinedRefreshProgress
|
|
|
|
|
|
|
|
if progress.isComplete {
|
|
|
|
if self.progress != 0 {
|
|
|
|
finishProgress()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let percent = Float(progress.numberCompleted) / Float(progress.numberOfTasks)
|
2019-09-15 00:53:37 +02:00
|
|
|
setProgress(percent, animated: true) {}
|
2019-04-23 14:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|