Line data Source code
1 : import 'package:flutter/foundation.dart';
2 : import 'package:flutter/material.dart';
3 : import 'package:stream_feed/stream_feed.dart';
4 : import 'package:stream_feed_flutter_core/src/states/states.dart';
5 : import 'package:stream_feed_flutter_core/src/typedefs.dart';
6 : import 'package:stream_feed_flutter_core/stream_feed_flutter_core.dart';
7 :
8 : // ignore_for_file: cascade_invocations
9 :
10 : //TODO: other things to add to core: FollowListCore, UserListCore
11 :
12 : /// [ReactionListCore] is a simplified class that allows fetching a list of
13 : /// reactions while exposing UI builders.
14 : ///
15 : ///
16 : /// ```dart
17 : /// class FlatActivityListPage extends StatelessWidget {
18 : /// @override
19 : /// Widget build(BuildContext context) {
20 : /// return Scaffold(
21 : /// body: ReactionListCore(
22 : /// onErrorWidget: Center(
23 : /// child: Text('An error has occurred'),
24 : /// ),
25 : /// onEmptyWidget: Center(
26 : /// child: Text('Nothing here...'),
27 : /// ),
28 : /// onProgressWidget: Center(
29 : /// child: CircularProgressIndicator(),
30 : /// ),
31 : /// feedBuilder: (context, reactions, idx) {
32 : /// return YourReactionWidget(reaction: reactions[idx]);
33 : /// }
34 : /// ),
35 : /// );
36 : /// }
37 : /// }
38 : /// ```
39 : ///
40 : /// Make sure to have a [StreamFeedCore] ancestor in order to provide the
41 : /// information about the reactions.
42 : class GenericReactionListCore<A, Ob, T, Or> extends StatefulWidget {
43 1 : const GenericReactionListCore({
44 : Key? key,
45 : required this.reactionsBuilder,
46 : required this.lookupValue,
47 : this.onErrorWidget = const ErrorStateWidget(),
48 : this.onProgressWidget = const ProgressStateWidget(),
49 : this.onEmptyWidget =
50 : const EmptyStateWidget(message: 'No comments to display'),
51 : this.lookupAttr = LookupAttribute.activityId,
52 : this.filter,
53 : this.flags,
54 : this.kind,
55 : this.limit,
56 1 : }) : super(key: key);
57 :
58 : /// A builder that allows building a ListView of Reaction based Widgets
59 : final ReactionsBuilder reactionsBuilder;
60 :
61 : /// A builder for building widgets to show on error
62 : final Widget onErrorWidget;
63 :
64 : /// A builder for building widgets to show on progress
65 : final Widget onProgressWidget;
66 :
67 : /// A builder for building widgets to show on empty
68 : final Widget onEmptyWidget;
69 :
70 : /// Lookup objects based on attributes
71 : final LookupAttribute lookupAttr;
72 :
73 : /// TODO: document me
74 : final String lookupValue;
75 :
76 : /// {@macro filter}
77 : final Filter? filter;
78 :
79 : /// The flags to use for the request
80 : final EnrichmentFlags? flags;
81 :
82 : /// The limit of activities to fetch
83 : final int? limit;
84 :
85 : /// The kind of reaction
86 : final String? kind;
87 :
88 1 : @override
89 : _GenericReactionListCoreState<A, Ob, T, Or> createState() =>
90 1 : _GenericReactionListCoreState<A, Ob, T, Or>();
91 : }
92 :
93 : class _GenericReactionListCoreState<A, Ob, T, Or>
94 : extends State<GenericReactionListCore<A, Ob, T, Or>> {
95 : late GenericFeedBloc<A, Ob, T, Or> bloc;
96 :
97 1 : @override
98 : void didChangeDependencies() {
99 1 : super.didChangeDependencies();
100 4 : bloc = GenericFeedProvider<A, Ob, T, Or>.of(context).bloc;
101 1 : loadData();
102 : }
103 :
104 : /// Fetches initial reactions and updates the widget
105 3 : Future<void> loadData() => bloc.queryReactions(
106 2 : widget.lookupAttr,
107 2 : widget.lookupValue,
108 2 : filter: widget.filter,
109 2 : flags: widget.flags,
110 2 : limit: widget.limit,
111 2 : kind: widget.kind,
112 : );
113 :
114 1 : @override
115 : Widget build(BuildContext context) {
116 1 : return StreamBuilder<List<Reaction>>(
117 4 : stream: bloc.getReactionsStream(widget.lookupValue,
118 2 : widget.kind), //reactionsStreamFor(widget.lookupValue)
119 1 : builder: (context, snapshot) {
120 1 : if (snapshot.hasError) {
121 0 : return widget.onErrorWidget; //snapshot.error
122 : }
123 1 : if (!snapshot.hasData) {
124 2 : return widget.onProgressWidget;
125 : }
126 0 : final reactions = snapshot.data!;
127 0 : if (reactions.isEmpty) {
128 0 : return widget.onEmptyWidget;
129 : }
130 0 : return ListView.builder(
131 : shrinkWrap: true,
132 0 : itemCount: reactions.length,
133 0 : itemBuilder: (context, idx) => widget.reactionsBuilder(
134 : context,
135 : reactions,
136 : idx,
137 : ),
138 : );
139 : });
140 : }
141 : }
|