2024-01-26 14:46:09 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2024-01-31 12:17:27 +00:00
|
|
|
import 'package:bloc_concurrency/bloc_concurrency.dart';
|
2024-01-26 14:46:09 +00:00
|
|
|
import 'package:equatable/equatable.dart';
|
2024-02-23 16:50:28 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-01-26 14:46:09 +00:00
|
|
|
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<ServerJobsEvent, ServerJobsState> {
|
|
|
|
ServerJobsBloc()
|
|
|
|
: super(
|
|
|
|
ServerJobsInitialState(),
|
|
|
|
) {
|
|
|
|
on<ServerJobsListChanged>(
|
|
|
|
_mapServerJobsListChangedToState,
|
2024-01-31 12:17:27 +00:00
|
|
|
transformer: sequential(),
|
2024-01-26 14:46:09 +00:00
|
|
|
);
|
|
|
|
on<RemoveServerJob>(
|
|
|
|
_mapRemoveServerJobToState,
|
2024-01-31 12:17:27 +00:00
|
|
|
transformer: sequential(),
|
2024-01-26 14:46:09 +00:00
|
|
|
);
|
|
|
|
on<RemoveAllFinishedJobs>(
|
|
|
|
_mapRemoveAllFinishedJobsToState,
|
2024-01-31 12:17:27 +00:00
|
|
|
transformer: droppable(),
|
2024-01-26 14:46:09 +00:00
|
|
|
);
|
2024-01-31 11:04:59 +00:00
|
|
|
|
|
|
|
final apiConnectionRepository = getIt<ApiConnectionRepository>();
|
|
|
|
_apiDataSubscription = apiConnectionRepository.dataStream.listen(
|
|
|
|
(final ApiData apiData) {
|
|
|
|
add(
|
|
|
|
ServerJobsListChanged([...apiData.serverJobs.data ?? []]),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2024-01-26 14:46:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamSubscription? _apiDataSubscription;
|
|
|
|
|
|
|
|
Future<void> _mapServerJobsListChangedToState(
|
|
|
|
final ServerJobsListChanged event,
|
|
|
|
final Emitter<ServerJobsState> emit,
|
|
|
|
) async {
|
|
|
|
if (event.serverJobList.isEmpty) {
|
|
|
|
emit(ServerJobsListEmptyState());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final newState =
|
|
|
|
ServerJobsListWithJobsState(serverJobList: event.serverJobList);
|
|
|
|
emit(newState);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _mapRemoveServerJobToState(
|
|
|
|
final RemoveServerJob event,
|
|
|
|
final Emitter<ServerJobsState> emit,
|
|
|
|
) async {
|
|
|
|
await getIt<ApiConnectionRepository>().removeServerJob(event.uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _mapRemoveAllFinishedJobsToState(
|
|
|
|
final RemoveAllFinishedJobs event,
|
|
|
|
final Emitter<ServerJobsState> emit,
|
|
|
|
) async {
|
2024-01-29 16:14:12 +00:00
|
|
|
await getIt<ApiConnectionRepository>().removeAllFinishedServerJobs();
|
2024-01-26 14:46:09 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 16:50:28 +00:00
|
|
|
Future<void> migrateToBinds(final Map<String, String> serviceToDisk) async {
|
|
|
|
final fallbackDrive = getIt<ApiConnectionRepository>()
|
|
|
|
.apiData
|
|
|
|
.volumes
|
|
|
|
.data
|
|
|
|
?.where((final drive) => drive.root)
|
|
|
|
.first
|
|
|
|
.name ??
|
|
|
|
'sda1';
|
|
|
|
final result = await getIt<ApiConnectionRepository>()
|
|
|
|
.api
|
|
|
|
.migrateToBinds(serviceToDisk, fallbackDrive);
|
|
|
|
if (result.data == null) {
|
|
|
|
getIt<NavigationService>()
|
|
|
|
.showSnackBar(result.message!, behavior: SnackBarBehavior.floating);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 14:46:09 +00:00
|
|
|
@override
|
|
|
|
void onChange(final Change<ServerJobsState> change) {
|
|
|
|
super.onChange(change);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> close() {
|
|
|
|
_apiDataSubscription?.cancel();
|
|
|
|
return super.close();
|
|
|
|
}
|
|
|
|
}
|