LCOV - code coverage report
Current view: top level - view - view_state_listener.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 13 13 100.0 %
Date: 2020-03-25 12:10:52 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:flutter/widgets.dart';
       2             : import 'package:flutter_bloc/flutter_bloc.dart';
       3             : import 'package:flutter_bloc_patterns/src/view/view_state.dart';
       4             : 
       5             : /// Callback function for the data loading state.
       6             : typedef LoadingCallback = void Function(BuildContext context);
       7             : 
       8             : /// Callback function for a success. The data was fetched and nonnull
       9             : /// element was returned.
      10             : typedef SuccessCallback<T> = void Function(BuildContext context, T data);
      11             : 
      12             : /// Callback function for the data refreshing state
      13             : typedef RefreshingCallback<T> = void Function(BuildContext context, T data);
      14             : 
      15             : /// Callback function for no result. The data was fetched
      16             : /// successfully, but a null element was returned.
      17             : typedef EmptyCallback = void Function(BuildContext context);
      18             : 
      19             : /// Callback function for an error. It contains an [error] that has caused
      20             : /// which may allow a view to react differently on different errors.
      21             : typedef ErrorCallback = void Function(
      22             :   BuildContext context,
      23             :   dynamic error,
      24             : );
      25             : 
      26             : /// [ViewStateListener] is responsible for performing an action based on the
      27             : /// [ViewState].
      28             : /// It should be used for functionality that needs to occur only in response to
      29             : /// a [state] change such as navigation, showing a [SnackBar], showing
      30             : /// a [Dialog], etc.
      31             : /// [ViewStateListener] is a wrapper over the [BlocListener] widget so it accepts
      32             : /// a [bloc] object as well as [child] widget and a set of handy callbacks
      33             : /// corresponding to a given state:
      34             : /// [onLoading] callback for the data loading state,
      35             : /// [onRefreshing] callback for the data refreshing state,
      36             : /// [onSuccess] callback for the data success state,
      37             : /// [onEmpty] callback for for no result state,
      38             : /// [onError] callback function for an error state.
      39             : ///
      40             : /// [T] - the type of elements,
      41             : /// [B] - the type of bloc.
      42             : class ViewStateListener<T, B extends Bloc<dynamic, ViewState>>
      43             :     extends BlocListener<B, ViewState> {
      44           1 :   ViewStateListener({
      45             :     Key key,
      46             :     B bloc,
      47             :     BlocListenerCondition<ViewState> condition,
      48             :     LoadingCallback onLoading,
      49             :     RefreshingCallback<T> onRefreshing,
      50             :     SuccessCallback<T> onSuccess,
      51             :     EmptyCallback onEmpty,
      52             :     ErrorCallback onError,
      53             :     Widget child,
      54           1 :   }) : super(
      55             :           key: key,
      56             :           bloc: bloc,
      57             :           condition: condition,
      58             :           child: child,
      59           1 :           listener: (BuildContext context, ViewState state) {
      60           1 :             if (state is Loading) {
      61           1 :               onLoading?.call(context);
      62           1 :             } else if (state is Refreshing<T>) {
      63           2 :               onRefreshing?.call(context, state.data);
      64           1 :             } else if (state is Success<T>) {
      65           2 :               onSuccess?.call(context, state.data);
      66           1 :             } else if (state is Empty) {
      67           1 :               onEmpty?.call(context);
      68           1 :             } else if (state is Failure) {
      69           2 :               onError?.call(context, state.error);
      70             :             }
      71             :           },
      72             :         );
      73             : }

Generated by: LCOV version 1.14