Refactor theTimelineItemIdentifier handling; stop relying on optional EventOrTransactionIds and be explicit when setting composer modes from the draft service.

This commit is contained in:
Stefan Ceriu
2024-10-16 14:07:54 +03:00
parent 3d797ccabb
commit 35d49c4d85
56 changed files with 313 additions and 298 deletions
@@ -0,0 +1,66 @@
//
// Copyright 2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
//
import Foundation
import MatrixRustSDK
/// A timeline item identifier
/// - uniqueID: Stable id across state changes of the timeline item, it uniquely identifies an item in a timeline.
/// Its value is consistent only per timeline instance, it should **not** be used to identify an item across timeline instances.
/// - eventOrTransactionID: Contains the 2 possible identifiers of an event, either it has a remote event id or
/// a local transaction id, never both or none.
enum TimelineItemIdentifier: Hashable {
case event(uniqueID: String, eventOrTransactionID: EventOrTransactionId)
case virtual(uniqueID: String)
var uniqueID: String {
switch self {
case .event(let uniqueID, _):
return uniqueID
case .virtual(let uniqueID):
return uniqueID
}
}
var eventID: String? {
guard case let .event(_, eventOrTransactionID) = self else {
return nil
}
switch eventOrTransactionID {
case .eventId(let eventID):
return eventID
default:
return nil
}
}
var transactionID: String? {
guard case let .event(_, eventOrTransactionID) = self else {
return nil
}
switch eventOrTransactionID {
case .transactionId(let transactionID):
return transactionID
default:
return nil
}
}
}
// MARK: - Mocks
extension TimelineItemIdentifier {
static var randomEvent: Self {
.event(uniqueID: UUID().uuidString, eventOrTransactionID: .eventId(eventId: UUID().uuidString))
}
static var randomVirtual: Self {
.virtual(uniqueID: UUID().uuidString)
}
}