mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-26 19:34:25 +02:00
VB: Make the view model aware of every chunk new coming
this reactive approach will help to cache and reorder them by sequence
This commit is contained in:
@@ -25,6 +25,7 @@ public protocol VoiceBroadcastAggregatorDelegate: AnyObject {
|
||||
func voiceBroadcastAggregatorDidStartLoading(_ aggregator: VoiceBroadcastAggregator)
|
||||
func voiceBroadcastAggregatorDidEndLoading(_ aggregator: VoiceBroadcastAggregator)
|
||||
func voiceBroadcastAggregator(_ aggregator: VoiceBroadcastAggregator, didFailWithError: Error)
|
||||
func voiceBroadcastAggregator(_ aggregator: VoiceBroadcastAggregator, didReceiveChunk: VoiceBroadcastChunk)
|
||||
func voiceBroadcastAggregatorDidUpdateData(_ aggregator: VoiceBroadcastAggregator)
|
||||
}
|
||||
|
||||
@@ -109,8 +110,6 @@ public class VoiceBroadcastAggregator {
|
||||
return
|
||||
}
|
||||
|
||||
MXLog.debug("[VoiceBroadcastAggregator] Start aggregation for broadcast \(self.voiceBroadcastStartEventId)")
|
||||
|
||||
self.events.removeAll()
|
||||
|
||||
self.events.append(contentsOf: response.chunk)
|
||||
@@ -125,6 +124,10 @@ public class VoiceBroadcastAggregator {
|
||||
return
|
||||
}
|
||||
|
||||
if let chunk = self.voiceBroadcastBuilder.buildChunk(event: event, mediaManager: self.session.mediaManager, voiceBroadcastStartEventId: self.voiceBroadcastStartEventId) {
|
||||
self.delegate?.voiceBroadcastAggregator(self, didReceiveChunk: chunk)
|
||||
}
|
||||
|
||||
self.events.append(event)
|
||||
MXLog.debug("[VoiceBroadcastAggregator] Got a new chunk for broadcast \(relatedEventId). Total: \(self.events.count)")
|
||||
|
||||
@@ -135,12 +138,22 @@ public class VoiceBroadcastAggregator {
|
||||
currentUserIdentifier: self.session.myUserId)
|
||||
} as Any
|
||||
|
||||
|
||||
self.events.forEach { event in
|
||||
guard let chunk = self.voiceBroadcastBuilder.buildChunk(event: event, mediaManager: self.session.mediaManager, voiceBroadcastStartEventId: self.voiceBroadcastStartEventId) else {
|
||||
return
|
||||
}
|
||||
self.delegate?.voiceBroadcastAggregator(self, didReceiveChunk: chunk)
|
||||
}
|
||||
|
||||
self.voiceBroadcast = self.voiceBroadcastBuilder.build(mediaManager: self.session.mediaManager,
|
||||
voiceBroadcastStartEventId: self.voiceBroadcastStartEventId,
|
||||
voiceBroadcastInvoiceBroadcastStartEventContent: self.voiceBroadcastInfoStartEventContent,
|
||||
events: self.events,
|
||||
currentUserIdentifier: self.session.myUserId)
|
||||
|
||||
MXLog.debug("[VoiceBroadcastAggregator] Start aggregation with \(self.voiceBroadcast.chunks.count) chunks for broadcast \(self.voiceBroadcastStartEventId)")
|
||||
|
||||
self.delegate?.voiceBroadcastAggregatorDidEndLoading(self)
|
||||
|
||||
} failure: { [weak self] error in
|
||||
|
||||
@@ -28,15 +28,19 @@ struct VoiceBroadcastBuilder {
|
||||
var voiceBroadcast = VoiceBroadcast()
|
||||
|
||||
voiceBroadcast.chunks = Set(events.compactMap { event in
|
||||
guard let attachment = MXKAttachment(event: event, andMediaManager: mediaManager),
|
||||
let chunkInfo = event.content[VoiceBroadcastSettings.voiceBroadcastContentKeyChunkType] as? [String: UInt],
|
||||
let sequence = chunkInfo[VoiceBroadcastSettings.voiceBroadcastContentKeyChunkSequence] else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return VoiceBroadcastChunk(voiceBroadcastInfoEventId: voiceBroadcastStartEventId, sequence: sequence, attachment: attachment)
|
||||
buildChunk(event: event, mediaManager: mediaManager, voiceBroadcastStartEventId: voiceBroadcastStartEventId)
|
||||
})
|
||||
|
||||
return voiceBroadcast
|
||||
}
|
||||
|
||||
func buildChunk(event: MXEvent, mediaManager: MXMediaManager, voiceBroadcastStartEventId: String) -> VoiceBroadcastChunk? {
|
||||
guard let attachment = MXKAttachment(event: event, andMediaManager: mediaManager),
|
||||
let chunkInfo = event.content[VoiceBroadcastSettings.voiceBroadcastContentKeyChunkType] as? [String: UInt],
|
||||
let sequence = chunkInfo[VoiceBroadcastSettings.voiceBroadcastContentKeyChunkSequence] else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return VoiceBroadcastChunk(voiceBroadcastInfoEventId: voiceBroadcastStartEventId, sequence: sequence, attachment: attachment)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user