session-ios/SignalMessaging/utils/ConversationStyle.swift

262 lines
8.2 KiB
Swift
Raw Normal View History

2018-06-22 19:48:23 +02:00
//
2019-03-30 14:24:40 +01:00
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
2018-06-22 19:48:23 +02:00
//
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()
}
}
2019-12-10 04:27:38 +01:00
@objc public let contentMarginTop: CGFloat = 24 // Values.largeSpacing
@objc public let contentMarginBottom: CGFloat = 24 // Values.largeSpacing
2018-06-22 19:48:23 +02:00
@objc public var gutterLeading: CGFloat = 0
@objc public var gutterTrailing: CGFloat = 0
2019-12-10 04:27:38 +01:00
@objc public var headerGutterLeading: CGFloat = 35 // Values.veryLargeSpacing
@objc public var headerGutterTrailing: CGFloat = 35 // Values.veryLargeSpacing
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() {
2019-12-11 00:25:53 +01:00
if let thread = thread as? TSGroupThread, thread.isRSSFeed {
gutterLeading = 16
gutterTrailing = 16
} else {
gutterLeading = 16 + 35 + 24 // Values.mediumSpacing + Values.smallProfilePictureSize + Values.largeSpacing
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
2019-12-10 04:27:38 +01:00
headerGutterLeading = 16
headerGutterTrailing = 16
2018-07-05 16:28:47 +02:00
errorGutterTrailing = 16
2018-06-22 19:48:23 +02:00
2019-12-11 00:25:53 +01:00
if let thread = thread as? TSGroupThread, thread.isRSSFeed {
maxMessageWidth = floor(contentWidth)
} else {
maxMessageWidth = floor(contentWidth - 32)
}
2019-12-10 04:27:38 +01:00
let messageTextFont = UIFont.systemFont(ofSize: 13) // Values.smallFontSize
2018-07-06 20:11:40 +02:00
2019-12-10 04:27:38 +01:00
let baseFontOffset: CGFloat = 16
2018-07-06 20:11:40 +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)))
// 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)))
2019-12-10 04:27:38 +01:00
textInsetHorizontal = 16
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 {
2019-12-10 04:27:38 +01:00
return UIColor(rgbHex: 0x222325) // Colors.receivedMessageBackgroundColor
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
2019-12-10 04:27:38 +01:00
public let bubbleColorOutgoingFailed = UIColor(rgbHex: 0x3F4146) // Colors.sentMessageBackgroundColor
2018-10-09 22:06:23 +02:00
@objc
2019-12-10 04:27:38 +01:00
public let bubbleColorOutgoingSending = UIColor(rgbHex: 0x3F4146) // Colors.sentMessageBackgroundColor
2018-10-09 22:06:23 +02:00
@objc
2019-12-10 04:27:38 +01:00
public let bubbleColorOutgoingSent = UIColor(rgbHex: 0x3F4146) // Colors.sentMessageBackgroundColor
2018-10-09 22:06:23 +02:00
@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 {
2019-12-10 04:27:38 +01:00
return UIColor(rgbHex: 0xFFFFFF) // Colors.text
2018-08-16 17:31:55 +02:00
}
@objc
2018-09-26 20:59:23 +02:00
public static var bubbleTextColorOutgoing: UIColor {
2019-12-10 04:27:38 +01:00
return UIColor(rgbHex: 0xFFFFFF) // Colors.text
2018-09-26 20:59:23 +02:00
}
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 {
2019-12-10 04:27:38 +01:00
return bubbleTextColor(isIncoming: isIncoming).withAlphaComponent(0.6) // Values.unimportantElementOpacity
2018-07-06 22:18:45 +02:00
}
2018-07-09 15:58:02 +02:00
@objc
public func quotedReplyBubbleColor(isIncoming: Bool) -> UIColor {
2019-12-10 05:11:46 +01:00
if isIncoming {
return UIColor(rgbHex: 0x3F4146) // Colors.sentMessageBackgroundColor
2018-07-09 15:58:02 +02:00
} else {
2019-12-10 05:11:46 +01:00
return UIColor(rgbHex: 0x222325) // Colors.receivedMessageBackgroundColor
2018-07-09 15:58:02 +02:00
}
}
@objc
public func quotedReplyStripeColor(isIncoming: Bool) -> UIColor {
2019-12-10 05:11:46 +01:00
return UIColor(rgbHex: 0x00F782) // Colors.accent
2018-07-09 15:58:02 +02:00
}
@objc
public func quotingSelfHighlightColor() -> UIColor {
2019-12-11 00:25:53 +01:00
return UIColor.init(rgbHex: 0xB5B5B5)
2018-07-09 15:58:02 +02:00
}
@objc
public func quotedReplyAuthorColor() -> UIColor {
2019-12-10 05:11:46 +01:00
return UIColor(rgbHex: 0xFFFFFF) // Colors.text
2018-07-09 15:58:02 +02:00
}
@objc
public func quotedReplyTextColor() -> UIColor {
2019-12-10 05:11:46 +01:00
return UIColor(rgbHex: 0xFFFFFF) // Colors.text
2018-07-09 15:58:02 +02:00
}
@objc
public func quotedReplyAttachmentColor() -> UIColor {
2019-12-10 05:11:46 +01:00
return UIColor(rgbHex: 0xFFFFFF) // Colors.text
2018-07-09 15:58:02 +02:00
}
2018-06-22 19:48:23 +02:00
}