Skip to content

Commit

Permalink
Revert feature logout
Browse files Browse the repository at this point in the history
  • Loading branch information
abc873693 committed Sep 24, 2019
1 parent 28f797e commit a7c994c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 41 deletions.
6 changes: 6 additions & 0 deletions lib/api/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,10 @@ class Helper {
'Authorization': 'Bearer $token',
};
}

static void clearSetting() {
expireTime = null;
username = null;
password = null;
}
}
9 changes: 9 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_analytics/observer.dart';
Expand All @@ -20,8 +21,10 @@ import 'package:nkust_ap/utils/app_localizations.dart';
import 'package:nkust_ap/utils/firebase_analytics_utils.dart';
import 'package:nkust_ap/utils/preferences.dart';
import 'package:nkust_ap/utils/utils.dart';
import 'package:nkust_ap/widgets/drawer_body.dart';
import 'package:nkust_ap/widgets/share_data_widget.dart';

import 'api/helper.dart';
import 'models/login_response.dart';
import 'models/user_info.dart';

Expand Down Expand Up @@ -74,6 +77,7 @@ class MyAppState extends State<MyApp> {
ThemeData themeData;
UserInfo userInfo;
LoginResponse loginResponse;
Uint8List pictureBytes;
bool isLogin = false, offlineLogin = false;

setThemeData(ThemeData themeData) {
Expand All @@ -85,6 +89,11 @@ class MyAppState extends State<MyApp> {
logout() {
setState(() {
this.isLogin = false;
this.offlineLogin = false;
this.userInfo = null;
this.loginResponse = null;
this.pictureBytes = null;
Helper.clearSetting();
});
}

Expand Down
6 changes: 4 additions & 2 deletions lib/pages/home/user_info_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:nkust_ap/utils/cache_utils.dart';
import 'package:nkust_ap/utils/global.dart';
import 'package:nkust_ap/utils/preferences.dart';
import 'package:nkust_ap/widgets/drawer_body.dart';
import 'package:nkust_ap/widgets/share_data_widget.dart';

enum _Status { loading, finish, error, empty }

Expand Down Expand Up @@ -47,14 +48,15 @@ class UserInfoPageState extends State<UserInfoPage>
child: Column(
children: <Widget>[
SizedBox(height: 8.0),
pictureBytes != null
ShareDataWidget.of(context).data.pictureBytes != null
? SizedBox(
height: 320,
child: AspectRatio(
aspectRatio: 2.0,
child: Hero(
tag: Constants.TAG_STUDENT_PICTURE,
child: Image.memory(pictureBytes),
child: Image.memory(
ShareDataWidget.of(context).data.pictureBytes),
),
),
)
Expand Down
18 changes: 9 additions & 9 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:nkust_ap/models/announcements_data.dart';
import 'package:nkust_ap/models/login_response.dart';
Expand Down Expand Up @@ -32,7 +33,6 @@ class HomePageState extends State<HomePage> {

_State state = _State.loading;
AppLocalizations app;
UserInfo userInfo = UserInfo();

int _currentNewsIndex = 0;

Expand Down Expand Up @@ -74,8 +74,10 @@ class HomePageState extends State<HomePage> {
],
),
drawer: DrawerBody(
userInfo: userInfo,
),
userInfo: ShareDataWidget.of(context).data.userInfo,
onClickLogout: () {
checkLogin();
}),
body: OrientationBuilder(
builder: (_, orientation) {
return Container(
Expand Down Expand Up @@ -314,15 +316,16 @@ class HomePageState extends State<HomePage> {

_getUserInfo() async {
if (Preferences.getBool(Constants.PREF_IS_OFFLINE_LOGIN, false)) {
userInfo = await CacheUtils.loadUserInfo();
ShareDataWidget.of(context).data.userInfo =
await CacheUtils.loadUserInfo();
setState(() {
state = _State.offline;
});
} else
Helper.instance.getUsersInfo().then((userInfo) {
if (this.mounted) {
setState(() {
this.userInfo = userInfo;
ShareDataWidget.of(context).data.userInfo = userInfo;
});
FA.setUserProperty('department', userInfo.department);
FA.setUserProperty('student_id', userInfo.id);
Expand Down Expand Up @@ -357,10 +360,7 @@ class HomePageState extends State<HomePage> {
leftActionText: app.cancel,
rightActionText: app.ok,
rightActionFunction: () {
Navigator.popUntil(
context,
ModalRoute.withName(Navigator.defaultRouteName),
);
ShareDataWidget.of(context).data.logout();
},
),
);
Expand Down
9 changes: 5 additions & 4 deletions lib/pages/login_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_autofill/flutter_autofill.dart';
import 'package:nkust_ap/models/login_response.dart';
Expand Down Expand Up @@ -134,7 +137,7 @@ class LoginPageState extends State<LoginPage> {
_password.value = TextEditingValue(
text: val,
selection:
TextSelection.fromPosition(TextPosition(offset: val.length)));
TextSelection.fromPosition(TextPosition(offset: val.length)));
},
autofillHints: [FlutterAutofill.AUTOFILL_HINT_PASSWORD],
autofillType: FlutterAutofill.AUTOFILL_TYPE_TEXT,
Expand All @@ -155,7 +158,6 @@ class LoginPageState extends State<LoginPage> {
style: _editTextStyle,
),
),

SizedBox(height: 8.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
Expand Down Expand Up @@ -341,7 +343,7 @@ class LoginPageState extends State<LoginPage> {
}
Preferences.setBool(Constants.PREF_IS_OFFLINE_LOGIN, false);
Navigator.of(context).pop(true);
await FlutterAutofill.commit();
if (!kIsWeb && Platform.isAndroid) await FlutterAutofill.commit();
}).catchError((e) {
if (Navigator.canPop(context))
Navigator.of(context, rootNavigator: true).pop();
Expand Down Expand Up @@ -386,7 +388,6 @@ class LoginPageState extends State<LoginPage> {
Preferences.setBool(Constants.PREF_AUTO_LOGIN, false);
setState(() {
isAutoLogin = false;
pictureBytes = null;
});
}
}
49 changes: 23 additions & 26 deletions lib/widgets/drawer_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import 'package:nkust_ap/utils/preferences.dart';
import 'package:nkust_ap/utils/utils.dart';
import 'package:nkust_ap/widgets/share_data_widget.dart';

Uint8List pictureBytes;

class DrawerBody extends StatefulWidget {
final UserInfo userInfo;
final Function onClickLogout;

const DrawerBody({
Key key,
@required this.userInfo,
@required this.onClickLogout,
}) : super(key: key);

@override
Expand Down Expand Up @@ -74,7 +74,8 @@ class DrawerBodyState extends State<DrawerBody> {
UserAccountsDrawerHeader(
margin: const EdgeInsets.all(0),
currentAccountPicture:
pictureBytes != null && displayPicture
ShareDataWidget.of(context).data.pictureBytes != null &&
displayPicture
? Hero(
tag: Constants.TAG_STUDENT_PICTURE,
child: Container(
Expand All @@ -85,7 +86,9 @@ class DrawerBodyState extends State<DrawerBody> {
image: DecorationImage(
fit: BoxFit.fitWidth,
image: MemoryImage(
pictureBytes,
ShareDataWidget.of(context)
.data
.pictureBytes,
),
),
),
Expand Down Expand Up @@ -244,26 +247,19 @@ class DrawerBodyState extends State<DrawerBody> {
title: app.settings,
page: SettingPage(),
),
// ListTile(
// leading: Icon(
// AppIcon.powerSettingsNew,
// color: Resource.Colors.grey,
// ),
// onTap: () async {
// print(
// 'login ${Preferences.getBool(Constants.PREF_AUTO_LOGIN, false)} ');
// if (Preferences.getBool(Constants.PREF_AUTO_LOGIN, false)) {
// await Preferences.setBool(Constants.PREF_AUTO_LOGIN, false);
// ShareDataWidget.of(context).data.logout();
// } else {
// Navigator.popUntil(
// context,
// ModalRoute.withName(Navigator.defaultRouteName),
// );
// }
// },
// title: Text(app.logout, style: _defaultStyle),
// ),
ListTile(
leading: Icon(
AppIcon.powerSettingsNew,
color: Resource.Colors.grey,
),
onTap: () async {
await Preferences.setBool(Constants.PREF_AUTO_LOGIN, false);
ShareDataWidget.of(context).data.logout();
Navigator.of(context).pop();
widget.onClickLogout();
},
title: Text(app.logout, style: _defaultStyle),
),
],
),
),
Expand Down Expand Up @@ -321,15 +317,15 @@ class DrawerBodyState extends State<DrawerBody> {
if (!response.body.contains('html')) {
if (mounted) {
setState(() {
pictureBytes = response.bodyBytes;
ShareDataWidget.of(context).data.pictureBytes = response.bodyBytes;
});
}
CacheUtils.savePictureData(response.bodyBytes);
} else {
var bytes = await CacheUtils.loadPictureData();
if (mounted) {
setState(() {
pictureBytes = bytes;
ShareDataWidget.of(context).data.pictureBytes = bytes;
});
}
}
Expand All @@ -339,6 +335,7 @@ class DrawerBodyState extends State<DrawerBody> {
}

_getPreference() async {
//TODO implement by future builder
if (!Preferences.getBool(Constants.PREF_IS_OFFLINE_LOGIN, false)) {
_getUserPicture();
}
Expand Down

0 comments on commit a7c994c

Please sign in to comment.