diff --git a/android/src/main/java/io/github/v7lin/weibo_kit/WeiboKitPlugin.java b/android/src/main/java/io/github/v7lin/weibo_kit/WeiboKitPlugin.java index 912aa29..8a34ea7 100644 --- a/android/src/main/java/io/github/v7lin/weibo_kit/WeiboKitPlugin.java +++ b/android/src/main/java/io/github/v7lin/weibo_kit/WeiboKitPlugin.java @@ -177,9 +177,9 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis @Override public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { if (METHOD_REGISTERAPP.equals(call.method)) { - String appKey = call.argument(ARGUMENT_KEY_APPKEY); - String scope = call.argument(ARGUMENT_KEY_SCOPE); - String redirectUrl = call.argument(ARGUMENT_KEY_REDIRECTURL); + final String appKey = call.argument(ARGUMENT_KEY_APPKEY); + final String scope = call.argument(ARGUMENT_KEY_SCOPE); + final String redirectUrl = call.argument(ARGUMENT_KEY_REDIRECTURL); iwbapi = WBAPIFactory.createWBAPI(activityPluginBinding.getActivity()); iwbapi.registerApp(applicationContext, new AuthInfo(applicationContext, appKey, redirectUrl, scope)); result.success(null); @@ -202,13 +202,13 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis iwbapi.authorize(new WbAuthListener() { @Override public void onComplete(Oauth2AccessToken token) { - Map map = new HashMap<>(); + final Map map = new HashMap<>(); if (token.isSessionValid()) { map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.SUCCESS); map.put(ARGUMENT_KEY_RESULT_USERID, token.getUid()); map.put(ARGUMENT_KEY_RESULT_ACCESSTOKEN, token.getAccessToken()); map.put(ARGUMENT_KEY_RESULT_REFRESHTOKEN, token.getRefreshToken()); - long expiresIn = (long) Math.ceil((token.getExpiresTime() - System.currentTimeMillis()) / 1000.0); + final long expiresIn = (long) Math.ceil((token.getExpiresTime() - System.currentTimeMillis()) / 1000.0); map.put(ARGUMENT_KEY_RESULT_EXPIRESIN, expiresIn);// 向上取整 } else { map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.UNKNOWN); @@ -220,7 +220,7 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis @Override public void onError(UiError uiError) { - Map map = new HashMap<>(); + final Map map = new HashMap<>(); map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.UNKNOWN); if (channel != null) { channel.invokeMethod(METHOD_ONAUTHRESP, map); @@ -229,7 +229,7 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis @Override public void onCancel() { - Map map = new HashMap<>(); + final Map map = new HashMap<>(); map.put(ARGUMENT_KEY_RESULT_ERRORCODE, WeiboErrorCode.USERCANCEL); if (channel != null) { channel.invokeMethod(METHOD_ONAUTHRESP, map); @@ -241,9 +241,9 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis } private void handleShareTextCall(@NonNull MethodCall call, @NonNull Result result) { - WeiboMultiMessage message = new WeiboMultiMessage(); + final WeiboMultiMessage message = new WeiboMultiMessage(); - TextObject object = new TextObject(); + final TextObject object = new TextObject(); object.text = call.argument(ARGUMENT_KEY_TEXT);// 1024 message.textObject = object; @@ -255,17 +255,17 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis } private void handleShareMediaCall(@NonNull MethodCall call, @NonNull Result result) { - WeiboMultiMessage message = new WeiboMultiMessage(); + final WeiboMultiMessage message = new WeiboMultiMessage(); if (METHOD_SHAREIMAGE.equals(call.method)) { if (call.hasArgument(ARGUMENT_KEY_TEXT)) { - TextObject object = new TextObject(); + final TextObject object = new TextObject(); object.text = call.argument(ARGUMENT_KEY_TEXT);// 1024 message.textObject = object; } - ImageObject object = new ImageObject(); + final ImageObject object = new ImageObject(); if (call.hasArgument(ARGUMENT_KEY_IMAGEDATA)) { object.imageData = call.argument(ARGUMENT_KEY_IMAGEDATA);// 2 * 1024 * 1024 } else if (call.hasArgument(ARGUMENT_KEY_IMAGEURI)) { @@ -275,7 +275,7 @@ public class WeiboKitPlugin implements FlutterPlugin, ActivityAware, PluginRegis message.mediaObject = object; } else if (METHOD_SHAREWEBPAGE.equals(call.method)) { - WebpageObject object = new WebpageObject(); + final WebpageObject object = new WebpageObject(); object.identify = UUID.randomUUID().toString(); object.title = call.argument(ARGUMENT_KEY_TITLE);// 512 object.description = call.argument(ARGUMENT_KEY_DESCRIPTION);// 1024 diff --git a/lib/src/model/resp.dart b/lib/src/model/resp.dart new file mode 100644 index 0000000..66c40b9 --- /dev/null +++ b/lib/src/model/resp.dart @@ -0,0 +1,95 @@ +import 'dart:convert'; + +import 'package:json_annotation/json_annotation.dart'; + +part 'resp.g.dart'; + +abstract class BaseResp { + const BaseResp({ + required this.errorCode, + this.errorMessage, + }); + + /// 成功 + static const int SUCCESS = 0; + + /// 用户取消发送 + static const int USERCANCEL = -1; + + /// 发送失败 + static const int SENT_FAIL = -2; + + /// 授权失败 + static const int AUTH_DENY = -3; + + /// 用户取消安装微博客户端 + static const int USERCANCEL_INSTALL = -4; + + /// 支付失败 + static const int PAY_FAIL = -5; + + /// 分享失败 详情见response UserInfo + static const int SHARE_IN_SDK_FAILED = -8; + + /// 不支持的请求 + static const int UNSUPPORT = -99; + + /// 未知 + static const int UNKNOWN = -100; + + @JsonKey( + defaultValue: SUCCESS, + ) + final int errorCode; + final String? errorMessage; + + bool get isSuccessful => errorCode == SUCCESS; + + bool get isCancelled => errorCode == USERCANCEL; + + Map toJson(); + + @override + String toString() => const JsonEncoder.withIndent(' ').convert(toJson()); +} + +@JsonSerializable( + explicitToJson: true, +) +class AuthResp extends BaseResp { + AuthResp({ + required int errorCode, + String? errorMessage, + this.userId, + this.accessToken, + this.refreshToken, + this.expiresIn, + }) : super(errorCode: errorCode, errorMessage: errorMessage); + + factory AuthResp.fromJson(Map json) => + _$AuthRespFromJson(json); + + final String? userId; + final String? accessToken; + final String? refreshToken; + final int? expiresIn; + + @override + Map toJson() => _$AuthRespToJson(this); +} + +@JsonSerializable( + explicitToJson: true, +) +class ShareMsgResp extends BaseResp { + const ShareMsgResp({ + required int errorCode, + String? errorMessage, + }) : super(errorCode: errorCode, errorMessage: errorMessage); + + factory ShareMsgResp.fromJson(Map json) => + _$ShareMsgRespFromJson(json); + + @override + Map toJson() => _$ShareMsgRespToJson(this); +} diff --git a/lib/src/model/weibo_auth_resp.g.dart b/lib/src/model/resp.g.dart similarity index 59% rename from lib/src/model/weibo_auth_resp.g.dart rename to lib/src/model/resp.g.dart index c20b88a..b6a0147 100644 --- a/lib/src/model/weibo_auth_resp.g.dart +++ b/lib/src/model/resp.g.dart @@ -1,13 +1,13 @@ // GENERATED CODE - DO NOT MODIFY BY HAND -part of 'weibo_auth_resp.dart'; +part of 'resp.dart'; // ************************************************************************** // JsonSerializableGenerator // ************************************************************************** -WeiboAuthResp _$WeiboAuthRespFromJson(Map json) { - return WeiboAuthResp( +AuthResp _$AuthRespFromJson(Map json) { + return AuthResp( errorCode: json['errorCode'] as int? ?? 0, errorMessage: json['errorMessage'] as String?, userId: json['userId'] as String?, @@ -17,8 +17,7 @@ WeiboAuthResp _$WeiboAuthRespFromJson(Map json) { ); } -Map _$WeiboAuthRespToJson(WeiboAuthResp instance) => - { +Map _$AuthRespToJson(AuthResp instance) => { 'errorCode': instance.errorCode, 'errorMessage': instance.errorMessage, 'userId': instance.userId, @@ -26,3 +25,16 @@ Map _$WeiboAuthRespToJson(WeiboAuthResp instance) => 'refreshToken': instance.refreshToken, 'expiresIn': instance.expiresIn, }; + +ShareMsgResp _$ShareMsgRespFromJson(Map json) { + return ShareMsgResp( + errorCode: json['errorCode'] as int? ?? 0, + errorMessage: json['errorMessage'] as String?, + ); +} + +Map _$ShareMsgRespToJson(ShareMsgResp instance) => + { + 'errorCode': instance.errorCode, + 'errorMessage': instance.errorMessage, + }; diff --git a/lib/src/model/weibo_auth_resp.dart b/lib/src/model/weibo_auth_resp.dart deleted file mode 100644 index a907a11..0000000 --- a/lib/src/model/weibo_auth_resp.dart +++ /dev/null @@ -1,29 +0,0 @@ -import 'package:json_annotation/json_annotation.dart'; -import 'package:weibo_kit/src/model/weibo_sdk_resp.dart'; - -part 'weibo_auth_resp.g.dart'; - -@JsonSerializable( - explicitToJson: true, -) -class WeiboAuthResp extends WeiboSdkResp { - WeiboAuthResp({ - required int errorCode, - String? errorMessage, - this.userId, - this.accessToken, - this.refreshToken, - this.expiresIn, - }) : super(errorCode: errorCode, errorMessage: errorMessage); - - factory WeiboAuthResp.fromJson(Map json) => - _$WeiboAuthRespFromJson(json); - - final String? userId; - final String? accessToken; - final String? refreshToken; - final int? expiresIn; - - @override - Map toJson() => _$WeiboAuthRespToJson(this); -} diff --git a/lib/src/model/weibo_sdk_resp.dart b/lib/src/model/weibo_sdk_resp.dart deleted file mode 100644 index c8b5b6f..0000000 --- a/lib/src/model/weibo_sdk_resp.dart +++ /dev/null @@ -1,55 +0,0 @@ -import 'package:json_annotation/json_annotation.dart'; - -part 'weibo_sdk_resp.g.dart'; - -@JsonSerializable( - explicitToJson: true, -) -class WeiboSdkResp { - const WeiboSdkResp({ - required this.errorCode, - this.errorMessage, - }); - - factory WeiboSdkResp.fromJson(Map json) => - _$WeiboSdkRespFromJson(json); - - /// 成功 - static const int SUCCESS = 0; - - /// 用户取消发送 - static const int USERCANCEL = -1; - - /// 发送失败 - static const int SENT_FAIL = -2; - - /// 授权失败 - static const int AUTH_DENY = -3; - - /// 用户取消安装微博客户端 - static const int USERCANCEL_INSTALL = -4; - - /// 支付失败 - static const int PAY_FAIL = -5; - - /// 分享失败 详情见response UserInfo - static const int SHARE_IN_SDK_FAILED = -8; - - /// 不支持的请求 - static const int UNSUPPORT = -99; - - /// 未知 - static const int UNKNOWN = -100; - - @JsonKey( - defaultValue: SUCCESS, - ) - final int errorCode; - final String? errorMessage; - - bool get isSuccessful => errorCode == SUCCESS; - - bool get isCancelled => errorCode == USERCANCEL; - - Map toJson() => _$WeiboSdkRespToJson(this); -} diff --git a/lib/src/model/weibo_sdk_resp.g.dart b/lib/src/model/weibo_sdk_resp.g.dart deleted file mode 100644 index bfe939c..0000000 --- a/lib/src/model/weibo_sdk_resp.g.dart +++ /dev/null @@ -1,20 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'weibo_sdk_resp.dart'; - -// ************************************************************************** -// JsonSerializableGenerator -// ************************************************************************** - -WeiboSdkResp _$WeiboSdkRespFromJson(Map json) { - return WeiboSdkResp( - errorCode: json['errorCode'] as int? ?? 0, - errorMessage: json['errorMessage'] as String?, - ); -} - -Map _$WeiboSdkRespToJson(WeiboSdkResp instance) => - { - 'errorCode': instance.errorCode, - 'errorMessage': instance.errorMessage, - }; diff --git a/lib/src/weibo.dart b/lib/src/weibo.dart index c1bcd87..e3bb6f3 100644 --- a/lib/src/weibo.dart +++ b/lib/src/weibo.dart @@ -3,8 +3,7 @@ import 'dart:io'; import 'dart:typed_data'; import 'package:flutter/services.dart'; -import 'package:weibo_kit/src/model/weibo_auth_resp.dart'; -import 'package:weibo_kit/src/model/weibo_sdk_resp.dart'; +import 'package:weibo_kit/src/model/resp.dart'; class Weibo { /// @@ -45,11 +44,8 @@ class Weibo { const MethodChannel('v7lin.github.io/weibo_kit') ..setMethodCallHandler(_handleMethod); - final StreamController _authRespStreamController = - StreamController.broadcast(); - - final StreamController _shareMsgRespStreamController = - StreamController.broadcast(); + final StreamController _respStreamController = + StreamController.broadcast(); Future registerApp({ required String appKey, @@ -73,24 +69,19 @@ class Weibo { Future _handleMethod(MethodCall call) async { switch (call.method) { case _METHOD_ONAUTHRESP: - _authRespStreamController.add(WeiboAuthResp.fromJson( + _respStreamController.add(AuthResp.fromJson( (call.arguments as Map).cast())); break; case _METHOD_ONSHAREMSGRESP: - _shareMsgRespStreamController.add(WeiboSdkResp.fromJson( + _respStreamController.add(ShareMsgResp.fromJson( (call.arguments as Map).cast())); break; } } - /// 登录 - Stream authResp() { - return _authRespStreamController.stream; - } - - /// 分享 - Stream shareMsgResp() { - return _shareMsgRespStreamController.stream; + /// + Stream respStream() { + return _respStreamController.stream; } Future isInstalled() async { diff --git a/lib/weibo_kit.dart b/lib/weibo_kit.dart index b0aae97..a2ef444 100644 --- a/lib/weibo_kit.dart +++ b/lib/weibo_kit.dart @@ -1,6 +1,5 @@ library weibo_kit; -export 'src/model/weibo_auth_resp.dart'; -export 'src/model/weibo_sdk_resp.dart'; +export 'src/model/resp.dart'; export 'src/weibo.dart'; export 'src/weibo_constant.dart'; diff --git a/test/weibo_kit_test.dart b/test/weibo_kit_test.dart index 6682d95..36b149d 100644 --- a/test/weibo_kit_test.dart +++ b/test/weibo_kit_test.dart @@ -54,21 +54,24 @@ void main() { }); test('auth', () async { - final StreamSubscription sub = - Weibo.instance.authResp().listen((WeiboAuthResp resp) { - expect(resp.errorCode, WeiboSdkResp.USERCANCEL); + final StreamSubscription subs = + Weibo.instance.respStream().listen((BaseResp resp) { + expect(resp.runtimeType, AuthResp); + expect(resp.errorCode, BaseResp.USERCANCEL); }); await Weibo.instance.auth( appKey: 'your weibo app key', scope: [WeiboScope.ALL], ); - await sub.cancel(); + await Future.delayed(const Duration(seconds: 1)); + await subs.cancel(); }); test('share', () async { - final StreamSubscription sub = - Weibo.instance.shareMsgResp().listen((WeiboSdkResp resp) { - expect(resp.errorCode, WeiboSdkResp.USERCANCEL); + final StreamSubscription sub = + Weibo.instance.respStream().listen((BaseResp resp) { + expect(resp.runtimeType, ShareMsgResp); + expect(resp.errorCode, BaseResp.USERCANCEL); }); await Weibo.instance.shareText( text: 'share text',