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

458 lines
17 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/change_rotation_quotas_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/pages/backups/snapshot_modal.dart';
2023-07-02 15:24:07 +00:00
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: [
if (preventActions)
const Center(
child: Padding(
padding: EdgeInsets.all(16.0),
child: CircularProgressIndicator(),
),
),
if (!preventActions)
BrandButton.rised(
onPressed: preventActions
? null
: () => context.pushRoute(const BackupsInitializingRoute()),
text: 'backup.initialize'.tr(),
),
2023-07-02 15:24:07 +00:00
],
);
}
final Color? overrideColor =
preventActions ? Theme.of(context).colorScheme.secondary : null;
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: Icon(
2023-06-29 09:52:09 +00:00
Icons.add_circle_outline_rounded,
color: overrideColor,
),
2023-06-29 09:52:09 +00:00
title: Text(
'backup.create_new'.tr(),
style: TextStyle(
color: overrideColor,
),
2023-06-29 09:52:09 +00:00
),
),
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: Icon(
2023-07-02 15:24:07 +00:00
Icons.manage_history_outlined,
color: overrideColor,
2023-07-02 15:24:07 +00:00
),
title: Text(
'backup.autobackup_period_title'.tr(),
style: TextStyle(
color: overrideColor,
),
2023-07-02 15:24:07 +00:00
),
subtitle: Text(
style: TextStyle(
color: overrideColor,
),
2023-07-02 15:24:07 +00:00
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.4,
initialChildSize: 0.6,
builder: (final context, final scrollController) =>
ChangeRotationQuotasModal(
scrollController: scrollController,
),
),
);
},
leading: Icon(
Icons.auto_delete_outlined,
color: overrideColor,
),
title: Text(
'backup.rotation_quotas_title'.tr(),
style: TextStyle(
color: overrideColor,
),
),
),
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: Icon(
Icons.key_outlined,
color: overrideColor,
),
title: Text(
'backup.backups_encryption_key'.tr(),
style: TextStyle(
color: overrideColor,
),
),
subtitle: Text(
'backup.backups_encryption_key_subtitle'.tr(),
style: TextStyle(
color: overrideColor,
),
),
),
const SizedBox(height: 8),
const Divider(),
const SizedBox(height: 8),
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(
leading: Icon(
Icons.error_outline,
color: overrideColor,
),
title: Text(
'backup.no_backups'.tr(),
style: TextStyle(
color: overrideColor,
),
),
),
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
: () {
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,
) =>
SnapshotModal(
snapshot: backup,
scrollController: scrollController,
),
),
);
},
onLongPress: preventActions
? null
: () {
showPopUpAlert(
alertTitle: 'backup.forget_snapshot'.tr(),
description:
'backup.forget_snapshot_alert'.tr(),
actionButtonTitle:
'backup.forget_snapshot'.tr(),
actionButtonOnPressed: () => {
context.read<BackupsCubit>().forgetSnapshot(
backup.id,
)
},
);
},
title: Text(
style: TextStyle(
color: overrideColor,
),
'${MaterialLocalizations.of(context).formatShortDate(backup.time)} ${TimeOfDay.fromDateTime(backup.time).format(context)}',
),
subtitle: Text(
style: TextStyle(
color: overrideColor,
),
service?.displayName ?? backup.fallbackServiceName,
),
leading: service != null
? SvgPicture.string(
service.svgIcon,
height: 24,
width: 24,
colorFilter: ColorFilter.mode(
overrideColor ??
Theme.of(context)
.colorScheme
.onBackground,
BlendMode.srcIn,
),
)
: Icon(
Icons.question_mark_outlined,
color: overrideColor,
),
);
},
).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(),
style: TextStyle(
color: overrideColor,
),
2023-07-02 15:24:07 +00:00
),
subtitle: Text(
'backup.refetch_backups_subtitle'.tr(),
style: TextStyle(
color: overrideColor,
),
2023-07-02 15:24:07 +00:00
),
leading: Icon(
2023-07-02 15:24:07 +00:00
Icons.cached_outlined,
color: overrideColor,
),
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(),
style: TextStyle(
color: overrideColor,
),
),
2023-07-02 15:24:07 +00:00
subtitle: Text(
'backup.reupload_key_subtitle'.tr(),
style: TextStyle(
color: overrideColor,
),
2023-07-02 15:24:07 +00:00
),
leading: Icon(
2023-07-02 15:24:07 +00:00
Icons.warning_amber_outlined,
color: overrideColor,
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
);
}
}