2021-03-15 10:52:28 +01:00
|
|
|
//
|
|
|
|
// PlayerContainerView+MediaTypeIndicotorView.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-15.
|
|
|
|
//
|
|
|
|
|
2021-03-15 10:53:06 +01:00
|
|
|
import UIKit
|
|
|
|
|
|
|
|
extension PlayerContainerView {
|
|
|
|
|
2021-06-17 12:43:06 +02:00
|
|
|
final class MediaTypeIndicatorView: UIView {
|
2021-03-15 10:53:06 +01:00
|
|
|
|
|
|
|
static let indicatorViewSize = CGSize(width: 47, height: 25)
|
|
|
|
|
|
|
|
let maskLayer = CAShapeLayer()
|
|
|
|
|
|
|
|
let label: UILabel = {
|
|
|
|
let label = UILabel()
|
|
|
|
label.textColor = .white
|
|
|
|
label.textAlignment = .right
|
|
|
|
label.adjustsFontSizeToFitWidth = true
|
|
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func layoutSubviews() {
|
|
|
|
super.layoutSubviews()
|
|
|
|
|
|
|
|
let path = UIBezierPath()
|
|
|
|
path.move(to: CGPoint(x: bounds.width, y: bounds.height))
|
|
|
|
path.addLine(to: CGPoint(x: bounds.width, y: 0))
|
|
|
|
path.addLine(to: CGPoint(x: bounds.width * 0.5, y: 0))
|
|
|
|
path.addCurve(
|
|
|
|
to: CGPoint(x: 0, y: bounds.height),
|
|
|
|
controlPoint1: CGPoint(x: bounds.width * 0.2, y: 0),
|
|
|
|
controlPoint2: CGPoint(x: 0, y: bounds.height * 0.3)
|
|
|
|
)
|
|
|
|
path.close()
|
|
|
|
|
|
|
|
maskLayer.frame = bounds
|
|
|
|
maskLayer.path = path.cgPath
|
|
|
|
layer.mask = maskLayer
|
2021-03-15 10:58:03 +01:00
|
|
|
|
|
|
|
layer.cornerRadius = PlayerContainerView.cornerRadius
|
|
|
|
layer.maskedCorners = [.layerMaxXMaxYCorner]
|
|
|
|
layer.cornerCurve = .continuous
|
2021-03-15 10:53:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-17 12:43:06 +02:00
|
|
|
extension PlayerContainerView.MediaTypeIndicatorView {
|
2021-03-15 10:53:06 +01:00
|
|
|
|
|
|
|
private func _init() {
|
2021-07-20 13:24:24 +02:00
|
|
|
backgroundColor = Asset.Colors.mediaTypeIndicotor.color
|
2021-03-15 10:53:06 +01:00
|
|
|
layoutMargins = UIEdgeInsets(top: 3, left: 13, bottom: 0, right: 6)
|
|
|
|
|
|
|
|
addSubview(label)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
label.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
|
|
|
|
label.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
|
|
|
|
label.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
|
|
|
|
label.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func roundedFont(weight: UIFont.Weight,fontSize: CGFloat) -> UIFont {
|
|
|
|
let systemFont = UIFont.systemFont(ofSize: fontSize, weight: weight)
|
|
|
|
guard let descriptor = systemFont.fontDescriptor.withDesign(.rounded) else { return systemFont }
|
|
|
|
let roundedFont = UIFont(descriptor: descriptor, size: fontSize)
|
|
|
|
return roundedFont
|
|
|
|
}
|
|
|
|
|
|
|
|
func setMediaKind(kind: VideoPlayerViewModel.Kind) {
|
|
|
|
let fontSize: CGFloat = 18
|
|
|
|
|
|
|
|
switch kind {
|
|
|
|
case .gif:
|
2021-06-17 12:43:06 +02:00
|
|
|
label.font = PlayerContainerView.MediaTypeIndicatorView.roundedFont(weight: .heavy, fontSize: fontSize)
|
2021-03-15 10:53:06 +01:00
|
|
|
label.text = "GIF"
|
|
|
|
case .video:
|
2021-06-17 12:43:06 +02:00
|
|
|
label.text = " "
|
2021-03-15 10:53:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-15 11:02:42 +01:00
|
|
|
#if canImport(SwiftUI) && DEBUG
|
|
|
|
import SwiftUI
|
|
|
|
|
2021-06-17 12:43:06 +02:00
|
|
|
struct PlayerContainerViewMediaTypeIndicatorView_Previews: PreviewProvider {
|
2021-03-15 11:02:42 +01:00
|
|
|
|
|
|
|
static var previews: some View {
|
|
|
|
Group {
|
|
|
|
UIViewPreview(width: 47) {
|
2021-06-17 12:43:06 +02:00
|
|
|
let view = PlayerContainerView.MediaTypeIndicatorView()
|
2021-03-15 11:02:42 +01:00
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
view.heightAnchor.constraint(equalToConstant: 25),
|
|
|
|
view.widthAnchor.constraint(equalToConstant: 47),
|
|
|
|
])
|
|
|
|
view.setMediaKind(kind: .gif)
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 47, height: 25))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2021-03-15 10:53:06 +01:00
|
|
|
|