Refactoring

This commit is contained in:
Ali
2021-09-20 23:07:38 +03:00
parent 6e5c503338
commit b778c66226
91 changed files with 290 additions and 3473 deletions

View File

@@ -1,6 +1,5 @@
import Foundation
import UIKit
import Postbox
import TelegramCore
import TelegramPresentationData
import TelegramUIPreferences
@@ -12,11 +11,11 @@ import Markdown
private let titleFont = Font.regular(13.0)
private let titleBoldFont = Font.bold(13.0)
private func peerMentionAttributes(primaryTextColor: UIColor, peerId: PeerId) -> MarkdownAttributeSet {
private func peerMentionAttributes(primaryTextColor: UIColor, peerId: EnginePeer.Id) -> MarkdownAttributeSet {
return MarkdownAttributeSet(font: titleBoldFont, textColor: primaryTextColor, additionalAttributes: [TelegramTextAttributes.PeerMention: TelegramPeerMention(peerId: peerId, mention: "")])
}
private func peerMentionsAttributes(primaryTextColor: UIColor, peerIds: [(Int, PeerId?)]) -> [Int: MarkdownAttributeSet] {
private func peerMentionsAttributes(primaryTextColor: UIColor, peerIds: [(Int, EnginePeer.Id?)]) -> [Int: MarkdownAttributeSet] {
var result: [Int: MarkdownAttributeSet] = [:]
for (index, peerId) in peerIds {
if let peerId = peerId {
@@ -26,11 +25,11 @@ private func peerMentionsAttributes(primaryTextColor: UIColor, peerIds: [(Int, P
return result
}
public func plainServiceMessageString(strings: PresentationStrings, nameDisplayOrder: PresentationPersonNameOrder, dateTimeFormat: PresentationDateTimeFormat, message: Message, accountPeerId: PeerId, forChatList: Bool) -> String? {
public func plainServiceMessageString(strings: PresentationStrings, nameDisplayOrder: PresentationPersonNameOrder, dateTimeFormat: PresentationDateTimeFormat, message: EngineMessage, accountPeerId: EnginePeer.Id, forChatList: Bool) -> String? {
return universalServiceMessageString(presentationData: nil, strings: strings, nameDisplayOrder: nameDisplayOrder, dateTimeFormat: dateTimeFormat, message: message, accountPeerId: accountPeerId, forChatList: forChatList)?.string
}
public func universalServiceMessageString(presentationData: (PresentationTheme, TelegramWallpaper)?, strings: PresentationStrings, nameDisplayOrder: PresentationPersonNameOrder, dateTimeFormat: PresentationDateTimeFormat, message: Message, accountPeerId: PeerId, forChatList: Bool) -> NSAttributedString? {
public func universalServiceMessageString(presentationData: (PresentationTheme, TelegramWallpaper)?, strings: PresentationStrings, nameDisplayOrder: PresentationPersonNameOrder, dateTimeFormat: PresentationDateTimeFormat, message: EngineMessage, accountPeerId: EnginePeer.Id, forChatList: Bool) -> NSAttributedString? {
var attributedString: NSAttributedString?
let primaryTextColor: UIColor
@@ -45,7 +44,7 @@ public func universalServiceMessageString(presentationData: (PresentationTheme,
for media in message.media {
if let action = media as? TelegramMediaAction {
let authorName = message.author.flatMap(EnginePeer.init)?.displayTitle(strings: strings, displayOrder: nameDisplayOrder) ?? ""
let authorName = message.author?.displayTitle(strings: strings, displayOrder: nameDisplayOrder) ?? ""
var isChannel = false
if message.id.peerId.namespace == Namespaces.Peer.CloudChannel, let peer = message.peers[message.id.peerId] as? TelegramChannel, case .broadcast = peer.info {
@@ -71,7 +70,7 @@ public func universalServiceMessageString(presentationData: (PresentationTheme,
attributedString = addAttributesToStringWithRanges(strings.Notification_JoinedChat(authorName)._tuple, body: bodyAttributes, argumentAttributes: peerMentionsAttributes(primaryTextColor: primaryTextColor, peerIds: [(0, peerId)]))
}
} else {
var attributePeerIds: [(Int, PeerId?)] = [(0, message.author?.id)]
var attributePeerIds: [(Int, EnginePeer.Id?)] = [(0, message.author?.id)]
let resultTitleString: PresentationStrings.FormattedString
if peerIds.count == 1 {
attributePeerIds.append((1, peerIds.first))
@@ -90,7 +89,7 @@ public func universalServiceMessageString(presentationData: (PresentationTheme,
attributedString = addAttributesToStringWithRanges(strings.Notification_LeftChat(authorName)._tuple, body: bodyAttributes, argumentAttributes: peerMentionsAttributes(primaryTextColor: primaryTextColor, peerIds: [(0, message.author?.id)]))
}
} else {
var attributePeerIds: [(Int, PeerId?)] = [(0, message.author?.id)]
var attributePeerIds: [(Int, EnginePeer.Id?)] = [(0, message.author?.id)]
if peerIds.count == 1 {
attributePeerIds.append((1, peerIds.first))
}
@@ -153,10 +152,10 @@ public func universalServiceMessageString(presentationData: (PresentationTheme,
case deleted
}
var pinnedMessage: Message?
var pinnedMessage: EngineMessage?
for attribute in message.attributes {
if let attribute = attribute as? ReplyMessageAttribute, let message = message.associatedMessages[attribute.messageId] {
pinnedMessage = message
pinnedMessage = EngineMessage(message)
}
}
@@ -263,7 +262,7 @@ public func universalServiceMessageString(presentationData: (PresentationTheme,
case let .messageAutoremoveTimeoutUpdated(timeout):
let authorString: String
if let author = messageMainPeer(message) {
authorString = EnginePeer(author).compactDisplayTitle
authorString = author.compactDisplayTitle
} else {
authorString = ""
}
@@ -325,8 +324,8 @@ public func universalServiceMessageString(presentationData: (PresentationTheme,
break
case .historyScreenshot:
let text: String
if message.effectivelyIncoming(accountPeerId) {
text = strings.Notification_SecretChatMessageScreenshot(message.author.flatMap(EnginePeer.init)?.compactDisplayTitle ?? "").string
if message._asMessage().effectivelyIncoming(accountPeerId) {
text = strings.Notification_SecretChatMessageScreenshot(message.author?.compactDisplayTitle ?? "").string
} else {
text = strings.Notification_SecretChatMessageScreenshotSelf
}
@@ -372,10 +371,10 @@ public func universalServiceMessageString(presentationData: (PresentationTheme,
argumentAttributes[1] = MarkdownAttributeSet(font: titleBoldFont, textColor: primaryTextColor, additionalAttributes: [:])
attributedString = addAttributesToStringWithRanges(formatWithArgumentRanges(baseString, ranges, [authorName, gameTitle ?? ""]), body: bodyAttributes, argumentAttributes: argumentAttributes)
case let .paymentSent(currency, totalAmount):
var invoiceMessage: Message?
var invoiceMessage: EngineMessage?
for attribute in message.attributes {
if let attribute = attribute as? ReplyMessageAttribute, let message = message.associatedMessages[attribute.messageId] {
invoiceMessage = message
invoiceMessage = EngineMessage(message)
}
}
@@ -391,7 +390,7 @@ public func universalServiceMessageString(presentationData: (PresentationTheme,
if let invoiceTitle = invoiceTitle {
let botString: String
if let peer = messageMainPeer(message) {
botString = EnginePeer(peer).compactDisplayTitle
botString = peer.compactDisplayTitle
} else {
botString = ""
}
@@ -441,7 +440,7 @@ public func universalServiceMessageString(presentationData: (PresentationTheme,
if let scheduleDate = scheduleDate {
if message.author?.id.namespace == Namespaces.Peer.CloudChannel {
let titleString: PresentationStrings.FormattedString
if let channel = message.author as? TelegramChannel, case .broadcast = channel.info {
if case let .channel(channel) = message.author, case .broadcast = channel.info {
titleString = humanReadableStringForTimestamp(strings: strings, dateTimeFormat: dateTimeFormat, timestamp: scheduleDate, alwaysShowTime: true, allowYesterday: false, format: HumanReadableStringFormat(dateFormatString: { strings.Notification_LiveStreamScheduled($0) }, tomorrowFormatString: { strings.Notification_LiveStreamScheduledTomorrow($0) }, todayFormatString: { strings.Notification_LiveStreamScheduledToday($0) }))
} else {
titleString = humanReadableStringForTimestamp(strings: strings, dateTimeFormat: dateTimeFormat, timestamp: scheduleDate, alwaysShowTime: true, allowYesterday: false, format: HumanReadableStringFormat(dateFormatString: { strings.Notification_VoiceChatScheduledChannel($0) }, tomorrowFormatString: { strings.Notification_VoiceChatScheduledTomorrowChannel($0) }, todayFormatString: { strings.Notification_VoiceChatScheduledTodayChannel($0) }))
@@ -449,34 +448,34 @@ public func universalServiceMessageString(presentationData: (PresentationTheme,
attributedString = NSAttributedString(string: titleString.string, font: titleFont, textColor: primaryTextColor)
} else {
let titleString = humanReadableStringForTimestamp(strings: strings, dateTimeFormat: dateTimeFormat, timestamp: scheduleDate, alwaysShowTime: true, allowYesterday: false, format: HumanReadableStringFormat(dateFormatString: { strings.Notification_VoiceChatScheduled(authorName, $0) }, tomorrowFormatString: { strings.Notification_VoiceChatScheduledTomorrow(authorName, $0) }, todayFormatString: { strings.Notification_VoiceChatScheduledToday(authorName, $0) }))
let attributePeerIds: [(Int, PeerId?)] = [(0, message.author?.id)]
let attributePeerIds: [(Int, EnginePeer.Id?)] = [(0, message.author?.id)]
attributedString = addAttributesToStringWithRanges(titleString._tuple, body: bodyAttributes, argumentAttributes: peerMentionsAttributes(primaryTextColor: primaryTextColor, peerIds: attributePeerIds))
}
} else if let duration = duration {
if message.author?.id.namespace == Namespaces.Peer.CloudChannel {
let titleString: String
if let channel = message.author as? TelegramChannel, case .broadcast = channel.info {
if case let .channel(channel) = message.author, case .broadcast = channel.info {
titleString = strings.Notification_LiveStreamEnded(callDurationString(strings: strings, value: duration)).string
} else {
titleString = strings.Notification_VoiceChatEnded(callDurationString(strings: strings, value: duration)).string
}
attributedString = NSAttributedString(string: titleString, font: titleFont, textColor: primaryTextColor)
} else {
let attributePeerIds: [(Int, PeerId?)] = [(0, message.author?.id)]
let attributePeerIds: [(Int, EnginePeer.Id?)] = [(0, message.author?.id)]
let titleString = strings.Notification_VoiceChatEndedGroup(authorName, callDurationString(strings: strings, value: duration))
attributedString = addAttributesToStringWithRanges(titleString._tuple, body: bodyAttributes, argumentAttributes: peerMentionsAttributes(primaryTextColor: primaryTextColor, peerIds: attributePeerIds))
}
} else {
if message.author?.id.namespace == Namespaces.Peer.CloudChannel {
let titleString: String
if let channel = message.author as? TelegramChannel, case .broadcast = channel.info {
if case let .channel(channel) = message.author, case .broadcast = channel.info {
titleString = strings.Notification_LiveStreamStarted
} else {
titleString = strings.Notification_VoiceChatStartedChannel
}
attributedString = NSAttributedString(string: titleString, font: titleFont, textColor: primaryTextColor)
} else {
let attributePeerIds: [(Int, PeerId?)] = [(0, message.author?.id)]
let attributePeerIds: [(Int, EnginePeer.Id?)] = [(0, message.author?.id)]
let titleString = strings.Notification_VoiceChatStarted(authorName)
attributedString = addAttributesToStringWithRanges(titleString._tuple, body: bodyAttributes, argumentAttributes: peerMentionsAttributes(primaryTextColor: primaryTextColor, peerIds: attributePeerIds))
}
@@ -529,7 +528,7 @@ public func universalServiceMessageString(presentationData: (PresentationTheme,
attributedString = addAttributesToStringWithRanges(strings.Notification_ProximityReached(message.peers[fromId].flatMap(EnginePeer.init)?.displayTitle(strings: strings, displayOrder: nameDisplayOrder) ?? "", distanceString, message.peers[toId].flatMap(EnginePeer.init)?.displayTitle(strings: strings, displayOrder: nameDisplayOrder) ?? "")._tuple, body: bodyAttributes, argumentAttributes: peerMentionsAttributes(primaryTextColor: primaryTextColor, peerIds: [(0, fromId), (2, toId)]))
}
case let .inviteToGroupPhoneCall(_, _, peerIds):
var attributePeerIds: [(Int, PeerId?)] = [(0, message.author?.id)]
var attributePeerIds: [(Int, EnginePeer.Id?)] = [(0, message.author?.id)]
let resultTitleString: PresentationStrings.FormattedString
if peerIds.count == 1 {
if peerIds[0] == accountPeerId {
@@ -551,7 +550,7 @@ public func universalServiceMessageString(presentationData: (PresentationTheme,
} else if message.author?.id == accountPeerId {
attributedString = NSAttributedString(string: strings.Notification_YouDisabledTheme, font: titleFont, textColor: primaryTextColor)
} else {
let attributePeerIds: [(Int, PeerId?)] = [(0, message.author?.id)]
let attributePeerIds: [(Int, EnginePeer.Id?)] = [(0, message.author?.id)]
let resultTitleString = strings.Notification_DisabledTheme(authorName)
attributedString = addAttributesToStringWithRanges(resultTitleString._tuple, body: bodyAttributes, argumentAttributes: peerMentionsAttributes(primaryTextColor: primaryTextColor, peerIds: attributePeerIds))
}