mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
2.9 KiB
Swift
100 lines
2.9 KiB
Swift
8 years ago
|
//
|
||
7 years ago
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||
8 years ago
|
//
|
||
|
|
||
|
import UIKit
|
||
5 years ago
|
import SignalUtilitiesKit
|
||
8 years ago
|
|
||
7 years ago
|
@objc public class AudioProgressView: UIView {
|
||
8 years ago
|
|
||
7 years ago
|
@objc public override var bounds: CGRect {
|
||
8 years ago
|
didSet {
|
||
|
if oldValue != bounds {
|
||
|
updateSubviews()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
@objc public override var frame: CGRect {
|
||
8 years ago
|
didSet {
|
||
|
if oldValue != frame {
|
||
|
updateSubviews()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
@objc public var horizontalBarColor = UIColor.black {
|
||
8 years ago
|
didSet {
|
||
|
updateContent()
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
@objc public var progressColor = UIColor.blue {
|
||
8 years ago
|
didSet {
|
||
|
updateContent()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private let horizontalBarLayer: CAShapeLayer
|
||
|
private let progressLayer: CAShapeLayer
|
||
|
|
||
7 years ago
|
@objc public var progress: CGFloat = 0 {
|
||
8 years ago
|
didSet {
|
||
|
if oldValue != progress {
|
||
|
updateContent()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
@available(*, unavailable, message:"use other constructor instead.")
|
||
7 years ago
|
@objc public required init?(coder aDecoder: NSCoder) {
|
||
7 years ago
|
notImplemented()
|
||
8 years ago
|
}
|
||
|
|
||
|
public required init() {
|
||
|
self.horizontalBarLayer = CAShapeLayer()
|
||
|
self.progressLayer = CAShapeLayer()
|
||
|
|
||
7 years ago
|
super.init(frame: CGRect.zero)
|
||
8 years ago
|
|
||
|
self.layer.addSublayer(self.horizontalBarLayer)
|
||
|
self.layer.addSublayer(self.progressLayer)
|
||
|
}
|
||
|
|
||
|
internal func updateSubviews() {
|
||
7 years ago
|
AssertIsOnMainThread()
|
||
8 years ago
|
|
||
|
self.horizontalBarLayer.frame = self.bounds
|
||
|
self.progressLayer.frame = self.bounds
|
||
|
|
||
|
updateContent()
|
||
|
}
|
||
|
|
||
|
internal func updateContent() {
|
||
7 years ago
|
AssertIsOnMainThread()
|
||
8 years ago
|
|
||
7 years ago
|
// Prevent the shape layer from animating changes.
|
||
|
CATransaction.begin()
|
||
7 years ago
|
CATransaction.setDisableActions(true)
|
||
7 years ago
|
|
||
8 years ago
|
let horizontalBarPath = UIBezierPath()
|
||
|
let horizontalBarHeightFraction = CGFloat(0.25)
|
||
|
let horizontalBarHeight = bounds.size.height * horizontalBarHeightFraction
|
||
7 years ago
|
horizontalBarPath.append(UIBezierPath(rect: CGRect(x: 0, y: (bounds.size.height - horizontalBarHeight) * 0.5, width: bounds.size.width, height: horizontalBarHeight)))
|
||
8 years ago
|
horizontalBarLayer.path = horizontalBarPath.cgPath
|
||
|
horizontalBarLayer.fillColor = horizontalBarColor.cgColor
|
||
|
|
||
|
let progressHeight = bounds.self.height
|
||
|
let progressWidth = progressHeight * 0.15
|
||
|
let progressX = (bounds.self.width - progressWidth) * max(0.0, min(1.0, progress))
|
||
7 years ago
|
let progressBounds = CGRect(x: progressX, y: 0, width: progressWidth, height: progressHeight)
|
||
8 years ago
|
let progressCornerRadius = progressWidth * 0.5
|
||
|
let progressPath = UIBezierPath()
|
||
|
progressPath.append(UIBezierPath(roundedRect: progressBounds, cornerRadius: progressCornerRadius))
|
||
|
progressLayer.path = progressPath.cgPath
|
||
|
progressLayer.fillColor = progressColor.cgColor
|
||
7 years ago
|
|
||
|
CATransaction.commit()
|
||
8 years ago
|
}
|
||
|
}
|