forked from MessageKit/MessageKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add AvatarView along with `AvatarView.Playground` Implemented code review changes. * Add tests because your are supposed to add tests for everything. * Replace references to the Avatar with AvatarView. Update Example Project. * Updated my `swiftLint` and fixed issues.
- Loading branch information
Showing
11 changed files
with
254 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
line_length: | ||
warning: 150 | ||
ignores_comments: true | ||
|
||
disabled_rules: | ||
identifier_name | ||
- identifier_name | ||
- trailing_whitespace | ||
- line_length |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import UIKit | ||
import MessageKit | ||
import PlaygroundSupport | ||
|
||
//: Discover what is possible with the Avatar Class | ||
//Get an image | ||
let testImage = #imageLiteral(resourceName: "NiceSelfi.jpg") | ||
|
||
let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 200)) | ||
|
||
view.backgroundColor = UIColor.white | ||
|
||
//: Uncomment any line to see how it changes the `Avatar`. | ||
//: By default its a circlular avatar with a gray background and initals of ? | ||
let avatar = AvatarView() | ||
|
||
//: Configure any one of the initilization parameters and delete the ones you dont want to set. | ||
//let avatar = AvatarView(size: 50, image: testImage, highlightedImage: testImage, initals: "PL", cornerRounding: 9) | ||
|
||
//: Throw in just an image. | ||
//let avatar = AvatarView(image: testImage) | ||
|
||
//: Dont have an image just add the users initals | ||
//let avatar = AvatarView(initals: "PL") | ||
|
||
//: Want rounded squares instead of circles just change the `cornderRounding`. | ||
//let avatar = AvatarView(image: testImage, cornerRounding: 9) | ||
|
||
//:Change its size | ||
//let avatar = AvatarView(size: 5) | ||
|
||
//let avatar = AvatarView(size: 100) | ||
|
||
//: Everything has a default so if you dont want to set it then you dont have to. | ||
|
||
//Helper method. | ||
PlaygroundPage.current.liveView = avatar |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='ios'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
MIT License | ||
|
||
Copyright (c) 2017 MessageKit | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
import Foundation | ||
|
||
open class AvatarView: UIView { | ||
// MARK: - Properties | ||
internal var initalsLabel = UILabel() | ||
internal var imageView = UIImageView() | ||
internal var initals: String = "?" | ||
|
||
// MARK: - initializers | ||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
prepareView() | ||
} | ||
|
||
convenience public init(size: CGFloat = 30, image: UIImage? = nil, highlightedImage: UIImage? = nil, initals inInitals: String = "?", cornerRounding: CGFloat? = nil) { | ||
let frame = CGRect(x: 0, y: 0, width: size, height: size) | ||
self.init(frame: frame) | ||
setCorner(radius: cornerRounding) | ||
setBackground(color: UIColor.gray) | ||
imageView.image = image | ||
imageView.highlightedImage = highlightedImage | ||
initals = inInitals | ||
prepareView() | ||
} | ||
|
||
convenience public init() { | ||
let frame = CGRect(x: 0, y: 0, width: 30, height: 30) | ||
self.init(frame: frame) | ||
setBackground(color: UIColor.gray) | ||
setCorner(radius: nil) | ||
prepareView() | ||
} | ||
|
||
required public init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
// MARK: - internal methods | ||
|
||
internal func prepareView() { | ||
prepareInitalsLabel() | ||
prepareImageView() | ||
imageView.isHidden = imageView.image == nil | ||
} | ||
|
||
internal func prepareInitalsLabel() { | ||
initalsLabel.text = initals | ||
initalsLabel.textAlignment = .center | ||
setInitalsFont() | ||
addSubview(initalsLabel) | ||
initalsLabel.center = center | ||
initalsLabel.frame = frame | ||
} | ||
|
||
internal func prepareImageView() { | ||
contentMode = .scaleAspectFill | ||
layer.masksToBounds = true | ||
clipsToBounds = true | ||
addSubview(imageView) | ||
imageView.contentMode = .scaleAspectFill | ||
imageView.frame = frame | ||
} | ||
|
||
// MARK: - Open methods | ||
|
||
open func set(image: UIImage) { | ||
imageView.image = image | ||
} | ||
|
||
open func setInitalsFont(size: CGFloat = 16, color: UIColor = .white) { | ||
initalsLabel.font = UIFont.systemFont(ofSize: size) | ||
initalsLabel.textColor = color | ||
} | ||
|
||
open func setBackground(color: UIColor) { | ||
backgroundColor = color | ||
} | ||
|
||
open func getImage() -> UIImage? { | ||
return imageView.image | ||
} | ||
|
||
open func setCorner(radius: CGFloat?) { | ||
guard let radius = radius else { | ||
//if corner radius not set default to Circle | ||
layer.cornerRadius = frame.height/2 | ||
return | ||
} | ||
layer.cornerRadius = radius | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.