session-ios/SignalMessaging/utils/ConversationStyle.swift

280 lines
8.3 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-09-27 15:15:15 +02:00
self.conversationColor = ConversationStyle.conversationColor(thread: thread)
2018-06-22 19:48:23 +02:00
super.init()
updateProperties()
NotificationCenter.default.addObserver(self,
selector: #selector(uiContentSizeCategoryDidChange),
2019-03-30 14:22:31 +01:00
name: UIContentSizeCategory.didChangeNotification,
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
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
@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 {
let colorName = thread.conversationColorName
2018-06-28 19:28:14 +02:00
return OWSConversationColor.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-10-09 22:06:23 +02:00
@objc
public let bubbleColorOutgoingFailed = UIColor.ows_darkSkyBlue
@objc
public let bubbleColorOutgoingSending = UIColor.ows_darkSkyBlue
@objc
public let bubbleColorOutgoingSent = UIColor.ows_darkSkyBlue
@objc
2018-09-19 16:08:27 +02:00
public let dateBreakTextColor = UIColor.ows_gray60
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-10-09 22:06:23 +02:00
return ConversationStyle.defaultBubbleColorIncoming
} else if let outgoingMessage = message as? TSOutgoingMessage {
switch outgoingMessage.messageState {
case .failed:
return bubbleColorOutgoingFailed
case .sending:
return bubbleColorOutgoingSending
default:
return bubbleColorOutgoingSent
}
2018-06-28 19:26:17 +02:00
} else {
2018-10-09 22:06:23 +02:00
owsFailDebug("Unexpected message type: \(message)")
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 {
2018-10-09 22:06:23 +02:00
return self.bubbleColorOutgoingSent
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
@objc
2018-07-09 15:58:02 +02:00
public func bubbleSecondaryTextColor(isIncoming: Bool) -> UIColor {
2018-10-09 22:06:23 +02:00
return bubbleTextColor(isIncoming: isIncoming).withAlphaComponent(0.7)
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-10-09 22:06:23 +02:00
if isIncoming {
return UIColor(rgbHex: 0x1976D2).withAlphaComponent(0.75)
} else {
return UIColor.ows_black.withAlphaComponent(0.4)
}
} else if isIncoming {
return bubbleColorOutgoingSent.withAlphaComponent(0.25)
2018-07-09 15:58:02 +02:00
} else {
2018-10-09 22:06:23 +02:00
return ConversationStyle.defaultBubbleColorIncoming.withAlphaComponent(0.75)
2018-07-09 15:58:02 +02:00
}
}
@objc
public func quotedReplyStripeColor(isIncoming: Bool) -> UIColor {
2018-10-09 22:06:23 +02:00
if Theme.isDarkThemeEnabled {
if isIncoming {
return UIColor(rgbHex: 0x1976D2)
} else {
return UIColor.ows_black
}
} else if isIncoming {
return bubbleColorOutgoingSent
2018-07-09 15:58:02 +02:00
} else {
2018-10-09 22:06:23 +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 quotedReplyTextColor()
2018-07-09 15:58:02 +02:00
}
@objc
public func quotedReplyTextColor() -> UIColor {
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
}