diff --git a/lib/logic/cubit/server_jobs/server_jobs_cubit.dart b/lib/logic/cubit/server_jobs/server_jobs_cubit.dart index 43b2b2d5..1e808956 100644 --- a/lib/logic/cubit/server_jobs/server_jobs_cubit.dart +++ b/lib/logic/cubit/server_jobs/server_jobs_cubit.dart @@ -72,42 +72,35 @@ class ServerJobsCubit return job; } - /// Get the job object and change its isHidden to true. - /// Emit the new state. - /// Call the api to remove the job. - /// If the api call fails, change the isHidden to false and emit the new state. - /// If the api call succeeds, remove the job from the list and emit the new state. Future removeServerJob(final String uid) async { - final ServerJob? job = getServerJobByUid(uid); - if (job == null) { - return; - } - - job.isHidden = true; - emit( - ServerJobsState( - serverJobList: state.serverJobList, - ), - ); - final result = await api.removeApiJob(uid); if (!result.success) { - job.isHidden = false; - emit( - ServerJobsState( - serverJobList: state.serverJobList, - ), - ); getIt().showSnackBar(result.message!); return; } - state.serverJobList.remove(job); emit( ServerJobsState( - serverJobList: state.serverJobList, + serverJobList: [ + for (final ServerJob job in state.serverJobList) + if (job.uid != uid) job + ], ), ); + print('removed job $uid'); + } + + Future removeAllFinishedJobs() async { + final List finishedJobs = state.serverJobList + .where( + (final ServerJob job) => + job.status == JobStatusEnum.finished || + job.status == JobStatusEnum.error, + ) + .toList(); + for (final ServerJob job in finishedJobs) { + await removeServerJob(job.uid); + } } Future reload({final bool useTimer = false}) async { diff --git a/lib/logic/cubit/server_jobs/server_jobs_state.dart b/lib/logic/cubit/server_jobs/server_jobs_state.dart index cf9cb8e0..2acc487c 100644 --- a/lib/logic/cubit/server_jobs/server_jobs_state.dart +++ b/lib/logic/cubit/server_jobs/server_jobs_state.dart @@ -17,8 +17,14 @@ class ServerJobsState extends ServerInstallationDependendState { return list; } + bool get hasRemovableJobs => serverJobList.any( + (final job) => + job.status == JobStatusEnum.finished || + job.status == JobStatusEnum.error, + ); + @override - List get props => [migrationJobUid, ..._serverJobList]; + List get props => [migrationJobUid, _serverJobList]; ServerJobsState copyWith({ final List? serverJobList, diff --git a/lib/ui/components/jobs_content/jobs_content.dart b/lib/ui/components/jobs_content/jobs_content.dart index d43a194c..877770dc 100644 --- a/lib/ui/components/jobs_content/jobs_content.dart +++ b/lib/ui/components/jobs_content/jobs_content.dart @@ -23,6 +23,9 @@ class JobsContent extends StatelessWidget { final List serverJobs = context.watch().state.serverJobList; + final bool hasRemovableJobs = + context.watch().state.hasRemovableJobs; + return BlocBuilder( builder: (final context, final state) { late List widgets; @@ -129,9 +132,23 @@ class JobsContent extends StatelessWidget { if (serverJobs.isNotEmpty) Padding( padding: const EdgeInsets.all(8.0), - child: Text( - 'jobs.server_jobs'.tr(), - style: Theme.of(context).textTheme.titleMedium, + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + 'jobs.server_jobs'.tr(), + style: Theme.of(context).textTheme.titleMedium, + ), + IconButton( + onPressed: hasRemovableJobs + ? () => context + .read() + .removeAllFinishedJobs() + : null, + icon: const Icon(Icons.clear_all), + color: Theme.of(context).colorScheme.onBackground, + ), + ], ), ), ...serverJobs.map(