Skip to content

Commit

Permalink
refactor(polls_container): improve _streamBuilder params definition
Browse files Browse the repository at this point in the history
* add class generic types
  • Loading branch information
albertms10 committed Jan 9, 2021
1 parent 2ece435 commit 476cf3a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
8 changes: 4 additions & 4 deletions lib/pages/feed_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import 'package:stay_home_polls_app/model/poll.dart';
import 'package:stay_home_polls_app/pages/page_content.dart';
import 'package:stay_home_polls_app/widgets/polls_container.dart';

class FeedContent extends StatelessWidget {
class FeedContent<T extends Poll> extends StatelessWidget {
const FeedContent();

List<Poll> _filterCallback(List<Poll> pollsList, List<Poll> userPollsList) {
List<T> _filterCallback(List<T> pollsList, List<T> userPollsList) {
return pollsList.where((poll) {
final userPoll = userPollsList.firstWhere(
(userPoll) => userPoll != null ? poll.id == userPoll.id : false,
Expand All @@ -33,11 +33,11 @@ class FeedContent extends StatelessWidget {
],
emptyMessage: 'You ran out of polls',
children: [
PollsContainer(
PollsContainer<T>(
streamPollsList: popularPollListSnapshots(),
filterCallback: _filterCallback,
),
PollsContainer(
PollsContainer<T>(
streamPollsList: latestPollListSnapshots(),
filterCallback: _filterCallback,
),
Expand Down
14 changes: 7 additions & 7 deletions lib/pages/user_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import 'package:stay_home_polls_app/model/poll.dart';
import 'package:stay_home_polls_app/pages/page_content.dart';
import 'package:stay_home_polls_app/widgets/polls_container.dart';

class UserContent extends StatelessWidget {
class UserContent<T extends Poll> extends StatelessWidget {
const UserContent();

List<Poll> _filterCallback(
List<Poll> pollsList,
List<Poll> userPollsList,
bool Function(Poll, Poll) check,
List<T> _filterCallback(
List<T> pollsList,
List<T> userPollsList,
bool Function(T, T) check,
) {
return pollsList.where((poll) {
final userPoll = userPollsList.firstWhere(
Expand All @@ -35,15 +35,15 @@ class UserContent extends StatelessWidget {
],
emptyMessage: 'No polls yet',
children: [
PollsContainer(
PollsContainer<T>(
streamPollsList: popularPollListSnapshots(),
filterCallback: (pollsList, userPollsList) => _filterCallback(
pollsList,
userPollsList,
(poll, userPoll) => poll.id == userPoll.id && userPoll.isAuth,
),
),
PollsContainer(
PollsContainer<T>(
streamPollsList: popularPollListSnapshots(),
filterCallback: (pollsList, userPollsList) => _filterCallback(
pollsList,
Expand Down
37 changes: 22 additions & 15 deletions lib/widgets/polls_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import 'package:stay_home_polls_app/model/poll.dart';
import 'package:stay_home_polls_app/model/user.dart';
import 'package:stay_home_polls_app/widgets/polls_list.dart';

class PollsContainer extends StatelessWidget {
final Stream<List<Poll>> streamPollsList;
final List<Poll> Function(List<Poll>, List<Poll>) filterCallback;
class PollsContainer<T extends Poll> extends StatelessWidget {
final Stream<List<T>> streamPollsList;
final List<T> Function(List<T>, List<T>) filterCallback;

const PollsContainer({this.streamPollsList, this.filterCallback});

StreamBuilder<List<Poll>> _streamBuilder(
stream,
Widget Function(List<Poll>) callback,
) {
StreamBuilder<List<T>> _streamBuilder({
Stream<List<T>> stream,
Widget Function(BuildContext, List<T>) builder,
}) {
return StreamBuilder(
stream: stream,
builder: (context, snapshot) {
Expand All @@ -26,7 +26,7 @@ class PollsContainer extends StatelessWidget {
return const Center(child: CircularProgressIndicator());

case ConnectionState.active:
return callback(snapshot.data);
return builder(context, snapshot.data);

case ConnectionState.done:
return const Center(child: Text('Connection done'));
Expand All @@ -43,13 +43,20 @@ class PollsContainer extends StatelessWidget {
Widget build(BuildContext context) {
final user = Provider.of<User>(context);

return _streamBuilder(streamPollsList, (polls) {
return _streamBuilder(user.pollsSnapshots(), (userPolls) {
return PollsList(
polls:
filterCallback != null ? filterCallback(polls, userPolls) : polls,
return _streamBuilder(
stream: streamPollsList,
builder: (context, polls) {
return _streamBuilder(
stream: user.pollsSnapshots(),
builder: (context, userPolls) {
return PollsList(
polls: filterCallback != null
? filterCallback(polls, userPolls)
: polls,
);
},
);
});
});
},
);
}
}

0 comments on commit 476cf3a

Please sign in to comment.