2024-02-09 15:01:05 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2024-01-26 14:46:09 +00:00
|
|
|
import 'package:equatable/equatable.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
|
|
|
|
|
|
|
part 'connection_status_event.dart';
|
|
|
|
part 'connection_status_state.dart';
|
|
|
|
|
|
|
|
class ConnectionStatusBloc
|
|
|
|
extends Bloc<ConnectionStatusEvent, ConnectionStatusState> {
|
|
|
|
ConnectionStatusBloc()
|
|
|
|
: super(
|
|
|
|
const ConnectionStatusState(
|
|
|
|
connectionStatus: ConnectionStatus.nonexistent,
|
|
|
|
),
|
|
|
|
) {
|
2024-02-09 15:01:05 +00:00
|
|
|
on<ConnectionStatusChanged>((final event, final emit) {
|
|
|
|
emit(ConnectionStatusState(connectionStatus: event.connectionStatus));
|
|
|
|
});
|
2024-01-26 14:46:09 +00:00
|
|
|
final apiConnectionRepository = getIt<ApiConnectionRepository>();
|
2024-02-09 15:01:05 +00:00
|
|
|
_apiConnectionStatusSubscription =
|
|
|
|
apiConnectionRepository.connectionStatusStream.listen(
|
2024-01-26 14:46:09 +00:00
|
|
|
(final ConnectionStatus connectionStatus) {
|
|
|
|
add(
|
|
|
|
ConnectionStatusChanged(connectionStatus),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamSubscription? _apiConnectionStatusSubscription;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> close() {
|
|
|
|
_apiConnectionStatusSubscription?.cancel();
|
|
|
|
return super.close();
|
2024-01-26 14:46:09 +00:00
|
|
|
}
|
|
|
|
}
|