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

341 lines
13 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-07-02 15:24:07 +00:00
import 'package:selfprivacy/ui/components/jobs_content/server_job_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';
2023-07-02 15:24:07 +00:00
import 'package:selfprivacy/ui/pages/backups/change_period_modal.dart';
import 'package:selfprivacy/ui/pages/backups/copy_encryption_key_modal.dart';
2023-07-02 15:24:07 +00:00
import 'package:selfprivacy/ui/pages/backups/create_backups_modal.dart';
import 'package:selfprivacy/ui/router/router.dart';
import 'package:selfprivacy/utils/extensions/duration.dart';
2021-12-06 18:31:19 +00:00
@RoutePage()
class BackupDetailsPage extends StatelessWidget {
const BackupDetailsPage({super.key});
2021-12-06 18:31:19 +00:00
@override
2022-06-05 19:36:32 +00:00
Widget build(final BuildContext context) {
final bool isReady = context.watch<ServerInstallationCubit>().state
is ServerInstallationFinished;
final BackupsState backupsState = context.watch<BackupsCubit>().state;
final bool isBackupInitialized = backupsState.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 = backupsState.preventActions;
final List<Backup> backups = backupsState.backups;
final bool refreshing = backupsState.refreshing;
2023-06-29 09:52:09 +00:00
final List<Service> services =
context.watch<ServicesCubit>().state.servicesThatCanBeBackedUp;
final Duration? autobackupPeriod = backupsState.autobackupPeriod;
2023-07-02 15:24:07 +00:00
final List<ServerJob> backupJobs = context
.watch<ServerJobsCubit>()
.state
.backupJobList
.where((final job) => job.status != JobStatusEnum.finished)
.toList();
2021-12-06 18:31:19 +00:00
2023-07-02 15:24:07 +00:00
if (!isReady) {
return BrandHeroScreen(
heroIcon: BrandIcons.save,
heroTitle: 'backup.card_title'.tr(),
heroSubtitle: 'not_ready_card.in_menu'.tr(),
children: const [],
);
}
if (!isBackupInitialized) {
return BrandHeroScreen(
heroIcon: BrandIcons.save,
heroTitle: 'backup.card_title'.tr(),
heroSubtitle: 'backup.description'.tr(),
children: [
BrandButton.rised(
onPressed: preventActions
? null
: () async {
2023-06-29 09:52:09 +00:00
await context.read<BackupsCubit>().initializeBackups();
},
text: 'backup.initialize'.tr(),
),
2023-07-02 15:24:07 +00:00
],
);
}
return BrandHeroScreen(
heroIcon: BrandIcons.save,
heroTitle: 'backup.card_title'.tr(),
heroSubtitle: 'backup.description'.tr(),
children: [
2023-06-29 09:52:09 +00:00
ListTile(
onTap: preventActions
? null
: () {
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(),
),
),
2023-07-02 15:24:07 +00:00
ListTile(
onTap: preventActions
? null
: () {
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) =>
ChangeAutobackupsPeriodModal(
scrollController: scrollController,
),
),
);
},
leading: const Icon(
Icons.manage_history_outlined,
),
title: Text(
'backup.autobackup_period_title'.tr(),
),
subtitle: Text(
autobackupPeriod != null
? 'backup.autobackup_period_subtitle'.tr(
namedArgs: {
'period': autobackupPeriod.toPrettyString(context.locale)
},
)
: 'backup.autobackup_period_never'.tr(),
),
),
ListTile(
onTap: preventActions
? null
: () {
showModalBottomSheet(
useRootNavigator: true,
context: context,
isScrollControlled: true,
builder: (final BuildContext context) =>
DraggableScrollableSheet(
expand: false,
maxChildSize: 0.9,
minChildSize: 0.5,
initialChildSize: 0.7,
builder: (final context, final scrollController) =>
CopyEncryptionKeyModal(
scrollController: scrollController,
),
),
);
},
leading: const Icon(
Icons.key_outlined,
),
title: Text(
'backup.backups_encryption_key'.tr(),
),
subtitle: Text(
'backup.backups_encryption_key_subtitle'.tr(),
),
),
2022-05-24 18:55:39 +00:00
const SizedBox(height: 16),
2023-07-02 15:24:07 +00:00
if (backupJobs.isNotEmpty)
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListTile(
title: Text(
'backup.pending_jobs'.tr(),
style: Theme.of(context).textTheme.headlineSmall!.copyWith(
color: Theme.of(context).colorScheme.secondary,
),
),
),
for (final job in backupJobs)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: ServerJobCard(
serverJob: job,
),
),
],
),
2023-06-29 09:52:09 +00:00
if (isBackupInitialized)
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListTile(
title: Text(
2023-07-02 15:24:07 +00:00
'backup.latest_snapshots'.tr(),
style: Theme.of(context).textTheme.headlineSmall!.copyWith(
color: Theme.of(context).colorScheme.secondary,
),
),
subtitle: Text(
2023-07-02 15:24:07 +00:00
'backup.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(
2023-07-02 15:24:07 +00:00
children: backups.take(15).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(),
),
2023-07-02 15:24:07 +00:00
if (backups.isNotEmpty && backups.length > 15)
ListTile(
title: Text(
2023-07-02 15:24:07 +00:00
'backup.show_more'.tr(),
style: Theme.of(context).textTheme.labelMedium,
),
leading: const Icon(
Icons.arrow_drop_down,
),
2023-07-02 15:24:07 +00:00
onTap: () =>
context.pushRoute(BackupsListRoute(service: null)),
)
],
),
2023-07-02 15:24:07 +00:00
const SizedBox(height: 8),
const Divider(),
const SizedBox(height: 8),
ListTile(
title: Text(
'backup.refresh'.tr(),
),
onTap: refreshing
? null
: () => {context.read<BackupsCubit>().updateBackups()},
enabled: !refreshing,
leading: const Icon(
Icons.refresh_outlined,
),
),
if (providerState != StateType.uninitialized)
Column(
children: [
ListTile(
title: Text(
2023-07-02 15:24:07 +00:00
'backup.refetch_backups'.tr(),
),
subtitle: Text(
'backup.refetch_backups_subtitle'.tr(),
),
leading: const Icon(
Icons.cached_outlined,
),
2023-07-02 15:24:07 +00:00
onTap: preventActions
? null
2023-07-02 15:24:07 +00:00
: () => {context.read<BackupsCubit>().forceUpdateBackups()},
),
2023-07-02 15:24:07 +00:00
const SizedBox(height: 8),
const Divider(),
const SizedBox(height: 8),
ListTile(
title: Text(
'backup.reupload_key'.tr(),
),
2023-07-02 15:24:07 +00:00
subtitle: Text(
'backup.reupload_key_subtitle'.tr(),
),
leading: const Icon(
Icons.warning_amber_outlined,
2023-06-29 09:52:09 +00:00
),
2023-07-02 15:24:07 +00:00
onTap: preventActions
? null
: () => {context.read<BackupsCubit>().reuploadKey()},
2023-06-29 09:52:09 +00:00
),
2023-07-02 15:24:07 +00:00
],
),
],
2021-12-06 18:31:19 +00:00
);
}
}