session-ios/SignalMessaging/utils/ConversationStyle.swift

271 lines
8 KiB
Swift
Raw Normal View History

2018-06-22 19:48:23 +02:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
@objc
2018-06-25 21:20:17 +02:00
public class ConversationStyle: NSObject {
2018-06-22 19:48:23 +02:00
private let thread: TSThread
// The width of the collection view.
@objc public var viewWidth: CGFloat = 0 {
didSet {
AssertIsOnMainThread()
2018-06-22 19:48:23 +02:00
updateProperties()
}
}
2018-06-25 21:25:12 +02:00
@objc public let contentMarginTop: CGFloat = 24
@objc public let contentMarginBottom: CGFloat = 24
2018-06-22 19:48:23 +02:00
@objc public var gutterLeading: CGFloat = 0
@objc public var gutterTrailing: CGFloat = 0
@objc public var headerGutterLeading: CGFloat = 28
@objc public var headerGutterTrailing: CGFloat = 28
2018-06-22 19:48:23 +02:00
// These are the gutters used by "full width" views
// like "contact offer" and "info message".
2018-06-22 19:48:23 +02:00
@objc public var fullWidthGutterLeading: CGFloat = 0
@objc public var fullWidthGutterTrailing: CGFloat = 0
2018-07-05 16:28:47 +02:00
@objc public var errorGutterTrailing: CGFloat = 0
2018-06-22 19:48:23 +02:00
@objc public var contentWidth: CGFloat {
return viewWidth - (gutterLeading + gutterTrailing)
}
@objc public var fullWidthContentWidth: CGFloat {
return viewWidth - (fullWidthGutterLeading + fullWidthGutterTrailing)
}
2018-06-22 19:48:23 +02:00
@objc public var headerViewContentWidth: CGFloat {
return viewWidth - (headerGutterLeading + headerGutterTrailing)
}
2018-06-22 19:48:23 +02:00
@objc public var maxMessageWidth: CGFloat = 0
2018-06-26 00:06:08 +02:00
@objc public var textInsetTop: CGFloat = 0
@objc public var textInsetBottom: CGFloat = 0
@objc public var textInsetHorizontal: CGFloat = 0
2018-06-25 20:31:09 +02:00
// We want to align "group sender" avatars with the v-center of the
// "last line" of the message body text - or where it would be for
// non-text content.
//
// This is the distance from that v-center to the bottom of the
// message bubble.
@objc public var lastTextLineAxis: CGFloat = 0
2018-06-22 19:48:23 +02:00
@objc
public required init(thread: TSThread) {
self.thread = thread
2018-06-28 19:28:14 +02:00
self.primaryColor = ConversationStyle.primaryColor(thread: thread)
2018-06-22 19:48:23 +02:00
super.init()
updateProperties()
NotificationCenter.default.addObserver(self,
selector: #selector(uiContentSizeCategoryDidChange),
name: NSNotification.Name.UIContentSizeCategoryDidChange,
object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
@objc func uiContentSizeCategoryDidChange() {
AssertIsOnMainThread()
updateProperties()
2018-06-22 19:48:23 +02:00
}
// MARK: -
2018-06-28 19:28:14 +02:00
@objc
public func updateProperties() {
2018-06-22 19:48:23 +02:00
if thread.isGroupThread() {
2018-07-09 17:48:45 +02:00
gutterLeading = 52
2018-07-12 21:02:25 +02:00
gutterTrailing = 16
2018-06-22 19:48:23 +02:00
} else {
2018-06-22 22:56:33 +02:00
gutterLeading = 16
2018-07-12 21:02:25 +02:00
gutterTrailing = 16
2018-06-22 19:48:23 +02:00
}
fullWidthGutterLeading = 16
fullWidthGutterTrailing = 16
headerGutterLeading = 28
headerGutterTrailing = 28
2018-07-05 16:28:47 +02:00
errorGutterTrailing = 16
2018-06-22 19:48:23 +02:00
2018-07-12 21:02:25 +02:00
maxMessageWidth = floor(contentWidth - 32)
let messageTextFont = UIFont.ows_dynamicTypeBody
2018-07-06 20:11:40 +02:00
let baseFontOffset: CGFloat = 11
// Don't include the distance from the "cap height" to the top of the UILabel
// in the top margin.
2018-07-06 20:11:40 +02:00
textInsetTop = max(0, round(baseFontOffset - (messageTextFont.ascender - messageTextFont.capHeight)))
// Don't include the distance from the "baseline" to the bottom of the UILabel
// (e.g. the descender) in the top margin. Note that UIFont.descender is a
// negative value.
2018-07-06 20:11:40 +02:00
textInsetBottom = max(0, round(baseFontOffset - abs(messageTextFont.descender)))
if _isDebugAssertConfiguration(), UIFont.ows_dynamicTypeBody.pointSize == 17 {
assert(textInsetTop == 7)
assert(textInsetBottom == 7)
}
2018-06-26 00:06:08 +02:00
textInsetHorizontal = 12
2018-07-06 20:11:40 +02:00
lastTextLineAxis = CGFloat(round(baseFontOffset + messageTextFont.capHeight * 0.5))
2018-06-28 19:28:14 +02:00
self.primaryColor = ConversationStyle.primaryColor(thread: thread)
2018-06-22 19:48:23 +02:00
}
2018-06-28 19:26:17 +02:00
// MARK: Colors
2018-06-28 19:28:14 +02:00
private class func primaryColor(thread: TSThread) -> UIColor {
guard let colorName = thread.conversationColorName else {
return self.defaultBubbleColorIncoming
}
guard let color = UIColor.ows_conversationColor(colorName: colorName) else {
return self.defaultBubbleColorIncoming
}
return color
}
2018-08-16 17:31:55 +02:00
@objc
private static var defaultBubbleColorIncoming: UIColor {
return Theme.isDarkThemeEnabled ? UIColor.ows_dark70 : UIColor.ows_messageBubbleLightGray
}
2018-06-28 19:26:17 +02:00
@objc
2018-07-10 01:00:41 +02:00
public let bubbleColorOutgoingFailed = UIColor.ows_darkSkyBlue
2018-06-28 19:26:17 +02:00
@objc
public let bubbleColorOutgoingSending = UIColor.ows_darkSkyBlue
2018-06-28 19:28:14 +02:00
@objc
2018-07-09 22:31:34 +02:00
public let bubbleColorOutgoingSent = UIColor.ows_darkSkyBlue
2018-06-28 19:26:17 +02:00
@objc
public let dateBreakTextColor = UIColor.ows_light60
2018-06-28 19:26:17 +02:00
@objc
2018-06-28 19:28:14 +02:00
public var primaryColor: UIColor
2018-06-28 19:26:17 +02:00
@objc
2018-06-28 19:28:14 +02:00
public func bubbleColor(message: TSMessage) -> UIColor {
2018-06-28 19:26:17 +02:00
if message is TSIncomingMessage {
2018-07-06 22:18:45 +02:00
return ConversationStyle.defaultBubbleColorIncoming
2018-06-28 19:26:17 +02:00
} else if let outgoingMessage = message as? TSOutgoingMessage {
switch outgoingMessage.messageState {
case .failed:
2018-07-10 01:00:41 +02:00
return bubbleColorOutgoingFailed
2018-06-28 19:26:17 +02:00
case .sending:
2018-07-06 22:18:45 +02:00
return bubbleColorOutgoingSending
2018-06-28 19:26:17 +02:00
default:
2018-07-06 22:18:45 +02:00
return bubbleColorOutgoingSent
2018-06-28 19:26:17 +02:00
}
} else {
2018-08-27 16:27:48 +02:00
owsFailDebug("Unexpected message type: \(message)")
2018-07-06 22:18:45 +02:00
return bubbleColorOutgoingSent
2018-06-28 19:26:17 +02:00
}
}
2018-07-06 22:18:45 +02:00
@objc
public func bubbleColor(isIncoming: Bool) -> UIColor {
if isIncoming {
return ConversationStyle.defaultBubbleColorIncoming
2018-07-06 21:31:38 +02:00
} else {
return self.bubbleColorOutgoingSent
}
}
2018-07-02 15:42:48 +02:00
@objc
2018-08-16 17:31:55 +02:00
public static var bubbleTextColorIncoming: UIColor {
return Theme.isDarkThemeEnabled ? UIColor.ows_white : UIColor.ows_light90
}
@objc
2018-07-06 22:18:45 +02:00
public static var bubbleTextColorOutgoing = UIColor.ows_white
2018-07-02 15:42:48 +02:00
2018-06-28 19:26:17 +02:00
@objc
2018-06-28 19:28:14 +02:00
public func bubbleTextColor(message: TSMessage) -> UIColor {
2018-06-28 19:26:17 +02:00
if message is TSIncomingMessage {
2018-07-02 15:42:48 +02:00
return ConversationStyle.bubbleTextColorIncoming
2018-07-09 22:31:34 +02:00
} else if message is TSOutgoingMessage {
2018-07-06 22:18:45 +02:00
return ConversationStyle.bubbleTextColorOutgoing
2018-06-28 19:26:17 +02:00
} else {
2018-08-27 16:27:48 +02:00
owsFailDebug("Unexpected message type: \(message)")
2018-07-06 22:18:45 +02:00
return ConversationStyle.bubbleTextColorOutgoing
2018-06-28 19:26:17 +02:00
}
}
2018-07-06 21:31:38 +02:00
2018-07-06 22:18:45 +02:00
@objc
public func bubbleTextColor(isIncoming: Bool) -> UIColor {
if isIncoming {
2018-07-06 21:31:38 +02:00
return ConversationStyle.bubbleTextColorIncoming
} else {
2018-07-06 22:18:45 +02:00
return ConversationStyle.bubbleTextColorOutgoing
2018-07-06 21:31:38 +02:00
}
}
2018-07-06 22:18:45 +02:00
@objc
2018-07-09 15:58:02 +02:00
public func bubbleSecondaryTextColor(isIncoming: Bool) -> UIColor {
2018-07-06 22:18:45 +02:00
return bubbleTextColor(isIncoming: isIncoming).withAlphaComponent(0.7)
}
2018-07-09 15:58:02 +02:00
@objc
public func quotedReplyBubbleColor(isIncoming: Bool) -> UIColor {
2018-08-21 22:11:51 +02:00
if Theme.isDarkThemeEnabled {
2018-08-21 22:36:58 +02:00
let alpha: CGFloat = (isIncoming ? 0.6 :0.5)
2018-08-21 22:11:51 +02:00
return UIColor.white.blend(with: bubbleColorOutgoingSent, alpha: alpha)
} else if isIncoming {
2018-07-09 22:31:34 +02:00
return bubbleColorOutgoingSent.withAlphaComponent(0.25)
2018-07-09 15:58:02 +02:00
} else {
2018-08-21 22:13:12 +02:00
return ConversationStyle.defaultBubbleColorIncoming.withAlphaComponent(0.75)
2018-07-09 15:58:02 +02:00
}
}
@objc
public func quotedReplyStripeColor(isIncoming: Bool) -> UIColor {
if isIncoming {
return bubbleColorOutgoingSent
} else {
2018-07-09 22:31:34 +02:00
return UIColor.white
2018-07-09 15:58:02 +02:00
}
}
@objc
public func quotingSelfHighlightColor() -> UIColor {
// TODO:
return UIColor.init(rgbHex: 0xB5B5B5)
}
@objc
public func quotedReplyAuthorColor() -> UIColor {
return UIColor.ows_light90
}
@objc
public func quotedReplyTextColor() -> UIColor {
return UIColor.ows_light90
}
@objc
public func quotedReplyAttachmentColor() -> UIColor {
// TODO:
2018-08-08 17:07:05 +02:00
return Theme.middleGrayColor
2018-07-09 15:58:02 +02:00
}
2018-06-22 19:48:23 +02:00
}