Skip to content

Commit 7928532

Browse files
author
Freek van de Ven
committed
fix: add the catalog user repository to the catalog service
1 parent fbad48e commit 7928532

5 files changed

Lines changed: 44 additions & 27 deletions

File tree

packages/flutter_catalog/lib/src/flutter_catalog_userstory.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,16 @@ abstract class _BaseCatalogNavigatorUserstory extends HookWidget {
119119
var catalogService = useMemoized(
120120
() => CatalogService(
121121
repository: options.catalogRepository,
122+
userRepository: options.catalogUserRepository,
122123
userId: userId,
123124
userLocation: userLocation,
124125
),
125-
[options.catalogRepository],
126+
[
127+
options.catalogRepository,
128+
options.catalogUserRepository,
129+
userId,
130+
userLocation,
131+
],
126132
);
127133

128134
var filterService = useMemoized(

packages/flutter_catalog/lib/src/services/catalog_service.dart

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import "package:flutter_catalog_interface/flutter_catalog_interface.dart";
66
/// manipulate catalog data of type T.
77
class CatalogService<T extends CatalogItem> {
88
/// Creates a [CatalogService] with the given repository.
9-
CatalogService({
9+
const CatalogService({
1010
required CatalogRepository<T> repository,
11+
required CatalogUserRepository<CatalogUser> userRepository,
1112
required this.userId,
1213
required this.userLocation,
13-
}) : _repository = repository;
14+
}) : _repository = repository,
15+
_userRepository = userRepository;
1416

1517
final CatalogRepository<T> _repository;
1618

19+
final CatalogUserRepository<CatalogUser> _userRepository;
20+
1721
/// The ID of the user for whom catalog operations are performed.
1822
final String userId;
1923

@@ -34,6 +38,21 @@ class CatalogService<T extends CatalogItem> {
3438
offset: offset,
3539
);
3640

41+
/// Fetches users for a list of catalog items.
42+
Future<List<CatalogUser>> fetchUsersForItems(
43+
List<T> items,
44+
) async {
45+
var userIds =
46+
items.map((item) => item.authorId).whereType<String>().toSet().toList();
47+
return _userRepository.getUsers(userIds);
48+
}
49+
50+
/// Retrieves a user for a specific catalog item.
51+
Future<CatalogUser?> getUserForCatalogItem(T item) async {
52+
if (item.authorId == null) return null;
53+
return _userRepository.getUser(item.authorId!);
54+
}
55+
3756
/// Toggles the favorite status of an item for the current user.
3857
Future<void> toggleFavorite(String itemId) async =>
3958
_repository.toggleFavorite(itemId, userId);

packages/flutter_catalog/lib/src/views/catalog_detail_view.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ class CatalogDetailView extends HookWidget {
3737

3838
// ignore: discarded_futures
3939
var authorFuture = useMemoized(
40-
() async => item.authorId != null
41-
? options.catalogUserRepository.getUser(item.authorId!)
42-
: Future.value(null),
40+
() async => service.getUserForCatalogItem(item),
4341
[item.authorId],
4442
);
4543

packages/flutter_catalog/lib/src/views/catalog_overview_view.dart

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ class _Body extends HookWidget {
231231
var localizations = FlutterCatalogLocalizations.of(context)!;
232232

233233
var scope = CatalogScope.of(context);
234+
var catalogService = scope.catalogService;
234235
var filterService = scope.filterService;
235236
var filtersStream =
236237
useMemoized(() => filterService.getFiltersWithValues(), []);
@@ -242,28 +243,31 @@ class _Body extends HookWidget {
242243
var itemsFuture = useMemoized(
243244
() async {
244245
if (!filtersSnapshot.hasData) {
245-
return scope.catalogService.fetchCatalogItems();
246+
return catalogService.fetchCatalogItems();
246247
}
247-
return scope.catalogService.fetchCatalogItems(
248+
return catalogService.fetchCatalogItems(
248249
filters: filtersSnapshot.data!.toSerializedFilterMap(),
249250
);
250251
},
251252
[filtersSnapshot.data],
252253
);
253254
var snapshot = useFuture(itemsFuture);
255+
var items = snapshot.data;
256+
useEffect(
257+
() {
258+
if (items == null || items.isEmpty) {
259+
options.onNoItems?.call();
260+
}
261+
return;
262+
},
263+
[],
264+
);
254265

255266
// ignore: discarded_futures
256267
var usersFuture = useMemoized(
257268
() async {
258269
if (snapshot.hasData && snapshot.data!.isNotEmpty) {
259-
var authorIds = snapshot.data!
260-
.map((item) => item.authorId)
261-
.whereType<String>()
262-
.toSet()
263-
.toList();
264-
if (authorIds.isNotEmpty) {
265-
return scope.options.catalogUserRepository.getUsers(authorIds);
266-
}
270+
return catalogService.fetchUsersForItems(snapshot.data!);
267271
}
268272
return Future.value(<CatalogUser>[]);
269273
},
@@ -284,16 +288,6 @@ class _Body extends HookWidget {
284288
Center(child: Text(localizations.itemLoadingError));
285289
}
286290

287-
var items = snapshot.data;
288-
useEffect(
289-
() {
290-
if (items == null || items.isEmpty) {
291-
options.onNoItems?.call();
292-
}
293-
return;
294-
},
295-
[],
296-
);
297291
if (items == null || items.isEmpty) {
298292
return builders.noItemsPlaceholderBuilder?.call(context) ??
299293
Center(child: Text(localizations.noItemsFound));

packages/flutter_catalog/lib/src/widgets/catalog_grid_item.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class CatalogGridItem extends StatelessWidget {
8585
const SizedBox(width: 4),
8686
Expanded(
8787
child: Text(
88-
author?.name ?? item.authorId ?? "Unknown",
88+
author?.name ?? "Unknown",
8989
style: textTheme.bodySmall,
9090
maxLines: 1,
9191
overflow: TextOverflow.ellipsis,

0 commit comments

Comments
 (0)