Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added current world time label to open info screen #948

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion submodules/PeerInfoUI/Sources/ItemListCallListItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion submodules/Postbox/Sources/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<PeerId, Peer>, associatedMessages: SimpleDictionary<MessageId, Message>, 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<PeerId, Peer>, associatedMessages: SimpleDictionary<MessageId, Message>, associatedMessageIds: [MessageId], associatedMedia: [MediaId: Media], associatedThreadInfo: AssociatedThreadInfo?, currentWorldTimestamp: Int32 = 0) {
self.stableId = stableId
self.stableVersion = stableVersion
self.id = id
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,24 +222,30 @@ public extension TelegramEngine {
}

public func callList(scope: EngineCallList.Scope, index: EngineMessage.Index, itemCount: Int) -> Signal<EngineCallList, NoError> {
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
)
}
}
}

Expand Down Expand Up @@ -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<Int32, NoError> {
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()
}
}
}
}
}
9 changes: 9 additions & 0 deletions submodules/TelegramStringFormatting/Sources/DateFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down