Skip to content

Instantly share code, notes, and snippets.

View SergeyPetrachkov's full-sized avatar
:octocat:

Sergey Petrachkov SergeyPetrachkov

:octocat:
View GitHub Profile
@SergeyPetrachkov
SergeyPetrachkov / ChatsService.swift
Created September 3, 2018 06:59
chats service for medium article
protocol ChatsService {
func getChats(request: GetConversationsRequest) throws -> [Message]
func getChatMessages(request: GetConversationMessagesRequest) throws -> [Message]
func submitMessage(request: SubmitMessageRequest) throws -> Message
func getUnreadMessagesNumber() throws -> Int
}
@SergeyPetrachkov
SergeyPetrachkov / StringInitials.swift
Created January 11, 2018 10:15
Get string initials Swift
extension String {
public var initials: String {
var finalString = String()
var words = components(separatedBy: .whitespacesAndNewlines)
if let firstCharacter = words.first?.first {
finalString.append(String(firstCharacter))
words.removeFirst()
}
@SergeyPetrachkov
SergeyPetrachkov / RoundedInitialsAvatar.swift
Created January 11, 2018 10:13
Create image with specified color and text inside like Telegram's conversation default avatar
extension UIImage {
static func roundedTextAvatar(text: String?,
color: UIColor,
circular: Bool,
textAttributes: [NSAttributedStringKey: Any]?,
size: CGSize) -> UIImage? {
let scale = Float(UIScreen.main.scale)
UIGraphicsBeginImageContextWithOptions(size, false, CGFloat(scale))
let context = UIGraphicsGetCurrentContext()
@SergeyPetrachkov
SergeyPetrachkov / Copyable.swift
Created January 11, 2018 10:08
Copyable in Swift
public protocol Copyable {
init(other: Self)
}
public extension Copyable {
public func copy() -> Self {
return Self.init(other: self)
}
}