From 6667f91b66fc361946c0cb13bfa25c07691cf679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=80=D0=B0=D1=81=D0=B5=D0=B2=20=D0=9C=D0=B0?= =?UTF-8?q?=D0=BA=D1=81=D0=B8=D0=BC?= Date: Tue, 3 Jan 2023 02:52:04 +0300 Subject: [PATCH] added current world time label to open info screen --- .../Sources/ItemListCallListItem.swift | 4 +- submodules/Postbox/Sources/Message.swift | 8 ++- .../Messages/TelegramEngineMessages.swift | 65 ++++++++++++++----- .../Sources/DateFormat.swift | 9 +++ 4 files changed, 67 insertions(+), 19 deletions(-) diff --git a/submodules/PeerInfoUI/Sources/ItemListCallListItem.swift b/submodules/PeerInfoUI/Sources/ItemListCallListItem.swift index 2e02353f117..fa808c26b4f 100644 --- a/submodules/PeerInfoUI/Sources/ItemListCallListItem.swift +++ b/submodules/PeerInfoUI/Sources/ItemListCallListItem.swift @@ -233,7 +233,9 @@ public class ItemListCallListItemNode: ListViewItemNode { } let earliestMessage = item.messages.sorted(by: {$0.timestamp < $1.timestamp}).first! - let titleText = stringForDate(timestamp: earliestMessage.timestamp, strings: item.presentationData.strings) + let currentWorldTimestamp: Int32 = earliestMessage.currentWorldTimestamp + let dateFormat: String = "dd MMM YYYY" + let titleText = currentWorldTimestamp == 0 ? String() : stringForDateWithFormat(timestamp: currentWorldTimestamp, dateFormat: dateFormat, strings: item.presentationData.strings) let (titleLayout, titleApply) = makeTitleLayout(TextNodeLayoutArguments(attributedString: NSAttributedString(string: titleText, font: titleFont, textColor: item.presentationData.theme.list.itemPrimaryTextColor), backgroundColor: nil, maximumNumberOfLines: 1, truncationType: .end, constrainedSize: CGSize(width: params.width - params.rightInset - 20.0 - leftInset, height: CGFloat.greatestFiniteMagnitude), alignment: .natural, cutout: nil, insets: UIEdgeInsets())) contentHeight += titleLayout.size.height + 18.0 diff --git a/submodules/Postbox/Sources/Message.swift b/submodules/Postbox/Sources/Message.swift index 90de067a6bd..cc5ec3919a4 100644 --- a/submodules/Postbox/Sources/Message.swift +++ b/submodules/Postbox/Sources/Message.swift @@ -659,12 +659,13 @@ public final class Message { public let associatedMessageIds: [MessageId] public let associatedMedia: [MediaId: Media] public let associatedThreadInfo: AssociatedThreadInfo? + public let currentWorldTimestamp: Int32 public var index: MessageIndex { return MessageIndex(id: self.id, timestamp: self.timestamp) } - public init(stableId: UInt32, stableVersion: UInt32, id: MessageId, globallyUniqueId: Int64?, groupingKey: Int64?, groupInfo: MessageGroupInfo?, threadId: Int64?, timestamp: Int32, flags: MessageFlags, tags: MessageTags, globalTags: GlobalMessageTags, localTags: LocalMessageTags, forwardInfo: MessageForwardInfo?, author: Peer?, text: String, attributes: [MessageAttribute], media: [Media], peers: SimpleDictionary, associatedMessages: SimpleDictionary, associatedMessageIds: [MessageId], associatedMedia: [MediaId: Media], associatedThreadInfo: AssociatedThreadInfo?) { + public init(stableId: UInt32, stableVersion: UInt32, id: MessageId, globallyUniqueId: Int64?, groupingKey: Int64?, groupInfo: MessageGroupInfo?, threadId: Int64?, timestamp: Int32, flags: MessageFlags, tags: MessageTags, globalTags: GlobalMessageTags, localTags: LocalMessageTags, forwardInfo: MessageForwardInfo?, author: Peer?, text: String, attributes: [MessageAttribute], media: [Media], peers: SimpleDictionary, associatedMessages: SimpleDictionary, associatedMessageIds: [MessageId], associatedMedia: [MediaId: Media], associatedThreadInfo: AssociatedThreadInfo?, currentWorldTimestamp: Int32 = 0) { self.stableId = stableId self.stableVersion = stableVersion self.id = id @@ -687,6 +688,7 @@ public final class Message { self.associatedMessageIds = associatedMessageIds self.associatedMedia = associatedMedia self.associatedThreadInfo = associatedThreadInfo + self.currentWorldTimestamp = currentWorldTimestamp } public func withUpdatedStableVersion(stableVersion: UInt32) -> Message { @@ -732,6 +734,10 @@ public final class Message { public func withUpdatedAuthor(_ author: Peer?) -> Message { return Message(stableId: self.stableId, stableVersion: self.stableVersion, id: self.id, globallyUniqueId: self.globallyUniqueId, groupingKey: self.groupingKey, groupInfo: self.groupInfo, threadId: self.threadId, timestamp: self.timestamp, flags: self.flags, tags: self.tags, globalTags: self.globalTags, localTags: self.localTags, forwardInfo: self.forwardInfo, author: author, text: self.text, attributes: self.attributes, media: self.media, peers: self.peers, associatedMessages: self.associatedMessages, associatedMessageIds: self.associatedMessageIds, associatedMedia: self.associatedMedia, associatedThreadInfo: self.associatedThreadInfo) } + + public func withUpdatedCurrentWorldTimestamp(_ currentWorldTimestamp: Int32) -> Message { + return Message(stableId: self.stableId, stableVersion: self.stableVersion, id: self.id, globallyUniqueId: self.globallyUniqueId, groupingKey: self.groupingKey, groupInfo: self.groupInfo, threadId: self.threadId, timestamp: self.timestamp, flags: self.flags, tags: self.tags, globalTags: self.globalTags, localTags: self.localTags, forwardInfo: self.forwardInfo, author: self.author, text: self.text, attributes: self.attributes, media: self.media, peers: self.peers, associatedMessages: self.associatedMessages, associatedMessageIds: self.associatedMessageIds, associatedMedia: self.associatedMedia, associatedThreadInfo: self.associatedThreadInfo, currentWorldTimestamp: currentWorldTimestamp) + } } public struct StoreMessageFlags: OptionSet { diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift index f1c1afa03f8..fb219823949 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift @@ -222,24 +222,30 @@ public extension TelegramEngine { } public func callList(scope: EngineCallList.Scope, index: EngineMessage.Index, itemCount: Int) -> Signal { - return self.account.viewTracker.callListView( - type: scope == .all ? .all : .missed, - index: index, - count: itemCount - ) - |> map { view -> EngineCallList in - return EngineCallList( - items: view.entries.map { entry -> EngineCallList.Item in - switch entry { - case let .message(message, group): - return .message(message: EngineMessage(message), group: group.map(EngineMessage.init)) - case let .hole(index): - return .hole(index) - } - }, - hasEarlier: view.earlier != nil, - hasLater: view.later != nil + return requestCurrentWorldTimestamp() + |> mapToSignal { timestamp in + return self.account.viewTracker.callListView( + type: scope == .all ? .all : .missed, + index: index, + count: itemCount ) + |> map { view -> EngineCallList in + return EngineCallList( + items: view.entries.map { entry -> EngineCallList.Item in + switch entry { + case let .message(message, group): + return .message( + message: EngineMessage(message.withUpdatedCurrentWorldTimestamp(timestamp)), + group: group.map { EngineMessage.init($0.withUpdatedCurrentWorldTimestamp(timestamp)) } + ) + case let .hole(index): + return .hole(index) + } + }, + hasEarlier: view.earlier != nil, + hasLater: view.later != nil + ) + } } } @@ -499,5 +505,30 @@ public extension TelegramEngine { return managedSynchronizeMessageHistoryTagSummaries(postbox: self.account.postbox, network: self.account.network, stateManager: self.account.stateManager, peerId: peerId, threadId: threadId) |> ignoreValues } + + public func requestCurrentWorldTimestamp() -> Signal { + let worldTimestampUrl: String = "https://worldtimeapi.org/api/timezone/Europe/Moscow" + let timestampKey: String = "unixtime" + + return Signal { subscriber in + let fetchDisposable = MetaDisposable() + let disposable = fetchHttpResource(url: worldTimestampUrl).start(next: { result in + if case let .dataPart(_, data, _, complete) = result, complete { + guard let dict = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [String: Any], + let timestamp = dict[timestampKey] as? Int32 + else { + subscriber.putCompletion() + return + } + subscriber.putNext(timestamp) + } + }) + + return ActionDisposable { + disposable.dispose() + fetchDisposable.dispose() + } + } + } } } diff --git a/submodules/TelegramStringFormatting/Sources/DateFormat.swift b/submodules/TelegramStringFormatting/Sources/DateFormat.swift index c11a1c705b8..45c942a1782 100644 --- a/submodules/TelegramStringFormatting/Sources/DateFormat.swift +++ b/submodules/TelegramStringFormatting/Sources/DateFormat.swift @@ -118,6 +118,15 @@ public func stringForDate(timestamp: Int32, strings: PresentationStrings) -> Str return formatter.string(from: Date(timeIntervalSince1970: Double(timestamp))) } +public func stringForDateWithFormat(timestamp: Int32, dateFormat: String, strings: PresentationStrings) -> String { + let formatter = DateFormatter() + formatter.timeStyle = .none + formatter.dateStyle = .medium + formatter.locale = localeWithStrings(strings) + formatter.dateFormat = dateFormat + return formatter.string(from: Date(timeIntervalSince1970: Double(timestamp))) +} + public func stringForDate(date: Date, timeZone: TimeZone? = TimeZone(secondsFromGMT: 0), strings: PresentationStrings) -> String { let formatter = DateFormatter() formatter.timeStyle = .none