From ec40c2d7e5d93acb14c73773d6eea8c1ca295fec Mon Sep 17 00:00:00 2001 From: Levitating Pineapple Date: Fri, 23 Aug 2024 11:40:11 +0300 Subject: [PATCH] add date logic --- apps/ios/Shared/Views/Chat/ChatView.swift | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/apps/ios/Shared/Views/Chat/ChatView.swift b/apps/ios/Shared/Views/Chat/ChatView.swift index 1c307f93b6..3d228c19e6 100644 --- a/apps/ios/Shared/Views/Chat/ChatView.swift +++ b/apps/ios/Shared/Views/Chat/ChatView.swift @@ -723,6 +723,7 @@ struct ChatView: View { struct TimeSeparation { let isTimestampShown: Bool + let isDateShown: Bool // TODO: Implement UI to show date let isGapLarge: Bool init(for chatItem: ChatItem, at index: Int?) { @@ -731,9 +732,11 @@ struct ChatView: View { let nextItem = im.reversedChatItems[index - 1] isTimestampShown = nextItem.chatDir != chatItem.chatDir || Self.componentsToMinute(nextItem.meta.createdAt) != Self.componentsToMinute(chatItem.meta.createdAt) + isDateShown = Self.componentsToDate(nextItem.meta.createdAt) != Self.componentsToDate(chatItem.meta.createdAt) isGapLarge = nextItem.meta.createdAt.timeIntervalSince(chatItem.meta.createdAt) > 30 } else { isTimestampShown = true + isDateShown = false isGapLarge = true } } @@ -744,6 +747,13 @@ struct ChatView: View { from: date ) } + + private static func componentsToDate(_ date: Date) -> DateComponents { + Calendar.current.dateComponents( + [.year, .month, .day], + from: date + ) + } } var body: some View { @@ -867,7 +877,7 @@ struct ChatView: View { } } } - chatItemWithMenu(ci, range, maxWidth, timeSeparation.isTimestampShown) + chatItemWithMenu(ci, range, maxWidth, timeSeparation) } } } @@ -880,7 +890,7 @@ struct ChatView: View { SelectedChatItem(ciId: ci.id, selectedChatItems: $selectedChatItems) .padding(.leading, 12) } - chatItemWithMenu(ci, range, maxWidth, timeSeparation.isTimestampShown) + chatItemWithMenu(ci, range, maxWidth, timeSeparation) .padding(.trailing) .padding(.leading, memberImageSize + 8 + 12) } @@ -897,7 +907,7 @@ struct ChatView: View { .padding(.leading) } } - chatItemWithMenu(ci, range, maxWidth, timeSeparation.isTimestampShown) + chatItemWithMenu(ci, range, maxWidth, timeSeparation) .padding(.horizontal) } .padding(.bottom, bottomPadding) @@ -915,7 +925,7 @@ struct ChatView: View { } } - @ViewBuilder func chatItemWithMenu(_ ci: ChatItem, _ range: ClosedRange?, _ maxWidth: CGFloat, _ isTimeShown: Bool) -> some View { + @ViewBuilder func chatItemWithMenu(_ ci: ChatItem, _ range: ClosedRange?, _ maxWidth: CGFloat, _ timeSeparation: TimeSeparation) -> some View { let alignment: Alignment = ci.chatDir.sent ? .trailing : .leading VStack(alignment: alignment.horizontal, spacing: 3) { ChatItemView( @@ -925,8 +935,8 @@ struct ChatView: View { revealed: .constant(revealed), allowMenu: $allowMenu ) - .environment(\.showTimestamp, isTimeShown) - .modifier(ChatItemClipped(ci, showsTail: isTimeShown)) + .environment(\.showTimestamp, timeSeparation.isTimestampShown) + .modifier(ChatItemClipped(ci, showsTail: timeSeparation.isGapLarge)) .contextMenu { menu(ci, range, live: composeState.liveMessage != nil) } .accessibilityLabel("") if ci.content.msgContent != nil && (ci.meta.itemDeleted == nil || revealed) && ci.reactions.count > 0 {