From 2b8d3ee6d0357fd08f28b318a0609929449b316f Mon Sep 17 00:00:00 2001 From: Inex Code Date: Sun, 2 Jul 2023 18:23:12 +0300 Subject: [PATCH] refactor: Getters for backup-related jobs --- lib/logic/cubit/backups/backups_cubit.dart | 28 +++++++++++++++---- lib/logic/cubit/backups/backups_state.dart | 10 ++++++- .../cubit/server_jobs/server_jobs_state.dart | 3 +- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/logic/cubit/backups/backups_cubit.dart b/lib/logic/cubit/backups/backups_cubit.dart index 6e34b410..ef3cec4d 100644 --- a/lib/logic/cubit/backups/backups_cubit.dart +++ b/lib/logic/cubit/backups/backups_cubit.dart @@ -35,7 +35,7 @@ class BackupsCubit extends ServerInstallationDependendCubit { state.copyWith( backblazeBucket: bucket, isInitialized: backupConfig?.isInitialized, - autobackupPeriod: backupConfig?.autobackupPeriod, + autobackupPeriod: backupConfig?.autobackupPeriod ?? Duration.zero, backups: backups, preventActions: false, refreshing: false, @@ -164,8 +164,8 @@ class BackupsCubit extends ServerInstallationDependendCubit { Future forceUpdateBackups() async { emit(state.copyWith(preventActions: true)); - await api.forceBackupListReload(); getIt().showSnackBar('backup.refetching_list'.tr()); + await api.forceBackupListReload(); emit(state.copyWith(preventActions: false)); } @@ -187,12 +187,30 @@ class BackupsCubit extends ServerInstallationDependendCubit { Future restoreBackup(final String backupId) async { emit(state.copyWith(preventActions: true)); - - /// TOOD: ??? - //await api.restoreBackup(backupId); + await api.restoreBackup(backupId); emit(state.copyWith(preventActions: false)); } + Future setAutobackupPeriod(final Duration? period) async { + emit(state.copyWith(preventActions: true)); + final result = await api.setAutobackupPeriod(period: period?.inMinutes); + if (result.success == false) { + getIt() + .showSnackBar(result.message ?? 'Unknown error'); + emit(state.copyWith(preventActions: false)); + } else { + getIt() + .showSnackBar('backup.autobackup_period_set'.tr()); + emit( + state.copyWith( + preventActions: false, + autobackupPeriod: period ?? Duration.zero, + ), + ); + } + await updateBackups(); + } + @override void clear() async { emit(const BackupsState()); diff --git a/lib/logic/cubit/backups/backups_state.dart b/lib/logic/cubit/backups/backups_state.dart index 909e7f21..52b9b106 100644 --- a/lib/logic/cubit/backups/backups_state.dart +++ b/lib/logic/cubit/backups/backups_state.dart @@ -19,6 +19,10 @@ class BackupsState extends ServerInstallationDependendState { final Duration? autobackupPeriod; final BackblazeBucket? backblazeBucket; + List serviceBackups(final String serviceId) => backups + .where((final backup) => backup.serviceId == serviceId) + .toList(growable: false); + @override List get props => [ isInitialized, @@ -43,7 +47,11 @@ class BackupsState extends ServerInstallationDependendState { preventActions: preventActions ?? this.preventActions, refreshTimer: refreshTimer ?? this.refreshTimer, refreshing: refreshing ?? this.refreshing, - autobackupPeriod: autobackupPeriod ?? this.autobackupPeriod, + // The autobackupPeriod might be null, so if the duration is set to 0, we + // set it to null. + autobackupPeriod: autobackupPeriod?.inSeconds == 0 + ? null + : autobackupPeriod ?? this.autobackupPeriod, backblazeBucket: backblazeBucket ?? this.backblazeBucket, ); } diff --git a/lib/logic/cubit/server_jobs/server_jobs_state.dart b/lib/logic/cubit/server_jobs/server_jobs_state.dart index 2902ba3b..9a18dd51 100644 --- a/lib/logic/cubit/server_jobs/server_jobs_state.dart +++ b/lib/logic/cubit/server_jobs/server_jobs_state.dart @@ -24,7 +24,8 @@ class ServerJobsState extends ServerInstallationDependendState { List get backupJobList => serverJobList .where( // The backup jobs has the format of 'service..backup' - (final job) => job.typeId.contains('backup'), + (final job) => + job.typeId.contains('backup') || job.typeId.contains('restore'), ) .toList();