Last active
July 6, 2022 19:16
-
-
Save pawelsa/41b33e548fb917d3ccd79fd4a7e5c59d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoadingStatus { | |
BehaviorSubject<bool>? _loadingController; | |
bool _isLoading = false; | |
bool _streamedInitialEvent = false; | |
set isLoading(bool isLoading) { | |
_isLoading = isLoading; | |
_publishLoadingState(); | |
} | |
bool get isLoading => _isLoading; | |
Stream<bool> listen() { | |
_loadingController ??= BehaviorSubject( | |
onListen: _publishInitialLoadingState, | |
onCancel: () { | |
_loadingController?.close(); | |
_loadingController = null; | |
_streamedInitialEvent = false; | |
}); | |
return _loadingController!.stream.distinct(); | |
} | |
void _publishInitialLoadingState() { | |
if (!_streamedInitialEvent) { | |
_publishLoadingState(); | |
_streamedInitialEvent = true; | |
} | |
} | |
void _publishLoadingState() { | |
_loadingController?.sink.add(_isLoading); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment