Introduce sending and sent state for timeline messages (#319)

* Updated md files

* Changelog

* Update CONTRIBUTING.md

Co-authored-by: Stefan Ceriu <stefanc@matrix.org>

* PR fixes

* Sending state in the timeline

* Changelog

* Formating

* Project file

* Make MessageTimelineItemDeliveryStatus Equatable

* Renamed seconds ago to elepsed time

* Moved show/hide logic into TimelineDeliveryStatusView

* Hide view when unknown status

* Added delivery status for other items

* Cleanup

Co-authored-by: Stefan Ceriu <stefanc@matrix.org>
This commit is contained in:
Aleksandrs Proskurins
2022-11-17 17:47:28 +02:00
committed by GitHub
parent 276daf0355
commit 77a0d5a58f
7 changed files with 109 additions and 9 deletions
@@ -37,6 +37,10 @@ struct TimelineItemBubbledStylerView<Content: View>: View {
HStack {
Spacer()
styledContentWithReactions
if timelineItem.isOutgoing {
TimelineDeliveryStatusView(deliveryStatus: timelineItem.properties.deliveryStatus)
.padding(.top, 6)
}
}
.padding(.trailing, 16)
.padding(.leading, 16)
@@ -0,0 +1,67 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
struct TimelineDeliveryStatusView: View {
let deliveryStatus: MessageTimelineItemDeliveryStatus
@State var showDeliveryStatus: Bool
private var systemImageName: String {
switch deliveryStatus {
case .sending, .unknown:
return "circle"
case .sent:
return "checkmark.circle"
}
}
init(deliveryStatus: MessageTimelineItemDeliveryStatus) {
self.deliveryStatus = deliveryStatus
switch deliveryStatus {
case .sending:
showDeliveryStatus = true
case let .sent(elapsedTime: elapsedTime):
showDeliveryStatus = elapsedTime < 3
case .unknown:
showDeliveryStatus = false
}
}
var body: some View {
if showDeliveryStatus {
Image(systemName: systemImageName)
.task {
if case .sent = deliveryStatus {
try? await Task.sleep(nanoseconds: 1_000_000_000)
withAnimation {
showDeliveryStatus = false
}
}
}
}
}
}
struct TimelineDeliveryStatusView_Previews: PreviewProvider {
static var previews: some View {
VStack {
TimelineDeliveryStatusView(deliveryStatus: .sending)
TimelineDeliveryStatusView(deliveryStatus: .sent(elapsedTime: Date().timeIntervalSince1970))
}
}
}
@@ -23,6 +23,13 @@ protocol MessageContentProtocol: RoomMessageEventContentProtocol {
var body: String { get }
}
/// The delivery status for the item.
enum MessageTimelineItemDeliveryStatus: Equatable {
case unknown
case sending
case sent(elapsedTime: TimeInterval)
}
/// A timeline item that represents an `m.room.message` event.
struct MessageTimelineItem<Content: MessageContentProtocol> {
let item: MatrixRustSDK.EventTimelineItem
@@ -37,6 +44,15 @@ struct MessageTimelineItem<Content: MessageContentProtocol> {
return eventID
}
}
var deliveryStatus: MessageTimelineItemDeliveryStatus {
switch item.key() {
case .transactionId:
return .sending
case .eventId:
return .sent(elapsedTime: Date().timeIntervalSince1970 - originServerTs.timeIntervalSince1970)
}
}
var body: String {
content.body
@@ -22,4 +22,6 @@ struct RoomTimelineItemProperties: Equatable {
var isEdited = false
/// The aggregated reactions that have been sent for this item.
var reactions: [AggregatedReaction] = []
/// The delivery status for this item.
var deliveryStatus: MessageTimelineItemDeliveryStatus = .unknown
}
@@ -166,7 +166,8 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
senderDisplayName: displayName,
senderAvatar: avatarImage,
properties: RoomTimelineItemProperties(isEdited: message.isEdited,
reactions: aggregateReactions(message.reactions)))
reactions: aggregateReactions(message.reactions),
deliveryStatus: message.deliveryStatus))
}
private func buildImageTimelineItemFromMessage(_ message: MessageTimelineItem<ImageMessageContent>,
@@ -196,7 +197,8 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
aspectRatio: aspectRatio,
blurhash: message.blurhash,
properties: RoomTimelineItemProperties(isEdited: message.isEdited,
reactions: aggregateReactions(message.reactions)))
reactions: aggregateReactions(message.reactions),
deliveryStatus: message.deliveryStatus))
}
private func buildVideoTimelineItemFromMessage(_ message: MessageTimelineItem<VideoMessageContent>,
@@ -228,7 +230,8 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
aspectRatio: aspectRatio,
blurhash: message.blurhash,
properties: RoomTimelineItemProperties(isEdited: message.isEdited,
reactions: aggregateReactions(message.reactions)))
reactions: aggregateReactions(message.reactions),
deliveryStatus: message.deliveryStatus))
}
private func buildFileTimelineItemFromMessage(_ message: MessageTimelineItem<FileMessageContent>,
@@ -248,7 +251,8 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
source: message.source,
thumbnailSource: message.thumbnailSource,
properties: RoomTimelineItemProperties(isEdited: message.isEdited,
reactions: aggregateReactions(message.reactions)))
reactions: aggregateReactions(message.reactions),
deliveryStatus: message.deliveryStatus))
}
private func buildNoticeTimelineItemFromMessage(_ message: MessageTimelineItem<NoticeMessageContent>,
@@ -270,7 +274,8 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
senderDisplayName: displayName,
senderAvatar: avatarImage,
properties: RoomTimelineItemProperties(isEdited: message.isEdited,
reactions: aggregateReactions(message.reactions)))
reactions: aggregateReactions(message.reactions),
deliveryStatus: message.deliveryStatus))
}
private func buildEmoteTimelineItemFromMessage(_ message: MessageTimelineItem<EmoteMessageContent>,
@@ -292,7 +297,8 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
senderDisplayName: displayName,
senderAvatar: avatarImage,
properties: RoomTimelineItemProperties(isEdited: message.isEdited,
reactions: aggregateReactions(message.reactions)))
reactions: aggregateReactions(message.reactions),
deliveryStatus: message.deliveryStatus))
}
private func aggregateReactions(_ reactions: [Reaction]) -> [AggregatedReaction] {