This commit is contained in:
v7lin 2022-07-26 11:30:19 +08:00
parent f306734e7b
commit 7742eefd28
3 changed files with 69 additions and 33 deletions

View File

@ -95,7 +95,8 @@ class _HomeState extends State<Home> {
ListTile( ListTile(
title: Text('环境检查'), title: Text('环境检查'),
onTap: () async { onTap: () async {
final String content = 'weibo: ${await Weibo.instance.isInstalled()}'; final String content =
'weibo: ${await Weibo.instance.isInstalled()}';
_showTips('环境检查', content); _showTips('环境检查', content);
}, },
), ),
@ -112,15 +113,18 @@ class _HomeState extends State<Home> {
title: Text('用户信息'), title: Text('用户信息'),
onTap: () async { onTap: () async {
if (_authResp?.isSuccessful ?? false) { if (_authResp?.isSuccessful ?? false) {
final WeiboUserInfoResp userInfoResp = await WeiboApi.getUserInfo( final WeiboUserInfoResp userInfoResp =
await WeiboApi.getUserInfo(
appkey: _WEIBO_APP_KEY, appkey: _WEIBO_APP_KEY,
userId: _authResp!.userId!, userId: _authResp!.userId!,
accessToken: _authResp!.accessToken!, accessToken: _authResp!.accessToken!,
); );
if (userInfoResp.isSuccessful) { if (userInfoResp.isSuccessful) {
_showTips('用户信息', '${userInfoResp.screenName}\n${userInfoResp.description}\n${userInfoResp.location}\n${userInfoResp.profileImageUrl}'); _showTips('用户信息',
'${userInfoResp.screenName}\n${userInfoResp.description}\n${userInfoResp.location}\n${userInfoResp.profileImageUrl}');
} else { } else {
_showTips('用户信息', '获取用户信息失败\n${userInfoResp.errorCode}:${userInfoResp.error}'); _showTips('用户信息',
'获取用户信息失败\n${userInfoResp.errorCode}:${userInfoResp.error}');
} }
} }
}, },
@ -136,15 +140,19 @@ class _HomeState extends State<Home> {
ListTile( ListTile(
title: Text('图片分享'), title: Text('图片分享'),
onTap: () async { onTap: () async {
File file = await DefaultCacheManager().getSingleFile('https://www.baidu.com/img/bd_logo1.png?where=super'); File file = await DefaultCacheManager().getSingleFile(
'https://www.baidu.com/img/bd_logo1.png?where=super');
if (Platform.isAndroid) { if (Platform.isAndroid) {
// Context.getExternalFilesDir(null)/Context.getExternalCacheDirs(null) // Context.getExternalFilesDir(null)/Context.getExternalCacheDirs(null)
// path_provider.getExternalCacheDirectories(); // path_provider.getExternalCacheDirectories();
// path_provider.getExternalStorageDirectory(); // path_provider.getExternalStorageDirectory();
final Directory temporaryDir = await path_provider.getTemporaryDirectory(); final Directory temporaryDir =
await path_provider.getTemporaryDirectory();
if (path.isWithin(temporaryDir.parent.path, file.path)) { if (path.isWithin(temporaryDir.parent.path, file.path)) {
// //
final File copyFile = File(path.join((await path_provider.getExternalStorageDirectory())!.path, path.basename(file.path))); final File copyFile = File(path.join(
(await path_provider.getExternalStorageDirectory())!.path,
path.basename(file.path)));
if (copyFile.existsSync()) { if (copyFile.existsSync()) {
await copyFile.delete(); await copyFile.delete();
} }
@ -161,11 +169,14 @@ class _HomeState extends State<Home> {
ListTile( ListTile(
title: Text('网页分享'), title: Text('网页分享'),
onTap: () async { onTap: () async {
final File file = await DefaultCacheManager().getSingleFile('https://www.baidu.com/img/bd_logo1.png?where=super'); final File file = await DefaultCacheManager().getSingleFile(
final imglib.Image thumbnail = imglib.decodeImage(file.readAsBytesSync())!; 'https://www.baidu.com/img/bd_logo1.png?where=super');
final imglib.Image thumbnail =
imglib.decodeImage(file.readAsBytesSync())!;
Uint8List thumbData = thumbnail.getBytes(); Uint8List thumbData = thumbnail.getBytes();
if (thumbData.length > 32 * 1024) { if (thumbData.length > 32 * 1024) {
thumbData = Uint8List.fromList(imglib.encodeJpg(thumbnail, quality: 100 * 32 * 1024 ~/ thumbData.length)); thumbData = Uint8List.fromList(imglib.encodeJpg(thumbnail,
quality: 100 * 32 * 1024 ~/ thumbData.length));
} }
await Weibo.instance.shareWebpage( await Weibo.instance.shareWebpage(
title: 'title', title: 'title',

View File

@ -12,16 +12,21 @@ import 'package:weibo_kit/src/weibo_kit_platform_interface.dart';
class MethodChannelWeiboKit extends WeiboKitPlatform { class MethodChannelWeiboKit extends WeiboKitPlatform {
/// The method channel used to interact with the native platform. /// The method channel used to interact with the native platform.
@visibleForTesting @visibleForTesting
late final MethodChannel methodChannel = const MethodChannel('v7lin.github.io/weibo_kit')..setMethodCallHandler(_handleMethod); late final MethodChannel methodChannel =
final StreamController<BaseResp> _respStreamController = StreamController<BaseResp>.broadcast(); const MethodChannel('v7lin.github.io/weibo_kit')
..setMethodCallHandler(_handleMethod);
final StreamController<BaseResp> _respStreamController =
StreamController<BaseResp>.broadcast();
Future<dynamic> _handleMethod(MethodCall call) async { Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) { switch (call.method) {
case 'onAuthResp': case 'onAuthResp':
_respStreamController.add(AuthResp.fromJson((call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>())); _respStreamController.add(AuthResp.fromJson(
(call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
break; break;
case 'onShareMsgResp': case 'onShareMsgResp':
_respStreamController.add(ShareMsgResp.fromJson((call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>())); _respStreamController.add(ShareMsgResp.fromJson(
(call.arguments as Map<dynamic, dynamic>).cast<String, dynamic>()));
break; break;
} }
} }
@ -57,7 +62,8 @@ class MethodChannelWeiboKit extends WeiboKitPlatform {
@override @override
Future<bool> isSupportMultipleImage() async { Future<bool> isSupportMultipleImage() async {
return await methodChannel.invokeMethod<bool>('isSupportMultipleImage') ?? false; return await methodChannel.invokeMethod<bool>('isSupportMultipleImage') ??
false;
} }
@override @override
@ -99,7 +105,10 @@ class MethodChannelWeiboKit extends WeiboKitPlatform {
}) { }) {
assert(text == null || text.length <= 1024); assert(text == null || text.length <= 1024);
assert((imageData != null && imageData.lengthInBytes <= 2 * 1024 * 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)); (imageUri != null &&
imageUri.isScheme('file') &&
imageUri.toFilePath().length <= 512 &&
File.fromUri(imageUri).lengthSync() <= 10 * 1024 * 1024));
return methodChannel.invokeMethod<void>( return methodChannel.invokeMethod<void>(
'shareImage', 'shareImage',
<String, dynamic>{ <String, dynamic>{
@ -118,12 +127,18 @@ class MethodChannelWeiboKit extends WeiboKitPlatform {
bool clientOnly = false, bool clientOnly = false,
}) { }) {
assert(text == null || text.length <= 1024); assert(text == null || text.length <= 1024);
assert(imageUris.isNotEmpty && imageUris.every((Uri element) => element.isScheme('file')) && imageUris.map((Uri element) => File.fromUri(element).lengthSync()).reduce((int value, int element) => value + element) <= 30 * 1024 * 1024); assert(imageUris.isNotEmpty &&
imageUris.every((Uri element) => element.isScheme('file')) &&
imageUris
.map((Uri element) => File.fromUri(element).lengthSync())
.reduce((int value, int element) => value + element) <=
30 * 1024 * 1024);
return methodChannel.invokeMethod<void>( return methodChannel.invokeMethod<void>(
'shareMultiImage', 'shareMultiImage',
<String, dynamic>{ <String, dynamic>{
if (text != null && text.isNotEmpty) 'text': text, if (text != null && text.isNotEmpty) 'text': text,
'imageUris': imageUris.map((Uri element) => element.toString()).toList(), 'imageUris':
imageUris.map((Uri element) => element.toString()).toList(),
'clientOnly': clientOnly, 'clientOnly': clientOnly,
}, },
); );
@ -136,7 +151,8 @@ class MethodChannelWeiboKit extends WeiboKitPlatform {
bool clientOnly = false, bool clientOnly = false,
}) { }) {
assert(text == null || text.length <= 1024); assert(text == null || text.length <= 1024);
assert(videoUri.isScheme('file') && File.fromUri(videoUri).lengthSync() <= 50 * 1024 * 1024); assert(videoUri.isScheme('file') &&
File.fromUri(videoUri).lengthSync() <= 50 * 1024 * 1024);
return methodChannel.invokeMethod<void>( return methodChannel.invokeMethod<void>(
'shareVideo', 'shareVideo',
<String, dynamic>{ <String, dynamic>{

View File

@ -31,9 +31,11 @@ abstract class WeiboKitPlatform extends PlatformInterface {
required String appKey, required String appKey,
required String? universalLink, required String? universalLink,
required List<String> scope, required List<String> scope,
String redirectUrl = WeiboRegister.DEFAULT_REDIRECTURL, // -> -> -> -> OAuth2.0 String redirectUrl = WeiboRegister
.DEFAULT_REDIRECTURL, // -> -> -> -> OAuth2.0
}) { }) {
throw UnimplementedError('registerApp({required appKey, required universalLink, required scope, redirectUrl}) has not been implemented.'); throw UnimplementedError(
'registerApp({required appKey, required universalLink, required scope, redirectUrl}) has not been implemented.');
} }
/// ///
@ -48,7 +50,8 @@ abstract class WeiboKitPlatform extends PlatformInterface {
/// ///
Future<bool> isSupportMultipleImage() { Future<bool> isSupportMultipleImage() {
throw UnimplementedError('isSupportMultipleImage() has not been implemented.'); throw UnimplementedError(
'isSupportMultipleImage() has not been implemented.');
} }
/// ///
@ -57,15 +60,17 @@ abstract class WeiboKitPlatform extends PlatformInterface {
required List<String> scope, required List<String> scope,
String redirectUrl = WeiboRegister.DEFAULT_REDIRECTURL, String redirectUrl = WeiboRegister.DEFAULT_REDIRECTURL,
}) { }) {
throw UnimplementedError('auth({required appKey, required scope, redirectUrl}) has not been implemented.'); throw UnimplementedError(
'auth({required appKey, required scope, redirectUrl}) has not been implemented.');
} }
/// - /// -
Future<void> shareText({ Future<void> shareText({
required String text, required String text,
bool clientOnly = false/* Android Only */, bool clientOnly = false /* Android Only */,
}) { }) {
throw UnimplementedError('shareText({required text, clientOnly}) has not been implemented.'); throw UnimplementedError(
'shareText({required text, clientOnly}) has not been implemented.');
} }
/// - /// -
@ -74,27 +79,30 @@ abstract class WeiboKitPlatform extends PlatformInterface {
String? text, String? text,
Uint8List? imageData, Uint8List? imageData,
Uri? imageUri, Uri? imageUri,
bool clientOnly = false/* Android Only */, bool clientOnly = false /* Android Only */,
}) { }) {
throw UnimplementedError('shareImage({text, imageData, imageUri, clientOnly}) has not been implemented.'); throw UnimplementedError(
'shareImage({text, imageData, imageUri, clientOnly}) has not been implemented.');
} }
/// - /// -
Future<void> shareMultiImage({ Future<void> shareMultiImage({
String? text, String? text,
required List<Uri> imageUris, required List<Uri> imageUris,
bool clientOnly = false/* Android Only */, bool clientOnly = false /* Android Only */,
}) { }) {
throw UnimplementedError('shareMultiImage({text, required imageUris, clientOnly}) has not been implemented.'); throw UnimplementedError(
'shareMultiImage({text, required imageUris, clientOnly}) has not been implemented.');
} }
/// - /// -
Future<void> shareVideo({ Future<void> shareVideo({
String? text, String? text,
required Uri videoUri, required Uri videoUri,
bool clientOnly = false/* Android Only */, bool clientOnly = false /* Android Only */,
}) { }) {
throw UnimplementedError('shareVideo({text, required videoUri, clientOnly}) has not been implemented.'); throw UnimplementedError(
'shareVideo({text, required videoUri, clientOnly}) has not been implemented.');
} }
/// - /// -
@ -103,8 +111,9 @@ abstract class WeiboKitPlatform extends PlatformInterface {
required String description, required String description,
required Uint8List thumbData, required Uint8List thumbData,
required String webpageUrl, required String webpageUrl,
bool clientOnly = false/* Android Only */, bool clientOnly = false /* Android Only */,
}) { }) {
throw UnimplementedError('shareWebpage({required title, required description, required thumbData, required webpageUrl, clientOnly}) has not been implemented.'); throw UnimplementedError(
'shareWebpage({required title, required description, required thumbData, required webpageUrl, clientOnly}) has not been implemented.');
} }
} }