VB: Own code review

This commit is contained in:
manuroe
2022-10-20 13:39:27 +02:00
parent 64d4a91463
commit 34a49f966a
8 changed files with 13 additions and 115 deletions
@@ -47,7 +47,7 @@ final class VoiceBroadcastPlaybackCoordinator: Coordinator, Presentable {
let voiceBroadcastAggregator = try VoiceBroadcastAggregator(session: parameters.session, room: parameters.room, voiceBroadcastStartEventId: parameters.voiceBroadcastStartEvent.eventId)
let details = VoiceBroadcastPlaybackDetails(type: VoiceBroadcastPlaybackType.player, senderDisplayName: parameters.senderDisplayName)
let details = VoiceBroadcastPlaybackDetails(senderDisplayName: parameters.senderDisplayName)
viewModel = VoiceBroadcastPlaybackViewModel(details: details,
mediaServiceProvider: VoiceMessageMediaServiceProvider.sharedProvider,
cacheManager: VoiceMessageAttachmentCacheManager.sharedManager,
@@ -1,61 +0,0 @@
//
// 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 Foundation
class VoiceBroadcastPlaybackService: VoiceBroadcastPlaybackServiceProtocol {
// MARK: - Properties
private(set) var voiceBroadcastChunks: [VoiceBroadcastChunk] = []
private let roomId: String
// MARK: Private
// MARK: Public
var didUpdateVoiceBroadcastChunks: (([VoiceBroadcastChunk]) -> Void)?
// MARK: - Setup
init(roomId: String) {
self.roomId = roomId
updateVoiceBroadcastChunks(notifyUpdate: false)
}
// MARK: - Public
func startPlayingVoiceBroadcast() {
}
func pausePlayingVoiceBroadcast() {
}
// MARK: - Private
private func updateVoiceBroadcastChunks(notifyUpdate: Bool) {
// TODO: VB udpate voicebroadcast chunks. We already have a listener on voicebroadcast events in VoiceBroadcastAggregator
if notifyUpdate {
didUpdateVoiceBroadcastChunks?(voiceBroadcastChunks)
}
}
}
@@ -1,31 +0,0 @@
//
// 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 Combine
import CoreLocation
import Foundation
protocol VoiceBroadcastPlaybackServiceProtocol {
/// All shared voice broadcast chunks
var voiceBroadcastChunks: [VoiceBroadcastChunk] { get }
/// Called when voice broadcast chunks are updated.
var didUpdateVoiceBroadcastChunks: (([VoiceBroadcastChunk]) -> Void)? { get set }
func startPlayingVoiceBroadcast()
func pausePlayingVoiceBroadcast()
}
@@ -33,14 +33,7 @@ enum VoiceBroadcastPlaybackState {
case error
}
// TODO: Keept it? It is always player
enum VoiceBroadcastPlaybackType {
case player
case recorder
}
struct VoiceBroadcastPlaybackDetails {
let type: VoiceBroadcastPlaybackType // TODO: Keept it? It is always player
let senderDisplayName: String?
}
@@ -42,7 +42,7 @@ enum MockVoiceBroadcastPlaybackScreenState: MockScreenState, CaseIterable {
/// Generate the view struct for the screen state.
var screenView: ([Any], AnyView) {
let details = VoiceBroadcastPlaybackDetails(type: VoiceBroadcastPlaybackType.player, senderDisplayName: "Alice")
let details = VoiceBroadcastPlaybackDetails(senderDisplayName: "Alice")
let viewModel = MockVoiceBroadcastPlaybackViewModel(initialViewState: VoiceBroadcastPlaybackViewState(details: details, broadcastState: .live, playbackState: .stopped, bindings: VoiceBroadcastPlaybackViewStateBindings()))
return (
@@ -85,7 +85,7 @@ class VoiceBroadcastPlaybackViewModel: VoiceBroadcastPlaybackViewModelType, Voic
if voiceBroadcastAggregator.isStarted == false {
// Start the streaming by fetching broadcast chunks
// The audio player will start the automatically playback on incoming chunks
// The audio player will automatically start the playback on incoming chunks
MXLog.debug("[VoiceBroadcastPlaybackViewModel] play: Start streaming")
state.playbackState = .buffering
voiceBroadcastAggregator.start()
@@ -98,7 +98,7 @@ class VoiceBroadcastPlaybackViewModel: VoiceBroadcastPlaybackViewModelType, Voic
let chunks = voiceBroadcastAggregator.voiceBroadcast.chunks
MXLog.debug("[VoiceBroadcastPlaybackViewModel] play: restart from the beginning: \(chunks.count) chunks")
// Reinject all the chunck we already have
// Reinject all the chuncks we already have and play them
voiceBroadcastChunkQueue.append(contentsOf: chunks)
processPendingVoiceBroadcastChunks()
}
@@ -117,7 +117,7 @@ class VoiceBroadcastPlaybackViewModel: VoiceBroadcastPlaybackViewModelType, Voic
if voiceBroadcastAggregator.isStarted == false {
// Start the streaming by fetching broadcast chunks
// The audio player will start the automatically playback on incoming chunks
// The audio player will automatically start the playback on incoming chunks
MXLog.debug("[VoiceBroadcastPlaybackViewModel] playLive: Start streaming")
state.playbackState = .buffering
voiceBroadcastAggregator.start()
@@ -126,7 +126,7 @@ class VoiceBroadcastPlaybackViewModel: VoiceBroadcastPlaybackViewModelType, Voic
let chunks = voiceBroadcastAggregator.voiceBroadcast.chunks
MXLog.debug("[VoiceBroadcastPlaybackViewModel] playLive: restart from the last chunk: \(chunks.count) chunks")
// Reinject all the chunck we already have
// Reinject all the chuncks we already have and play the last one
voiceBroadcastChunkQueue.append(contentsOf: chunks)
processPendingVoiceBroadcastChunksForLivePlayback()
}
@@ -161,13 +161,13 @@ class VoiceBroadcastPlaybackViewModel: VoiceBroadcastPlaybackViewModelType, Voic
// MARK: - Voice broadcast chunks playback
/// Start the broadcast from the beginning or push more chunks
/// Start the playback from the beginning or push more chunks to it
private func processPendingVoiceBroadcastChunks() {
reorderPendingVoiceBroadcastChunks()
processNextVoiceBroadcastChunk()
}
/// Start the broadcast from the last known chunk
/// Start the playback from the last known chunk
private func processPendingVoiceBroadcastChunksForLivePlayback() {
let chunks = reorderVoiceBroadcastChunks(chunks: Array(voiceBroadcastAggregator.voiceBroadcast.chunks))
if let lastChunk = chunks.last {
@@ -178,7 +178,7 @@ class VoiceBroadcastPlaybackViewModel: VoiceBroadcastPlaybackViewModelType, Voic
}
private func reorderPendingVoiceBroadcastChunks() {
// Make sure we download and process check in the right order
// Make sure we download and process chunks in the right order
voiceBroadcastChunkQueue = reorderVoiceBroadcastChunks(chunks: voiceBroadcastChunkQueue)
}
private func reorderVoiceBroadcastChunks(chunks: [VoiceBroadcastChunk]) -> [VoiceBroadcastChunk] {
@@ -186,7 +186,6 @@ class VoiceBroadcastPlaybackViewModel: VoiceBroadcastPlaybackViewModelType, Voic
}
private func processNextVoiceBroadcastChunk() {
MXLog.debug("[VoiceBroadcastPlaybackViewModel] processNextVoiceBroadcastChunk: \(voiceBroadcastChunkQueue.count) chunks remaining")
guard voiceBroadcastChunkQueue.count > 0 else {
@@ -242,7 +241,7 @@ class VoiceBroadcastPlaybackViewModel: VoiceBroadcastPlaybackViewModelType, Voic
}
}
// MARK: - TODO: VoiceBroadcastAggregatorDelegate
// MARK: VoiceBroadcastAggregatorDelegate
extension VoiceBroadcastPlaybackViewModel: VoiceBroadcastAggregatorDelegate {
func voiceBroadcastAggregatorDidStartLoading(_ aggregator: VoiceBroadcastAggregator) {
}
@@ -261,7 +260,7 @@ extension VoiceBroadcastPlaybackViewModel: VoiceBroadcastAggregatorDelegate {
func voiceBroadcastAggregatorDidUpdateData(_ aggregator: VoiceBroadcastAggregator) {
if isLivePlayback && state.playbackState == .buffering {
// We started directly with a live playback but there was no known chuncks at that time
// This is the first chunks we get. Start the playback on the latest one
// These are the first chunks we get. Start the playback on the latest one
processPendingVoiceBroadcastChunksForLivePlayback()
}
else {
@@ -271,7 +270,7 @@ extension VoiceBroadcastPlaybackViewModel: VoiceBroadcastAggregatorDelegate {
}
// MARK: - TODO: VoiceMessageAudioPlayerDelegate
// MARK: - VoiceMessageAudioPlayerDelegate
extension VoiceBroadcastPlaybackViewModel: VoiceMessageAudioPlayerDelegate {
func audioPlayerDidFinishLoading(_ audioPlayer: VoiceMessageAudioPlayer) {
}