Skip to content

Commit 4174128

Browse files
committed
add event bus.
1 parent ca00b25 commit 4174128

File tree

6 files changed

+83
-56
lines changed

6 files changed

+83
-56
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Created by guoshuyu
3+
* Date: 2018-08-16
4+
*/
5+
6+
class HttpErrorEvent {
7+
final int code;
8+
9+
final String message;
10+
11+
HttpErrorEvent(this.code, this.message);
12+
}

lib/common/net/Code.dart

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import 'package:fluttertoast/fluttertoast.dart';
2-
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
3-
1+
import 'package:event_bus/event_bus.dart';
2+
import 'package:gsy_github_app_flutter/common/event/HttpErrorEvent.dart';
43
///错误编码
54
class Code {
65
///网络错误
@@ -14,43 +13,13 @@ class Code {
1413

1514
static const SUCCESS = 200;
1615

16+
static final EventBus eventBus = new EventBus();
17+
1718
static errorHandleFunction(code, message, noTip) {
18-
switch (code) {
19-
case NETWORK_ERROR:
20-
if(!noTip) {
21-
Fluttertoast.showToast(msg: GSYStrings.network_error);
22-
}
23-
return GSYStrings.network_error;
24-
case 401:
25-
//TODO 授权逻辑
26-
/*if (Actions.currentScene !== 'LoginPage') {
27-
Actions.reset("LoginPage");
28-
}*/
29-
if(!noTip) {
30-
Fluttertoast.showToast(msg: GSYStrings.network_error_401);
31-
}
32-
return GSYStrings.network_error_401; //401 Unauthorized
33-
case 403:
34-
if(!noTip) {
35-
Fluttertoast.showToast(msg: GSYStrings.network_error_403);
36-
}
37-
return GSYStrings.network_error_403;
38-
case 404:
39-
if(!noTip) {
40-
Fluttertoast.showToast(msg: GSYStrings.network_error_404);
41-
}
42-
return GSYStrings.network_error_404;
43-
case NETWORK_TIMEOUT:
44-
//超时
45-
if(!noTip) {
46-
Fluttertoast.showToast(msg: GSYStrings.network_error_timeout);
47-
}
48-
return GSYStrings.network_error_timeout;
49-
default:
50-
if(!noTip) {
51-
Fluttertoast.showToast(msg: GSYStrings.network_error_unknown + " " + message);
52-
}
53-
return GSYStrings.network_error_unknown;
19+
if(noTip) {
20+
return message;
5421
}
22+
eventBus.fire(new HttpErrorEvent(code, message));
23+
return message;
5524
}
5625
}

lib/common/style/GSYStyle.dart

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,6 @@ class GSYConstant {
207207
);
208208
}
209209

210-
///字符文本
211-
class GSYStrings {
212-
213-
214-
static const String network_error_401 = "[401错误可能: 未授权 \\ 授权登录失败 \\ 登录过期]";
215-
static const String network_error_403 = "403权限错误";
216-
static const String network_error_404 = "404错误";
217-
static const String network_error_timeout = "请求超时";
218-
static const String network_error_unknown = "其他异常";
219-
static const String network_error = "网络错误";
220-
}
221-
222210
class GSYICons {
223211
static const String FONT_FAMILY = 'wxcIconFont';
224212

lib/main.dart

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/foundation.dart';
24
import 'package:flutter/material.dart';
5+
import 'package:event_bus/event_bus.dart';
36
import 'package:flutter_localizations/flutter_localizations.dart';
7+
import 'package:fluttertoast/fluttertoast.dart';
8+
import 'package:gsy_github_app_flutter/common/event/HttpErrorEvent.dart';
49
import 'package:gsy_github_app_flutter/common/localization/GSYLocalizationsDelegate.dart';
510
import 'package:gsy_github_app_flutter/common/redux/GSYState.dart';
611
import 'package:gsy_github_app_flutter/common/model/User.dart';
712
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
13+
import 'package:gsy_github_app_flutter/common/utils/CommonUtils.dart';
814
import 'package:gsy_github_app_flutter/page/HomePage.dart';
915
import 'package:gsy_github_app_flutter/page/LoginPage.dart';
1016
import 'package:gsy_github_app_flutter/page/WelcomePage.dart';
1117
import 'package:flutter_redux/flutter_redux.dart';
1218
import 'package:redux/redux.dart';
19+
import 'package:gsy_github_app_flutter/common/net/Code.dart';
1320

1421
void main() {
1522
runApp(new FlutterReduxApp());
@@ -45,9 +52,7 @@ class FlutterReduxApp extends StatelessWidget {
4552
GSYLocalizationsDelegate.delegate,
4653
],
4754
locale: store.state.locale,
48-
supportedLocales: [
49-
store.state.locale
50-
],
55+
supportedLocales: [store.state.locale],
5156
theme: store.state.themeData,
5257
routes: {
5358
WelcomePage.sName: (context) {
@@ -82,6 +87,10 @@ class GSYLocalizations extends StatefulWidget {
8287
}
8388

8489
class _GSYLocalizations extends State<GSYLocalizations> {
90+
91+
92+
StreamSubscription stream;
93+
8594
@override
8695
Widget build(BuildContext context) {
8796
return new StoreBuilder<GSYState>(builder: (context, store) {
@@ -92,4 +101,45 @@ class _GSYLocalizations extends State<GSYLocalizations> {
92101
);
93102
});
94103
}
104+
105+
@override
106+
void initState() {
107+
super.initState();
108+
stream = Code.eventBus.on<HttpErrorEvent>().listen((event) {
109+
errorHandleFunction(event.code, event.message);
110+
});
111+
}
112+
113+
@override
114+
void dispose() {
115+
super.dispose();
116+
if(stream != null) {
117+
stream.cancel();
118+
stream = null;
119+
}
120+
}
121+
122+
errorHandleFunction(int code, message) {
123+
switch (code) {
124+
case Code.NETWORK_ERROR:
125+
Fluttertoast.showToast(msg: CommonUtils.getLocale(context).network_error);
126+
break;
127+
case 401:
128+
Fluttertoast.showToast(msg: CommonUtils.getLocale(context).network_error_401);
129+
break;
130+
case 403:
131+
Fluttertoast.showToast(msg: CommonUtils.getLocale(context).network_error_403);
132+
break;
133+
case 404:
134+
Fluttertoast.showToast(msg: CommonUtils.getLocale(context).network_error_404);
135+
break;
136+
case Code.NETWORK_TIMEOUT:
137+
//超时
138+
Fluttertoast.showToast(msg: CommonUtils.getLocale(context).network_error_timeout);
139+
break;
140+
default:
141+
Fluttertoast.showToast(msg: CommonUtils.getLocale(context).network_error_unknown + " " + message);
142+
break;
143+
}
144+
}
95145
}

pubspec.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ packages:
162162
url: "https://pub.flutter-io.cn"
163163
source: hosted
164164
version: "1.0.0"
165+
event_bus:
166+
dependency: "direct main"
167+
description:
168+
name: event_bus
169+
url: "https://pub.flutter-io.cn"
170+
source: hosted
171+
version: "1.0.1"
165172
fixnum:
166173
dependency: transitive
167174
description:

pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ dependencies:
2929
dio: ^1.0.0
3030
flutter_localizations:
3131
sdk: flutter
32-
intl: 0.15.6
33-
intl_translation: 0.16.7
32+
intl: ^0.15.6
33+
intl_translation: ^0.16.7
34+
event_bus: ^1.0.1
3435

3536
dev_dependencies:
3637
build_runner: ^0.7.6

0 commit comments

Comments
 (0)