Skip to content

Commit f77c142

Browse files
improve code in presentation common folder (#37)
* improve code in presentation common folder * fix imports
1 parent 1d08dd9 commit f77c142

16 files changed

Lines changed: 368 additions & 351 deletions

File tree

lib/data/repositories/activity_repository_impl.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:hooks_riverpod/hooks_riverpod.dart';
2-
import '../../domain/entities/activity_comment.dart';
32

43
import '../../domain/entities/activity.dart';
4+
import '../../domain/entities/activity_comment.dart';
55
import '../../domain/repositories/activity_repository.dart';
66
import '../api/activity_api.dart';
77
import '../model/request/activity_request.dart';

lib/presentation/common/activity/view_model/activity_item_view_model.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ class ActivityItemViewModel extends StateNotifier<ActivityItemState> {
7878
ActivityComment? activityComment = await ref
7979
.read(activityRepositoryProvider)
8080
.createComment(activity.id, commentController.text);
81-
Activity currentActivity = state.activity ?? activity;
82-
List<ActivityComment> comments = List.from(currentActivity.comments);
83-
comments.add(activityComment!);
84-
setActivity(activity.copy(comments: comments));
81+
List<ActivityComment> updatedComments = List.from(activity.comments)
82+
..add(activityComment!);
83+
setActivity(activity.copy(comments: updatedComments));
8584
commentController.text = '';
8685
}
8786

lib/presentation/common/activity/widgets/activity_comments.dart

Lines changed: 67 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ class ActivityComments extends HookConsumerWidget {
1919

2020
final currentUserPictureDataProvider =
2121
FutureProvider.family<Uint8List?, Activity>((ref, activity) async {
22-
User? user = await StorageUtils.getUser();
23-
if (user != null) {
24-
final provider =
25-
ref.read(activityItemViewModelProvider(activity.id).notifier);
26-
return provider.getProfilePicture(user.id);
27-
}
28-
return null;
22+
final user = await StorageUtils.getUser();
23+
final provider =
24+
ref.read(activityItemViewModelProvider(activity.id).notifier);
25+
26+
return user != null ? provider.getProfilePicture(user.id) : null;
2927
});
3028

3129
final commentUserPictureDataProvider =
@@ -42,10 +40,9 @@ class ActivityComments extends HookConsumerWidget {
4240

4341
Widget buildCommentList(WidgetRef ref, List<ActivityComment> comments) {
4442
return Expanded(
45-
child: ListView(
46-
children: [
47-
for (var comment in comments) buildCommentItem(ref, comment),
48-
],
43+
child: ListView.builder(
44+
itemCount: comments.length,
45+
itemBuilder: (context, index) => buildCommentItem(ref, comments[index]),
4946
),
5047
);
5148
}
@@ -54,109 +51,105 @@ class ActivityComments extends HookConsumerWidget {
5451
return Padding(
5552
padding: const EdgeInsets.fromLTRB(2.0, 8.0, 2.0, 0.0),
5653
child: ListTile(
57-
leading: GestureDetector(
58-
child: Container(
59-
height: 50.0,
60-
width: 50.0,
61-
decoration: BoxDecoration(
62-
color: ColorUtils.white,
63-
borderRadius: const BorderRadius.all(Radius.circular(50)),
64-
),
65-
child: ref.watch(commentUserPictureDataProvider(comment.user)).when(
66-
data: (pic) {
67-
return pic != null
68-
? CircleAvatar(
69-
radius: 50, backgroundImage: MemoryImage(pic))
70-
: UserUtils.personIcon;
71-
},
72-
loading: () {
73-
return UserUtils.personIcon;
74-
},
75-
error: (error, stackTrace) {
76-
return UserUtils.personIcon;
77-
},
78-
),
54+
leading: buildUserAvatar(ref, comment.user),
55+
title: GestureDetector(
56+
child: Text(
57+
UserUtils.getNameOrUsername(comment.user),
58+
style: const TextStyle(),
7959
),
8060
),
81-
title: GestureDetector(
82-
child: Text(
83-
comment.user.firstname != null && comment.user.lastname != null
84-
? '${comment.user.firstname} ${comment.user.lastname}'
85-
: comment.user.username,
86-
style: const TextStyle(),
87-
)),
8861
subtitle: Text(comment.content),
8962
),
9063
);
9164
}
9265

66+
Widget buildViewPreviousComments(ActivityItemViewModel provider,
67+
AppLocalizations appLocalizations, List<ActivityComment> comments) {
68+
return GestureDetector(
69+
onTap: () => provider.togglePreviousComments(),
70+
child: Padding(
71+
padding: const EdgeInsets.all(8.0),
72+
child: Text(
73+
appLocalizations.view_previous_comments(comments.length - 1),
74+
style: TextStyle(
75+
color: ColorUtils.mainMedium,
76+
fontWeight: FontWeight.bold,
77+
),
78+
),
79+
),
80+
);
81+
}
82+
9383
Widget buildCommentChild(
94-
WidgetRef ref,
95-
AppLocalizations appLocalizations,
96-
ActivityItemViewModel provider,
97-
List<ActivityComment> comments,
98-
bool displayPreviousComments) {
84+
WidgetRef ref,
85+
AppLocalizations appLocalizations,
86+
ActivityItemViewModel provider,
87+
List<ActivityComment> comments,
88+
bool displayPreviousComments,
89+
) {
9990
final lastComment = comments.isNotEmpty ? comments.last : null;
10091

10192
return Column(
10293
children: [
10394
if (comments.length > 1 && !displayPreviousComments)
104-
GestureDetector(
105-
onTap: () {
106-
provider.togglePreviousComments();
107-
},
108-
child: Padding(
109-
padding: const EdgeInsets.all(8.0),
110-
child: Text(
111-
appLocalizations.view_previous_comments(
112-
comments.length - 1,
113-
),
114-
style: TextStyle(
115-
color: ColorUtils.mainMedium,
116-
fontWeight: FontWeight.bold,
117-
),
118-
),
119-
),
120-
),
95+
buildViewPreviousComments(provider, appLocalizations, comments),
12196
if (lastComment != null && !displayPreviousComments)
12297
buildCommentItem(ref, lastComment),
12398
if (displayPreviousComments) buildCommentList(ref, comments),
12499
],
125100
);
126101
}
127102

103+
Widget buildUserAvatar(WidgetRef ref, User user) {
104+
return GestureDetector(
105+
child: Container(
106+
height: 50.0,
107+
width: 50.0,
108+
decoration: BoxDecoration(
109+
color: ColorUtils.white,
110+
borderRadius: const BorderRadius.all(Radius.circular(50)),
111+
),
112+
child: ref.watch(commentUserPictureDataProvider(user)).when(
113+
data: (pic) => pic != null
114+
? CircleAvatar(radius: 50, backgroundImage: MemoryImage(pic))
115+
: UserUtils.personIcon,
116+
loading: () => UserUtils.personIcon,
117+
error: (_, __) => UserUtils.personIcon,
118+
),
119+
),
120+
);
121+
}
122+
128123
@override
129124
Widget build(BuildContext context, WidgetRef ref) {
130125
final currentUserPictureProvider =
131126
ref.watch(currentUserPictureDataProvider(currentActivity));
132127
final provider =
133128
ref.read(activityItemViewModelProvider(currentActivity.id).notifier);
134129
final state = ref.read(activityItemViewModelProvider(currentActivity.id));
135-
136130
final appLocalizations = AppLocalizations.of(context)!;
137131

138132
return SizedBox(
139133
height: currentActivity.comments.isNotEmpty ? 210 : 80,
140134
child: CommentBox(
141135
userImage: currentUserPictureProvider.when(
142-
data: (pic) {
143-
return pic != null ? MemoryImage(pic) : null;
144-
},
145-
loading: () {
146-
return null;
147-
},
148-
error: (error, stackTrace) {
149-
return null;
150-
},
136+
data: (pic) => pic != null ? MemoryImage(pic) : null,
137+
loading: () => null,
138+
error: (_, __) => null,
151139
),
152140
sendButtonMethod: () => provider.comment(currentActivity),
153141
formKey: formKey,
154142
commentController: provider.commentController,
155143
backgroundColor: ColorUtils.white,
156144
textColor: ColorUtils.mainMedium,
157145
sendWidget: Icon(Icons.send_sharp, size: 30, color: ColorUtils.main),
158-
child: buildCommentChild(ref, appLocalizations, provider,
159-
currentActivity.comments.toList(), state.displayPreviousComments),
146+
child: buildCommentChild(
147+
ref,
148+
appLocalizations,
149+
provider,
150+
currentActivity.comments.toList(),
151+
state.displayPreviousComments,
152+
),
160153
),
161154
);
162155
}

0 commit comments

Comments
 (0)