session-ios/SignalMessaging/utils/ConversationStyle.swift

186 lines
5.7 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
private let isRTL: Bool
// The width of the collection view.
@objc public var viewWidth: CGFloat = 0 {
didSet {
SwiftAssertIsOnMainThread(#function)
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
// These are the gutters used by "full width" views
// like "date headers" and "unread indicator".
@objc public var fullWidthGutterLeading: CGFloat = 0
@objc public var fullWidthGutterTrailing: CGFloat = 0
// viewWidth - (gutterLeading + gutterTrailing)
@objc public var contentWidth: CGFloat = 0
// viewWidth - (gutterfullWidthGutterLeadingLeading + fullWidthGutterTrailing)
@objc public var fullWidthContentWidth: CGFloat = 0
@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
self.isRTL = CurrentAppContext().isRTL
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() {
SwiftAssertIsOnMainThread(#function)
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-06-22 22:56:33 +02:00
gutterLeading = 40
2018-06-22 19:48:23 +02:00
gutterTrailing = 20
} else {
2018-06-22 22:56:33 +02:00
gutterLeading = 16
2018-06-22 19:48:23 +02:00
gutterTrailing = 20
}
2018-06-25 21:25:12 +02:00
fullWidthGutterLeading = gutterLeading
fullWidthGutterTrailing = gutterTrailing
2018-06-22 19:48:23 +02:00
contentWidth = viewWidth - (gutterLeading + gutterTrailing)
fullWidthContentWidth = viewWidth - (fullWidthGutterLeading + fullWidthGutterTrailing)
2018-06-25 21:25:12 +02:00
maxMessageWidth = floor(contentWidth - 48)
let messageTextFont = UIFont.ows_dynamicTypeBody
// Don't include the distance from the "cap height" to the top of the UILabel
// in the top margin.
2018-06-26 00:06:08 +02:00
textInsetTop = max(0, 12 - (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-06-26 00:06:08 +02:00
textInsetBottom = max(0, 12 - abs(messageTextFont.descender))
textInsetHorizontal = 12
2018-06-25 20:31:09 +02:00
lastTextLineAxis = CGFloat(round(12 + 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
}
private static let defaultBubbleColorIncoming = UIColor.ows_messageBubbleLightGray
2018-06-28 19:26:17 +02:00
// TODO:
@objc
2018-06-28 19:28:14 +02:00
public let bubbleColorOutgoingUnsent = UIColor.ows_red
2018-06-28 19:26:17 +02:00
// TODO:
@objc
2018-06-28 19:28:14 +02:00
public let bubbleColorOutgoingSending = UIColor.ows_light35
@objc
public let bubbleColorOutgoingSent = UIColor.ows_light10
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-06-28 19:28:14 +02:00
return primaryColor
2018-06-28 19:26:17 +02:00
} else if let outgoingMessage = message as? TSOutgoingMessage {
switch outgoingMessage.messageState {
case .failed:
2018-06-28 19:28:14 +02:00
return self.bubbleColorOutgoingUnsent
2018-06-28 19:26:17 +02:00
case .sending:
2018-06-28 19:28:14 +02:00
return self.bubbleColorOutgoingSending
2018-06-28 19:26:17 +02:00
default:
2018-06-28 19:28:14 +02:00
return self.bubbleColorOutgoingSent
2018-06-28 19:26:17 +02:00
}
} else {
owsFail("Unexpected message type: \(message)")
return UIColor.ows_materialBlue
}
}
2018-07-02 15:42:48 +02:00
@objc
public static var bubbleTextColorIncoming = UIColor.ows_white
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-06-28 19:26:17 +02:00
} else if let outgoingMessage = message as? TSOutgoingMessage {
switch outgoingMessage.messageState {
case .failed:
return UIColor.ows_black
case .sending:
return UIColor.ows_black
default:
return UIColor.ows_black
}
} else {
owsFail("Unexpected message type: \(message)")
return UIColor.ows_materialBlue
}
}
2018-06-22 19:48:23 +02:00
}