Line data Source code
1 : import 'dart:async'; 2 : 3 : import 'package:rx_bloc/rx_bloc.dart'; 4 : import 'package:rxdart/rxdart.dart'; 5 : 6 : part 'loading_bloc.rxb.g.dart'; 7 : part 'loading_bloc_extensions.dart'; 8 : part 'loading_bloc_models.dart'; 9 : 10 : /// The states of LoadingBloc 11 : abstract class LoadingBlocStates { 12 : /// 13 : /// The isLoading stream starts with initial value false. 14 : /// It can be triggered by invoking [LoadingBlocEvents.setResult] 15 : /// 16 : /// **Example:** 17 : /// # Input [LoadingBlocEvents.setLoading] 18 : /// - |-------true-----false-----------> 19 : /// - |-------------true------false-> 20 : /// - |----------------------------------------true----------false-> 21 : /// # are merged into one stream *isLoading* 22 : /// - |false--true------------false------------true----------false-> 23 : Stream<LoadingWithTag> get isLoadingWithTag; 24 : 25 : /// 26 : /// Get global isLoading stream 27 : /// 28 : Stream<bool> get isLoading; 29 : 30 : /// 31 : /// Get isLoading stream for a specific tag 32 : /// 33 : @RxBlocIgnoreState() 34 : Stream<bool> isLoadingForTag(String tag); 35 : } 36 : 37 : /// The events of LoadingBloc 38 : abstract class LoadingBlocEvents { 39 : /// Set [result] to BloC 40 : /// 41 : /// To observe the current loading state subscribe for 42 : /// [LoadingBlocStates.isLoading] 43 : void setResult({required Result result}); 44 : } 45 : 46 : /// The BloC that handles is loading state. 47 : /// 48 : /// Each bloc has a internal property of [LoadingBloc], which allows to be used: 49 : /// 1. setStateHandler(...) 50 : /// 2. setLoadingStateHandler(...) 51 : @RxBloc() 52 : class LoadingBloc extends $LoadingBloc { 53 : /// Default constructor 54 1 : LoadingBloc() { 55 1 : _$setLoadingEvent 56 2 : .bindToLoadingCounts(_loadingCounts) 57 2 : .disposedBy(_compositeSubscription); 58 : } 59 : 60 : final _loadingCounts = BehaviorSubject.seeded({ 61 : '': BehaviorSubject.seeded( 62 : _TagCountTuple( 63 : tag: '', // default tag value 64 : count: 0, 65 : ), 66 : ) 67 : }); 68 : 69 : final _compositeSubscription = CompositeSubscription(); 70 : 71 1 : @override 72 : Stream<LoadingWithTag> _mapToIsLoadingWithTagState() => 73 3 : _loadingCounts.mapToLoadingWithTag().shareReplay(maxSize: 1); 74 : 75 1 : @override 76 1 : Stream<bool> _mapToIsLoadingState() => _isLoadingWithTagState 77 3 : .map((event) => event.loading) 78 1 : .shareReplay(maxSize: 1); 79 : 80 1 : @override 81 1 : Stream<bool> isLoadingForTag(String tag) => _isLoadingWithTagState 82 4 : .where((event) => event.tag == tag) 83 3 : .map((event) => event.loading) 84 1 : .startWith(false) 85 1 : .shareReplay(maxSize: 1); 86 : }