|
1 | 1 | import 'package:flutter/material.dart'; |
2 | 2 | import 'package:pr12er/protos/pkg/pr12er/messages.pb.dart'; |
| 3 | +import 'package:pr12er/service.dart'; |
| 4 | +import 'package:pr12er/view_models/view_model_videos.dart'; |
3 | 5 | import 'package:pr12er/widgets/main/pr12video.dart'; |
| 6 | +import 'package:provider/provider.dart'; |
4 | 7 |
|
5 | 8 | class VideoSearchDelegate extends SearchDelegate { |
6 | | - late List<Video> dataRef; |
7 | | - late bool ignoreBookmarkIcon; |
| 9 | + List<Video> dataRef = []; |
| 10 | + bool showOnlyBookmarkItems = false; |
8 | 11 |
|
9 | 12 | @override |
10 | 13 | ThemeData appBarTheme(BuildContext context) { |
@@ -33,30 +36,71 @@ class VideoSearchDelegate extends SearchDelegate { |
33 | 36 | ); |
34 | 37 | } |
35 | 38 |
|
36 | | - Widget getSearchResult() { |
37 | | - final videos = dataRef.where((video) { |
| 39 | + Widget getSearchResult(BuildContext context) { |
| 40 | + return FutureBuilder<List<Video>>( |
| 41 | + future: context.read<GrpcClient>().getVideos(), |
| 42 | + builder: (context, snapshot) { |
| 43 | + if (!snapshot.hasData) { |
| 44 | + return const Center(child: CircularProgressIndicator()); |
| 45 | + } |
| 46 | + |
| 47 | + dataRef = snapshot.requireData; |
| 48 | + |
| 49 | + if (showOnlyBookmarkItems) { |
| 50 | + return FutureBuilder<Set<int>>( |
| 51 | + future: context.read<FavoriteVideoViewModel>().favoriteVideosMap, |
| 52 | + builder: (context, snapshot) { |
| 53 | + if (!snapshot.hasData) { |
| 54 | + return const Center(child: CircularProgressIndicator()); |
| 55 | + } |
| 56 | + final map = snapshot.requireData; |
| 57 | + dataRef = dataRef |
| 58 | + .where((element) => map.contains(element.prId)) |
| 59 | + .toList(growable: false); |
| 60 | + |
| 61 | + final List<Video> videos = getInterestedVideos(); |
| 62 | + |
| 63 | + return ListView.builder( |
| 64 | + padding: const EdgeInsets.all(8), |
| 65 | + itemCount: videos.length, |
| 66 | + itemBuilder: (BuildContext context, int index) => PR12Video( |
| 67 | + prID: index, |
| 68 | + video: videos[index], |
| 69 | + hideBookmarkIcon: showOnlyBookmarkItems)); |
| 70 | + }, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + final List<Video> videos = getInterestedVideos(); |
| 75 | + |
| 76 | + return ListView.builder( |
| 77 | + padding: const EdgeInsets.all(8), |
| 78 | + itemCount: videos.length, |
| 79 | + itemBuilder: (BuildContext context, int index) => PR12Video( |
| 80 | + prID: index, |
| 81 | + video: videos[index], |
| 82 | + hideBookmarkIcon: showOnlyBookmarkItems)); |
| 83 | + }, |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + List<Video> getInterestedVideos() { |
| 88 | + var videos = dataRef.where((video) { |
38 | 89 | final searchLower = query.toLowerCase(); |
39 | 90 | final titleLower = video.title.toLowerCase(); |
40 | 91 |
|
41 | 92 | return titleLower.contains(searchLower); |
42 | 93 | }).toList(); |
43 | | - |
44 | | - return ListView.builder( |
45 | | - padding: const EdgeInsets.all(8), |
46 | | - itemCount: videos.length, |
47 | | - itemBuilder: (BuildContext context, int index) => PR12Video( |
48 | | - prID: index, |
49 | | - video: videos[index], |
50 | | - hideBookmarkIcon: ignoreBookmarkIcon)); |
| 94 | + return videos; |
51 | 95 | } |
52 | 96 |
|
53 | 97 | @override |
54 | 98 | Widget buildResults(BuildContext context) { |
55 | | - return getSearchResult(); |
| 99 | + return getSearchResult(context); |
56 | 100 | } |
57 | 101 |
|
58 | 102 | @override |
59 | 103 | Widget buildSuggestions(BuildContext context) { |
60 | | - return getSearchResult(); |
| 104 | + return getSearchResult(context); |
61 | 105 | } |
62 | 106 | } |
0 commit comments