Skip to content

Commit

Permalink
Merge pull request #18 from Ascenio/refactor/removes-null-checks
Browse files Browse the repository at this point in the history
Removes null checks when possible

closes #4
  • Loading branch information
tadaspetra committed Jul 7, 2021
2 parents 7aa6b42 + 4a3a543 commit 82be51e
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 144 deletions.
98 changes: 34 additions & 64 deletions lib/controllers/session_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ class SessionController extends ValueNotifier<AgoraSettings> {
);
}

void createEvents(AgoraEventHandlers? agoraEventHandlers) {
void createEvents(AgoraEventHandlers agoraEventHandlers) async {
value.engine?.setEventHandler(
RtcEngineEventHandler(
error: (code) {
final info = 'onError: $code';
print(info);
var onErrorFun = agoraEventHandlers?.onError;
if (onErrorFun != null) onErrorFun(code);
agoraEventHandlers.onError(code);
},
joinChannelSuccess: (channel, uid, elapsed) {
final info = 'onJoinChannel: $channel, uid: $uid';
Expand All @@ -66,15 +65,11 @@ class SessionController extends ValueNotifier<AgoraSettings> {
clientRole: value.clientRole,
),
);
var joinChannelSuccessFun = agoraEventHandlers?.joinChannelSuccess;
if (joinChannelSuccessFun != null) {
joinChannelSuccessFun(channel, uid, elapsed);
}
agoraEventHandlers.joinChannelSuccess(channel, uid, elapsed);
},
leaveChannel: (stats) {
_clearUsers();
var leaveChannelFun = agoraEventHandlers?.leaveChannel;
if (leaveChannelFun != null) leaveChannelFun(stats);
agoraEventHandlers.leaveChannel(stats);
},
userJoined: (uid, elapsed) {
final info = 'userJoined: $uid';
Expand All @@ -84,16 +79,14 @@ class SessionController extends ValueNotifier<AgoraSettings> {
uid: uid,
),
);
var userJoinedFun = agoraEventHandlers?.userJoined;
if (userJoinedFun != null) userJoinedFun(uid, elapsed);
agoraEventHandlers.userJoined(uid, elapsed);
},
userOffline: (uid, reason) {
final info = 'userOffline: $uid , reason: $reason';
print(info);
_checkForMaxUser(uid: uid);
_removeUser(uid: uid);
var userOfflineFun = agoraEventHandlers?.userOffline;
if (userOfflineFun != null) userOfflineFun(uid, reason);
agoraEventHandlers.userOffline(uid, reason);
},
tokenPrivilegeWillExpire: (token) async {
await _getToken(
Expand All @@ -102,11 +95,7 @@ class SessionController extends ValueNotifier<AgoraSettings> {
uid: value.connectionData!.uid,
);
await value.engine?.renewToken(token);
var tokenPrivilegeWillExpireFun =
agoraEventHandlers?.tokenPrivilegeWillExpire;
if (tokenPrivilegeWillExpireFun != null) {
tokenPrivilegeWillExpireFun(token);
}
agoraEventHandlers.tokenPrivilegeWillExpire(token);
},
remoteVideoStateChanged: (uid, state, reason, elapsed) {
final String info =
Expand All @@ -120,11 +109,8 @@ class SessionController extends ValueNotifier<AgoraSettings> {
_updateUserVideo(uid: uid, videoDisabled: false);
}
}
var remoteVideoStateChangedFun =
agoraEventHandlers?.remoteVideoStateChanged;
if (remoteVideoStateChangedFun != null) {
remoteVideoStateChangedFun(uid, state, reason, elapsed);
}
agoraEventHandlers.remoteVideoStateChanged(
uid, state, reason, elapsed);
},
remoteAudioStateChanged: (uid, state, reason, elapsed) {
final String info =
Expand All @@ -137,31 +123,20 @@ class SessionController extends ValueNotifier<AgoraSettings> {
reason == AudioRemoteStateReason.RemoteUnmuted) {
_updateUserAudio(uid: uid, muted: false);
}
var remoteAudioStateChangedFun =
agoraEventHandlers?.remoteAudioStateChanged;
if (remoteAudioStateChangedFun != null) {
remoteAudioStateChangedFun(uid, state, reason, elapsed);
}
agoraEventHandlers.remoteAudioStateChanged(
uid, state, reason, elapsed);
},
localAudioStateChanged: (state, error) {
final String info =
"Local audio state changed state: $state and error: $error";
print(info);
var localAudioStateChangedFun =
agoraEventHandlers?.localAudioStateChanged;
if (localAudioStateChangedFun != null) {
localAudioStateChangedFun(state, error);
}
agoraEventHandlers.localAudioStateChanged(state, error);
},
localVideoStateChanged: (localVideoState, error) {
final String info =
"Local audio state changed state: $localVideoState and error: $error";
print(info);
var localVideoStateChangedFun =
agoraEventHandlers?.localVideoStateChanged;
if (localVideoStateChangedFun != null) {
localVideoStateChangedFun(localVideoState, error);
}
agoraEventHandlers.localVideoStateChanged(localVideoState, error);
},
activeSpeaker: (uid) {
final String info = "Active speaker: $uid";
Expand All @@ -173,41 +148,37 @@ class SessionController extends ValueNotifier<AgoraSettings> {
} else {
print("Active speaker is disabled");
}
var activeSpeakerFun = agoraEventHandlers?.activeSpeaker;
if (activeSpeakerFun != null) activeSpeakerFun(uid);
agoraEventHandlers.activeSpeaker(uid);
},
),
);
}

/// Function to set all the channel properties.
Future<void> setChannelProperties(AgoraChannelData agoraChannelData) async {
await value.engine?.setChannelProfile(
agoraChannelData.channelProfile ?? ChannelProfile.Communication);

void setChannelProperties(AgoraChannelData agoraChannelData) async {
await value.engine?.setChannelProfile(agoraChannelData.channelProfile);
if (agoraChannelData.channelProfile == ChannelProfile.LiveBroadcasting) {
await value.engine?.setClientRole(
agoraChannelData.clientRole ?? ClientRole.Broadcaster);
await value.engine?.setClientRole(agoraChannelData.clientRole);
} else {
print('You can only set channel profile in case of Live Broadcasting');
}

value = value.copyWith(
isActiveSpeakerDisabled: agoraChannelData.isActiveSpeakerDisabled);

await value.engine?.muteAllRemoteVideoStreams(
agoraChannelData.muteAllRemoteVideoStreams ?? false);
await value.engine
?.muteAllRemoteVideoStreams(agoraChannelData.muteAllRemoteVideoStreams);

await value.engine?.muteAllRemoteAudioStreams(
agoraChannelData.muteAllRemoteAudioStreams ?? false);
await value.engine
?.muteAllRemoteAudioStreams(agoraChannelData.muteAllRemoteAudioStreams);

if (agoraChannelData.setBeautyEffectOptions != null) {
await value.engine?.setBeautyEffectOptions(
true, agoraChannelData.setBeautyEffectOptions!);
}

await value.engine
?.enableDualStreamMode(agoraChannelData.enableDualStreamMode ?? false);
?.enableDualStreamMode(agoraChannelData.enableDualStreamMode);

if (agoraChannelData.localPublishFallbackOption != null) {
await value.engine?.setLocalPublishFallbackOption(
Expand All @@ -224,15 +195,12 @@ class SessionController extends ValueNotifier<AgoraSettings> {
agoraChannelData.videoEncoderConfiguration!);
}

await value.engine?.setCameraAutoFocusFaceModeEnabled(
agoraChannelData.setCameraAutoFocusFaceModeEnabled ?? false);

await value.engine
?.setCameraTorchOn(agoraChannelData.setCameraTorchOn ?? false);
value.engine?.setCameraAutoFocusFaceModeEnabled(
agoraChannelData.setCameraAutoFocusFaceModeEnabled);

value.engine?.setCameraTorchOn(agoraChannelData.setCameraTorchOn);
await value.engine?.setAudioProfile(
agoraChannelData.audioProfile ?? AudioProfile.Default,
agoraChannelData.audioScenario ?? AudioScenario.Default);
agoraChannelData.audioProfile, agoraChannelData.audioScenario);
}

Future<void> joinVideoChannel() async {
Expand All @@ -249,7 +217,7 @@ class SessionController extends ValueNotifier<AgoraSettings> {
value.connectionData!.tempToken ?? value.generatedToken,
value.connectionData!.channelName,
null,
value.connectionData!.uid ?? 0,
value.connectionData!.uid,
);
}

Expand Down Expand Up @@ -310,10 +278,10 @@ class SessionController extends ValueNotifier<AgoraSettings> {
Timer? timer;

/// Function to auto hide the button class.
void toggleVisible({int? autoHideButtonTime}) {
void toggleVisible({int autoHideButtonTime = 5}) async {
if (!(value.visible)) {
value = value.copyWith(visible: !(value.visible));
timer = Timer(Duration(seconds: autoHideButtonTime ?? 5), () {
timer = Timer(Duration(seconds: autoHideButtonTime), () {
if (!(value.visible)) return;
value = value.copyWith(visible: !(value.visible));
});
Expand Down Expand Up @@ -376,9 +344,11 @@ class SessionController extends ValueNotifier<AgoraSettings> {
_removeUser(uid: newUser.uid);
}

Future<void> _getToken(
{String? tokenUrl, String? channelName, int? uid}) async {
uid = uid ?? 0;
Future<void> _getToken({
String? tokenUrl,
String? channelName,
int uid = 0,
}) async {
final response = await http
.get(Uri.parse('$tokenUrl/rtc/$channelName/publisher/uid/$uid'));
if (response.statusCode == 200) {
Expand Down
36 changes: 18 additions & 18 deletions lib/models/agora_channel_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ class AgoraChannelData {
/// The Agora [RtcEngine] differentiates channel profiles and applies different optimization algorithms accordingly.
///
/// You can choose between 3 default channel profiles [ChannelProfile.Communication], [ChannelProfile.LiveBroadcasting] and [ChannelProfile.Game]
final ChannelProfile? channelProfile;
final ChannelProfile channelProfile;

/// Sets the role of a user in a live interactive streaming.
/// This method applies only when [channelProfile] is set to [ChannelProfile.LiveBroadcasting]
///
/// Use this to set the user behaviour inside a channel. A user can choose between a [ClientRole.Audience] or [ClientRole.Broadcaster].
/// - An audience member can only receive the stream.
/// - A broadcaster can send and receive the stream.
ClientRole? clientRole;
ClientRole clientRole;

/// Each video encoder configuration corresponds to a set of video parameters, including the resolution, frame rate, bitrate, and video orientation.
///
Expand All @@ -25,7 +25,7 @@ class AgoraChannelData {
/// *Parameter* Sets whether to enable/disable the camera auto-face focus function:
/// - `true`: Enable the camera auto-face focus function.
/// - `false`: (Default) Disable the camera auto-face focus function.
bool? setCameraAutoFocusFaceModeEnabled;
bool setCameraAutoFocusFaceModeEnabled;

/// Enables/Disables the dual video stream mode.
///
Expand All @@ -34,7 +34,7 @@ class AgoraChannelData {
/// *Parameter* Sets the stream mode:
/// - `true`: Dual-stream mode.
/// - `false`: (Default) Single-stream mode.
bool? enableDualStreamMode;
bool enableDualStreamMode;

/// Sets the fallback option for the locally published video stream based on the network conditions.
///
Expand All @@ -60,13 +60,13 @@ class AgoraChannelData {
/// Sets the sample rate, bitrate, encoding mode, and the number of channels.
///
/// To know more about AudioProfile have a look over here [AudioProfile]
AudioProfile? audioProfile;
AudioProfile audioProfile;

/// Sets the audio parameters and application scenarios.
///
/// Sets the audio application scenarios. Under different audio scenarios, the device uses different volume tracks, i.e. either the in-call volume or the media volume. S
/// To know more about AudioScenario have a look over here [AudioScenario]
AudioScenario? audioScenario;
AudioScenario audioScenario;

/// Enhancess the image being streamed on the channel.
///
Expand All @@ -80,21 +80,21 @@ class AgoraChannelData {
/// *Parameter* Enables the camera flash:
/// - `true`: Enable the camera flash function.
/// - `false`: (Default) Disable the camera flash function.
bool? setCameraTorchOn;
bool setCameraTorchOn;

/// Stops/Resumes receiving all remote audio streams.
///
/// *Parameter* Determines whether to receive/stop receiving all remote audio streams:
/// - `true`: Stop receiving all remote audio streams.
/// - `false`: (Default) Receive all remote audio streams.
bool? muteAllRemoteVideoStreams;
bool muteAllRemoteVideoStreams;

/// Stops/Resumes receiving all remote video streams.
///
/// *Parameter* Determines whether to receive/stop receiving all remote audio streams:
/// - `true`: Stop receiving all remote video streams.
/// - `false`: (Default) Receive all remote video streams.
bool? muteAllRemoteAudioStreams;
bool muteAllRemoteAudioStreams;

/// Active Speaker method automatically pins the active speaker to the main view. By default active speaker is enabled.
///
Expand All @@ -104,19 +104,19 @@ class AgoraChannelData {
final bool isActiveSpeakerDisabled;

AgoraChannelData({
this.channelProfile,
this.clientRole,
this.channelProfile = ChannelProfile.Communication,
this.clientRole = ClientRole.Broadcaster,
this.videoEncoderConfiguration,
this.setCameraAutoFocusFaceModeEnabled,
this.enableDualStreamMode,
this.setCameraAutoFocusFaceModeEnabled = false,
this.enableDualStreamMode = false,
this.localPublishFallbackOption,
this.remoteSubscribeFallbackOption,
this.audioProfile,
this.audioScenario,
this.audioProfile = AudioProfile.Default,
this.audioScenario = AudioScenario.Default,
this.setBeautyEffectOptions,
this.setCameraTorchOn,
this.muteAllRemoteAudioStreams,
this.muteAllRemoteVideoStreams,
this.setCameraTorchOn = false,
this.muteAllRemoteAudioStreams = false,
this.muteAllRemoteVideoStreams = false,
this.isActiveSpeakerDisabled = false,
});

Expand Down
4 changes: 2 additions & 2 deletions lib/models/agora_connection_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AgoraConnectionData {
final String channelName;

/// (Optional) The user ID. A 32-bit unsigned integer with a value ranging from 1 to (232-1). This parameter must be unique. If uid is not assigned (or set as 0), the SDK assigns a uid and reports it in the onJoinChannelSuccess callback.
final int? uid;
final int uid;

/// (Optional) Link to the deployed token server. The UIKit automatically generates the token after a fixed interval. Have a look at this guide to learn how to set up your [token server](https://github.com/AgoraIO-Community/agora-token-service)
final String? tokenUrl;
Expand All @@ -22,7 +22,7 @@ class AgoraConnectionData {
AgoraConnectionData({
required this.appId,
required this.channelName,
this.uid,
this.uid = 0,
this.tokenUrl,
this.tempToken,
this.areaCode = AreaCode.GLOB,
Expand Down
Loading

0 comments on commit 82be51e

Please sign in to comment.