SHARE
TWEET

Untitled

a guest Mar 19th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'dart:async';
  2. import 'package:bloc/bloc.dart';
  3. import 'package:meta/meta.dart';
  4. import 'package:flutter_todos/blocs/filtered_todos/filtered_todos.dart';
  5. import 'package:flutter_todos/blocs/todos/todos.dart';
  6. import 'package:flutter_todos/models/models.dart';
  7.  
  8. class FilteredTodosBloc extends Bloc<FilteredTodosEvent, FilteredTodosState> {
  9.   final TodosBloc todosBloc;
  10.   StreamSubscription todosSubscription;
  11.  
  12.   FilteredTodosBloc({@required this.todosBloc}) {
  13.     todosSubscription = todosBloc.state.listen((state) {
  14.       if (state is TodosLoaded) {
  15.         dispatch(UpdateTodos((todosBloc.currentState as TodosLoaded).todos));
  16.       }
  17.     });
  18.   }
  19.  
  20.   @override
  21.   FilteredTodosState get initialState {
  22.     return todosBloc.currentState is TodosLoaded
  23.         ? FilteredTodosLoaded(
  24.             (todosBloc.currentState as TodosLoaded).todos,
  25.             VisibilityFilter.all,
  26.           )
  27.         : FilteredTodosLoading();
  28.   }
  29.  
  30.   @override
  31.   Stream<FilteredTodosState> mapEventToState(
  32.     FilteredTodosState currentState,
  33.     FilteredTodosEvent event,
  34.   ) async* {
  35.     if (event is UpdateFilter) {
  36.       yield* _mapUpdateFilterToState(currentState, event);
  37.     } else if (event is UpdateTodos) {
  38.       yield* _mapTodosUpdatedToState(currentState, event);
  39.     }
  40.   }
  41.  
  42.   Stream<FilteredTodosState> _mapUpdateFilterToState(
  43.     FilteredTodosState currentState,
  44.     UpdateFilter event,
  45.   ) async* {
  46.     if (todosBloc.currentState is TodosLoaded) {
  47.       yield FilteredTodosLoaded(
  48.         _mapTodosToFilteredTodos(
  49.           (todosBloc.currentState as TodosLoaded).todos,
  50.           event.filter,
  51.         ),
  52.         event.filter,
  53.       );
  54.     }
  55.   }
  56.  
  57.   Stream<FilteredTodosState> _mapTodosUpdatedToState(
  58.     FilteredTodosState currentState,
  59.     UpdateTodos event,
  60.   ) async* {
  61.     final visibilityFilter = currentState is FilteredTodosLoaded
  62.         ? currentState.activeFilter
  63.         : VisibilityFilter.all;
  64.     yield FilteredTodosLoaded(
  65.       _mapTodosToFilteredTodos(
  66.         (todosBloc.currentState as TodosLoaded).todos,
  67.         visibilityFilter,
  68.       ),
  69.       visibilityFilter,
  70.     );
  71.   }
  72.  
  73.   List<Todo> _mapTodosToFilteredTodos(
  74.       List<Todo> todos, VisibilityFilter filter) {
  75.     return todos.where((todo) {
  76.       if (filter == VisibilityFilter.all) {
  77.         return true;
  78.       } else if (filter == VisibilityFilter.active) {
  79.         return !todo.complete;
  80.       } else if (filter == VisibilityFilter.completed) {
  81.         return todo.complete;
  82.       }
  83.     }).toList();
  84.   }
  85.  
  86.   @override
  87.   void dispose() {
  88.     todosSubscription.cancel();
  89.     super.dispose();
  90.   }
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top