Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new settings with functionality #8

Merged
merged 5 commits into from
Apr 18, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Demo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12086"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -27,12 +26,6 @@
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="image" keyPath="rightButtonBackgroundImage" value="bubble_orange"/>
<userDefinedRuntimeAttribute type="string" keyPath="rightButtonText" value=" "/>
<userDefinedRuntimeAttribute type="color" keyPath="rightButtonTextColor">
<color key="value" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="rightButtonBackgroundColor">
<color key="value" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
</subviews>
Expand Down
2 changes: 1 addition & 1 deletion MessagesView/MessageCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MessageCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var backgroundLeadingConstraint: NSLayoutConstraint!

static var leftArrowImage = MessageCollectionViewCell.createArrowImage(inSize: CGSize(width: 10.0, height: 10.0)) ?? UIImage()
static var rightArrowImage = UIImage(cgImage: (leftArrowImage.cgImage)!, scale: 1.0, orientation: .upMirrored).withRenderingMode(.alwaysTemplate)
static var rightArrowImage = UIImage(cgImage: (leftArrowImage.cgImage)!, scale: UIScreen.main.scale, orientation: .upMirrored).withRenderingMode(.alwaysTemplate)

static let patternCell = MessageCollectionViewCell.fromNib()

Expand Down
78 changes: 49 additions & 29 deletions MessagesView/MessagesToolbarContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ import UIKit

class MessagesToolbarContentView: UIView {

/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
@IBOutlet weak var topSeparatorLineView: UIView!
@IBOutlet weak var topSeparatorLineViewHeightConstraint: NSLayoutConstraint!

Expand All @@ -38,6 +31,17 @@ class MessagesToolbarContentView: UIView {
private var originalRightButtonContainerViewMargin = CGFloat(0)
private var originalRightButtonContainerViewWidth = CGFloat(0)

private var leftButtonEnabled: Bool = true
private var rightButtonEnabled: Bool = true

private var leftTintColor: UIColor {
return leftButtonEnabled ? settings.leftButtonTextColor : settings.leftButtonDisabledColor
}

private var rightTintColor: UIColor {
return rightButtonEnabled ? settings.rightButtonTextColor : settings.rightButtonDisabledColor
}

override func awakeFromNib() {
super.awakeFromNib()
saveOriginalConstraintValues()
Expand All @@ -50,16 +54,24 @@ class MessagesToolbarContentView: UIView {
originalRightButtonContainerViewWidth = rightButtonContainerViewWidthConstraint.constant
}

@IBAction func didPressRightButton(_ sender: AnyObject) {
settings.rightButtonAction()
if settings.rightButtonHidesKeyboard {
@IBAction func didPressLeftButton(_ sender: AnyObject) {

if leftButtonEnabled {
settings.leftButtonAction()
}

if settings.leftButtonHidesKeyboard {
messageEditorTextView.resignFirstResponder()
}
}

@IBAction func didPressLeftButton(_ sender: AnyObject) {
settings.leftButtonAction()
if settings.leftButtonHidesKeyboard {
@IBAction func didPressRightButton(_ sender: AnyObject) {

if rightButtonEnabled {
settings.rightButtonAction()
}

if settings.rightButtonHidesKeyboard {
messageEditorTextView.resignFirstResponder()
}
}
Expand Down Expand Up @@ -89,6 +101,20 @@ class MessagesToolbarContentView: UIView {
move(view: self.leftButtonContainerView, animated: animated, constraint: self.leftButtonContainerViewLeadingConstraint, value: destination.margin, alpha: destination.alpha)
}

func setLeftButton(enabled: Bool) {
leftButtonEnabled = enabled

leftButtonLabel.textColor = leftTintColor
leftButtonContainerView.tintColor = leftTintColor
}

func setRightButton(enabled: Bool) {
rightButtonEnabled = enabled

rightButtonLabel.textColor = rightTintColor
rightButtonContainerView.tintColor = rightTintColor
}

private func calculateDestination(side: Side, show: Bool) -> (margin: CGFloat, alpha: CGFloat) {
let xMargin: CGFloat
let alpha: CGFloat
Expand All @@ -110,7 +136,7 @@ class MessagesToolbarContentView: UIView {
return (xMargin, alpha)
}

private func move( view: UIView, animated: Bool, constraint: NSLayoutConstraint, value: CGFloat, alpha: CGFloat) {
private func move(view: UIView, animated: Bool, constraint: NSLayoutConstraint, value: CGFloat, alpha: CGFloat) {
let performTransition = {
constraint.constant = value
view.alpha = alpha
Expand All @@ -127,7 +153,7 @@ class MessagesToolbarContentView: UIView {
}
}

func apply(settings: MessagesViewSettings) {
private func apply(settings: MessagesViewSettings) {
messageEditorTextView.applySettings(settings: settings)

backgroundColor = settings.inputToolbarBackgroundColor
Expand All @@ -137,37 +163,31 @@ class MessagesToolbarContentView: UIView {
topSeparatorLineViewHeightConstraint.constant = settings.textInputFieldTopSeparatorLineHeight

leftButtonLabel.text = settings.leftButtonText
leftButtonLabel.textColor = settings.leftButtonTextColor
leftButtonLabel.textColor = leftTintColor
leftButtonContainerView.tintColor = leftTintColor
leftButtonContainerView.backgroundColor = settings.leftButtonBackgroundColor
leftButtonContainerView.image = settings.leftButtonBackgroundImage
leftButtonContainerView.image = settings.leftButtonBackgroundImage?.withRenderingMode(.alwaysTemplate)
leftButtonContainerView.layer.cornerRadius = settings.leftButtonCornerRadius

rightButtonLabel.text = settings.rightButtonText
rightButtonLabel.textColor = settings.rightButtonTextColor
rightButtonLabel.textColor = rightTintColor
rightButtonContainerView.tintColor = rightTintColor
rightButtonContainerView.backgroundColor = settings.rightButtonBackgroundColor
rightButtonContainerView.image = settings.rightButtonBackgroundImage
rightButtonContainerView.layer.cornerRadius = settings.rightButtonCornerRadius
rightButtonContainerView.image = settings.rightButtonBackgroundImage?.withRenderingMode(.alwaysTemplate)
rightButtonContainerView.layer.cornerRadius = settings.rightButtonCornerRadius
}
}

extension MessagesToolbarContentView : UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
#if DEBUG
print(#function)
#endif
return true
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
#if DEBUG
print(#function)
#endif
return true
}

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
#if DEBUG
print(#function)
#endif
return true
}
}
Expand Down
121 changes: 70 additions & 51 deletions MessagesView/MessagesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,46 +40,53 @@ public class MessagesView: UIView {

fileprivate let messageMargin : CGFloat = 60.0
fileprivate let defaultCellSize : CGSize = CGSize(width: 250.0, height: 100.0)

//MARK:- Public properties

@IBInspectable public var leftMessageCellTextColor: UIColor = UIColor.black
@IBInspectable public var leftMessageCellBackgroundColor: UIColor = UIColor.antiflashWhite
@IBInspectable public var rightMessageCellTextColor: UIColor = UIColor.antiflashWhite
@IBInspectable public var rightMessageCellBackgroundColor: UIColor = UIColor.pumpkin
@IBInspectable public var leftMessageCellTextColor: UIColor = .black
@IBInspectable public var leftMessageCellBackgroundColor: UIColor = .antiflashWhite
@IBInspectable public var rightMessageCellTextColor: UIColor = .antiflashWhite
@IBInspectable public var rightMessageCellBackgroundColor: UIColor = .pumpkin

@IBInspectable public var collectionViewBackgroundColor: UIColor = UIColor.white
@IBInspectable public var collectionViewBackgroundColor: UIColor = .white

@IBInspectable public var textInputFieldTextColor: UIColor = UIColor.black
@IBInspectable public var textInputFieldBackgroundColor: UIColor = UIColor.clear
@IBInspectable public var textInputFieldTextColor: UIColor = .black
@IBInspectable public var textInputFieldBackgroundColor: UIColor = .clear
@IBInspectable public var textInputFieldTextPlaceholderText: String = "Write your message here"
@IBInspectable public var textInputFieldCornerRadius: CGFloat = 0.0
@IBInspectable public var textInputFieldFont: UIFont = UIFont.systemFont(ofSize: 10)

@IBInspectable public var textInputFieldTopSeparatorLineHeight: CGFloat = 1.0
@IBInspectable public var textInputFieldTopSeparatorLineColor: UIColor = UIColor.pumpkin
@IBInspectable public var textInputFieldTopSeparatorLineColor: UIColor = .pumpkin
@IBInspectable public var textInputFieldTopSeparatorLineAlpha: CGFloat = 0.3

@IBInspectable public var buttonSlideAnimationDuration: TimeInterval = 0.5
@IBInspectable public var inputToolbarBackgroundColor: UIColor = UIColor.white

@IBInspectable public var leftButtonText: String = "Left"
@IBInspectable public var leftButtonShow: Bool = false
@IBInspectable public var leftButtonShowAnimated: Bool = false
@IBInspectable public var leftButtonTextColor: UIColor = UIColor.black
@IBInspectable public var leftButtonBackgroundColor: UIColor = UIColor.gray
@IBInspectable public var leftButtonTextColor: UIColor = .black
@IBInspectable public var leftButtonDisabledColor: UIColor = .lightGray
@IBInspectable public var leftButtonBackgroundColor: UIColor = .clear
@IBInspectable public var leftButtonBackgroundImage: UIImage?
@IBInspectable public var leftButtonCornerRadius: CGFloat = 0.0

@IBInspectable public var rightButtonText: String = "Right"
@IBInspectable public var rightButtonShow: Bool = true
@IBInspectable public var rightButtonShowAnimated: Bool = true
@IBInspectable public var rightButtonTextColor: UIColor = UIColor.black
@IBInspectable public var rightButtonBackgroundColor: UIColor = UIColor.gray
@IBInspectable public var rightButtonTextColor: UIColor = .pumpkin
@IBInspectable public var rightButtonDisabledColor: UIColor = .lightGray
@IBInspectable public var rightButtonBackgroundColor: UIColor = .clear
@IBInspectable public var rightButtonBackgroundImage: UIImage?
@IBInspectable public var rightButtonCornerRadius: CGFloat = 0.0

public var buttonSlideAnimationDuration: TimeInterval = 0.5

public var delegate : MessagesViewDelegate?
public var dataSource: MessagesViewDataSource?

//NARK:-
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit of typo


var bubbleImageLeft: BubbleImage?
var bubbleImageRight: BubbleImage?

Expand Down Expand Up @@ -112,15 +119,17 @@ public class MessagesView: UIView {
setup()
}

deinit {
NotificationCenter.default.removeObserver(self)
}

//MARK:- Public methods

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

deinit {
NotificationCenter.default.removeObserver(self)
}

public override func awakeFromNib() {
super.awakeFromNib()
readSettingsFromInpectables(settings: &settings)
Expand All @@ -133,6 +142,42 @@ public class MessagesView: UIView {
apply(settings: settings)
}

public func refresh(scrollToLastMessage: Bool) {
DispatchQueue.main.async {
self.messagesCollectionView.reloadData()

if scrollToLastMessage {
self.scrollToLastMessage(animated: true)
}
}
}

public func scrollToLastMessage(animated: Bool) {
guard !self.messagesCollectionView.isDragging, self.messagesCollectionView.numberOfItems(inSection: 0) > 0 else {
return
}

self.messagesCollectionView.scrollToItem(at: IndexPath(row: self.messagesCollectionView.numberOfItems(inSection: 0) - 1, section: 0), at: .top, animated: animated)
}

public func leftButton(show: Bool, animated: Bool) {
messagesInputToolbar.leftButton(show: show, animated: animated)
}

public func rightButton(show: Bool, animated: Bool) {
messagesInputToolbar.rightButton(show: show, animated: animated)
}

public func setLeftButton(enabled: Bool) {
messagesInputToolbar.toolbarContentView.setLeftButton(enabled: enabled)
}

public func setRightButton(enabled: Bool) {
messagesInputToolbar.toolbarContentView.setRightButton(enabled: enabled)
}

//MARK:-

private func setup() {
view = loadFromNib()
addSubview(view)
Expand All @@ -152,14 +197,6 @@ public class MessagesView: UIView {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: .UIKeyboardWillHide, object: nil)
}

public func leftButton(show: Bool, animated: Bool) {
messagesInputToolbar.leftButton(show: show, animated: animated)
}

public func rightButton(show: Bool, animated: Bool) {
messagesInputToolbar.rightButton(show: show, animated: animated)
}

@objc private func keyboardWillShow(notification: Notification) {

guard settings.shouldAdjustToKeyboard,
Expand Down Expand Up @@ -230,24 +267,6 @@ public class MessagesView: UIView {
messagesCollectionView.register(UICollectionReusableView.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: Key.messagesCollectionViewFooter)
}

public func refresh(scrollToLastMessage: Bool) {
DispatchQueue.main.async {
self.messagesCollectionView.reloadData()

if scrollToLastMessage {
self.scrollToLastMessage(animated: true)
}
}
}

public func scrollToLastMessage(animated: Bool) {
guard !self.messagesCollectionView.isDragging, self.messagesCollectionView.numberOfItems(inSection: 0) > 0 else {
return
}

self.messagesCollectionView.scrollToItem(at: IndexPath(row: self.messagesCollectionView.numberOfItems(inSection: 0) - 1, section: 0), at: .top, animated: animated)
}

func selectBackgroundFor(index: Int, inMessages messages: [MessagesViewChatMessage], withBubble bubbbleImage: BubbleImage)->UIImage {
var result = UIImage()
let actualMessage = messages[index]
Expand Down Expand Up @@ -299,6 +318,7 @@ public class MessagesView: UIView {
settings.leftButtonShow = self.leftButtonShow
settings.leftButtonShowAnimated = self.leftButtonShowAnimated
settings.leftButtonTextColor = self.leftButtonTextColor
settings.leftButtonDisabledColor = self.leftButtonDisabledColor
settings.leftButtonBackgroundColor = self.leftButtonBackgroundColor
settings.leftButtonBackgroundImage = self.leftButtonBackgroundImage
settings.leftButtonCornerRadius = self.leftButtonCornerRadius
Expand All @@ -307,6 +327,7 @@ public class MessagesView: UIView {
settings.rightButtonShow = self.rightButtonShow
settings.rightButtonShowAnimated = self.rightButtonShowAnimated
settings.rightButtonTextColor = self.rightButtonTextColor
settings.rightButtonDisabledColor = self.rightButtonDisabledColor
settings.rightButtonBackgroundColor = self.rightButtonBackgroundColor
settings.rightButtonBackgroundImage = self.rightButtonBackgroundImage
settings.rightButtonCornerRadius = self.rightButtonCornerRadius
Expand All @@ -320,7 +341,8 @@ public class MessagesView: UIView {
}
}

extension MessagesView : UICollectionViewDataSource {
extension MessagesView: UICollectionViewDataSource {

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSource?.messages.count ?? 0
}
Expand Down Expand Up @@ -368,14 +390,11 @@ extension MessagesView : UICollectionViewDataSource {
}
}

extension MessagesView : UICollectionViewDelegateFlowLayout {
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("selected")
}

extension MessagesView: UICollectionViewDelegateFlowLayout {

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
guard let message = dataSource?.messages[indexPath.row].text,
let cell = MessageCollectionViewCell.fromNib() else {

guard let message = dataSource?.messages[indexPath.row].text, let cell = MessageCollectionViewCell.fromNib() else {
return defaultCellSize
}

Expand Down
Loading