forked from lichess-org/mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_screen.dart
More file actions
308 lines (288 loc) · 11.8 KB
/
user_screen.dart
File metadata and controls
308 lines (288 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:http/http.dart' show ClientException;
import 'package:lichess_mobile/src/model/account/account_repository.dart';
import 'package:lichess_mobile/src/model/auth/auth_controller.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/game/game_filter.dart';
import 'package:lichess_mobile/src/model/relation/relation_repository.dart';
import 'package:lichess_mobile/src/model/user/user.dart';
import 'package:lichess_mobile/src/model/user/user_repository.dart';
import 'package:lichess_mobile/src/network/connectivity.dart';
import 'package:lichess_mobile/src/network/http.dart';
import 'package:lichess_mobile/src/styles/lichess_icons.dart';
import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/utils/navigation.dart';
import 'package:lichess_mobile/src/utils/share.dart';
import 'package:lichess_mobile/src/view/message/conversation_screen.dart';
import 'package:lichess_mobile/src/view/play/challenge_odd_bots_screen.dart';
import 'package:lichess_mobile/src/view/play/create_challenge_bottom_sheet.dart';
import 'package:lichess_mobile/src/view/user/game_history_screen.dart';
import 'package:lichess_mobile/src/view/user/perf_cards.dart';
import 'package:lichess_mobile/src/view/user/recent_games.dart';
import 'package:lichess_mobile/src/view/user/user_activity.dart';
import 'package:lichess_mobile/src/view/user/user_profile.dart';
import 'package:lichess_mobile/src/view/watch/tv_screen.dart';
import 'package:lichess_mobile/src/widgets/buttons.dart';
import 'package:lichess_mobile/src/widgets/feedback.dart';
import 'package:lichess_mobile/src/widgets/haptic_refresh_indicator.dart';
import 'package:lichess_mobile/src/widgets/list.dart';
import 'package:lichess_mobile/src/widgets/platform.dart';
import 'package:lichess_mobile/src/widgets/text_badge.dart';
import 'package:lichess_mobile/src/widgets/user.dart';
import 'package:share_plus/share_plus.dart';
import 'package:url_launcher/url_launcher.dart';
final _userScreenDataProvider = FutureProvider.autoDispose.family<UserScreenData, UserId>(
(ref, id) => ref.read(userRepositoryProvider).getUserScreenData(id),
name: 'UserScreenDataProvider',
);
class UserScreen extends ConsumerStatefulWidget {
const UserScreen({required this.user, super.key});
final LightUser user;
static Route<dynamic> buildRoute(BuildContext context, LightUser user) {
return buildScreenRoute(context, screen: UserScreen(user: user));
}
static void challengeUser(User user, {required BuildContext context, required WidgetRef ref}) {
final authUser = ref.read(authControllerProvider);
if (authUser == null) {
showSnackBar(
context,
context.l10n.challengeRegisterToSendChallenges,
type: SnackBarType.error,
);
return;
}
final isOddBot = oddBots.contains(user.lightUser.name.toLowerCase());
if (isOddBot) {
Navigator.of(context).push(ChallengeOddBotsScreen.buildRoute(context, user.lightUser));
} else {
showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
useRootNavigator: true,
builder: (context) {
return CreateChallengeBottomSheet(user.lightUser);
},
);
}
}
@override
ConsumerState<UserScreen> createState() => _UserScreenState();
}
class _UserScreenState extends ConsumerState<UserScreen> {
bool isLoading = false;
void setIsLoading(bool value) {
if (mounted) {
setState(() {
isLoading = value;
});
}
}
@override
Widget build(BuildContext context) {
final userScreenData = ref.watch(_userScreenDataProvider(widget.user.id));
final updatedLightUser = userScreenData.maybeWhen(
data: (data) => data.user.lightUser.copyWith(isOnline: data.isOnline),
orElse: () => null,
);
return Scaffold(
appBar: PlatformAppBar(
title: UserFullNameWidget(
user: updatedLightUser ?? widget.user,
shouldShowOnline: updatedLightUser != null,
),
actions: [
if (isLoading) const PlatformAppBarLoadingIndicator(),
SemanticIconButton(
icon: const PlatformShareIcon(),
semanticsLabel: 'Share profile',
onPressed: () =>
launchShareDialog(context, ShareParams(uri: lichessUri('/@/${widget.user.name}'))),
),
],
),
body: userScreenData.when(
data: (data) => _UserProfileListView(
data,
isLoading,
setIsLoading,
onRefresh: () => ref.refresh(_userScreenDataProvider(widget.user.id).future),
),
loading: () => const Center(child: CircularProgressIndicator.adaptive()),
error: (error, _) {
if (error is ClientException && error.message.contains('404')) {
return Center(
child: Text(
textAlign: TextAlign.center,
context.l10n.usernameNotFound(widget.user.name),
style: Styles.bold,
),
);
}
return FullScreenRetryRequest(
onRetry: () => ref.invalidate(_userScreenDataProvider(widget.user.id)),
);
},
),
);
}
}
class _UserProfileListView extends ConsumerWidget {
const _UserProfileListView(
this.data,
this.isLoading,
this.setIsLoading, {
required this.onRefresh,
});
final UserScreenData data;
final bool isLoading;
final void Function(bool value) setIsLoading;
final RefreshCallback onRefresh;
String _scoreDisplay(double score) {
final integerPart = score.truncate();
final decimalPart = score - integerPart;
return integerPart == 0 && decimalPart == 0.5
? '½'
: '$integerPart${decimalPart == 0.5 ? '½' : ''}';
}
@override
Widget build(BuildContext context, WidgetRef ref) {
final UserScreenData(:user, :recentGames, :activity, :isPlayingLive, :crosstable) = data;
final connectivity = ref.watch(connectivityChangesProvider);
final nbOfGames = user.count?.all ?? 0;
final authUser = ref.watch(authControllerProvider);
final kidMode = ref.watch(kidModeProvider);
if (user.disabled == true) {
return Center(child: Text(context.l10n.settingsThisAccountIsClosed, style: Styles.bold));
}
Future<void> userAction(Future<void> Function(LichessClient client) action) async {
setIsLoading(true);
try {
await ref.withClient(action).then((_) => ref.invalidate(_userScreenDataProvider(user.id)));
} finally {
setIsLoading(false);
}
}
return HapticRefreshIndicator(
edgeOffset: Theme.of(context).platform == TargetPlatform.iOS
? MediaQuery.paddingOf(context).top + kToolbarHeight
: 0.0,
onRefresh: onRefresh,
child: ListView(
children: [
UserProfileWidget(user: user),
PerfCards(user: user, isMe: false),
ListSection(
hasLeading: true,
children: [
if (authUser != null && crosstable != null && (crosstable.nbGames) > 0) ...[
() {
final crosstableData = crosstable;
final currentUserScore = crosstableData.users[authUser.user.id] ?? 0;
final otherUserScore = crosstableData.users[user.id] ?? 0;
return ListTile(
title: Text(
context.l10n.yourScore(
'${_scoreDisplay(currentUserScore)} - ${_scoreDisplay(otherUserScore)}',
),
),
leading: const Icon(Icons.scoreboard_outlined),
onTap: () {
Navigator.of(context).push(
GameHistoryScreen.buildRoute(
context,
user: authUser.user,
isOnline: connectivity.value?.isOnline == true,
gameFilter: GameFilterState(opponent: user),
),
);
},
);
}(),
],
ListTile(
title: Text(context.l10n.watchGames),
leading: const Icon(Icons.live_tv_outlined),
trailing: isPlayingLive == true ? const TextBadge(text: 'LIVE') : null,
onTap: () {
Navigator.of(
context,
rootNavigator: true,
).push(TvScreen.buildRoute(context, user: user.lightUser));
},
),
if (authUser != null) ...[
if (user.canChallenge == true)
ListTile(
title: Text(context.l10n.challengeChallengeToPlay),
leading: const Icon(LichessIcons.crossed_swords),
onTap: () => UserScreen.challengeUser(user, context: context, ref: ref),
),
if (user.blocking != true && !user.isBot && kidMode.value == false)
ListTile(
leading: const Icon(Icons.chat_bubble_outline),
title: Text(context.l10n.composeMessage),
onTap: () {
Navigator.of(
context,
rootNavigator: true,
).push(ConversationScreen.buildRoute(context, user: user.lightUser));
},
),
if (user.followable == true && user.following != true)
ListTile(
leading: const Icon(Icons.person_add_outlined),
title: Text(context.l10n.follow),
onTap: isLoading
? null
: () => userAction((client) => RelationRepository(client).follow(user.id)),
)
else if (user.following == true)
ListTile(
leading: const Icon(Icons.person_remove_outlined),
title: Text(context.l10n.unfollow),
onTap: isLoading
? null
: () =>
userAction((client) => RelationRepository(client).unfollow(user.id)),
),
if (user.following != true && user.blocking != true)
ListTile(
leading: const Icon(Icons.block_outlined),
title: Text(context.l10n.block),
onTap: isLoading
? null
: () => userAction((client) => RelationRepository(client).block(user.id)),
)
else if (user.blocking == true)
ListTile(
leading: const Icon(Icons.block_outlined),
title: Text(context.l10n.unblock),
onTap: isLoading
? null
: () => userAction((client) => RelationRepository(client).unblock(user.id)),
),
ListTile(
leading: const Icon(Icons.report_problem_outlined),
title: Text(context.l10n.reportXToModerators(user.username)),
onTap: () {
launchUrl(
lichessUri('/report', {'username': user.id, 'login': authUser.user.id}),
);
},
),
],
],
),
UserActivityWidget(activity: AsyncData(activity), user: user.lightUser),
RecentGamesWidget(
recentGames: AsyncData(recentGames),
nbOfGames: nbOfGames,
user: user.lightUser,
),
],
),
);
}
}