Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yostyle committed Oct 11, 2022
1 parent 9ead883 commit 8b87055
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 205 deletions.
26 changes: 17 additions & 9 deletions Riot/Modules/Room/RoomViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -2317,15 +2317,23 @@ - (void)setupActions {
}

MXSession* session = self.roomDataSource.mxSession;

[session setupUserVoiceBroadcastServiceFor:self.roomDataSource.room];
NSInteger voiceBroadcastServiceState = session.userVoiceBroadcastService.state;
if (voiceBroadcastServiceState == StateStopped) {
[session.userVoiceBroadcastService start];
} else {
[session.userVoiceBroadcastService stop];
}

[session getOrCreateVoiceBroadcastServiceFor:self.roomDataSource.room completion:^(VoiceBroadcastService *voiceBroadcastService) {
if (voiceBroadcastService) {
if ([[voiceBroadcastService getState] isEqualToString:@"stopped"]) {
[session.voiceBroadcastService startVoiceBroadcastWithSuccess:^(NSString * _Nullable success) {

} failure:^(NSError * _Nonnull error) {

}];
} else {
[session.voiceBroadcastService stopVoiceBroadcastWithSuccess:^(NSString * _Nullable success) {

} failure:^(NSError * _Nonnull error) {

}];
}
}
}];
}]];
}
roomInputView.actionsBar.actionItems = actionItems;
Expand Down
12 changes: 6 additions & 6 deletions Riot/Modules/VoiceBroadcast/MXSession+VoiceBroadcast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import MatrixSDK

extension MXSession {

/// Convenient getter to retrieve UserVoiceBroadcastService associated to the session
@objc var userVoiceBroadcastService: UserVoiceBroadcastServiceProtocol? {
return UserVoiceBroadcastServiceProvider.shared.userVoiceBroadcastService
/// Convenient getter to retrieve VoiceBroadcastService associated to the session
@objc var voiceBroadcastService: VoiceBroadcastService? {
return VoiceBroadcastServiceProvider.shared.voiceBroadcastService
}

/// Initialize UserVoiceBroadcastService
@objc public func setupUserVoiceBroadcastService(for room: MXRoom) {
UserVoiceBroadcastServiceProvider.shared.setupUserVoiceBroadcastServiceIfNeeded(for: room)
/// Initialize VoiceBroadcastService
@objc public func getOrCreateVoiceBroadcastService(for room: MXRoom, completion: @escaping (VoiceBroadcastService?) -> Void) {
VoiceBroadcastServiceProvider.shared.getOrCreateVoiceBroadcastService(for: room, completion: completion)
}
}
55 changes: 0 additions & 55 deletions Riot/Modules/VoiceBroadcast/UserVoiceBroadcastService.swift

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,29 @@ public class VoiceBroadcastService: NSObject {
// MARK: - Properties

private var voiceBroadcastInfoEventId: String?
private unowned let room: MXRoom
public let room: MXRoom
public private(set) var state: State

// MARK: - Setup

public init(room: MXRoom) {
public init(room: MXRoom, state: State) {
self.room = room
state = State.stopped
self.state = state
}

// MARK: - Constants

@objc public enum State: Int {
public enum State: String {
case started
case paused
case resumed
case stopped

/// A string representation of the result.
var description: String {
switch self {
case .started:
return "started"
case .paused:
return "paused"
case .resumed:
return "resumed"
case .stopped:
return "stopped"
}
}
}

// MARK: - Public

// MARK: Voice broadcast info

/// Start a voice broadcast.
/// - Parameters:
/// - completion: A closure called when the operation completes. Provides the event id of the event generated on the home server on success.
Expand Down Expand Up @@ -106,6 +96,34 @@ public class VoiceBroadcastService: NSObject {
return sendVoiceBroadcastInfo(state: State.stopped, completion: completion)
}

func getState() -> String {
return self.state.rawValue
}

// MARK: Voice broadcast chunk

/// Send a bunch of a voice broadcast.
///
/// While sending, a fake event will be echoed in the messages list.
/// Once complete, this local echo will be replaced by the event saved by the homeserver.
///
/// - Parameters:
/// - audioFileLocalURL: the local filesystem path of the audio file to send.
/// - mimeType: (optional) the mime type of the file. Defaults to `audio/ogg`
/// - duration: the length of the voice message in milliseconds
/// - samples: an array of floating point values normalized to [0, 1], boxed within NSNumbers
/// - success: A block object called when the operation succeeds. It returns the event id of the event generated on the homeserver
/// - failure: A block object called when the operation fails.
func sendChunkOfVoiceBroadcast(audioFileLocalURL: URL, mimeType: String?, duration: UInt, samples: [Float]?, success:@escaping ((String?) -> Void), failure:@escaping ((Error?) -> Void)) {
guard let voiceBroadcastInfoEventId = self.voiceBroadcastInfoEventId else {
return failure(VoiceBroadcastServiceError.notStarted)
}

self.room.sendChunkOfVoiceBroadcast(localURL: audioFileLocalURL, voiceBroadcastInfoEventId: voiceBroadcastInfoEventId, mimeType: mimeType, duration: duration, samples: samples, success: success, failure: failure)
}

// MARK: - Private

private func sendVoiceBroadcastInfo(state: State, completion: @escaping (MXResponse<String?>) -> Void) -> MXHTTPOperation? {
guard let userId = self.room.mxSession.myUserId else {
completion(.failure(VoiceBroadcastServiceError.missingUserId))
Expand All @@ -115,7 +133,7 @@ public class VoiceBroadcastService: NSObject {
let stateKey = userId

let voiceBroadcastContent = VoiceBroadcastEventContent()
voiceBroadcastContent.state = state.description
voiceBroadcastContent.state = state.rawValue

if state != State.started {
guard let voiceBroadcastInfoEventId = self.voiceBroadcastInfoEventId else {
Expand Down Expand Up @@ -145,26 +163,6 @@ public class VoiceBroadcastService: NSObject {
}
}
}

/// Send a bunch of a voice broadcast.
///
/// While sending, a fake event will be echoed in the messages list.
/// Once complete, this local echo will be replaced by the event saved by the homeserver.
///
/// - Parameters:
/// - audioFileLocalURL: the local filesystem path of the audio file to send.
/// - mimeType: (optional) the mime type of the file. Defaults to `audio/ogg`
/// - duration: the length of the voice message in milliseconds
/// - samples: an array of floating point values normalized to [0, 1], boxed within NSNumbers
/// - success: A block object called when the operation succeeds. It returns the event id of the event generated on the homeserver
/// - failure: A block object called when the operation fails.
func sendChunkOfVoiceBroadcast(audioFileLocalURL: URL, mimeType: String?, duration: UInt, samples: [Float]?, success:@escaping ((String?) -> Void), failure:@escaping ((Error?) -> Void)) {
guard let voiceBroadcastInfoEventId = self.voiceBroadcastInfoEventId else {
return failure(VoiceBroadcastServiceError.notStarted)
}

self.room.sendChunkOfVoiceBroadcast(localURL: audioFileLocalURL, voiceBroadcastInfoEventId: voiceBroadcastInfoEventId, mimeType: mimeType, duration: duration, samples: samples, success: success, failure: failure)
}
}

// MARK: - Objective-C interface
Expand Down
Loading

0 comments on commit 8b87055

Please sign in to comment.