import 'dart:async'; import 'package:bloc_concurrency/bloc_concurrency.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:selfprivacy/config/get_it_config.dart'; import 'package:selfprivacy/logic/models/hive/user.dart'; part 'users_event.dart'; part 'users_state.dart'; class UsersBloc extends Bloc { UsersBloc() : super(UsersInitial()) { on( _updateList, transformer: sequential(), ); on( _reload, transformer: droppable(), ); on( _mapConnectionStatusChangedToState, transformer: sequential(), ); final apiConnectionRepository = getIt(); _apiConnectionStatusSubscription = apiConnectionRepository.connectionStatusStream.listen( (final ConnectionStatus connectionStatus) { add( UsersConnectionStatusChanged(connectionStatus), ); }, ); _apiDataSubscription = apiConnectionRepository.dataStream.listen( (final ApiData apiData) { add( UsersListChanged(apiData.users.data ?? []), ); }, ); } Future _updateList( final UsersListChanged event, final Emitter emit, ) async { if (event.users.isEmpty) { emit(UsersInitial()); return; } final newState = UsersLoaded( users: event.users, ); emit(newState); } Future refresh() async { getIt().apiData.users.invalidate(); await getIt().reload(null); } Future _reload( final UsersListRefresh event, final Emitter emit, ) async { emit(UsersRefreshing(users: state.users)); await refresh(); } Future _mapConnectionStatusChangedToState( final UsersConnectionStatusChanged event, final Emitter emit, ) async { switch (event.connectionStatus) { case ConnectionStatus.nonexistent: emit(UsersInitial()); break; case ConnectionStatus.connected: if (state is! UsersLoaded) { emit(UsersRefreshing(users: state.users)); } case ConnectionStatus.reconnecting: case ConnectionStatus.offline: case ConnectionStatus.unauthorized: break; } } StreamSubscription? _apiDataSubscription; StreamSubscription? _apiConnectionStatusSubscription; @override void onChange(final Change change) { super.onChange(change); } @override Future close() { _apiDataSubscription?.cancel(); _apiConnectionStatusSubscription?.cancel(); return super.close(); } }