Skip to content

Commit f18bd37

Browse files
committed
userModel & login
1 parent 2146651 commit f18bd37

File tree

12 files changed

+219
-102
lines changed

12 files changed

+219
-102
lines changed

lib/apis/passport_api.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,13 @@ class PassportAPI {
3535
contentType: Headers.formUrlEncodedContentType,
3636
);
3737
}
38+
39+
/// TODO(shirne): refresh token
40+
static Future<ResponseModel<UserPassportModel>> restore() {
41+
return HttpUtil.fetchModel(
42+
FetchType.post,
43+
url: '$_user/refresh/',
44+
contentType: Headers.formUrlEncodedContentType,
45+
);
46+
}
3847
}

lib/constants/providers.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class TokenNotifier extends StateNotifier<UserAuthen> {
1010
TokenNotifier(super.state);
1111

1212
bool get isLogin => state.token.isNotEmpty;
13+
String get token => state.token;
1314

1415
void restore() {
1516
final data = HiveUtil.box.get(_tokenKey);

lib/models/data_model.g.dart

Lines changed: 62 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/models/user_model.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ class UserPassportModel extends DataModel {
352352
this.description = '',
353353
this.email = '',
354354
this.newUser = 0,
355+
this.firstLoginApp = 0,
355356
required this.sessionKey,
356357
this.isRecommendAllowed = 0,
357358
this.recommendHintMessage = '',
@@ -365,7 +366,7 @@ class UserPassportModel extends DataModel {
365366
this.countryCode = 0,
366367
this.hasPassword = 0,
367368
this.shareToRepost = 0,
368-
this.userDecoration = 0,
369+
this.userDecoration = '',
369370
this.userPrivacyExtend = 0,
370371
this.oldUserId = 0,
371372
this.oldUserIdStr = '',
@@ -402,6 +403,8 @@ class UserPassportModel extends DataModel {
402403

403404
bool get isEmpty => userId == 0;
404405

406+
bool get isLogin => !isEmpty && sessionKey.isNotEmpty;
407+
405408
final int appId;
406409
final int userId;
407410
final String userIdStr;
@@ -428,6 +431,7 @@ class UserPassportModel extends DataModel {
428431
final String description;
429432
final String email;
430433
final int newUser;
434+
final int firstLoginApp;
431435
final String sessionKey;
432436
final int isRecommendAllowed;
433437
final String recommendHintMessage;
@@ -441,7 +445,7 @@ class UserPassportModel extends DataModel {
441445
final int countryCode;
442446
final int hasPassword;
443447
final int shareToRepost;
444-
final int userDecoration;
448+
final String userDecoration;
445449
final int userPrivacyExtend;
446450
final int oldUserId;
447451
final String oldUserIdStr;

lib/pages/home/mine.dart

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// LICENSE file.
44

55
import 'package:flutter/material.dart';
6+
import 'package:flutter_riverpod/flutter_riverpod.dart';
67
import 'package:juejin/exports.dart';
78

89
class MinePage extends StatefulWidget {
@@ -37,41 +38,48 @@ class _MinePageState extends State<MinePage> {
3738
);
3839
}
3940

40-
Widget _buildUserAvatar(BuildContext context) {
41-
return Container(
42-
padding: const EdgeInsets.all(8),
43-
color: context.theme.dividerColor,
44-
child: SvgPicture.asset(R.ASSETS_BRAND_SVG),
45-
);
46-
}
47-
48-
Widget _buildUsername(BuildContext context) {
49-
return Text(
50-
context.l10n.userSignInOrUp,
51-
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
52-
);
53-
}
54-
5541
Widget _buildUser(BuildContext context) {
56-
return GestureDetector(
57-
onTap: () => context.navigator.pushNamed(
58-
Routes.loginPage.name,
59-
),
60-
child: SizedBox(
61-
height: 54,
62-
child: Row(
63-
children: <Widget>[
64-
AspectRatio(
65-
aspectRatio: 1,
66-
child: ClipOval(
67-
child: _buildUserAvatar(context),
68-
),
42+
return Consumer(
43+
builder: (BuildContext context, WidgetRef ref, Widget? child) {
44+
final userModel = ref.watch(userProvider);
45+
return GestureDetector(
46+
onTap: () => context.navigator.pushNamed(
47+
userModel.isLogin ? Routes.userProfile.name : Routes.loginPage.name,
48+
),
49+
child: SizedBox(
50+
height: 54,
51+
child: Row(
52+
children: <Widget>[
53+
AspectRatio(
54+
aspectRatio: 1,
55+
child: ClipOval(
56+
child: userModel.isLogin
57+
? Image.network(
58+
userModel.avatarUrl,
59+
fit: BoxFit.cover,
60+
)
61+
: Container(
62+
padding: const EdgeInsets.all(8),
63+
color: context.theme.dividerColor,
64+
child: SvgPicture.asset(R.ASSETS_BRAND_SVG),
65+
),
66+
),
67+
),
68+
const Gap.h(16),
69+
Text(
70+
userModel.isLogin
71+
? userModel.screenName
72+
: context.l10n.userSignInOrUp,
73+
style: const TextStyle(
74+
fontSize: 16,
75+
fontWeight: FontWeight.w600,
76+
),
77+
),
78+
],
6979
),
70-
const Gap.h(16),
71-
_buildUsername(context),
72-
],
73-
),
74-
),
80+
),
81+
);
82+
},
7583
);
7684
}
7785

lib/pages/login/login.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:math' as math;
22
import 'package:flutter/material.dart';
3+
import 'package:flutter/services.dart';
34
import 'package:flutter_riverpod/flutter_riverpod.dart';
45

56
import '../../exports.dart';
@@ -31,6 +32,7 @@ class _LoginPageState extends ConsumerState<LoginPage> {
3132
if (result.isSucceed) {
3233
showToast(context.l10n.loginSuccess);
3334
ref.read(userProvider.notifier).update(result.data!);
35+
context.navigator.pop();
3436
} else {
3537
showToast(result.msg);
3638
}
@@ -44,7 +46,11 @@ class _LoginPageState extends ConsumerState<LoginPage> {
4446
@override
4547
Widget build(BuildContext context) {
4648
return Scaffold(
47-
appBar: AppBar(),
49+
appBar: AppBar(
50+
backgroundColor: Colors.transparent,
51+
elevation: 0,
52+
systemOverlayStyle: SystemUiOverlayStyle.dark,
53+
),
4854
resizeToAvoidBottomInset: true,
4955
body: SingleChildScrollView(
5056
padding: const EdgeInsets.all(16.0),
@@ -98,7 +104,7 @@ class _LoginPageState extends ConsumerState<LoginPage> {
98104
prefixIcon: const Icon(Icons.lock_outline),
99105
),
100106
),
101-
const Gap.v(8),
107+
const Gap.v(16),
102108
ElevatedButton(
103109
onPressed: _doLogin,
104110
child: Center(
@@ -123,14 +129,14 @@ class _LoginPageState extends ConsumerState<LoginPage> {
123129
children: [
124130
TextButton(
125131
onPressed: () {
126-
showToast('Comming soon');
132+
showToast(context.l10n.notSupported);
127133
},
128134
child: Text(context.l10n.linkSignUp),
129135
),
130136
const Spacer(),
131137
TextButton(
132138
onPressed: () {
133-
showToast('Comming soon');
139+
showToast(context.l10n.notSupported);
134140
},
135141
child: Text(context.l10n.linkRetrieve),
136142
),

lib/pages/splash.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,19 @@ class _SplashPageState extends ConsumerState<SplashPage> {
4242
await DeviceUtil.setHighestRefreshRate();
4343
await HttpUtil.fetch(FetchType.get, url: 'https://${Urls.domain}');
4444

45-
ref.read(tokenProvider.notifier).restore();
45+
final tokenNotifier = ref.read(tokenProvider.notifier);
46+
tokenNotifier.restore();
47+
48+
if (tokenNotifier.isLogin) {
49+
HttpUtil.setHeaders('token', tokenNotifier.token);
50+
PassportAPI.restore().then((data) {
51+
if (data.isSucceed) {
52+
ref.read(userProvider.notifier).update(data.data!);
53+
} else {
54+
tokenNotifier.logout();
55+
}
56+
});
57+
}
4658

4759
if (mounted) {
4860
navigator.pushReplacementNamed(Routes.homePage.name);

lib/pages/user/profile.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import 'package:flutter/material.dart';
2+
3+
import '../../exports.dart';
4+
5+
@FFRoute(name: 'user-profile')
6+
class ProfilePage extends StatefulWidget {
7+
const ProfilePage({super.key});
8+
9+
@override
10+
State<ProfilePage> createState() => _ProfilePageState();
11+
}
12+
13+
class _ProfilePageState extends State<ProfilePage> {
14+
@override
15+
Widget build(BuildContext context) {
16+
return Scaffold(
17+
appBar: AppBar(),
18+
body: Center(
19+
child: Text(context.l10n.notSupported),
20+
),
21+
);
22+
}
23+
}

0 commit comments

Comments
 (0)