-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseViewController.swift
138 lines (116 loc) · 5.02 KB
/
BaseViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import UIKit
protocol BaseViewControllerDelegate: AnyObject {
func setPlayerRate(with value: Float?)
}
protocol BaseViewControllerDelegateDate: AnyObject {
func setDate(with dateString: String)
}
class BaseViewController: UIViewController {
private var datePicker: UIDatePicker?
private var dateString = ""
weak var delegate: BaseViewControllerDelegate?
weak var dateDelegate: BaseViewControllerDelegateDate?
var optIn: Bool {
get {
let defaults = UserDefaults.standard
return defaults.bool(forKey: "optin")
}
}
lazy var gradient: CAGradientLayer = {
let gradient = CAGradientLayer()
gradient.type = .axial
gradient.colors = [
UIColor.black.cgColor,
UIColor.white.cgColor
]
gradient.locations = [0.5, 1]
return gradient
}()
override func viewDidLoad() {
super.viewDidLoad()
setNavigationBarGradient()
gradient.frame = view.bounds
view.layer.insertSublayer(gradient, at:0)
let crashBarButtonItem = UIBarButtonItem(title: "Crash", style: .done, target: self, action: #selector(crashButtonTapped))
self.navigationItem.rightBarButtonItem = crashBarButtonItem
self.navigationItem.rightBarButtonItem?.tintColor = .red
}
@objc func crashButtonTapped() {
fatalError("Crashing Demo app Test")
}
//MARK: NavigationBar
private func setNavigationBarGradient() {
if let navigationBar = self.navigationController?.navigationBar {
let gradient = CAGradientLayer()
var bounds = navigationBar.bounds
bounds.size.height += UIApplication.shared.statusBarFrame.size.height
gradient.frame = bounds
gradient.colors = [UIColor.orange.cgColor, UIColor.red.cgColor]
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 1, y: 0)
if let image = getImageFrom(gradientLayer: gradient) {
navigationBar.setBackgroundImage(image, for: UIBarMetrics.default)
}
}
}
private func getImageFrom(gradientLayer:CAGradientLayer) -> UIImage? {
var gradientImage:UIImage?
UIGraphicsBeginImageContext(gradientLayer.frame.size)
if let context = UIGraphicsGetCurrentContext() {
gradientLayer.render(in: context)
gradientImage = UIGraphicsGetImageFromCurrentImageContext()?.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch)
}
UIGraphicsEndImageContext()
return gradientImage
}
func setNavigationBarTitle(title: String) {
self.title = title
}
func showChangeSpeedAlert() {
let alert = UIAlertController(title: "Speed", message: nil, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "0.5x", style: UIAlertAction.Style.default, handler: changePlaybackSpeed))
alert.addAction(UIAlertAction(title: "1.0x", style: UIAlertAction.Style.default, handler: changePlaybackSpeed))
alert.addAction(UIAlertAction(title: "1.5x", style: UIAlertAction.Style.default, handler: changePlaybackSpeed))
alert.addAction(UIAlertAction(title: "2.0x", style: UIAlertAction.Style.default, handler: changePlaybackSpeed))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
@objc private func changePlaybackSpeed(alert: UIAlertAction) {
guard let input = alert.title?.dropLast() else {
return
}
delegate?.setPlayerRate(with: Float(input))
}
func createToolBar() -> UIToolbar {
let toolBar = UIToolbar()
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolBar.setItems([doneButton], animated: true)
return toolBar
}
func createDatePicker(for textField: UITextField) {
datePicker = UIDatePicker()
if #available(iOS 13.4, *) {
datePicker?.preferredDatePickerStyle = .wheels
} else {
// Fallback on earlier versions
}
textField.attributedPlaceholder = NSAttributedString(
string: "Select Date for Time Shift",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white]
)
textField.inputView = datePicker
textField.inputAccessoryView = createToolBar()
}
@objc func donePressed() {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateString = dateFormatter.string(from: datePicker?.date ?? Date())
dateString = dateString + "+0100"
dateDelegate?.setDate(with: dateString)
self.view.endEditing(true)
}
func getStreamStart() -> String {
return dateString
}
}