import 'dart:async'; import 'package:bloc_concurrency/bloc_concurrency.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:selfprivacy/config/get_it_config.dart'; import 'package:selfprivacy/logic/models/json/server_job.dart'; export 'package:provider/provider.dart'; part 'server_jobs_state.dart'; part 'server_jobs_event.dart'; class ServerJobsBloc extends Bloc { ServerJobsBloc() : super( ServerJobsInitialState(), ) { on( _mapServerJobsListChangedToState, transformer: sequential(), ); on( _mapRemoveServerJobToState, transformer: sequential(), ); on( _mapRemoveAllFinishedJobsToState, transformer: droppable(), ); final apiConnectionRepository = getIt(); _apiDataSubscription = apiConnectionRepository.dataStream.listen( (final ApiData apiData) { add( ServerJobsListChanged([...apiData.serverJobs.data ?? []]), ); }, ); } StreamSubscription? _apiDataSubscription; Future _mapServerJobsListChangedToState( final ServerJobsListChanged event, final Emitter emit, ) async { if (event.serverJobList.isEmpty) { emit(ServerJobsListEmptyState()); return; } final newState = ServerJobsListWithJobsState(serverJobList: event.serverJobList); emit(newState); } Future _mapRemoveServerJobToState( final RemoveServerJob event, final Emitter emit, ) async { await getIt().removeServerJob(event.uid); } Future _mapRemoveAllFinishedJobsToState( final RemoveAllFinishedJobs event, final Emitter emit, ) async { await getIt().removeAllFinishedServerJobs(); } Future migrateToBinds(final Map serviceToDisk) async { final fallbackDrive = getIt() .apiData .volumes .data ?.where((final drive) => drive.root) .first .name ?? 'sda1'; final result = await getIt() .api .migrateToBinds(serviceToDisk, fallbackDrive); if (result.data == null) { getIt() .showSnackBar(result.message!, behavior: SnackBarBehavior.floating); return; } } @override void onChange(final Change change) { super.onChange(change); } @override Future close() { _apiDataSubscription?.cancel(); return super.close(); } }