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 {
|
2018-08-22 19:44:22 +02:00
|
|
|
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
|
2018-07-14 01:24:42 +02:00
|
|
|
|
|
|
|
@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
|
2018-07-14 01:24:42 +02:00
|
|
|
// 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-14 01:24:42 +02:00
|
|
|
|
2018-07-05 16:28:47 +02:00
|
|
|
@objc public var errorGutterTrailing: CGFloat = 0
|
2018-06-22 19:48:23 +02:00
|
|
|
|
2018-07-16 18:17:50 +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
|
|
|
|
2018-07-16 18:17:50 +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-22 22:52:26 +02:00
|
|
|
|
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-09-27 15:15:15 +02:00
|
|
|
self.conversationColor = ConversationStyle.conversationColor(thread: thread)
|
2018-06-22 19:48:23 +02:00
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
updateProperties()
|
2018-06-22 22:52:26 +02:00
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self,
|
|
|
|
selector: #selector(uiContentSizeCategoryDidChange),
|
|
|
|
name: NSNotification.Name.UIContentSizeCategoryDidChange,
|
|
|
|
object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func uiContentSizeCategoryDidChange() {
|
2018-08-22 19:44:22 +02:00
|
|
|
AssertIsOnMainThread()
|
2018-06-22 22:52:26 +02:00
|
|
|
|
|
|
|
updateProperties()
|
2018-06-22 19:48:23 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 22:52:26 +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
|
|
|
}
|
2018-07-09 23:02:25 +02:00
|
|
|
fullWidthGutterLeading = 16
|
|
|
|
fullWidthGutterTrailing = 16
|
2018-07-14 01:24:42 +02:00
|
|
|
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)
|
2018-06-22 22:52:26 +02:00
|
|
|
|
|
|
|
let messageTextFont = UIFont.ows_dynamicTypeBody
|
2018-07-06 20:11:40 +02:00
|
|
|
|
|
|
|
let baseFontOffset: CGFloat = 11
|
|
|
|
|
2018-06-22 22:52:26 +02:00
|
|
|
// 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)))
|
2018-06-22 22:52:26 +02:00
|
|
|
// 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-06-22 22:52:26 +02:00
|
|
|
|
2018-07-06 20:11:40 +02:00
|
|
|
lastTextLineAxis = CGFloat(round(baseFontOffset + messageTextFont.capHeight * 0.5))
|
2018-06-28 19:28:14 +02:00
|
|
|
|
2018-09-27 15:15:15 +02:00
|
|
|
self.conversationColor = ConversationStyle.conversationColor(thread: thread)
|
2018-06-22 19:48:23 +02:00
|
|
|
}
|
2018-06-28 19:26:17 +02:00
|
|
|
|
|
|
|
// MARK: Colors
|
|
|
|
|
2018-09-26 20:27:30 +02:00
|
|
|
@objc
|
2018-09-27 15:15:15 +02:00
|
|
|
public var conversationColor: OWSConversationColor
|
2018-06-28 19:28:14 +02:00
|
|
|
|
2018-09-27 15:15:15 +02:00
|
|
|
private class func conversationColor(thread: TSThread) -> OWSConversationColor {
|
2018-09-26 20:27:30 +02:00
|
|
|
let colorName = thread.conversationColorName
|
2018-06-28 19:28:14 +02:00
|
|
|
|
2018-09-27 15:15:15 +02:00
|
|
|
return UIColor.ows_conversationColorOrDefault(colorName: colorName)
|
2018-06-28 19:28:14 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 17:31:55 +02:00
|
|
|
@objc
|
|
|
|
private static var defaultBubbleColorIncoming: UIColor {
|
2018-09-19 16:08:27 +02:00
|
|
|
return Theme.isDarkThemeEnabled ? UIColor.ows_gray75 : UIColor.ows_messageBubbleLightGray
|
2018-08-16 17:31:55 +02:00
|
|
|
}
|
2018-06-28 19:26:17 +02:00
|
|
|
|
2018-07-09 21:28:14 +02:00
|
|
|
@objc
|
2018-09-19 16:08:27 +02:00
|
|
|
public let dateBreakTextColor = UIColor.ows_gray60
|
2018-07-09 21:28:14 +02:00
|
|
|
|
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-09-26 20:27:30 +02:00
|
|
|
return bubbleColor(isIncoming: true)
|
2018-06-28 19:26:17 +02:00
|
|
|
} else {
|
2018-09-26 20:27:30 +02:00
|
|
|
return bubbleColor(isIncoming: false)
|
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 {
|
2018-09-27 15:28:16 +02:00
|
|
|
return conversationColor.primaryColor
|
2018-07-06 21:31:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 15:42:48 +02:00
|
|
|
@objc
|
2018-08-16 17:31:55 +02:00
|
|
|
public static var bubbleTextColorIncoming: UIColor {
|
2018-09-26 20:59:23 +02:00
|
|
|
return Theme.isDarkThemeEnabled ? UIColor.ows_gray05 : UIColor.ows_gray90
|
2018-08-16 17:31:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
2018-09-26 20:59:23 +02:00
|
|
|
public static var bubbleTextColorOutgoing: UIColor {
|
|
|
|
return Theme.isDarkThemeEnabled ? UIColor.ows_gray05 : 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
|
|
|
|
2018-09-27 17:18:58 +02:00
|
|
|
// Note that the exception for outgoing text only applies
|
|
|
|
// to secondary text within bubbles.
|
2018-07-06 22:18:45 +02:00
|
|
|
@objc
|
2018-07-09 15:58:02 +02:00
|
|
|
public func bubbleSecondaryTextColor(isIncoming: Bool) -> UIColor {
|
2018-09-26 20:59:23 +02:00
|
|
|
if !isIncoming {
|
|
|
|
// All Outgoing
|
|
|
|
return UIColor.ows_white.withAlphaComponent(0.8)
|
|
|
|
} else if Theme.isDarkThemeEnabled {
|
|
|
|
// Incoming, dark.
|
|
|
|
return UIColor.ows_gray25
|
|
|
|
} else {
|
|
|
|
// Incoming, light.
|
|
|
|
return UIColor.ows_gray60
|
|
|
|
}
|
2018-07-06 22:18:45 +02:00
|
|
|
}
|
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-09-27 15:15:15 +02:00
|
|
|
return conversationColor.shadeColor
|
2018-07-09 15:58:02 +02:00
|
|
|
} else {
|
2018-09-27 15:15:15 +02:00
|
|
|
return conversationColor.tintColor
|
2018-07-09 15:58:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
public func quotedReplyStripeColor(isIncoming: Bool) -> UIColor {
|
|
|
|
if isIncoming {
|
2018-09-27 15:28:16 +02:00
|
|
|
return conversationColor.primaryColor
|
2018-07-09 15:58:02 +02:00
|
|
|
} else {
|
2018-09-26 20:27:30 +02:00
|
|
|
return Theme.backgroundColor
|
2018-07-09 15:58:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
public func quotingSelfHighlightColor() -> UIColor {
|
|
|
|
// TODO:
|
|
|
|
return UIColor.init(rgbHex: 0xB5B5B5)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
public func quotedReplyAuthorColor() -> UIColor {
|
2018-09-26 20:40:06 +02:00
|
|
|
return quotedReplyTextColor()
|
2018-07-09 15:58:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
public func quotedReplyTextColor() -> UIColor {
|
2018-09-26 20:40:06 +02:00
|
|
|
if Theme.isDarkThemeEnabled {
|
|
|
|
return UIColor.ows_gray05
|
|
|
|
} else {
|
|
|
|
return UIColor.ows_gray90
|
|
|
|
}
|
2018-07-09 15:58:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|