import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:tencent_kit/src/constant.dart'; import 'package:tencent_kit/src/model/resp.dart'; import 'package:tencent_kit/src/tencent_kit_platform_interface.dart'; /// An implementation of [TencentKitPlatform] that uses method channels. class MethodChannelTencentKit extends TencentKitPlatform { /// The method channel used to interact with the native platform. @visibleForTesting late final MethodChannel methodChannel = const MethodChannel('v7lin.github.io/tencent_kit') ..setMethodCallHandler(_handleMethod); final StreamController _respStreamController = StreamController.broadcast(); Future _handleMethod(MethodCall call) async { switch (call.method) { case 'onLoginResp': _respStreamController.add(TencentLoginResp.fromJson( (call.arguments as Map).cast())); break; case 'onShareResp': _respStreamController.add(TencentShareMsgResp.fromJson( (call.arguments as Map).cast())); break; } } @override Future setIsPermissionGranted({ required bool granted, String? buildModel /* android.os.Build.MODEL */, }) { return methodChannel.invokeMethod( 'setIsPermissionGranted', { 'granted': granted, if (buildModel?.isNotEmpty ?? false) 'build_model': buildModel, }, ); } @override Future registerApp({ required String appId, String? universalLink, }) { return methodChannel.invokeMethod( 'registerApp', { 'appId': appId, if (universalLink?.isNotEmpty ?? false) 'universalLink': universalLink, }, ); } @override Stream respStream() { return _respStreamController.stream; } @override Future isQQInstalled() async { return await methodChannel.invokeMethod('isQQInstalled') ?? false; } @override Future isTIMInstalled() async { return await methodChannel.invokeMethod('isTIMInstalled') ?? false; } @override Future login({ required List scope, }) { return methodChannel.invokeMethod( 'login', { 'scope': scope.join(','), }, ); } @override Future logout() { return methodChannel.invokeMethod('logout'); } @override Future shareMood({ required int scene, String? summary, List? imageUris, Uri? videoUri, }) { assert(scene == TencentScene.SCENE_QZONE); assert((summary?.isNotEmpty ?? false) || ((imageUris?.isNotEmpty ?? false) && imageUris!.every((Uri element) => element.isScheme('file'))) || (videoUri != null && videoUri.isScheme('file'))); return methodChannel.invokeMethod( 'shareMood', { 'scene': scene, if (summary?.isNotEmpty ?? false) 'summary': summary, if (imageUris?.isNotEmpty ?? false) 'imageUris': imageUris!.map((Uri imageUri) => imageUri.toString()).toList(), if (videoUri != null) 'videoUri': videoUri.toString(), }, ); } @override Future shareText({ required int scene, required String summary, }) { assert(scene == TencentScene.SCENE_QQ); return methodChannel.invokeMethod( 'shareText', { 'scene': scene, 'summary': summary, }, ); } @override Future shareImage({ required int scene, required Uri imageUri, String? appName, int extInt = TencentQZoneFlag.DEFAULT, }) { assert(scene == TencentScene.SCENE_QQ); assert(imageUri.isScheme('file')); return methodChannel.invokeMethod( 'shareImage', { 'scene': scene, 'imageUri': imageUri.toString(), if (appName?.isNotEmpty ?? false) 'appName': appName, 'extInt': extInt, }, ); } @override Future shareMusic({ required int scene, required String title, String? summary, Uri? imageUri, required String musicUrl, required String targetUrl, String? appName, int extInt = TencentQZoneFlag.DEFAULT, }) { assert(scene == TencentScene.SCENE_QQ); return methodChannel.invokeMethod( 'shareMusic', { 'scene': scene, 'title': title, if (summary?.isNotEmpty ?? false) 'summary': summary, if (imageUri != null) 'imageUri': imageUri.toString(), 'musicUrl': musicUrl, 'targetUrl': targetUrl, if (appName?.isNotEmpty ?? false) 'appName': appName, 'extInt': extInt, }, ); } @override Future shareWebpage({ required int scene, required String title, String? summary, Uri? imageUri, required String targetUrl, String? appName, int extInt = TencentQZoneFlag.DEFAULT, }) { return methodChannel.invokeMethod( 'shareWebpage', { 'scene': scene, 'title': title, if (summary?.isNotEmpty ?? false) 'summary': summary, if (imageUri != null) 'imageUri': imageUri.toString(), 'targetUrl': targetUrl, if (appName?.isNotEmpty ?? false) 'appName': appName, 'extInt': extInt, }, ); } }