forked from SelfPrivacy/selfprivacy.org.app
Add a button to remove all removable server jobs
parent
10891881ae
commit
0425ea8ab8
|
@ -72,42 +72,35 @@ class ServerJobsCubit
|
||||||
return job;
|
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<void> removeServerJob(final String uid) async {
|
Future<void> 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);
|
final result = await api.removeApiJob(uid);
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
job.isHidden = false;
|
|
||||||
emit(
|
|
||||||
ServerJobsState(
|
|
||||||
serverJobList: state.serverJobList,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
getIt<NavigationService>().showSnackBar(result.message!);
|
getIt<NavigationService>().showSnackBar(result.message!);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
state.serverJobList.remove(job);
|
|
||||||
emit(
|
emit(
|
||||||
ServerJobsState(
|
ServerJobsState(
|
||||||
serverJobList: state.serverJobList,
|
serverJobList: [
|
||||||
|
for (final ServerJob job in state.serverJobList)
|
||||||
|
if (job.uid != uid) job
|
||||||
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
print('removed job $uid');
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> removeAllFinishedJobs() async {
|
||||||
|
final List<ServerJob> 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<void> reload({final bool useTimer = false}) async {
|
Future<void> reload({final bool useTimer = false}) async {
|
||||||
|
|
|
@ -17,8 +17,14 @@ class ServerJobsState extends ServerInstallationDependendState {
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool get hasRemovableJobs => serverJobList.any(
|
||||||
|
(final job) =>
|
||||||
|
job.status == JobStatusEnum.finished ||
|
||||||
|
job.status == JobStatusEnum.error,
|
||||||
|
);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object?> get props => [migrationJobUid, ..._serverJobList];
|
List<Object?> get props => [migrationJobUid, _serverJobList];
|
||||||
|
|
||||||
ServerJobsState copyWith({
|
ServerJobsState copyWith({
|
||||||
final List<ServerJob>? serverJobList,
|
final List<ServerJob>? serverJobList,
|
||||||
|
|
|
@ -23,6 +23,9 @@ class JobsContent extends StatelessWidget {
|
||||||
final List<ServerJob> serverJobs =
|
final List<ServerJob> serverJobs =
|
||||||
context.watch<ServerJobsCubit>().state.serverJobList;
|
context.watch<ServerJobsCubit>().state.serverJobList;
|
||||||
|
|
||||||
|
final bool hasRemovableJobs =
|
||||||
|
context.watch<ServerJobsCubit>().state.hasRemovableJobs;
|
||||||
|
|
||||||
return BlocBuilder<JobsCubit, JobsState>(
|
return BlocBuilder<JobsCubit, JobsState>(
|
||||||
builder: (final context, final state) {
|
builder: (final context, final state) {
|
||||||
late List<Widget> widgets;
|
late List<Widget> widgets;
|
||||||
|
@ -129,9 +132,23 @@ class JobsContent extends StatelessWidget {
|
||||||
if (serverJobs.isNotEmpty)
|
if (serverJobs.isNotEmpty)
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(8.0),
|
||||||
child: Text(
|
child: Row(
|
||||||
'jobs.server_jobs'.tr(),
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
style: Theme.of(context).textTheme.titleMedium,
|
children: [
|
||||||
|
Text(
|
||||||
|
'jobs.server_jobs'.tr(),
|
||||||
|
style: Theme.of(context).textTheme.titleMedium,
|
||||||
|
),
|
||||||
|
IconButton(
|
||||||
|
onPressed: hasRemovableJobs
|
||||||
|
? () => context
|
||||||
|
.read<ServerJobsCubit>()
|
||||||
|
.removeAllFinishedJobs()
|
||||||
|
: null,
|
||||||
|
icon: const Icon(Icons.clear_all),
|
||||||
|
color: Theme.of(context).colorScheme.onBackground,
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
...serverJobs.map(
|
...serverJobs.map(
|
||||||
|
|
Loading…
Reference in New Issue