selfprivacy.org.app/lib/ui/components/jobs_content/jobs_content.dart

177 lines
6.7 KiB
Dart
Raw Normal View History

2021-05-25 21:53:54 +00:00
import 'package:flutter/material.dart';
import 'package:easy_localization/easy_localization.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';
2021-12-06 18:31:19 +00:00
import 'package:selfprivacy/config/get_it_config.dart';
2022-08-30 03:09:09 +00:00
import 'package:selfprivacy/logic/cubit/client_jobs/client_jobs_cubit.dart';
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
2022-08-30 03:09:09 +00:00
import 'package:selfprivacy/logic/cubit/server_jobs/server_jobs_cubit.dart';
import 'package:selfprivacy/logic/models/json/server_job.dart';
2021-12-06 18:31:19 +00:00
import 'package:selfprivacy/ui/components/action_button/action_button.dart';
import 'package:selfprivacy/ui/components/brand_alert/brand_alert.dart';
2021-05-25 21:53:54 +00:00
import 'package:selfprivacy/ui/components/brand_button/brand_button.dart';
import 'package:selfprivacy/ui/components/brand_cards/brand_cards.dart';
2021-07-29 05:24:42 +00:00
import 'package:selfprivacy/ui/components/brand_loader/brand_loader.dart';
2021-05-25 21:53:54 +00:00
import 'package:selfprivacy/ui/components/brand_text/brand_text.dart';
2022-08-26 02:34:25 +00:00
import 'package:selfprivacy/ui/components/jobs_content/server_job_card.dart';
2021-05-25 21:53:54 +00:00
class JobsContent extends StatelessWidget {
const JobsContent({super.key});
2021-05-25 21:53:54 +00:00
@override
Widget build(final BuildContext context) {
final List<ServerJob> serverJobs =
context.watch<ServerJobsCubit>().state.serverJobList;
final bool hasRemovableJobs =
context.watch<ServerJobsCubit>().state.hasRemovableJobs;
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),
Center(child: BrandText.body1('jobs.empty'.tr())),
];
if (installationState is ServerInstallationFinished) {
widgets = [
...widgets,
const SizedBox(height: 80),
BrandButton.rised(
onPressed: () => context.read<JobsCubit>().upgradeServer(),
text: 'jobs.upgrade_server'.tr(),
),
const SizedBox(height: 10),
BrandButton.text(
onPressed: () {
final NavigationService nav = getIt<NavigationService>();
nav.showPopUpDialog(
BrandAlert(
title: 'jobs.reboot_server'.tr(),
contentText: 'modals.are_you_sure'.tr(),
actions: [
ActionButton(
text: 'basis.cancel'.tr(),
2021-05-25 21:53:54 +00:00
),
ActionButton(
onPressed: () =>
{context.read<JobsCubit>().rebootServer()},
text: 'modals.reboot'.tr(),
)
],
),
);
},
title: 'jobs.reboot_server'.tr(),
),
];
}
} else if (state is JobsStateLoading) {
widgets = [
const SizedBox(height: 80),
BrandLoader.horizontal(),
];
} else if (state is JobsStateWithJobs) {
widgets = [
...state.clientJobList
.map(
(final j) => Row(
children: [
Expanded(
child: BrandCards.small(
child: Text(j.title),
),
),
const SizedBox(width: 10),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor:
Theme.of(context).colorScheme.errorContainer,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
onPressed: () =>
context.read<JobsCubit>().removeJob(j.id),
child: Text(
'basis.remove'.tr(),
style: TextStyle(
color:
Theme.of(context).colorScheme.onErrorContainer,
),
),
),
],
),
)
.toList(),
const SizedBox(height: 20),
BrandButton.rised(
onPressed: () => context.read<JobsCubit>().applyAll(),
text: 'jobs.start'.tr(),
),
];
}
return ListView(
padding: paddingH15V0,
children: [
const SizedBox(height: 15),
Center(
child: BrandText.h2(
'jobs.title'.tr(),
),
),
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),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
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,
),
],
2022-09-19 00:21:08 +00:00
),
),
...serverJobs.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<ServerJobsCubit>().removeServerJob(job.uid);
},
2021-07-29 05:24:42 +00:00
),
),
const SizedBox(height: 24),
],
);
},
);
}
2021-05-25 21:53:54 +00:00
}