git.iko.soy/dashared/hookah-tau
on develop help sign in
dashared/hookah-tau/Hookah Tau/Controllers/BaseViewController.swift
126 lines 5.0 kB history raw
1//
2// BaseViewController.swift
3// Hookah Tau
4//
5// Created by cstore on 19/10/2019.
6// Copyright © 2019 Daria Rednikina. All rights reserved.
7//
9import UIKit
11class BaseViewController: UIViewController {
13 // MARK: - Properties
15 var bottomConstraint: NSLayoutConstraint?
17 // MARK: - Buttons
19 func addStackViewWithButtons(leftBtn: Button? = nil,
20 rightBtn: Button? = nil,
21 constant: CGFloat = -20) {
23 let stackView = UIStackView()
24 stackView.translatesAutoresizingMaskIntoConstraints = false
25 stackView.backgroundColor = .red
26 view.addSubview(stackView)
28 leftBtn?.translatesAutoresizingMaskIntoConstraints = false
29 rightBtn?.translatesAutoresizingMaskIntoConstraints = false
31 bottomConstraint = NSLayoutConstraint(item: stackView,
32 attribute: .bottom,
33 relatedBy: .equal,
34 toItem: view.safeAreaLayoutGuide,
35 attribute: .bottom,
36 multiplier: 1,
37 constant: constant)
39 guard let bottomContraint = bottomConstraint else { return }
40 view.addConstraint(bottomContraint)
42 NSLayoutConstraint.activate([
43 stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
44 stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
45 stackView.heightAnchor.constraint(equalToConstant: 40)
46 ])
48 switch (leftBtn, rightBtn) {
49 case (.some(let leftButton), .none):
50 setUpLeftButton(leftButton, stackView)
51 case (.none, .some(let rigthButton)):
52 setUpRightButton(rigthButton, stackView)
53 case (.some(let leftButton), .some(let rightButton)):
54 setUpTwoButtons(leftButton, rightButton, stackView)
55 case (.none, .none):
56 return;
57 }
59 view.layoutIfNeeded()
60 }
62 private func setUpTwoButtons(_ leftBtn: UIButton, _ rightBtn: UIButton, _ stackView: UIStackView) {
63 NSLayoutConstraint.activate([
64 leftBtn.widthAnchor.constraint(equalToConstant: 155),
65 rightBtn.widthAnchor.constraint(equalToConstant: 155)
66 ])
68 stackView.addArrangedSubview(leftBtn)
69 stackView.addArrangedSubview(rightBtn)
71 stackView.distribution = .equalSpacing
72 }
74 private func setUpLeftButton(_ leftBtn: UIButton, _ stackView: UIStackView) {
75 leftBtn.widthAnchor.constraint(equalToConstant: 155).isActive = true
77 stackView.addArrangedSubview(leftBtn)
78 stackView.alignment = .trailing
79 }
81 private func setUpRightButton(_ rightBtn: UIButton, _ stackView: UIStackView) {
82 rightBtn.widthAnchor.constraint(equalToConstant: 155).isActive = true
84 stackView.addArrangedSubview(rightBtn)
86 stackView.axis = .vertical
87 stackView.alignment = .trailing
88 stackView.distribution = .fillProportionally
89 }
91 // MARK: - Keyboard
93 func setUpKeyboard() {
94 NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotifications), name: UIResponder.keyboardWillShowNotification, object: nil)
96 NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotifications), name: UIResponder.keyboardDidHideNotification, object: nil)
97 }
99 @objc
100 func handleKeyboardNotifications(notification: NSNotification){
101 if let userInfo = notification.userInfo {
102 let keyBoardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
103 bottomConstraint?.constant = notification.name == UIResponder.keyboardWillShowNotification ? -keyBoardFrame.height + view.safeAreaInsets.bottom - 20 : 0
105 UIView.animate(withDuration: 0, delay: 0, options: .curveEaseOut, animations: {
106 self.view.layoutIfNeeded()
107 })
108 }
109 }
111 // MARK: - Alert
113 func displayAlert(forError error: GeneralError = GeneralError.noData, with message: String? = nil) {
114 var title: String? = message == nil ? error.localizedDescription : message
115 switch error {
116 case .serverError(let se):
117 title = se.error
118 fallthrough // да тоже произведение искусства
119 default:
120 let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
121 alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
122 self.present(alert, animated: true, completion: nil)
123 }
124 }