Skip to content

Commit

Permalink
Merge pull request #4 from pgs-dkanak/develop
Browse files Browse the repository at this point in the history
Develop 0.0.5
  • Loading branch information
pgs-dkanak authored Apr 3, 2017
2 parents fc9b6dc + cab5a55 commit 8496430
Show file tree
Hide file tree
Showing 13 changed files with 355 additions and 120 deletions.
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
*.xcuserdata

## Various Xcode settings and user-related
*.pbxuser
*.xccheckout
*.xcuserdatad
*.xcscheme
*.xcuserstate

## OSX
.DS_Store
4 changes: 4 additions & 0 deletions MessagesView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
05925ADF1E82D21B00421928 /* MessagesView.swift in Headers */ = {isa = PBXBuildFile; fileRef = 05ECAFA51E7ADFEF00833D84 /* MessagesView.swift */; settings = {ATTRIBUTES = (Public, ); }; };
05925AE71E82E24A00421928 /* MessagesViewSettings.swift in Headers */ = {isa = PBXBuildFile; fileRef = 05ECAFAA1E7ADFEF00833D84 /* MessagesViewSettings.swift */; settings = {ATTRIBUTES = (Public, ); }; };
05B94A351E842FC400CAB715 /* ColorUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05B94A341E842FC400CAB715 /* ColorUtils.swift */; };
05ECAF8E1E7ADF8400833D84 /* MessagesView.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 05ECAF841E7ADF8400833D84 /* MessagesView.framework */; };
05ECAF931E7ADF8400833D84 /* MessagesViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05ECAF921E7ADF8400833D84 /* MessagesViewTests.swift */; };
05ECAF951E7ADF8400833D84 /* MessagesView.h in Headers */ = {isa = PBXBuildFile; fileRef = 05ECAF871E7ADF8400833D84 /* MessagesView.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand All @@ -35,6 +36,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
05B94A341E842FC400CAB715 /* ColorUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorUtils.swift; sourceTree = "<group>"; };
05ECAF841E7ADF8400833D84 /* MessagesView.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MessagesView.framework; sourceTree = BUILT_PRODUCTS_DIR; };
05ECAF871E7ADF8400833D84 /* MessagesView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MessagesView.h; sourceTree = "<group>"; };
05ECAF881E7ADF8400833D84 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -94,6 +96,7 @@
isa = PBXGroup;
children = (
05ECAF871E7ADF8400833D84 /* MessagesView.h */,
05B94A341E842FC400CAB715 /* ColorUtils.swift */,
05ECAF881E7ADF8400833D84 /* Info.plist */,
05ECAF9E1E7ADFEF00833D84 /* MessageCollectionViewCell.swift */,
05ECAF9F1E7ADFEF00833D84 /* MessageCollectionViewCell.xib */,
Expand Down Expand Up @@ -239,6 +242,7 @@
05ECAFB01E7ADFEF00833D84 /* MessagesToolbarContentView.swift in Sources */,
05ECAFAF1E7ADFEF00833D84 /* MessagesInputToolbar.swift in Sources */,
05ECAFB21E7ADFEF00833D84 /* MessagesView.swift in Sources */,
05B94A351E842FC400CAB715 /* ColorUtils.swift in Sources */,
05ECAFAD1E7ADFEF00833D84 /* MessageEditorTextView.swift in Sources */,
05ECAFB71E7ADFEF00833D84 /* MessagesViewSettings.swift in Sources */,
05ECAFAB1E7ADFEF00833D84 /* MessageCollectionViewCell.swift in Sources */,
Expand Down
20 changes: 20 additions & 0 deletions MessagesView/ColorUtils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// ColorUtils.swift
// MessagesView
//
// Created by Damian Kanak on 23/03/17.
// Copyright © 2017 pgs-dkanak. All rights reserved.
//

import Foundation

extension UIColor {
var rgba : (CGFloat, CGFloat, CGFloat, CGFloat) {
var red = CGFloat()
var green = CGFloat()
var blue = CGFloat()
var alpha = CGFloat()
self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
return (red, green, blue, alpha)
}
}
43 changes: 41 additions & 2 deletions MessagesView/MessageCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ class MessageCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var leftArrowView: UIView!
@IBOutlet weak var rightArrowView: UIView!

@IBOutlet weak var labelWidthLayoutConstraint: NSLayoutConstraint!
@IBOutlet weak var labelLeadingConstraint: NSLayoutConstraint!
@IBOutlet weak var labelTrailingConstraint: NSLayoutConstraint!

@IBOutlet weak var backgroundTrailingConstraint: NSLayoutConstraint!
@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)
static var rightArrowImage = UIImage(cgImage: (leftArrowImage.cgImage)!, scale: 1.0, orientation: .upMirrored).withRenderingMode(.alwaysTemplate)

static let patternCell = MessageCollectionViewCell.fromNib()
static var hostPeerSide = Side.right
Expand All @@ -47,6 +54,8 @@ class MessageCollectionViewCell: UICollectionViewCell {
}
}

private var backgroundMarginConstant : CGFloat = 0.0

class func fromNib() -> MessageCollectionViewCell?
{
var cell: MessageCollectionViewCell?
Expand All @@ -72,6 +81,7 @@ class MessageCollectionViewCell: UICollectionViewCell {
super.awakeFromNib()
messageBackgroundView.backgroundColor = self.textBackgroundColor
messageBackgroundView.layer.cornerRadius = self.cornerRadius
backgroundMarginConstant = self.backgroundTrailingConstraint.constant
}

func addTails() {
Expand All @@ -92,6 +102,17 @@ class MessageCollectionViewCell: UICollectionViewCell {
}
}

func addMessageMargin(side: Side, margin: CGFloat) {
switch side {
case .left:
backgroundLeadingConstraint.constant = backgroundMarginConstant
backgroundTrailingConstraint.constant = backgroundMarginConstant + margin
case .right:
backgroundLeadingConstraint.constant = backgroundMarginConstant + margin
backgroundTrailingConstraint.constant = backgroundMarginConstant
}
}

static func createArrowImage(inSize size: CGSize) -> UIImage! {
UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
Expand All @@ -105,7 +126,7 @@ class MessageCollectionViewCell: UICollectionViewCell {
context?.fillPath()
context?.strokePath()

let result = UIGraphicsGetImageFromCurrentImageContext()
let result = UIGraphicsGetImageFromCurrentImageContext()?.withRenderingMode(.alwaysTemplate)
UIGraphicsEndImageContext()
return result
}
Expand All @@ -124,4 +145,22 @@ class MessageCollectionViewCell: UICollectionViewCell {

return mutablePath
}

func size(message: String, containerInsets: CGFloat) -> CGSize {
let labelMargins = labelLeadingConstraint.constant + backgroundLeadingConstraint.constant + backgroundTrailingConstraint.constant + labelTrailingConstraint.constant

textLabel.text = message
textLabel.preferredMaxLayoutWidth = containerInsets - labelMargins
labelWidthLayoutConstraint.constant = containerInsets - labelMargins

return contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
}

func applySettings(settings: MessagesViewSettings) {
textLabel.textColor = settings.messageCellTextColor
messageBackgroundView.backgroundColor = settings.messageCellBackgroundColor
leftArrowView.tintColor = settings.messageCellBackgroundColor
rightArrowView.tintColor = settings.messageCellBackgroundColor

}
}
28 changes: 16 additions & 12 deletions MessagesView/MessageCollectionViewCell.xib
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="11762" systemVersion="15G1217" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="11762" systemVersion="15G1108" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
Expand Down Expand Up @@ -36,10 +36,9 @@
<rect key="frame" x="4" y="0.0" width="308" height="120"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="1000" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="2qa-5e-3Ca">
<rect key="frame" x="4" y="0.0" width="300" height="120"/>
<rect key="frame" x="15" y="15" width="278" height="90"/>
<constraints>
<constraint firstAttribute="width" constant="300" id="MRO-Ve-WzL"/>
<constraint firstAttribute="height" relation="greaterThanOrEqual" constant="30" id="koe-To-wEN"/>
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="100" identifier="LabelWidth" id="1hU-jZ-0oE"/>
</constraints>
<string key="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris eget vehicula quam. Phasellus fermentum arcu et nibh vehicula vestibulum. Integer sed.</string>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
Expand All @@ -49,22 +48,22 @@
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="2qa-5e-3Ca" secondAttribute="trailing" constant="4" id="50q-g0-tul"/>
<constraint firstAttribute="bottom" secondItem="2qa-5e-3Ca" secondAttribute="bottom" id="FUU-Z0-img"/>
<constraint firstItem="2qa-5e-3Ca" firstAttribute="top" secondItem="uWC-cc-3na" secondAttribute="top" id="biN-oX-Ydt"/>
<constraint firstItem="2qa-5e-3Ca" firstAttribute="leading" secondItem="uWC-cc-3na" secondAttribute="leading" constant="4" id="xyj-dZ-MNv"/>
<constraint firstAttribute="trailing" secondItem="2qa-5e-3Ca" secondAttribute="trailing" constant="15" identifier="LabelTrailing" id="50q-g0-tul"/>
<constraint firstAttribute="bottom" secondItem="2qa-5e-3Ca" secondAttribute="bottom" constant="15" identifier="LabelBottom" id="FUU-Z0-img"/>
<constraint firstItem="2qa-5e-3Ca" firstAttribute="top" secondItem="uWC-cc-3na" secondAttribute="top" constant="15" identifier="LabelTop" id="biN-oX-Ydt"/>
<constraint firstItem="2qa-5e-3Ca" firstAttribute="leading" secondItem="uWC-cc-3na" secondAttribute="leading" constant="15" identifier="LabelLeading" id="xyj-dZ-MNv"/>
</constraints>
</view>
</subviews>
</view>
<constraints>
<constraint firstItem="CJS-15-32Z" firstAttribute="leading" secondItem="LAY-9S-DAx" secondAttribute="leading" id="01B-Uk-1fE"/>
<constraint firstAttribute="bottom" secondItem="uWC-cc-3na" secondAttribute="bottom" id="AMf-il-HQw"/>
<constraint firstAttribute="bottom" secondItem="uWC-cc-3na" secondAttribute="bottom" identifier="BackgroundBottom" id="AMf-il-HQw"/>
<constraint firstAttribute="bottom" secondItem="VAr-8b-Ve6" secondAttribute="bottom" id="E9Q-zc-wLh"/>
<constraint firstItem="uWC-cc-3na" firstAttribute="top" secondItem="LAY-9S-DAx" secondAttribute="top" id="NQ1-Nh-lTa"/>
<constraint firstItem="uWC-cc-3na" firstAttribute="top" secondItem="LAY-9S-DAx" secondAttribute="top" identifier="BackgroundTop" id="NQ1-Nh-lTa"/>
<constraint firstAttribute="trailing" secondItem="VAr-8b-Ve6" secondAttribute="trailing" id="RIQ-Rc-efU"/>
<constraint firstItem="uWC-cc-3na" firstAttribute="leading" secondItem="LAY-9S-DAx" secondAttribute="leading" constant="4" id="crS-JE-wZw"/>
<constraint firstAttribute="trailing" secondItem="uWC-cc-3na" secondAttribute="trailing" constant="4" id="dcA-tX-vXV"/>
<constraint firstItem="uWC-cc-3na" firstAttribute="leading" secondItem="LAY-9S-DAx" secondAttribute="leading" constant="4" identifier="BackgroundLeading" id="crS-JE-wZw"/>
<constraint firstAttribute="trailing" secondItem="uWC-cc-3na" secondAttribute="trailing" constant="4" identifier="BackgroundTrailing" id="dcA-tX-vXV"/>
<constraint firstAttribute="bottom" secondItem="CJS-15-32Z" secondAttribute="bottom" id="kma-mL-CjR"/>
</constraints>
<size key="customSize" width="325" height="120"/>
Expand All @@ -83,6 +82,11 @@
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<outlet property="backgroundLeadingConstraint" destination="crS-JE-wZw" id="DG4-eZ-1EV"/>
<outlet property="backgroundTrailingConstraint" destination="dcA-tX-vXV" id="5WS-s3-US3"/>
<outlet property="labelLeadingConstraint" destination="xyj-dZ-MNv" id="4Zl-t4-oIz"/>
<outlet property="labelTrailingConstraint" destination="50q-g0-tul" id="66s-tE-UMV"/>
<outlet property="labelWidthLayoutConstraint" destination="1hU-jZ-0oE" id="xMw-2T-zhE"/>
<outlet property="leftArrowView" destination="CJS-15-32Z" id="uO2-I1-7AH"/>
<outlet property="messageBackgroundView" destination="uWC-cc-3na" id="4gE-KD-oIT"/>
<outlet property="rightArrowView" destination="VAr-8b-Ve6" id="tuR-Vo-0fM"/>
Expand Down
6 changes: 6 additions & 0 deletions MessagesView/MessageEditorTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@ class MessageEditorTextView: UITextView {
func setupTextView() {
self.backgroundColor = UIColor.yellow
}

func applySettings(settings: MessagesViewSettings) {
self.textColor = settings.textInputFieldTextColor
self.backgroundColor = settings.textInputFieldBackgroundColor
self.layer.cornerRadius = settings.textInputFieldCornerRadius
}

}
3 changes: 3 additions & 0 deletions MessagesView/MessagesCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ class MessagesCollectionView: UICollectionView {
}
*/

func apply(settings: MessagesViewSettings) {
self.backgroundColor = settings.collectionViewBackgroundColor
}
}
8 changes: 8 additions & 0 deletions MessagesView/MessagesInputToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ class MessagesInputToolbar: UIToolbar {
addSubview(toolbarContentView)
toolbarContentView.frame = self.bounds
}

func rightButton(show: Bool, animated: Bool) {
toolbarContentView.righButton(show: show, animated: animated)
}

func leftButton(show: Bool, animated: Bool) {
toolbarContentView.leftButton(show: show, animated: animated)
}
}
107 changes: 102 additions & 5 deletions MessagesView/MessagesToolbarContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,34 @@ class MessagesToolbarContentView: UIView {
}
*/

@IBOutlet weak var leftButtonContainerView: UIView!
@IBOutlet weak var rightButtonContainerView: UIView!
@IBOutlet weak var leftButtonContainerView: UIImageView!
@IBOutlet weak var leftButtonLabel: UILabel!

@IBOutlet weak var rightButtonContainerView: UIImageView!
@IBOutlet weak var rightButtonLabel: UILabel!

@IBOutlet weak var leftButtonContainerViewLeadingConstraint: NSLayoutConstraint!
@IBOutlet weak var leftButtonContainerViewWidthConstraint: NSLayoutConstraint!
@IBOutlet weak var rightButtonContainerViewTrailingConstraint: NSLayoutConstraint!
@IBOutlet weak var rightButtonContainerViewWidthConstraint: NSLayoutConstraint!

@IBOutlet weak var messageEditorTextView: MessageEditorTextView!

var settings = MessagesViewSettings()
var messageText : String {
return messageEditorTextView.text
private var originalLeftButtonContainerViewMargin = CGFloat(0)
private var originalLeftButtonContainerViewWidth = CGFloat(0)
private var originalRightButtonContainerViewMargin = CGFloat(0)
private var originalRightButtonContainerViewWidth = CGFloat(0)

override func awakeFromNib() {
super.awakeFromNib()
saveOriginalConstraintValues()
}

private func saveOriginalConstraintValues() {
originalLeftButtonContainerViewMargin = leftButtonContainerViewLeadingConstraint.constant
originalLeftButtonContainerViewWidth = leftButtonContainerViewWidthConstraint.constant
originalRightButtonContainerViewMargin = rightButtonContainerViewTrailingConstraint.constant
originalRightButtonContainerViewWidth = rightButtonContainerViewWidthConstraint.constant
}

@IBAction func didPressRightButton(_ sender: AnyObject) {
Expand All @@ -46,6 +67,81 @@ class MessagesToolbarContentView: UIView {
let nibViews = bundle.loadNibNamed(String(describing: MessagesToolbarContentView.self), owner: nil, options: nil)
return nibViews!.first as! MessagesToolbarContentView
}

var settings = MessagesViewSettings() {
didSet {
apply(settings: settings)
}
}
var messageText : String {
return messageEditorTextView.text
}

func righButton(show: Bool, animated: Bool) {
let destination = calculateDestination(side: .right, show: show)
move(view: self.rightButtonContainerView, animated: animated, constraint: self.rightButtonContainerViewTrailingConstraint, value: destination.margin, alpha: destination.alpha)
}

func leftButton(show: Bool, animated: Bool) {
let destination = calculateDestination(side: .left, show: show)
move(view: self.leftButtonContainerView, animated: animated, constraint: self.leftButtonContainerViewLeadingConstraint, value: destination.margin, alpha: destination.alpha)
}

private func calculateDestination(side: Side, show: Bool) -> (margin: CGFloat, alpha: CGFloat) {
let xMargin: CGFloat
let alpha: CGFloat
switch (side, show) {
case (.right, true):
xMargin = originalRightButtonContainerViewMargin
alpha = 1.0
case (.right, false):
xMargin = -originalRightButtonContainerViewWidth
alpha = 0.0
case (.left, true):
xMargin = originalLeftButtonContainerViewMargin
alpha = 1.0
case (.left, false):
xMargin = -originalLeftButtonContainerViewWidth
alpha = 0.0
}

return (xMargin, alpha)
}

private func move( view: UIView, animated: Bool, constraint: NSLayoutConstraint, value: CGFloat, alpha: CGFloat) {
let performTransition = {
constraint.constant = value
view.alpha = alpha
view.superview?.layoutIfNeeded()
}

switch animated {
case true:
UIView.animate(withDuration: settings.buttonSlideAnimationDuration, animations: {
performTransition()
})
case false:
performTransition()
}
}

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

backgroundColor = settings.inputToolbarBackgroundColor

leftButtonLabel.text = settings.leftButtonText
leftButtonLabel.textColor = settings.leftButtonTextColor
leftButtonContainerView.backgroundColor = settings.leftButtonBackgroundColor
leftButtonContainerView.image = settings.leftButtonBackgroundImage
leftButtonContainerView.layer.cornerRadius = settings.leftButtonCornerRadius

rightButtonLabel.text = settings.rightButtonText
rightButtonLabel.textColor = settings.rightButtonTextColor
rightButtonContainerView.backgroundColor = settings.rightButtonBackgroundColor
rightButtonContainerView.image = settings.rightButtonBackgroundImage
rightButtonContainerView.layer.cornerRadius = settings.rightButtonCornerRadius
}
}

extension MessagesToolbarContentView : UITextFieldDelegate {
Expand All @@ -69,3 +165,4 @@ extension MessagesToolbarContentView : UITextFieldDelegate {
return true
}
}

Loading

0 comments on commit 8496430

Please sign in to comment.