MessageAccessoryView.swift 4.13 KB
Newer Older
1
/*
2
 *  Copyright (C) 2017-2019 Savoir-faire Linux Inc.
3 4
 *
 *  Author: Silbino Gonçalves Matado <silbino.gmatado@savoirfairelinux.com>
5
 *  Author: Quentin Muret <quentin.muret@savoirfairelinux.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

import UIKit
23
import Reusable
24
import RxSwift
25

26
class MessageAccessoryView: UIView, NibLoadable, GrowingTextViewDelegate {
27

28
    @IBOutlet weak var sendButton: UIButton!
29 30
    @IBOutlet weak var shareButton: UIButton!
    @IBOutlet weak var cameraButton: UIButton!
31
    @IBOutlet weak var emojisButton: UIButton!
32 33
    @IBOutlet weak var blurEffect: UIVisualEffectView!
    @IBOutlet weak var messageTextView: GrowingTextView!
34
    @IBOutlet weak var emojisButtonTrailingConstraint: NSLayoutConstraint!
35 36 37 38
    @IBOutlet weak var sendButtonLeftConstraint: NSLayoutConstraint!
    @IBOutlet weak var textViewHeightConstraints: NSLayoutConstraint!
    var messageTextViewHeight = Variable<CGFloat>(0.00)
    var messageTextViewContent = Variable<String>("")
39

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
40
    override open func didMoveToWindow() {
41
        self.setupMessageTextView()
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
42
        super.didMoveToWindow()
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
43 44 45 46 47
        if #available(iOS 11.0, *) {
            guard let window = self.window else {
                return
            }
            self.bottomAnchor
48
                .constraint(lessThanOrEqualToSystemSpacingBelow: window.safeAreaLayoutGuide.bottomAnchor,
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
49 50 51 52
                                                               multiplier: 1)
                .isActive = true
        }
    }
53

54 55
    func setupMessageTextView() {
        self.messageTextView.delegate = self
56
        self.messageTextView.placeholder = L10n.Conversation.messagePlaceholder
57
        self.messageTextView.layer.cornerRadius = 18
Quentin Muret's avatar
Quentin Muret committed
58
        self.messageTextView.tintColor = UIColor.jamiMain
59 60
        self.messageTextView.textContainerInset = UIEdgeInsets(top: 8, left: 7, bottom: 8, right: 7)
        self.messageTextView.layer.borderWidth = 1
Quentin Muret's avatar
Quentin Muret committed
61
        self.messageTextView.layer.borderColor = UIColor.jamiMsgTextFieldBorder.cgColor
62
        self.messageTextView.maxHeight = 70
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
63 64
        self.shareButton.tintColor = UIColor.jamiMain
        self.cameraButton.tintColor = UIColor.jamiMain
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    }

    func textViewDidChangeHeight(_ textView: GrowingTextView, height: CGFloat) {
        if height > self.messageTextViewHeight.value {
            UIView.animate(withDuration: 0.2) {
                self.layoutIfNeeded()
            }
        }
        self.messageTextViewHeight.value = height
    }

    func textViewDidChange(_ textView: UITextView) {
        self.messageTextViewContent.value = textView.text
    }

    func editingChanges() {
        if self.messageTextView.text != nil {
            if self.messageTextView.text!.count >= 1 {
83 84 85 86 87 88 89 90 91
                    setEmojiButtonVisibility(hide: true)
            } else {
                setEmojiButtonVisibility(hide: false)
            }
        } else {
            setEmojiButtonVisibility(hide: false)
        }
    }
    func setEmojiButtonVisibility(hide: Bool) {
92
        UIView.animate(withDuration: 0.2, animations: {
93 94
            if hide {
                self.emojisButtonTrailingConstraint.constant = -27
95
                self.sendButtonLeftConstraint.constant = 13
Quentin Muret's avatar
Quentin Muret committed
96
                self.sendButton.tintColor = UIColor.jamiMain
97
            } else {
98 99
                self.emojisButtonTrailingConstraint.constant = 14
                self.sendButtonLeftConstraint.constant = 35
Quentin Muret's avatar
Quentin Muret committed
100
                self.sendButton.tintColor = UIColor.jamiMsgTextFieldBackground
101 102 103 104
            }
            self.layoutIfNeeded()
        })
    }
105
}