From 0573dc82218fa0ca2a11579f2a8449821532548f Mon Sep 17 00:00:00 2001 From: NaiJi Date: Wed, 4 Oct 2023 05:54:42 -0300 Subject: [PATCH] feat: Implement clean wizard backups initialization --- assets/translations/en.json | 14 +++---- lib/logic/cubit/backups/backups_cubit.dart | 41 ++++++++++++++++++- .../backups_wizard/backups_wizard_cubit.dart | 21 +--------- .../backups/setup/backup_confirmation.dart | 11 +++-- .../backups/setup/backup_initializing.dart | 16 ++++++-- 5 files changed, 69 insertions(+), 34 deletions(-) diff --git a/assets/translations/en.json b/assets/translations/en.json index 5e9c5896..4e02ba69 100644 --- a/assets/translations/en.json +++ b/assets/translations/en.json @@ -288,13 +288,13 @@ }, "yearly_infinite": "All yearly backups will be kept" }, - "steps": { - "hosting": "Storage Provider", - "period": "Automatic backups", - "rotation": "Rotation settings" - }, - "settings": { - "initialize_settings_title": "Backup Settings" + "wizard": { + "initialize_settings_title": "Backup Settings", + "steps": { + "hosting": "Storage Provider", + "settings": "Settings", + "confirmation": "Confirmation" + } } }, "storage": { diff --git a/lib/logic/cubit/backups/backups_cubit.dart b/lib/logic/cubit/backups/backups_cubit.dart index 1411fff7..f805dd7d 100644 --- a/lib/logic/cubit/backups/backups_cubit.dart +++ b/lib/logic/cubit/backups/backups_cubit.dart @@ -65,8 +65,13 @@ class BackupsCubit extends ServerInstallationDependendCubit { ); } - Future initializeBackups() async { + Future initializeBackups( + final BackupsCredential backupsCredential, + final AutobackupQuotas quotas, + final Duration? autobackupPeriod, + ) async { emit(state.copyWith(preventActions: true)); + await getIt().storeBackblazeCredential(backupsCredential); final String? encryptionKey = (await api.getBackupsConfiguration())?.encryptionKey; if (encryptionKey == null) { @@ -125,6 +130,9 @@ class BackupsCubit extends ServerInstallationDependendCubit { 'Backups repository is now initializing. It may take a while.', ); + await setAutobackupPeriod(autobackupPeriod); + await setAutobackupQuotas(quotas); + emit(state.copyWith(preventActions: false)); } @@ -292,6 +300,37 @@ class BackupsCubit extends ServerInstallationDependendCubit { await updateBackups(); } + Future recoverState(final BackupsCredential backupsCredential) async { + BackblazeBucket? bucket; + BackupConfiguration? backupConfig; + try { + backupConfig = await ServerApi().getBackupsConfiguration(); + await getIt().storeBackblazeCredential(backupsCredential); + final bucket = await BackblazeApi() + .fetchBucket(state.backupsCredential!, backupConfig!); + await getIt().storeBackblazeBucket(bucket!); + } catch (e) { + print(e); + getIt().showSnackBar('jobs.generic_error'.tr()); + return; + } + + final List backups = await api.getBackups(); + backups.sort((final a, final b) => b.time.compareTo(a.time)); + emit( + state.copyWith( + backupsCredential: backupsCredential, + backblazeBucket: bucket, + isInitialized: backupConfig.isInitialized, + autobackupPeriod: backupConfig.autobackupPeriod ?? Duration.zero, + autobackupQuotas: backupConfig.autobackupQuotas, + backups: backups, + preventActions: false, + refreshing: false, + ), + ); + } + @override void clear() async { emit(const BackupsState()); diff --git a/lib/logic/cubit/backups_wizard/backups_wizard_cubit.dart b/lib/logic/cubit/backups_wizard/backups_wizard_cubit.dart index 89c9dff1..9eb5be31 100644 --- a/lib/logic/cubit/backups_wizard/backups_wizard_cubit.dart +++ b/lib/logic/cubit/backups_wizard/backups_wizard_cubit.dart @@ -1,10 +1,7 @@ import 'dart:async'; import 'package:cubit_form/cubit_form.dart'; -import 'package:easy_localization/easy_localization.dart'; -import 'package:selfprivacy/config/get_it_config.dart'; import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server_api.dart'; -import 'package:selfprivacy/logic/api_maps/rest_maps/backblaze.dart'; import 'package:selfprivacy/logic/models/backup.dart'; import 'package:selfprivacy/logic/models/hive/backups_credential.dart'; @@ -13,10 +10,8 @@ part 'backups_wizard_state.dart'; class BackupsWizardCubit extends Cubit { BackupsWizardCubit() : super(const BackupsWizardState()); - BackupConfiguration? serverBackupConfig; - Future load() async { - serverBackupConfig = await ServerApi().getBackupsConfiguration(); + final serverBackupConfig = await ServerApi().getBackupsConfiguration(); /// If config already exists, then user only lacks credentials, /// we don't need full re-initialization @@ -60,19 +55,7 @@ class BackupsWizardCubit extends Cubit { ); } - void recoverBackupsInformation() async { - try { - await getIt() - .storeBackblazeCredential(state.backupsCredential!); - final bucket = await BackblazeApi() - .fetchBucket(state.backupsCredential!, serverBackupConfig!); - await getIt().storeBackblazeBucket(bucket!); - } catch (e) { - print(e); - getIt().showSnackBar('jobs.generic_error'.tr()); - return; - } - + void finish() async { emit( state.copyWith( currentStep: BackupsWizardStep.finished, diff --git a/lib/ui/pages/backups/setup/backup_confirmation.dart b/lib/ui/pages/backups/setup/backup_confirmation.dart index 3102e5e6..0db15aeb 100644 --- a/lib/ui/pages/backups/setup/backup_confirmation.dart +++ b/lib/ui/pages/backups/setup/backup_confirmation.dart @@ -1,16 +1,18 @@ import 'package:cubit_form/cubit_form.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; -import 'package:selfprivacy/logic/cubit/backups/backups_cubit.dart'; import 'package:selfprivacy/logic/cubit/backups_wizard/backups_wizard_cubit.dart'; import 'package:selfprivacy/ui/components/buttons/brand_button.dart'; import 'package:selfprivacy/ui/layouts/responsive_layout_with_infobox.dart'; class BackupConfirmationPage extends StatelessWidget { const BackupConfirmationPage({ + required this.onConfirmCallback, super.key, }); + final Function() onConfirmCallback; + @override Widget build(final BuildContext context) => ResponsiveLayoutWithInfobox( topChild: Column( @@ -27,9 +29,10 @@ class BackupConfirmationPage extends StatelessWidget { children: [ const SizedBox(height: 32), BrandButton.rised( - onPressed: () => context - .read() - .recoverBackupsInformation(), + onPressed: () { + onConfirmCallback(); + context.read().finish(); + }, text: 'basis.connect'.tr(), ), const SizedBox(height: 10), diff --git a/lib/ui/pages/backups/setup/backup_initializing.dart b/lib/ui/pages/backups/setup/backup_initializing.dart index 9d33d823..668fbabd 100644 --- a/lib/ui/pages/backups/setup/backup_initializing.dart +++ b/lib/ui/pages/backups/setup/backup_initializing.dart @@ -30,11 +30,22 @@ class BackupsInitializingPage extends StatelessWidget { final currentStep = cubit.state.currentStep; switch (currentStep) { case BackupsWizardStep.confirmInitialization: - actualInitializingPage = const BackupConfirmationPage(); + actualInitializingPage = BackupConfirmationPage( + onConfirmCallback: () => + context.read().initializeBackups( + cubit.state.backupsCredential!, + cubit.state.autobackupQuotas!, + cubit.state.autobackupPeriod, + ), + ); progressDrawerStep = 2; break; case BackupsWizardStep.confirmRecovery: - actualInitializingPage = const BackupConfirmationPage(); + actualInitializingPage = BackupConfirmationPage( + onConfirmCallback: () => context + .read() + .recoverState(cubit.state.backupsCredential!), + ); progressDrawerStep = 2; break; case BackupsWizardStep.settingsInitialization: @@ -73,7 +84,6 @@ class BackupsInitializingPage extends StatelessWidget { return BlocListener( listener: (final context, final state) { if (cubit.state.currentStep == BackupsWizardStep.finished) { - context.read().load(); context.router.popUntilRoot(); } },