Skip to content

Commit 8cf33c1

Browse files
committed
refactor: use user as profile state
1 parent b585484 commit 8cf33c1

File tree

3 files changed

+25
-71
lines changed

3 files changed

+25
-71
lines changed

lib/profile/cubit/profile_cubit.dart

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import 'dart:async';
22

33
import 'package:bloc/bloc.dart';
4-
import 'package:equatable/equatable.dart';
54
import 'package:user_repository/user_repository.dart';
65
import 'package:shared/shared.dart';
76

8-
part 'profile_state.dart';
9-
10-
class ProfileCubit extends Cubit<ProfileState> {
7+
class ProfileCubit extends Cubit<User> {
118
ProfileCubit({required UserRepository userRepository})
129
: _userRepository = userRepository,
13-
super(const ProfileState.initial()) {
10+
super(User.none) {
1411
_watchUser();
1512
}
1613

@@ -22,13 +19,7 @@ class ProfileCubit extends Cubit<ProfileState> {
2219
return super.close();
2320
}
2421

25-
void logOut() {
26-
emit(state.copyWith(action: ProfileAction.logOut));
27-
}
28-
29-
void _onUserChanged(User user) {
30-
emit(state.copyWith(user: user));
31-
}
22+
void _onUserChanged(User user) => emit(user);
3223

3324
late final StreamSubscription _userSubscription;
3425
void _watchUser() {

lib/profile/cubit/profile_state.dart

Lines changed: 0 additions & 32 deletions
This file was deleted.

lib/profile/view/profile_view.dart

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,23 @@ class Profile extends StatelessWidget {
3737

3838
@override
3939
Widget build(BuildContext context) {
40-
return BlocListener<ProfileCubit, ProfileState>(
41-
listenWhen: (_, current) => current.action.isLogOut,
42-
listener: (_, __) => context.read<AppCubit>().logOut(),
43-
child: Scaffold(
44-
appBar: AppBar(
45-
backgroundColor: QuizColors.deepOrange,
46-
title: const DisplayName(),
47-
),
48-
body: Column(
49-
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
50-
children: const [
51-
SizedBox(height: 50),
52-
ProfilePhoto(),
53-
EmailAddress(),
54-
Spacer(),
55-
TotalCompletedQuizzes(),
56-
Spacer(),
57-
LogOutButton(),
58-
Spacer(),
59-
],
60-
),
40+
return Scaffold(
41+
appBar: AppBar(
42+
backgroundColor: QuizColors.deepOrange,
43+
title: const DisplayName(),
44+
),
45+
body: Column(
46+
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
47+
children: const [
48+
SizedBox(height: 50),
49+
ProfilePhoto(),
50+
EmailAddress(),
51+
Spacer(),
52+
TotalCompletedQuizzes(),
53+
Spacer(),
54+
LogOutButton(),
55+
Spacer(),
56+
],
6157
),
6258
);
6359
}
@@ -69,7 +65,7 @@ class DisplayName extends StatelessWidget {
6965
@override
7066
Widget build(BuildContext context) {
7167
final displayName =
72-
context.select((ProfileCubit cubit) => cubit.state.user.displayName);
68+
context.select((ProfileCubit cubit) => cubit.state.displayName);
7369
return Text(displayName.isNotEmpty
7470
? displayName
7571
: context.l10n.guestProfileDisplayName);
@@ -84,7 +80,7 @@ class ProfilePhoto extends StatelessWidget {
8480
@override
8581
Widget build(BuildContext context) {
8682
final photoURL =
87-
context.select((ProfileCubit cubit) => cubit.state.user.photoURL);
83+
context.select((ProfileCubit cubit) => cubit.state.photoURL);
8884
return photoURL.isNotEmpty
8985
? Stack(
9086
alignment: AlignmentDirectional.center,
@@ -119,8 +115,7 @@ class EmailAddress extends StatelessWidget {
119115

120116
@override
121117
Widget build(BuildContext context) {
122-
final email =
123-
context.select((ProfileCubit cubit) => cubit.state.user.email);
118+
final email = context.select((ProfileCubit cubit) => cubit.state.email);
124119
return Text(email, style: context.textTheme.headline5);
125120
}
126121
}
@@ -131,7 +126,7 @@ class TotalCompletedQuizzes extends StatelessWidget {
131126
@override
132127
Widget build(BuildContext context) {
133128
final totalCompletedQuizzes = context
134-
.select((ProfileCubit cubit) => cubit.state.user.totalCompletedQuizzes);
129+
.select((ProfileCubit cubit) => cubit.state.totalCompletedQuizzes);
135130
return Column(
136131
mainAxisSize: MainAxisSize.min,
137132
children: [
@@ -154,7 +149,7 @@ class LogOutButton extends StatelessWidget {
154149
@override
155150
Widget build(BuildContext context) {
156151
return ActionButton(
157-
onPressed: context.read<ProfileCubit>().logOut,
152+
onPressed: context.read<AppCubit>().logOut,
158153
backgroundColor: QuizColors.red,
159154
label: Text(context.l10n.logOutButtonLabel),
160155
);

0 commit comments

Comments
 (0)