import 'dart:async'; import 'dart:io'; import 'dart:typed_data'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:weibo_kit/src/model/resp.dart'; import 'package:weibo_kit/src/weibo_constant.dart'; import 'package:weibo_kit/src/weibo_kit_platform_interface.dart'; /// An implementation of [WeiboKitPlatform] that uses method channels. class MethodChannelWeiboKit extends WeiboKitPlatform { /// The method channel used to interact with the native platform. @visibleForTesting late final MethodChannel methodChannel = const MethodChannel('v7lin.github.io/weibo_kit')..setMethodCallHandler(_handleMethod); final StreamController _respStreamController = StreamController.broadcast(); Future _handleMethod(MethodCall call) async { switch (call.method) { case 'onAuthResp': _respStreamController.add(AuthResp.fromJson((call.arguments as Map).cast())); break; case 'onShareMsgResp': _respStreamController.add(ShareMsgResp.fromJson((call.arguments as Map).cast())); break; } } @override Future registerApp({ required String appKey, required String? universalLink, required List scope, String redirectUrl = WeiboRegister.DEFAULT_REDIRECTURL, }) { assert(!Platform.isIOS || (universalLink?.isNotEmpty ?? false)); return methodChannel.invokeMethod( 'registerApp', { 'appKey': appKey, 'universalLink': universalLink, 'scope': scope.join(','), 'redirectUrl': redirectUrl, }, ); } @override Stream respStream() { return _respStreamController.stream; } @override Future isInstalled() async { return await methodChannel.invokeMethod('isInstalled') ?? false; } @override Future isSupportMultipleImage() async { return await methodChannel.invokeMethod('isSupportMultipleImage') ?? false; } @override Future auth({ required String appKey, required List scope, String redirectUrl = WeiboRegister.DEFAULT_REDIRECTURL, }) { return methodChannel.invokeMethod( 'auth', { 'appKey': appKey, 'scope': scope.join(','), 'redirectUrl': redirectUrl, }, ); } @override Future shareText({ required String text, bool clientOnly = false, }) { return methodChannel.invokeMethod( 'shareText', { 'text': text, 'clientOnly': clientOnly, }, ); } @override Future shareImage({ String? text, Uint8List? imageData, Uri? imageUri, bool clientOnly = false, }) { assert(text == null || text.length <= 1024); assert((imageData != null && imageData.lengthInBytes <= 2 * 1024 * 1024) || (imageUri != null && imageUri.isScheme('file') && imageUri.toFilePath().length <= 512 && File.fromUri(imageUri).lengthSync() <= 10 * 1024 * 1024)); return methodChannel.invokeMethod( 'shareImage', { if (text != null && text.isNotEmpty) 'text': text, if (imageData != null) 'imageData': imageData, if (imageUri != null) 'imageUri': imageUri.toString(), 'clientOnly': clientOnly, }, ); } @override Future shareMultiImage({ String? text, required List imageUris, bool clientOnly = false, }) { assert(text == null || text.length <= 1024); assert(imageUris.isNotEmpty && imageUris.every((Uri element) => element.isScheme('file'))); return methodChannel.invokeMethod( 'shareMultiImage', { if (text != null && text.isNotEmpty) 'text': text, 'imageUris': imageUris.map((Uri element) => element.toString()).toList(), 'clientOnly': clientOnly, }, ); } @override Future shareVideo({ String? text, required Uri videoUri, bool clientOnly = false, }) { assert(text == null || text.length <= 1024); assert(videoUri.isScheme('file')); return methodChannel.invokeMethod( 'shareVideo', { if (text != null && text.isNotEmpty) 'text': text, 'videoUri': videoUri.toString(), 'clientOnly': clientOnly, }, ); } @override Future shareWebpage({ required String title, required String description, required Uint8List thumbData, required String webpageUrl, bool clientOnly = false, }) { assert(title.length <= 512); assert(description.isNotEmpty && description.length <= 1024); assert(thumbData.lengthInBytes <= 32 * 1024); assert(webpageUrl.length <= 255); return methodChannel.invokeMethod( 'shareWebpage', { 'title': title, 'description': description, 'thumbData': thumbData, 'webpageUrl': webpageUrl, 'clientOnly': clientOnly, }, ); } }