|
| 1 | +// |
| 2 | +// CMFeedbackViewController.swift |
| 3 | +// Commun |
| 4 | +// |
| 5 | +// Created by Sergey Monastyrskiy on 04.03.2020. |
| 6 | +// Copyright © 2020 Commun Limited. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import RxSwift |
| 11 | + |
| 12 | +class CMFeedbackViewController: UIViewController { |
| 13 | + // MARK: - Properties |
| 14 | + let disposeBag = DisposeBag() |
| 15 | + lazy var closeButton = UIButton.close() |
| 16 | + let sendButton = CommunButton.default(height: .adaptive(height: 50.0), label: "send".localized().uppercaseFirst, isDisabled: true) |
| 17 | + |
| 18 | + let titleLabel = UILabel.init(text: "feedback".localized().uppercaseFirst, |
| 19 | + font: .systemFont(ofSize: .adaptive(width: 15.0), weight: .bold), |
| 20 | + numberOfLines: 1, |
| 21 | + color: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)) |
| 22 | + |
| 23 | + lazy var textView: UITextView = { |
| 24 | + let textViewInstance = UITextView() |
| 25 | + textViewInstance.placeholder = "text view feedback placeholder".localized().uppercaseFirst |
| 26 | + textViewInstance.tune(withTextColors: blackWhiteColorPickers, font: .systemFont(ofSize: .adaptive(width: 17.0), weight: .regular), alignment: .left) |
| 27 | + |
| 28 | + return textViewInstance |
| 29 | + }() |
| 30 | + |
| 31 | + |
| 32 | + // MARK: - Class Initialization |
| 33 | + override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { |
| 34 | + super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) |
| 35 | + |
| 36 | + self.bind() |
| 37 | + self.setUp() |
| 38 | + } |
| 39 | + |
| 40 | + required init?(coder: NSCoder) { |
| 41 | + fatalError("init(coder:) has not been implemented") |
| 42 | + } |
| 43 | + |
| 44 | + deinit { |
| 45 | + Logger.log(message: "Success", event: .severe) |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + // MARK: - Class Functions |
| 50 | + override func viewDidLoad() { |
| 51 | + super.viewDidLoad() |
| 52 | + |
| 53 | + // Do any additional setup after loading the view. |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + // MARK: - Custom Functions |
| 58 | + private func bind() { |
| 59 | + let tap = UITapGestureRecognizer(target: self, action: #selector(viewTapped)) |
| 60 | + view.addGestureRecognizer(tap) |
| 61 | + |
| 62 | + self.textView.rx.didChange |
| 63 | + .map { self.textView.text } |
| 64 | + .filter { $0 != nil } |
| 65 | + .map { $0! } |
| 66 | + .subscribe(onNext: { text in |
| 67 | + self.sendButton.isDisabled = text.count == 0 || text == "\n" |
| 68 | + }) |
| 69 | + .disposed(by: self.disposeBag) |
| 70 | + } |
| 71 | + |
| 72 | + private func setUp() { |
| 73 | + view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) |
| 74 | + |
| 75 | + view.addSubview(closeButton) |
| 76 | + closeButton.autoPinTopAndTrailingToSuperView(inset: .adaptive(height: 15.0), xInset: .adaptive(width: 15.0)) |
| 77 | + closeButton.addTarget(self, action: #selector(closeButtonTapped), for: .touchUpInside) |
| 78 | + |
| 79 | + view.addSubview(titleLabel) |
| 80 | + titleLabel.autoAlignAxis(toSuperviewAxis: .vertical) |
| 81 | + titleLabel.autoAlignAxis(.horizontal, toSameAxisOf: closeButton) |
| 82 | + |
| 83 | + view.addSubview(textView) |
| 84 | + textView.autoPinEdgesToSuperviewSafeArea(with: UIEdgeInsets(horizontal: .adaptive(width: 30.0), vertical: .adaptive(height: 108.0)), excludingEdge: .bottom) |
| 85 | + |
| 86 | + view.addSubview(sendButton) |
| 87 | + sendButton.autoAlignAxis(toSuperviewAxis: .vertical) |
| 88 | + sendButton.autoPinEdge(toSuperviewEdge: .leading, withInset: .adaptive(width: 15.0)) |
| 89 | + sendButton.autoPinEdge(toSuperviewEdge: .trailing, withInset: .adaptive(width: 15.0)) |
| 90 | + sendButton.autoPinEdge(.top, to: .bottom, of: textView, withOffset: .adaptive(height: 15.0)) |
| 91 | + sendButton.addTarget(self, action: #selector(sendButtonTapped), for: .touchUpInside) |
| 92 | + |
| 93 | + let keyboardViewV = KeyboardLayoutConstraint(item: view!.safeAreaLayoutGuide, |
| 94 | + attribute: .bottom, |
| 95 | + relatedBy: .equal, |
| 96 | + toItem: sendButton, |
| 97 | + attribute: .bottom, |
| 98 | + multiplier: 1.0, |
| 99 | + constant: .adaptive(height: 10.0)) |
| 100 | + keyboardViewV.observeKeyboardHeight() |
| 101 | + view.addConstraint(keyboardViewV) |
| 102 | + } |
| 103 | + |
| 104 | + private func checkValues() -> Bool { |
| 105 | + guard !textView.text.isEmpty else { |
| 106 | + self.hintView?.display(inPosition: sendButton.frame.origin, withType: .enterText, completion: {}) |
| 107 | + return false |
| 108 | + } |
| 109 | + |
| 110 | + return true |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + // MARK: - Actions |
| 115 | + @objc func closeButtonTapped( _ sender: UIButton) { |
| 116 | + self.dismiss(animated: true, completion: nil) |
| 117 | + } |
| 118 | + |
| 119 | + @objc func sendButtonTapped( _ sender: UIButton) { |
| 120 | + guard checkValues() else { return } |
| 121 | + |
| 122 | + self.dismiss(animated: true, completion: { |
| 123 | + AnalyticsManger.shared.sendFeedback(message: self.textView.text) |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + @objc func viewTapped( _ sender: UITapGestureRecognizer) { |
| 128 | + view.endEditing(true) |
| 129 | + } |
| 130 | +} |
0 commit comments