2024-02-20 16:33:24 +00:00
|
|
|
import 'package:collection/collection.dart';
|
2021-05-25 21:53:54 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2024-01-31 05:14:23 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2021-07-29 05:24:42 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2021-06-20 21:08:52 +00:00
|
|
|
import 'package:selfprivacy/config/brand_theme.dart';
|
2024-01-31 10:57:12 +00:00
|
|
|
import 'package:selfprivacy/logic/bloc/server_jobs/server_jobs_bloc.dart';
|
2022-08-30 03:09:09 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/client_jobs/client_jobs_cubit.dart';
|
2022-05-18 09:07:14 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
2022-09-18 20:12:09 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/server_job.dart';
|
2024-01-31 05:14:23 +00:00
|
|
|
import 'package:selfprivacy/ui/components/buttons/brand_button.dart';
|
2022-08-26 02:34:25 +00:00
|
|
|
import 'package:selfprivacy/ui/components/jobs_content/server_job_card.dart';
|
2022-11-09 15:49:37 +00:00
|
|
|
import 'package:selfprivacy/ui/helpers/modals.dart';
|
2021-05-25 21:53:54 +00:00
|
|
|
|
|
|
|
class JobsContent extends StatelessWidget {
|
2023-06-29 09:53:13 +00:00
|
|
|
const JobsContent({
|
|
|
|
required this.controller,
|
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
|
|
|
final ScrollController controller;
|
2021-05-25 21:53:54 +00:00
|
|
|
|
2024-02-20 16:33:24 +00:00
|
|
|
IconData _getIcon(final JobStatusEnum status) {
|
|
|
|
switch (status) {
|
|
|
|
case JobStatusEnum.created:
|
|
|
|
return Icons.query_builder_outlined;
|
|
|
|
case JobStatusEnum.running:
|
|
|
|
return Icons.pending_outlined;
|
|
|
|
case JobStatusEnum.finished:
|
|
|
|
return Icons.check_circle_outline;
|
|
|
|
case JobStatusEnum.error:
|
|
|
|
return Icons.error_outline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Color _getColor(final JobStatusEnum status, final BuildContext context) {
|
|
|
|
switch (status) {
|
|
|
|
case JobStatusEnum.created:
|
|
|
|
return Theme.of(context).colorScheme.secondary;
|
|
|
|
case JobStatusEnum.running:
|
|
|
|
return Theme.of(context).colorScheme.tertiary;
|
|
|
|
case JobStatusEnum.finished:
|
|
|
|
return Theme.of(context).colorScheme.primary;
|
|
|
|
case JobStatusEnum.error:
|
|
|
|
return Theme.of(context).colorScheme.error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 21:53:54 +00:00
|
|
|
@override
|
2022-09-18 20:12:09 +00:00
|
|
|
Widget build(final BuildContext context) {
|
|
|
|
final List<ServerJob> serverJobs =
|
2024-01-26 14:46:09 +00:00
|
|
|
context.watch<ServerJobsBloc>().state.serverJobList;
|
2022-06-05 22:40:34 +00:00
|
|
|
|
2022-09-19 00:42:00 +00:00
|
|
|
final bool hasRemovableJobs =
|
2024-01-26 14:46:09 +00:00
|
|
|
context.watch<ServerJobsBloc>().state.hasRemovableJobs;
|
2022-09-19 00:42:00 +00:00
|
|
|
|
2024-03-08 11:14:23 +00:00
|
|
|
final bool hasBlockingJobs =
|
|
|
|
context.watch<ServerJobsBloc>().state.hasJobsBlockingRebuild;
|
|
|
|
|
2022-09-18 20:12:09 +00:00
|
|
|
return BlocBuilder<JobsCubit, JobsState>(
|
|
|
|
builder: (final context, final state) {
|
|
|
|
late List<Widget> widgets;
|
|
|
|
final ServerInstallationState installationState =
|
|
|
|
context.read<ServerInstallationCubit>().state;
|
|
|
|
if (state is JobsStateEmpty) {
|
|
|
|
widgets = [
|
|
|
|
const SizedBox(height: 80),
|
2023-03-27 17:02:44 +00:00
|
|
|
Center(
|
|
|
|
child: Text(
|
|
|
|
'jobs.empty'.tr(),
|
|
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
|
|
),
|
|
|
|
),
|
2022-09-18 20:12:09 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if (installationState is ServerInstallationFinished) {
|
2022-06-05 22:40:34 +00:00
|
|
|
widgets = [
|
2022-09-18 20:12:09 +00:00
|
|
|
...widgets,
|
2022-06-05 22:40:34 +00:00
|
|
|
const SizedBox(height: 80),
|
2022-09-18 20:12:09 +00:00
|
|
|
BrandButton.rised(
|
|
|
|
onPressed: () => context.read<JobsCubit>().upgradeServer(),
|
2022-10-03 23:32:35 +00:00
|
|
|
text: 'jobs.upgrade_server'.tr(),
|
2022-09-18 20:12:09 +00:00
|
|
|
),
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
BrandButton.text(
|
2022-11-09 15:49:37 +00:00
|
|
|
title: 'jobs.reboot_server'.tr(),
|
2022-09-18 20:12:09 +00:00
|
|
|
onPressed: () {
|
2022-11-09 15:49:37 +00:00
|
|
|
showPopUpAlert(
|
|
|
|
alertTitle: 'jobs.reboot_server'.tr(),
|
|
|
|
description: 'modals.are_you_sure'.tr(),
|
|
|
|
actionButtonTitle: 'modals.reboot'.tr(),
|
|
|
|
actionButtonOnPressed: () =>
|
|
|
|
{context.read<JobsCubit>().rebootServer()},
|
2022-09-18 20:12:09 +00:00
|
|
|
);
|
|
|
|
},
|
2022-06-05 22:40:34 +00:00
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
2022-09-18 20:12:09 +00:00
|
|
|
} else if (state is JobsStateLoading) {
|
|
|
|
widgets = [
|
2024-02-20 16:33:24 +00:00
|
|
|
...state.clientJobList.map(
|
|
|
|
(final j) => Row(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Icon(
|
|
|
|
_getIcon(j.status),
|
|
|
|
color: _getColor(j.status, context),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Card(
|
|
|
|
color: Theme.of(context).colorScheme.surfaceVariant,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 15,
|
|
|
|
vertical: 10,
|
|
|
|
),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
j.title,
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.labelLarge
|
|
|
|
?.copyWith(
|
|
|
|
color: Theme.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.onSurfaceVariant,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (j.message != null)
|
|
|
|
Text(
|
|
|
|
j.message!,
|
|
|
|
style: Theme.of(context).textTheme.labelSmall,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (state.rebuildRequired)
|
|
|
|
Builder(
|
|
|
|
builder: (final context) {
|
|
|
|
final rebuildJob = serverJobs.firstWhereOrNull(
|
|
|
|
(final job) => job.uid == state.rebuildJobUid,
|
|
|
|
);
|
|
|
|
return Row(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Icon(
|
|
|
|
_getIcon(rebuildJob?.status ?? JobStatusEnum.created),
|
|
|
|
color: _getColor(
|
|
|
|
rebuildJob?.status ?? JobStatusEnum.created,
|
|
|
|
context,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Card(
|
|
|
|
color: Theme.of(context).colorScheme.surfaceVariant,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 15,
|
|
|
|
vertical: 10,
|
|
|
|
),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
rebuildJob?.name ??
|
|
|
|
'jobs.rebuild_system'.tr(),
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.labelLarge
|
|
|
|
?.copyWith(
|
|
|
|
color: Theme.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.onSurfaceVariant,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (rebuildJob?.description != null)
|
|
|
|
Text(
|
|
|
|
rebuildJob!.description,
|
|
|
|
style:
|
|
|
|
Theme.of(context).textTheme.labelSmall,
|
|
|
|
),
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
LinearProgressIndicator(
|
|
|
|
value: rebuildJob?.progress == null
|
|
|
|
? 0.0
|
|
|
|
: ((rebuildJob!.progress ?? 0) < 1)
|
|
|
|
? null
|
|
|
|
: rebuildJob.progress! / 100.0,
|
|
|
|
color: _getColor(
|
|
|
|
rebuildJob?.status ?? JobStatusEnum.created,
|
|
|
|
context,
|
|
|
|
),
|
|
|
|
backgroundColor: Theme.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.surfaceVariant,
|
|
|
|
minHeight: 7.0,
|
|
|
|
borderRadius: BorderRadius.circular(7.0),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
if (rebuildJob?.error != null ||
|
|
|
|
rebuildJob?.result != null ||
|
|
|
|
rebuildJob?.statusText != null)
|
|
|
|
Text(
|
|
|
|
rebuildJob?.error ??
|
|
|
|
rebuildJob?.result ??
|
|
|
|
rebuildJob?.statusText ??
|
|
|
|
'',
|
|
|
|
style:
|
|
|
|
Theme.of(context).textTheme.labelSmall,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
];
|
|
|
|
} else if (state is JobsStateFinished) {
|
|
|
|
widgets = [
|
|
|
|
...state.clientJobList.map(
|
|
|
|
(final j) => Row(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Icon(
|
|
|
|
_getIcon(j.status),
|
|
|
|
color: _getColor(j.status, context),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Card(
|
|
|
|
color: Theme.of(context).colorScheme.surfaceVariant,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 15,
|
|
|
|
vertical: 10,
|
|
|
|
),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
j.title,
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.labelLarge
|
|
|
|
?.copyWith(
|
|
|
|
color: Theme.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.onSurfaceVariant,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (j.message != null)
|
|
|
|
Text(
|
|
|
|
j.message!,
|
|
|
|
style: Theme.of(context).textTheme.labelSmall,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (state.rebuildRequired)
|
|
|
|
Builder(
|
|
|
|
builder: (final context) {
|
|
|
|
final rebuildJob = serverJobs.firstWhereOrNull(
|
|
|
|
(final job) => job.uid == state.rebuildJobUid,
|
|
|
|
);
|
|
|
|
if (rebuildJob == null) {
|
|
|
|
return const SizedBox();
|
|
|
|
}
|
|
|
|
return Row(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Icon(
|
|
|
|
_getIcon(rebuildJob.status),
|
|
|
|
color: _getColor(
|
|
|
|
rebuildJob.status,
|
|
|
|
context,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Card(
|
|
|
|
color: Theme.of(context).colorScheme.surfaceVariant,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 15,
|
|
|
|
vertical: 10,
|
|
|
|
),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
rebuildJob.name,
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.labelLarge
|
|
|
|
?.copyWith(
|
|
|
|
color: Theme.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.onSurfaceVariant,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
rebuildJob.description,
|
|
|
|
style: Theme.of(context).textTheme.labelSmall,
|
|
|
|
),
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
LinearProgressIndicator(
|
|
|
|
value: rebuildJob.progress == null
|
|
|
|
? 0.0
|
|
|
|
: ((rebuildJob.progress ?? 0) < 1)
|
|
|
|
? null
|
|
|
|
: rebuildJob.progress! / 100.0,
|
|
|
|
color: _getColor(
|
|
|
|
rebuildJob.status,
|
|
|
|
context,
|
|
|
|
),
|
|
|
|
backgroundColor: Theme.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.surfaceVariant,
|
|
|
|
minHeight: 7.0,
|
|
|
|
borderRadius: BorderRadius.circular(7.0),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
if (rebuildJob.error != null ||
|
|
|
|
rebuildJob.result != null ||
|
|
|
|
rebuildJob.statusText != null)
|
|
|
|
Text(
|
|
|
|
rebuildJob.error ??
|
|
|
|
rebuildJob.result ??
|
|
|
|
rebuildJob.statusText ??
|
|
|
|
'',
|
|
|
|
style:
|
|
|
|
Theme.of(context).textTheme.labelSmall,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
BrandButton.rised(
|
|
|
|
onPressed: () => context.read<JobsCubit>().acknowledgeFinished(),
|
|
|
|
text: 'basis.done'.tr(),
|
|
|
|
),
|
2022-09-18 20:12:09 +00:00
|
|
|
];
|
|
|
|
} else if (state is JobsStateWithJobs) {
|
|
|
|
widgets = [
|
2023-03-27 17:29:02 +00:00
|
|
|
...state.clientJobList.map(
|
|
|
|
(final j) => Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
2023-04-05 10:33:53 +00:00
|
|
|
child: Card(
|
|
|
|
color: Theme.of(context).colorScheme.surfaceVariant,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 15,
|
|
|
|
vertical: 10,
|
|
|
|
),
|
2024-02-20 16:33:24 +00:00
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
j.title,
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.labelLarge
|
|
|
|
?.copyWith(
|
2023-04-05 10:33:53 +00:00
|
|
|
color: Theme.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.onSurfaceVariant,
|
|
|
|
),
|
2024-02-20 16:33:24 +00:00
|
|
|
),
|
|
|
|
if (j.message != null)
|
|
|
|
Text(
|
|
|
|
j.message!,
|
|
|
|
style: Theme.of(context).textTheme.labelSmall,
|
|
|
|
),
|
|
|
|
],
|
2023-04-05 10:33:53 +00:00
|
|
|
),
|
|
|
|
),
|
2023-03-27 17:29:02 +00:00
|
|
|
),
|
|
|
|
),
|
2024-02-20 16:33:24 +00:00
|
|
|
const SizedBox(width: 8),
|
2023-03-27 17:29:02 +00:00
|
|
|
ElevatedButton(
|
|
|
|
style: ElevatedButton.styleFrom(
|
|
|
|
backgroundColor:
|
|
|
|
Theme.of(context).colorScheme.errorContainer,
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(10),
|
2022-09-18 20:12:09 +00:00
|
|
|
),
|
2023-03-27 17:29:02 +00:00
|
|
|
),
|
|
|
|
onPressed: () => context.read<JobsCubit>().removeJob(j.id),
|
|
|
|
child: Text(
|
|
|
|
'basis.remove'.tr(),
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.onErrorContainer,
|
2022-09-18 20:12:09 +00:00
|
|
|
),
|
2023-03-27 17:29:02 +00:00
|
|
|
),
|
2022-09-18 20:12:09 +00:00
|
|
|
),
|
2023-03-27 17:29:02 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2024-02-20 16:33:24 +00:00
|
|
|
const SizedBox(height: 16),
|
2022-09-18 20:12:09 +00:00
|
|
|
BrandButton.rised(
|
2024-03-08 11:14:23 +00:00
|
|
|
onPressed: hasBlockingJobs
|
|
|
|
? null
|
|
|
|
: () => context.read<JobsCubit>().applyAll(),
|
2022-09-18 20:12:09 +00:00
|
|
|
text: 'jobs.start'.tr(),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return ListView(
|
2023-06-29 09:53:13 +00:00
|
|
|
controller: controller,
|
2022-09-18 20:12:09 +00:00
|
|
|
padding: paddingH15V0,
|
|
|
|
children: [
|
2023-06-29 09:53:13 +00:00
|
|
|
const SizedBox(height: 16),
|
2022-09-18 20:12:09 +00:00
|
|
|
Center(
|
2023-03-27 17:02:44 +00:00
|
|
|
child: Text(
|
2022-09-18 20:12:09 +00:00
|
|
|
'jobs.title'.tr(),
|
2023-03-27 17:02:44 +00:00
|
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
2022-09-18 20:12:09 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
...widgets,
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
const Divider(height: 0),
|
|
|
|
const SizedBox(height: 8),
|
2022-09-19 00:21:08 +00:00
|
|
|
if (serverJobs.isNotEmpty)
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
2022-09-19 00:42:00 +00:00
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
'jobs.server_jobs'.tr(),
|
|
|
|
style: Theme.of(context).textTheme.titleMedium,
|
|
|
|
),
|
|
|
|
IconButton(
|
|
|
|
onPressed: hasRemovableJobs
|
|
|
|
? () => context
|
2024-01-26 14:46:09 +00:00
|
|
|
.read<ServerJobsBloc>()
|
|
|
|
.add(RemoveAllFinishedJobs())
|
2022-09-19 00:42:00 +00:00
|
|
|
: null,
|
|
|
|
icon: const Icon(Icons.clear_all),
|
|
|
|
color: Theme.of(context).colorScheme.onBackground,
|
|
|
|
),
|
|
|
|
],
|
2022-09-19 00:21:08 +00:00
|
|
|
),
|
2022-09-18 20:12:09 +00:00
|
|
|
),
|
2024-02-20 16:33:24 +00:00
|
|
|
...serverJobs
|
|
|
|
.whereNot((final job) => job.uid == state.rebuildJobUid)
|
|
|
|
.map(
|
|
|
|
(final job) => Dismissible(
|
|
|
|
key: ValueKey(job.uid),
|
|
|
|
direction: job.status == JobStatusEnum.finished ||
|
|
|
|
job.status == JobStatusEnum.error
|
|
|
|
? DismissDirection.horizontal
|
|
|
|
: DismissDirection.none,
|
|
|
|
child: ServerJobCard(
|
|
|
|
serverJob: job,
|
|
|
|
),
|
|
|
|
onDismissed: (final direction) {
|
|
|
|
context.read<ServerJobsBloc>().add(
|
|
|
|
RemoveServerJob(job.uid),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2022-06-05 22:40:34 +00:00
|
|
|
),
|
2022-09-18 20:12:09 +00:00
|
|
|
const SizedBox(height: 24),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2021-05-25 21:53:54 +00:00
|
|
|
}
|