Skip to content

Commit

Permalink
feat: Flutter - Specify AreaCode in RTM #87 close #146 (#147)
Browse files Browse the repository at this point in the history
* feat: Flutter - Specify AreaCode in RTM #87

* ci: test flutter 2.5.0

* ci: fix

* ci: fix

* ci: fix
  • Loading branch information
LichKing-2234 authored Jun 5, 2023
1 parent 3bba6a9 commit c86657d
Show file tree
Hide file tree
Showing 12 changed files with 415 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
name: Build Android on Windows
strategy:
matrix:
version: ['2.10.5', '3.0.0']
version: ['2.8.0', '3.0.0']
runs-on: windows-2019
steps:
- uses: actions/checkout@v3
Expand All @@ -58,7 +58,7 @@ jobs:
name: Build iOS
strategy:
matrix:
version: ['2.10.5', '3.0.0']
version: ['2.8.0', '3.0.0']
runs-on: macos-12
timeout-minutes: 120
steps:
Expand Down
27 changes: 23 additions & 4 deletions android/src/main/kotlin/io/agora/agorartm/AgoraRtmPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ class AgoraRtmPlugin : FlutterPlugin, MethodCallHandler {
(args?.get("remoteInvitation") as? Map<*, *>)?.toRemoteInvitation(
agoraClient.call
)
callManager.acceptRemoteInvitation(remoteInvitation,
callManager.acceptRemoteInvitation(
remoteInvitation,
object : Callback<Void>(result, handler) {
override fun toJson(responseInfo: Void): Any? {
agoraClient.call.remoteInvitations.remove(remoteInvitation.hashCode())
return null
}
})
},
)
}

"refuseRemoteInvitation" -> {
Expand Down Expand Up @@ -192,6 +194,21 @@ class AgoraRtmPlugin : FlutterPlugin, MethodCallHandler {
object : Callback<String>(result, handler) {}.onSuccess(RtmClient.getSdkVersion())
}

"setRtmServiceContext" -> {
val context = (params?.get("context") as? Map<*, *>)?.toRtmServiceContext()
val ret = RtmClient.setRtmServiceContext(context)
if (ret.swigValue() == 0) {
object : Callback<Void>(result, handler) {}.onSuccess(null)
} else {
object : Callback<Void>(result, handler) {}.onFailure(
ErrorInfo(
ret.swigValue(),
ret.toString(),
)
)
}
}

else -> {
result.notImplemented()
}
Expand Down Expand Up @@ -490,9 +507,11 @@ class AgoraRtmPlugin : FlutterPlugin, MethodCallHandler {
val message =
(args?.get("message") as? Map<*, *>)?.toRtmMessage(agoraClient.client)
val options = (args?.get("options") as? Map<*, *>)?.toSendMessageOptions()
channel.sendMessage(message,
channel.sendMessage(
message,
options,
object : Callback<Void>(result, handler) {})
object : Callback<Void>(result, handler) {},
)
}

"getMembers" -> {
Expand Down
8 changes: 8 additions & 0 deletions android/src/main/kotlin/io/agora/agorartm/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.agora.rtm.RtmChannelMember
import io.agora.rtm.RtmChannelMemberCount
import io.agora.rtm.RtmClient
import io.agora.rtm.RtmMessage
import io.agora.rtm.RtmServiceContext
import io.agora.rtm.SendMessageOptions

fun RtmMessage.toJson(): Map<String, Any?> {
Expand Down Expand Up @@ -153,6 +154,13 @@ fun ErrorInfo.toJson(): Map<String, Any?> {
)
}

fun Map<*, *>.toRtmServiceContext(): RtmServiceContext {
return RtmServiceContext().apply {
areaCode = this@toRtmServiceContext["areaCode"] as Int
proxyType = this@toRtmServiceContext["proxyType"] as Int
}
}

fun List<*>.toStringSet(): Set<String> {
return toStringList().toSet()
}
Expand Down
5 changes: 2 additions & 3 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ packages:
dependency: transitive
description:
name: json_annotation
sha256: cb314f00b2488de7bc575207e54402cd2f92363f333a7933fd1b0631af226baa
sha256: b10a7b2ff83d83c777edba3c6a0f97045ddadd56c944e1a23a3fdf43a1bf4467
url: "https://pub.dev"
source: hosted
version: "4.6.0"
version: "4.8.1"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -201,4 +201,3 @@ packages:
version: "2.1.4"
sdks:
dart: ">=3.0.0-0 <4.0.0"
flutter: ">=1.12.13+hotfix.6"
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: Demonstrates how to use the agora_rtm plugin.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

environment:
sdk: ">=2.14.0 <3.0.0"
sdk: ">=2.14.0 <4.0.0"

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
Expand Down
7 changes: 7 additions & 0 deletions ios/Classes/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ extension Dictionary where Key == String {
func toSendMessageOptions() -> AgoraRtmSendMessageOptions {
return AgoraRtmSendMessageOptions()
}

func toRtmServiceContext() -> AgoraRtmServiceContext {
let context = AgoraRtmServiceContext()
context.areaCode = AgoraRtmAreaCode(rawValue: self["areaCode"] as! UInt)
context.proxyType = AgoraRtmCloudProxyType(rawValue: self["proxyType"] as! UInt)
return context
}
}

extension Array {
Expand Down
4 changes: 2 additions & 2 deletions ios/Classes/RTMClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class RTMClient: NSObject, FlutterStreamHandler, AgoraRtmDelegate {
}

func rtmKit(_ kit: AgoraRtmKit, peersOnlineStatusChanged onlineStatus: [AgoraRtmPeerOnlineStatus]) {
sendEvent(eventName: "onPeersOnlineStatusChanged", params: ["peersStatus": onlineStatus.reduce(into: [String: Bool]()) {
$0[$1.peerId] = $1.isOnline
sendEvent(eventName: "onPeersOnlineStatusChanged", params: ["peersStatus": onlineStatus.reduce(into: [String: Int]()) {
$0[$1.peerId] = $1.state.rawValue
}])
}

Expand Down
7 changes: 5 additions & 2 deletions ios/Classes/SwiftAgoraRtmPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public class SwiftAgoraRtmPlugin: NSObject, FlutterPlugin {
}
case "getSdkVersion":
result(["errorCode": 0, "result": AgoraRtmKit.getSDKVersion()])
case "setRtmServiceContext":
let context = params?["context"] as? [String: Any?] ?? [:]
result(["errorCode": AgoraRtmKit.setRtmServiceContext(context.toRtmServiceContext())])
default:
result(FlutterMethodNotImplemented)
}
Expand Down Expand Up @@ -142,8 +145,8 @@ public class SwiftAgoraRtmPlugin: NSObject, FlutterPlugin {
case "queryPeersOnlineStatus":
let peerIds = args?["peerIds"] as? [String]
client.queryPeersOnlineStatus(peerIds!) {
result(["errorCode": $1.rawValue, "result": $0?.reduce(into: [String: Bool]()) {
$0[$1.peerId] = $1.isOnline
result(["errorCode": $1.rawValue, "result": $0?.reduce(into: [String: Int]()) {
$0[$1.peerId] = $1.state.rawValue
}])
}
case "subscribePeersOnlineStatus":
Expand Down
55 changes: 40 additions & 15 deletions lib/src/agora_rtm_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class AgoraRtmClient {
void Function(dynamic error)? onError;

/// Occurs when the connection state between the SDK and the Agora RTM system changes.
void Function(int state, int reason)? onConnectionStateChanged;
void Function(RtmConnectionState state, RtmConnectionChangeReason reason)?
onConnectionStateChanged2;

/// Occurs when the local user receives a peer-to-peer message.
void Function(RtmMessage message, String peerId)? onMessageReceived;
Expand All @@ -37,7 +38,8 @@ class AgoraRtmClient {

void Function()? onTokenPrivilegeWillExpire;

void Function(Map<String, bool> peersStatus)? onPeersOnlineStatusChanged;
void Function(Map<String, RtmPeerOnlineState> peersStatus)?
onPeersOnlineStatusChanged;

final int _clientIndex;
final _channels = <String, AgoraRtmChannel>{};
Expand All @@ -58,12 +60,13 @@ class AgoraRtmClient {
final map = Map.from(event['data']);
switch (event['event']) {
case 'onConnectionStateChanged':
int state = map['state'];
int reason = map['reason'];
onConnectionStateChanged?.call(state, reason);
var state = RtmConnectionStateExtension.fromJson(map['state']);
var reason =
RtmConnectionChangeReasonExtension.fromJson(map['reason']);
onConnectionStateChanged2?.call(state, reason);
break;
case 'onMessageReceived':
RtmMessage message =
var message =
RtmMessage.fromJson(Map<String, dynamic>.from(map["message"]));
String peerId = map["peerId"];
onMessageReceived?.call(message, peerId);
Expand All @@ -75,7 +78,9 @@ class AgoraRtmClient {
onTokenPrivilegeWillExpire?.call();
break;
case 'onPeersOnlineStatusChanged':
Map<String, bool> peersStatus = map["peersStatus"];
var peersStatus = Map<String, int>.from(map["peersStatus"]).map(
(key, value) =>
MapEntry(key, RtmPeerOnlineStateExtension.fromJson(value)));
onPeersOnlineStatusChanged?.call(peersStatus);
break;
}
Expand Down Expand Up @@ -167,9 +172,12 @@ class AgoraRtmClient {
}

/// Queries the online status of the specified user(s).
Future<Map<String, bool>> queryPeersOnlineStatus(List<String> peerIds) async {
return Map<String, bool>.from(
await _callNative("queryPeersOnlineStatus", {'peerIds': peerIds}));
Future<Map<String, RtmPeerOnlineState>> queryPeersOnlineStatus(
List<String> peerIds) async {
return Map<String, int>.from(
await _callNative("queryPeersOnlineStatus", {'peerIds': peerIds}))
.map((key, value) =>
MapEntry(key, RtmPeerOnlineStateExtension.fromJson(value)));
}

Future<void> subscribePeersOnlineStatus(List<String> peerIds) {
Expand All @@ -180,9 +188,10 @@ class AgoraRtmClient {
return _callNative("unsubscribePeersOnlineStatus", {'peerIds': peerIds});
}

Future<List<String>> queryPeersBySubscriptionOption(int option) async {
Future<List<String>> queryPeersBySubscriptionOption(
RtmPeerSubscriptionOption option) async {
return List<String>.from(await _callNative(
"queryPeersBySubscriptionOption", {'option': option}));
"queryPeersBySubscriptionOption", {'option': option.toJson()}));
}

/// Renews the token.
Expand Down Expand Up @@ -312,8 +321,8 @@ class AgoraRtmClient {
return _callNative("setLogFile", {'filePath': filePath});
}

Future<void> setLogFilter(int filter) {
return _callNative("setLogFilter", {'filter': filter});
Future<void> setLogFilter(RtmLogFilter filter) {
return _callNative("setLogFilter", {'filter': filter.toJson()});
}

Future<void> setLogFileSize(int fileSizeInKBytes) {
Expand All @@ -325,13 +334,18 @@ class AgoraRtmClient {
return await AgoraRtmPlugin.callMethodForStatic("getSdkVersion", null);
}

static Future<void> setRtmServiceContext(RtmServiceContext context) async {
return await AgoraRtmPlugin.callMethodForStatic(
"setRtmServiceContext", {'context': context.toJson()});
}

/// [setLogFile]
/// [setLogFilter]
/// [setLogFileSize]
@Deprecated('Use `setLogFile` `setLogFilter` `setLogFileSize` instead of.')
Future<void> setLog(int level, int size, String path) async {
await setLogFile(path);
await setLogFilter(level);
await setLogFilter(RtmLogFilterExtension.fromJson(level));
await setLogFileSize(size);
}

Expand All @@ -341,6 +355,17 @@ class AgoraRtmClient {
await _channels[channelId]?.release();
}

/// [onConnectionStateChanged2]
@Deprecated('Use onConnectionStateChanged2 instead of.')
set onConnectionStateChanged(
Function(int state, int reason)? onConnectionStateChanged) {
onConnectionStateChanged2 = onConnectionStateChanged == null
? null
: (RtmConnectionState state, RtmConnectionChangeReason reason) {
onConnectionStateChanged(state.toJson(), reason.toJson());
};
}

/// [AgoraRtmCallManager.onLocalInvitationReceivedByPeer]
@Deprecated(
'Use AgoraRtmCallManager.onLocalInvitationReceivedByPeer instead of.')
Expand Down
Loading

0 comments on commit c86657d

Please sign in to comment.