Skip to content

Commit

Permalink
fix: fromJson case
Browse files Browse the repository at this point in the history
  • Loading branch information
LichKing-2234 committed Jun 6, 2023
1 parent 9d425f8 commit c6b1cc5
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 108 deletions.
30 changes: 22 additions & 8 deletions ios/Classes/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,23 @@ extension Dictionary where Key == String {

func toRtmAttribute() -> AgoraRtmAttribute {
let rtmAttribute = AgoraRtmAttribute()
rtmAttribute.key = self["key"] as! String
rtmAttribute.value = self["value"] as! String
if let key = self["key"] as? String {
rtmAttribute.key = key
}
if let value = self["value"] as? String {
rtmAttribute.value = value
}
return rtmAttribute
}

func toRtmChannelAttribute() -> AgoraRtmChannelAttribute {
let rtmAttribute = AgoraRtmChannelAttribute()
rtmAttribute.key = self["key"] as! String
rtmAttribute.value = self["value"] as! String
if let key = self["key"] as? String {
rtmAttribute.key = key
}
if let value = self["value"] as? String {
rtmAttribute.value = value
}
return rtmAttribute
}

Expand Down Expand Up @@ -148,7 +156,9 @@ extension Dictionary where Key == String {

func toChannelAttributeOptions() -> AgoraRtmChannelAttributeOptions {
let channelAttributeOptions = AgoraRtmChannelAttributeOptions()
channelAttributeOptions.enableNotificationToChannelMembers = self["enableNotificationToChannelMembers"] as! Bool
if let enableNotificationToChannelMembers = self["enableNotificationToChannelMembers"] as? Bool {
channelAttributeOptions.enableNotificationToChannelMembers = enableNotificationToChannelMembers
}
return channelAttributeOptions
}

Expand All @@ -158,8 +168,12 @@ extension Dictionary where Key == String {

func toRtmServiceContext() -> AgoraRtmServiceContext {
let context = AgoraRtmServiceContext()
context.areaCode = AgoraRtmAreaCode(rawValue: self["areaCode"] as! UInt)
context.proxyType = AgoraRtmCloudProxyType(rawValue: self["proxyType"] as! UInt)
if let areaCode = self["areaCode"] as? UInt {
context.areaCode = AgoraRtmAreaCode(rawValue: areaCode)
}
if let proxyType = self["proxyType"] as? UInt {
context.proxyType = AgoraRtmCloudProxyType(rawValue: proxyType)
}
return context
}
}
Expand All @@ -174,7 +188,7 @@ extension Array {
}

func toStringList() -> [String] {
return self.map { $0 as! String }
return self.map { $0 as? String ?? "" }
}

func toJson() -> [[String: Any?]] {
Expand Down
40 changes: 18 additions & 22 deletions lib/src/agora_rtm_call_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,51 +65,47 @@ class AgoraRtmCallManager {
final map = Map.from(event['data']);
switch (event['event']) {
case 'onLocalInvitationReceivedByPeer':
onLocalInvitationReceivedByPeer?.call(LocalInvitation.fromJson(
Map<String, dynamic>.from(map['localInvitation'])));
onLocalInvitationReceivedByPeer
?.call(LocalInvitation.fromJson(map['localInvitation']));
break;
case 'onLocalInvitationAccepted':
onLocalInvitationAccepted?.call(
LocalInvitation.fromJson(
Map<String, dynamic>.from(map['localInvitation'])),
LocalInvitation.fromJson(map['localInvitation']),
map['response']);
break;
case 'onLocalInvitationRefused':
onLocalInvitationRefused?.call(
LocalInvitation.fromJson(
Map<String, dynamic>.from(map['localInvitation'])),
LocalInvitation.fromJson(map['localInvitation']),
map['response']);
break;
case 'onLocalInvitationCanceled':
onLocalInvitationCanceled?.call(LocalInvitation.fromJson(
Map<String, dynamic>.from(map['localInvitation'])));
onLocalInvitationCanceled
?.call(LocalInvitation.fromJson(map['localInvitation']));
break;
case 'onLocalInvitationFailure':
onLocalInvitationFailure?.call(
LocalInvitation.fromJson(
Map<String, dynamic>.from(map['localInvitation'])),
LocalInvitation.fromJson(map['localInvitation']),
map['errorCode']);
break;
case 'onRemoteInvitationReceived':
onRemoteInvitationReceived?.call(RemoteInvitation.fromJson(
Map<String, dynamic>.from(map['remoteInvitation'])));
onRemoteInvitationReceived
?.call(RemoteInvitation.fromJson(map['remoteInvitation']));
break;
case 'onRemoteInvitationAccepted':
onRemoteInvitationAccepted?.call(RemoteInvitation.fromJson(
Map<String, dynamic>.from(map['remoteInvitation'])));
onRemoteInvitationAccepted
?.call(RemoteInvitation.fromJson(map['remoteInvitation']));
break;
case 'onRemoteInvitationRefused':
onRemoteInvitationRefused?.call(RemoteInvitation.fromJson(
Map<String, dynamic>.from(map['remoteInvitation'])));
onRemoteInvitationRefused
?.call(RemoteInvitation.fromJson(map['remoteInvitation']));
break;
case 'onRemoteInvitationCanceled':
onRemoteInvitationCanceled?.call(RemoteInvitation.fromJson(
Map<String, dynamic>.from(map['remoteInvitation'])));
onRemoteInvitationCanceled
?.call(RemoteInvitation.fromJson(map['remoteInvitation']));
break;
case 'onRemoteInvitationFailure':
onRemoteInvitationFailure?.call(
RemoteInvitation.fromJson(
Map<String, dynamic>.from(map['remoteInvitation'])),
RemoteInvitation.fromJson(map['remoteInvitation']),
map['errorCode']);
break;
}
Expand All @@ -122,8 +118,8 @@ class AgoraRtmCallManager {
}

Future<LocalInvitation> createLocalInvitation(String calleeId) async {
return LocalInvitation.fromJson(Map<String, dynamic>.from(
await _callNative("createLocalInvitation", {'calleeId': calleeId})));
return LocalInvitation.fromJson(
await _callNative("createLocalInvitation", {'calleeId': calleeId}));
}

/// Allows the caller to send a call invitation to the callee.
Expand Down
24 changes: 10 additions & 14 deletions lib/src/agora_rtm_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,24 @@ class AgoraRtmChannel {
onMemberCountUpdated?.call(memberCount);
break;
case 'onAttributesUpdated':
List<RtmChannelAttribute> attributeList = List<Map>.from(
map['attributeList'])
.map((e) =>
RtmChannelAttribute.fromJson(Map<String, dynamic>.from(e)))
.toList();
List<RtmChannelAttribute> attributeList =
List<Map>.from(map['attributeList'])
.map((e) => RtmChannelAttribute.fromJson(e))
.toList();
onAttributesUpdated?.call(attributeList);
break;
case 'onMessageReceived':
RtmMessage message =
RtmMessage.fromJson(Map<String, dynamic>.from(map['message']));
RtmChannelMember fromMember = RtmChannelMember.fromJson(
Map<String, dynamic>.from(map['fromMember']));
RtmMessage message = RtmMessage.fromJson(map['message']);
RtmChannelMember fromMember =
RtmChannelMember.fromJson(map['fromMember']);
onMessageReceived?.call(message, fromMember);
break;
case 'onMemberJoined':
RtmChannelMember member = RtmChannelMember.fromJson(
Map<String, dynamic>.from(map['member']));
RtmChannelMember member = RtmChannelMember.fromJson(map['member']);
onMemberJoined?.call(member);
break;
case 'onMemberLeft':
RtmChannelMember member = RtmChannelMember.fromJson(
Map<String, dynamic>.from(map['member']));
RtmChannelMember member = RtmChannelMember.fromJson(map['member']);
onMemberLeft?.call(member);
break;
}
Expand Down Expand Up @@ -105,7 +101,7 @@ class AgoraRtmChannel {

Future<List<RtmChannelMember>> getMembers() async {
return List<Map>.from(await _callNative("getMembers", null))
.map((e) => RtmChannelMember.fromJson(Map<String, dynamic>.from(e)))
.map((e) => RtmChannelMember.fromJson(e))
.toList();
}

Expand Down
42 changes: 18 additions & 24 deletions lib/src/agora_rtm_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ class AgoraRtmClient {
onConnectionStateChanged2?.call(state, reason);
break;
case 'onMessageReceived':
var message =
RtmMessage.fromJson(Map<String, dynamic>.from(map["message"]));
var message = RtmMessage.fromJson(map["message"]);
String peerId = map["peerId"];
onMessageReceived?.call(message, peerId);
break;
Expand Down Expand Up @@ -229,7 +228,7 @@ class AgoraRtmClient {
Future<List<RtmAttribute>> getUserAttributes2(String userId) async {
return List<Map>.from(
await _callNative("getUserAttributes", {'userId': userId}))
.map((e) => RtmAttribute.fromJson(Map<String, dynamic>.from(e)))
.map((e) => RtmAttribute.fromJson(e))
.toList();
}

Expand All @@ -238,7 +237,7 @@ class AgoraRtmClient {
String userId, List<String> attributeKeys) async {
return List<Map>.from(await _callNative("getUserAttributesByKeys",
{'userId': userId, 'attributeKeys': attributeKeys}))
.map((e) => RtmAttribute.fromJson(Map<String, dynamic>.from(e)))
.map((e) => RtmAttribute.fromJson(e))
.toList();
}

Expand Down Expand Up @@ -289,8 +288,7 @@ class AgoraRtmClient {
String channelId) async {
return List<Map>.from(
await _callNative("getChannelAttributes", {'channelId': channelId}))
.map((attr) =>
RtmChannelAttribute.fromJson(Map<String, dynamic>.from(attr)))
.map((attr) => RtmChannelAttribute.fromJson(attr))
.toList();
}

Expand All @@ -299,17 +297,15 @@ class AgoraRtmClient {
String channelId, List<String> attributeKeys) async {
return List<Map>.from(await _callNative("getChannelAttributesByKeys",
{'channelId': channelId, 'attributeKeys': attributeKeys}))
.map((attr) =>
RtmChannelAttribute.fromJson(Map<String, dynamic>.from(attr)))
.map((attr) => RtmChannelAttribute.fromJson(attr))
.toList();
}

Future<List<RtmChannelMember>> getChannelMemberCount(
List<String> channelIds) async {
return List<Map>.from(await _callNative(
"getChannelMemberCount", {'channelIds': channelIds}))
.map((attr) =>
RtmChannelMember.fromJson(Map<String, dynamic>.from(attr)))
.map((attr) => RtmChannelMember.fromJson(attr))
.toList();
}

Expand Down Expand Up @@ -447,29 +443,29 @@ class AgoraRtmClient {
/// [AgoraRtmCallManager.sendLocalInvitation]
@Deprecated('Use AgoraRtmCallManager.sendLocalInvitation instead of.')
Future<void> sendLocalInvitation(Map<String, dynamic> arguments) {
return _callManager.sendLocalInvitation(
LocalInvitation.fromJson(Map<String, dynamic>.from(arguments)));
return _callManager
.sendLocalInvitation(LocalInvitation.fromJson(arguments));
}

/// [AgoraRtmCallManager.acceptRemoteInvitation]
@Deprecated('Use AgoraRtmCallManager.acceptRemoteInvitation instead of.')
Future<void> acceptRemoteInvitation(Map<String, dynamic> arguments) {
return _callManager.acceptRemoteInvitation(
RemoteInvitation.fromJson(Map<String, dynamic>.from(arguments)));
return _callManager
.acceptRemoteInvitation(RemoteInvitation.fromJson(arguments));
}

/// [AgoraRtmCallManager.refuseRemoteInvitation]
@Deprecated('Use AgoraRtmCallManager.refuseRemoteInvitation instead of.')
Future<void> refuseRemoteInvitation(Map<String, dynamic> arguments) {
return _callManager.refuseRemoteInvitation(
RemoteInvitation.fromJson(Map<String, dynamic>.from(arguments)));
return _callManager
.refuseRemoteInvitation(RemoteInvitation.fromJson(arguments));
}

/// [AgoraRtmCallManager.cancelLocalInvitation]
@Deprecated('Use AgoraRtmCallManager.cancelLocalInvitation instead of.')
Future<void> cancelLocalInvitation(Map<String, dynamic> arguments) {
return _callManager.cancelLocalInvitation(
LocalInvitation.fromJson(Map<String, dynamic>.from(arguments)));
return _callManager
.cancelLocalInvitation(LocalInvitation.fromJson(arguments));
}

/// [sendMessageToPeer2]
Expand All @@ -487,18 +483,16 @@ class AgoraRtmClient {
/// [setLocalUserAttributes2]
@Deprecated('Use setLocalUserAttributes2 instead of.')
Future<void> setLocalUserAttributes(List<Map<String, String>> attributes) {
return setLocalUserAttributes2(attributes
.map((e) => RtmAttribute.fromJson(Map<String, dynamic>.from(e)))
.toList());
return setLocalUserAttributes2(
attributes.map((e) => RtmAttribute.fromJson(e)).toList());
}

/// [addOrUpdateLocalUserAttributes2]
@Deprecated('Use addOrUpdateLocalUserAttributes2 instead of.')
Future<void> addOrUpdateLocalUserAttributes(
List<Map<String, String>> attributes) {
return addOrUpdateLocalUserAttributes2(attributes
.map((e) => RtmAttribute.fromJson(Map<String, dynamic>.from(e)))
.toList());
return addOrUpdateLocalUserAttributes2(
attributes.map((e) => RtmAttribute.fromJson(e)).toList());
}

/// [getUserAttributes2]
Expand Down
Loading

0 comments on commit c6b1cc5

Please sign in to comment.