2023-03-22 11:38:18 +00:00
|
|
|
import 'package:auto_route/auto_route.dart';
|
2022-02-16 07:01:05 +00:00
|
|
|
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';
|
2022-05-17 13:31:34 +00:00
|
|
|
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';
|
2023-03-27 17:02:44 +00:00
|
|
|
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';
|
2023-02-23 14:49:14 +00:00
|
|
|
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';
|
2022-11-09 15:49:37 +00:00
|
|
|
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
|
|
|
|
2023-03-22 11:38:18 +00:00
|
|
|
@RoutePage()
|
2023-02-24 16:45:32 +00:00
|
|
|
class BackupDetailsPage extends StatefulWidget {
|
|
|
|
const BackupDetailsPage({super.key});
|
2021-12-06 18:31:19 +00:00
|
|
|
|
|
|
|
@override
|
2023-02-24 16:45:32 +00:00
|
|
|
State<BackupDetailsPage> createState() => _BackupDetailsPageState();
|
2021-12-06 18:31:19 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 16:45:32 +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
|
2022-05-17 13:31:34 +00:00
|
|
|
is ServerInstallationFinished;
|
2022-06-05 22:40:34 +00:00
|
|
|
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;
|
2022-06-05 22:40:34 +00:00
|
|
|
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 =
|
2023-06-29 10:51:38 +00:00
|
|
|
context.watch<ServicesCubit>().state.servicesThatCanBeBackedUp;
|
2021-12-06 18:31:19 +00:00
|
|
|
|
2022-02-16 07:01:05 +00:00
|
|
|
return BrandHeroScreen(
|
|
|
|
heroIcon: BrandIcons.save,
|
2022-10-03 23:32:35 +00:00
|
|
|
heroTitle: 'backup.card_title'.tr(),
|
|
|
|
heroSubtitle: 'backup.description'.tr(),
|
2022-02-16 07:01:05 +00:00
|
|
|
children: [
|
|
|
|
if (isReady && !isBackupInitialized)
|
|
|
|
BrandButton.rised(
|
|
|
|
onPressed: preventActions
|
|
|
|
? null
|
|
|
|
: () async {
|
2023-06-29 09:52:09 +00:00
|
|
|
await context.read<BackupsCubit>().initializeBackups();
|
2022-02-16 07:01:05 +00:00
|
|
|
},
|
2022-10-03 23:32:35 +00:00
|
|
|
text: 'backup.initialize'.tr(),
|
2022-02-16 07:01:05 +00:00
|
|
|
),
|
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,
|
2023-06-29 10:51:38 +00:00
|
|
|
builder: (final context, final scrollController) =>
|
2023-06-29 09:52:09 +00:00
|
|
|
CreateBackupsModal(
|
|
|
|
services: services,
|
|
|
|
scrollController: scrollController,
|
2022-06-05 22:40:34 +00:00
|
|
|
),
|
2022-02-16 07:01:05 +00:00
|
|
|
),
|
2023-06-29 09:52:09 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
leading: const Icon(
|
|
|
|
Icons.add_circle_outline_rounded,
|
2022-02-16 07:01:05 +00:00
|
|
|
),
|
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),
|
2022-02-16 07:01:05 +00:00
|
|
|
// 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)
|
2023-07-02 11:41:31 +00:00
|
|
|
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)
|
2022-02-16 07:01:05 +00:00
|
|
|
ListTile(
|
2022-05-24 18:55:39 +00:00
|
|
|
leading: const Icon(
|
2023-07-02 11:41:31 +00:00
|
|
|
Icons.error_outline,
|
2022-02-16 07:01:05 +00:00
|
|
|
),
|
2023-07-02 11:41:31 +00:00
|
|
|
title: Text('backup.no_backups'.tr()),
|
2022-02-16 07:01:05 +00:00
|
|
|
),
|
2023-07-02 11:41:31 +00:00
|
|
|
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(),
|
2022-02-16 07:01:05 +00:00
|
|
|
),
|
2023-07-02 11:41:31 +00:00
|
|
|
if (backups.isNotEmpty && backups.length > 20)
|
|
|
|
ListTile(
|
|
|
|
title: Text(
|
|
|
|
'backups.show_more'.tr(),
|
|
|
|
style: Theme.of(context).textTheme.labelMedium,
|
2022-02-16 07:01:05 +00:00
|
|
|
),
|
2023-07-02 11:41:31 +00:00
|
|
|
leading: const Icon(
|
|
|
|
Icons.arrow_drop_down,
|
2022-02-16 07:01:05 +00:00
|
|
|
),
|
2023-07-02 11:41:31 +00:00
|
|
|
onTap: null,
|
|
|
|
)
|
|
|
|
],
|
2022-02-16 07:01:05 +00:00
|
|
|
),
|
2022-05-24 18:55:39 +00:00
|
|
|
const SizedBox(height: 16),
|
2022-09-15 16:57:26 +00:00
|
|
|
OutlinedCard(
|
2022-02-16 07:01:05 +00:00
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
ListTile(
|
|
|
|
title: Text(
|
2022-10-03 23:32:35 +00:00
|
|
|
'backup.refresh'.tr(),
|
2022-02-16 07:01:05 +00:00
|
|
|
),
|
|
|
|
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(
|
2022-02-16 07:01:05 +00:00
|
|
|
height: 1.0,
|
2021-12-06 18:31:19 +00:00
|
|
|
),
|
2022-02-16 07:01:05 +00:00
|
|
|
ListTile(
|
|
|
|
title: Text(
|
2022-10-03 23:32:35 +00:00
|
|
|
'backup.refetch_backups'.tr(),
|
2021-12-06 18:31:19 +00:00
|
|
|
),
|
2022-02-16 07:01:05 +00:00
|
|
|
onTap: preventActions
|
|
|
|
? null
|
|
|
|
: () => {
|
|
|
|
context
|
|
|
|
.read<BackupsCubit>()
|
|
|
|
.forceUpdateBackups()
|
|
|
|
},
|
|
|
|
),
|
2022-05-24 18:55:39 +00:00
|
|
|
const Divider(
|
2022-02-16 07:01:05 +00:00
|
|
|
height: 1.0,
|
|
|
|
),
|
|
|
|
ListTile(
|
|
|
|
title: Text(
|
2022-10-03 23:32:35 +00:00
|
|
|
'backup.reupload_key'.tr(),
|
2021-12-06 18:31:19 +00:00
|
|
|
),
|
2022-02-16 07:01:05 +00:00
|
|
|
onTap: preventActions
|
|
|
|
? null
|
|
|
|
: () => {context.read<BackupsCubit>().reuploadKey()},
|
2021-12-06 18:31:19 +00:00
|
|
|
),
|
2022-02-16 07:01:05 +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,
|
2023-06-29 10:51:38 +00:00
|
|
|
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
|
2023-06-29 10:51:38 +00:00
|
|
|
.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();
|
2023-06-29 10:51:38 +00:00
|
|
|
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
|
2023-06-29 10:51:38 +00:00
|
|
|
.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(
|
2023-06-29 10:51:38 +00:00
|
|
|
'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();
|
2023-06-29 10:51:38 +00:00
|
|
|
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(
|
2023-07-02 11:41:31 +00:00
|
|
|
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(
|
2023-06-29 10:51:38 +00:00
|
|
|
'backup.start'.tr(),
|
2023-03-27 17:02:44 +00:00
|
|
|
),
|
2023-06-29 09:52:09 +00:00
|
|
|
),
|
2022-02-16 07:01:05 +00:00
|
|
|
],
|
2021-12-06 18:31:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|