selfprivacy.org.app/lib/ui/pages/backup_details/backup_details.dart

385 lines
14 KiB
Dart
Raw Normal View History

import 'package:auto_route/auto_route.dart';
import 'package:easy_localization/easy_localization.dart';
2021-12-06 18:31:19 +00:00
import 'package:flutter/material.dart';
2023-06-29 09:52:09 +00:00
import 'package:flutter_svg/flutter_svg.dart';
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
2021-12-06 18:31:19 +00:00
import 'package:selfprivacy/logic/cubit/backups/backups_cubit.dart';
2023-06-29 09:52:09 +00:00
import 'package:selfprivacy/logic/cubit/server_jobs/server_jobs_cubit.dart';
import 'package:selfprivacy/logic/cubit/services/services_cubit.dart';
import 'package:selfprivacy/logic/models/backup.dart';
import 'package:selfprivacy/logic/models/json/server_job.dart';
import 'package:selfprivacy/logic/models/service.dart';
2021-12-06 18:31:19 +00:00
import 'package:selfprivacy/logic/models/state_types.dart';
import 'package:selfprivacy/ui/components/buttons/brand_button.dart';
2023-04-05 10:33:53 +00:00
import 'package:selfprivacy/ui/components/cards/outlined_card.dart';
import 'package:selfprivacy/ui/layouts/brand_hero_screen.dart';
2021-12-06 18:31:19 +00:00
import 'package:selfprivacy/ui/components/brand_icons/brand_icons.dart';
import 'package:selfprivacy/ui/helpers/modals.dart';
2021-12-06 18:31:19 +00:00
2022-06-05 19:36:32 +00:00
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
2021-12-06 18:31:19 +00:00
@RoutePage()
class BackupDetailsPage extends StatefulWidget {
const BackupDetailsPage({super.key});
2021-12-06 18:31:19 +00:00
@override
State<BackupDetailsPage> createState() => _BackupDetailsPageState();
2021-12-06 18:31:19 +00:00
}
class _BackupDetailsPageState extends State<BackupDetailsPage>
2021-12-06 18:31:19 +00:00
with SingleTickerProviderStateMixin {
@override
2022-06-05 19:36:32 +00:00
Widget build(final BuildContext context) {
final bool isReady = context.watch<ServerInstallationCubit>().state
is ServerInstallationFinished;
final bool isBackupInitialized =
context.watch<BackupsCubit>().state.isInitialized;
2022-06-05 19:36:32 +00:00
final StateType providerState = isReady && isBackupInitialized
2023-06-29 09:52:09 +00:00
? StateType.stable
2021-12-06 18:31:19 +00:00
: StateType.uninitialized;
final bool preventActions =
context.watch<BackupsCubit>().state.preventActions;
2022-06-05 19:36:32 +00:00
final List<Backup> backups = context.watch<BackupsCubit>().state.backups;
final bool refreshing = context.watch<BackupsCubit>().state.refreshing;
2023-06-29 09:52:09 +00:00
final List<Service> services =
context.watch<ServicesCubit>().state.servicesThatCanBeBackedUp;
2021-12-06 18:31:19 +00:00
return BrandHeroScreen(
heroIcon: BrandIcons.save,
heroTitle: 'backup.card_title'.tr(),
heroSubtitle: 'backup.description'.tr(),
children: [
if (isReady && !isBackupInitialized)
BrandButton.rised(
onPressed: preventActions
? null
: () async {
2023-06-29 09:52:09 +00:00
await context.read<BackupsCubit>().initializeBackups();
},
text: 'backup.initialize'.tr(),
),
2023-06-29 09:52:09 +00:00
ListTile(
onTap: preventActions
? null
: () {
// await context.read<BackupsCubit>().createBackup();
showModalBottomSheet(
useRootNavigator: true,
context: context,
isScrollControlled: true,
builder: (final BuildContext context) =>
DraggableScrollableSheet(
expand: false,
maxChildSize: 0.9,
minChildSize: 0.4,
initialChildSize: 0.6,
builder: (final context, final scrollController) =>
2023-06-29 09:52:09 +00:00
CreateBackupsModal(
services: services,
scrollController: scrollController,
),
),
2023-06-29 09:52:09 +00:00
);
},
leading: const Icon(
Icons.add_circle_outline_rounded,
),
2023-06-29 09:52:09 +00:00
title: Text(
'backup.create_new'.tr(),
),
),
2022-05-24 18:55:39 +00:00
const SizedBox(height: 16),
// Card with a list of existing backups
// Each list item has a date
// When clicked, starts the restore action
2023-06-29 09:52:09 +00:00
if (isBackupInitialized)
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListTile(
title: Text(
'backups.latest_snapshots'.tr(),
style: Theme.of(context).textTheme.headlineSmall!.copyWith(
color: Theme.of(context).colorScheme.secondary,
),
),
subtitle: Text(
'backups.latest_snapshots_subtitle'.tr(),
style: Theme.of(context).textTheme.labelMedium,
),
),
if (backups.isEmpty)
ListTile(
2022-05-24 18:55:39 +00:00
leading: const Icon(
Icons.error_outline,
),
title: Text('backup.no_backups'.tr()),
),
if (backups.isNotEmpty)
Column(
children: backups.take(20).map(
(final Backup backup) {
final service = context
.read<ServicesCubit>()
.state
.getServiceById(backup.serviceId);
return ListTile(
onTap: preventActions
? null
: () {
showPopUpAlert(
alertTitle: 'backup.restoring'.tr(),
description: 'backup.restore_alert'.tr(
args: [backup.time.toString()],
),
actionButtonTitle: 'modals.yes'.tr(),
actionButtonOnPressed: () => {
context
.read<BackupsCubit>()
.restoreBackup(backup.id)
},
);
},
title: Text(
'${MaterialLocalizations.of(context).formatShortDate(backup.time)} ${TimeOfDay.fromDateTime(backup.time).format(context)}',
),
subtitle: Text(
service?.displayName ?? backup.fallbackServiceName,
),
leading: service != null
? SvgPicture.string(
service.svgIcon,
height: 24,
width: 24,
colorFilter: ColorFilter.mode(
Theme.of(context).colorScheme.onBackground,
BlendMode.srcIn,
),
)
: const Icon(
Icons.question_mark_outlined,
),
);
},
).toList(),
),
if (backups.isNotEmpty && backups.length > 20)
ListTile(
title: Text(
'backups.show_more'.tr(),
style: Theme.of(context).textTheme.labelMedium,
),
leading: const Icon(
Icons.arrow_drop_down,
),
onTap: null,
)
],
),
2022-05-24 18:55:39 +00:00
const SizedBox(height: 16),
OutlinedCard(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListTile(
title: Text(
'backup.refresh'.tr(),
),
onTap: refreshing
? null
: () => {context.read<BackupsCubit>().updateBackups()},
enabled: !refreshing,
),
if (providerState != StateType.uninitialized)
Column(
children: [
2022-05-24 18:55:39 +00:00
const Divider(
height: 1.0,
2021-12-06 18:31:19 +00:00
),
ListTile(
title: Text(
'backup.refetch_backups'.tr(),
2021-12-06 18:31:19 +00:00
),
onTap: preventActions
? null
: () => {
context
.read<BackupsCubit>()
.forceUpdateBackups()
},
),
2022-05-24 18:55:39 +00:00
const Divider(
height: 1.0,
),
ListTile(
title: Text(
'backup.reupload_key'.tr(),
2021-12-06 18:31:19 +00:00
),
onTap: preventActions
? null
: () => {context.read<BackupsCubit>().reuploadKey()},
2021-12-06 18:31:19 +00:00
),
],
),
],
),
2021-12-06 18:31:19 +00:00
),
2023-06-29 09:52:09 +00:00
],
);
}
}
class CreateBackupsModal extends StatefulWidget {
const CreateBackupsModal({
required this.services,
required this.scrollController,
super.key,
2023-06-29 09:52:09 +00:00
});
final List<Service> services;
final ScrollController scrollController;
@override
State<CreateBackupsModal> createState() => _CreateBackupsModalState();
}
class _CreateBackupsModalState extends State<CreateBackupsModal> {
// Store in state the selected services to backup
List<Service> selectedServices = [];
// Select all services on modal open
@override
void initState() {
super.initState();
final List<String> busyServices = context
.read<ServerJobsCubit>()
.state
.backupJobList
.where(
(final ServerJob job) =>
job.status == JobStatusEnum.running ||
job.status == JobStatusEnum.created,
)
2023-06-29 09:52:09 +00:00
.map((final ServerJob job) => job.typeId.split('.')[1])
.toList();
selectedServices.addAll(
widget.services
.where((final Service service) => !busyServices.contains(service.id)),
);
2023-06-29 09:52:09 +00:00
}
@override
Widget build(final BuildContext context) {
final List<String> busyServices = context
.watch<ServerJobsCubit>()
.state
.backupJobList
.where(
(final ServerJob job) =>
job.status == JobStatusEnum.running ||
job.status == JobStatusEnum.created,
)
2023-06-29 09:52:09 +00:00
.map((final ServerJob job) => job.typeId.split('.')[1])
.toList();
return ListView(
controller: widget.scrollController,
padding: const EdgeInsets.all(16),
children: [
const SizedBox(height: 16),
Text(
'backup.create_new_select_heading'.tr(),
2023-06-29 09:52:09 +00:00
style: Theme.of(context).textTheme.headlineSmall,
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
// Select all services tile
CheckboxListTile(
onChanged: (final bool? value) {
setState(() {
if (value ?? true) {
setState(() {
selectedServices.clear();
selectedServices.addAll(
widget.services.where(
(final service) => !busyServices.contains(service.id),
),
);
2023-06-29 09:52:09 +00:00
});
} else {
selectedServices.clear();
}
});
},
title: Text(
'backup.select_all'.tr(),
),
secondary: const Icon(
Icons.checklist_outlined,
),
value: selectedServices.length >=
widget.services.length - busyServices.length,
),
const Divider(
height: 1.0,
),
...widget.services.map(
(final Service service) {
final bool busy = busyServices.contains(service.id);
return CheckboxListTile(
onChanged: !busy
? (final bool? value) {
setState(() {
if (value ?? true) {
setState(() {
selectedServices.add(service);
});
} else {
setState(() {
selectedServices.remove(service);
});
}
});
}
: null,
title: Text(
service.displayName,
),
subtitle: Text(
busy ? 'backup.service_busy'.tr() : service.backupDescription,
2023-06-29 09:52:09 +00:00
),
secondary: SvgPicture.string(
service.svgIcon,
height: 24,
width: 24,
colorFilter: ColorFilter.mode(
busy
? Theme.of(context).colorScheme.outlineVariant
: Theme.of(context).colorScheme.onBackground,
BlendMode.srcIn,
),
),
value: selectedServices.contains(service),
);
},
),
const SizedBox(height: 16),
// Create backup button
FilledButton(
onPressed: selectedServices.isEmpty
? null
: () {
context
.read<BackupsCubit>()
.createMultipleBackups(selectedServices);
Navigator.of(context).pop();
},
child: Text(
'backup.start'.tr(),
),
2023-06-29 09:52:09 +00:00
),
],
2021-12-06 18:31:19 +00:00
);
}
}