refactor: Revert backups cubit back to normal

This commit is contained in:
NaiJi 2023-09-28 22:09:57 -03:00
parent a7b11779eb
commit e8ebc8be72
2 changed files with 19 additions and 115 deletions

View file

@ -25,33 +25,24 @@ class BackupsCubit extends ServerInstallationDependendCubit<BackupsState> {
@override
Future<void> load() async {
if (serverInstallationCubit.state is! ServerInstallationFinished) {
return;
if (serverInstallationCubit.state is ServerInstallationFinished) {
final BackblazeBucket? bucket = getIt<ApiConfigModel>().backblazeBucket;
final BackupConfiguration? backupConfig =
await api.getBackupsConfiguration();
final List<Backup> backups = await api.getBackups();
backups.sort((final a, final b) => b.time.compareTo(a.time));
emit(
state.copyWith(
backblazeBucket: bucket,
isInitialized: backupConfig?.isInitialized,
autobackupPeriod: backupConfig?.autobackupPeriod ?? Duration.zero,
autobackupQuotas: backupConfig?.autobackupQuotas,
backups: backups,
preventActions: false,
refreshing: false,
),
);
}
final BackblazeBucket? bucket = getIt<ApiConfigModel>().backblazeBucket;
final BackupConfiguration? backupConfig =
await api.getBackupsConfiguration();
final BackupsCredential? backupsCredential =
getIt<ApiConfigModel>().backblazeCredential;
final List<Backup> backups = await api.getBackups();
backups.sort((final a, final b) => b.time.compareTo(a.time));
final bool? initialized = backupConfig?.isInitialized;
final nextState = state.copyWith(
backblazeBucket: bucket,
isInitialized: initialized,
autobackupPeriod: backupConfig?.autobackupPeriod ?? Duration.zero,
autobackupQuotas: backupConfig?.autobackupQuotas,
backupsCredential: backupsCredential,
backups: backups,
preventActions: false,
refreshing: false,
);
emit(
(initialized == null || initialized == false)
? BackupsNotFinishedState.fromBackupsState(nextState)
: nextState,
);
}
Future<void> setBackupsKey(
@ -64,15 +55,6 @@ class BackupsCubit extends ServerInstallationDependendCubit<BackupsState> {
provider: BackupsProviderType.backblaze,
);
await getIt<ApiConfigModel>().storeBackblazeCredential(backupsCredential);
if (state is BackupsNotFinishedState) {
emit(
(state as BackupsNotFinishedState).copyNotFinishedWith(
backupsCredential: backupsCredential,
step: BackupsInitializingStep.period,
),
);
return;
}
emit(
state.copyWith(
backupsCredential: backupsCredential,
@ -309,6 +291,6 @@ class BackupsCubit extends ServerInstallationDependendCubit<BackupsState> {
@override
void clear() async {
emit(BackupsNotFinishedState.fromBackupsState(const BackupsState()));
emit(const BackupsState());
}
}

View file

@ -53,9 +53,9 @@ class BackupsState extends ServerInstallationDependendState {
preventActions: preventActions ?? this.preventActions,
refreshTimer: refreshTimer ?? this.refreshTimer,
refreshing: refreshing ?? this.refreshing,
backupsCredential: backupsCredential ?? this.backupsCredential,
backblazeBucket: backblazeBucket ?? this.backblazeBucket,
autobackupQuotas: autobackupQuotas ?? this.autobackupQuotas,
backupsCredential: backupsCredential ?? this.backupsCredential,
// The autobackupPeriod might be null, so if the duration is set to 0, we
// set it to null.
autobackupPeriod: autobackupPeriod?.inSeconds == 0
@ -63,81 +63,3 @@ class BackupsState extends ServerInstallationDependendState {
: autobackupPeriod ?? this.autobackupPeriod,
);
}
class BackupsNotFinishedState extends BackupsState {
BackupsNotFinishedState.fromBackupsState(final BackupsState backupsState)
: this(
step: BackupsInitializingStep.hosting,
isInitialized: false,
autobackupPeriod: backupsState.autobackupPeriod,
autobackupQuotas: backupsState.autobackupQuotas,
backblazeBucket: backupsState.backblazeBucket,
backups: backupsState.backups,
backupsCredential: backupsState.backupsCredential,
preventActions: backupsState.preventActions,
refreshTimer: backupsState.refreshTimer,
refreshing: backupsState.refreshing,
);
const BackupsNotFinishedState({
required this.step,
super.isInitialized = false,
super.backups = const [],
super.preventActions = true,
super.refreshTimer = const Duration(seconds: 60),
super.refreshing = true,
super.autobackupPeriod,
super.backblazeBucket,
super.autobackupQuotas,
super.backupsCredential,
});
final BackupsInitializingStep step;
BackupsNotFinishedState copyNotFinishedWith({
required final BackupsInitializingStep step,
final bool? isInitialized,
final List<Backup>? backups,
final bool? preventActions,
final Duration? refreshTimer,
final bool? refreshing,
final Duration? autobackupPeriod,
final BackblazeBucket? backblazeBucket,
final AutobackupQuotas? autobackupQuotas,
final BackupsCredential? backupsCredential,
}) =>
BackupsNotFinishedState(
isInitialized: isInitialized ?? this.isInitialized,
backups: backups ?? this.backups,
preventActions: preventActions ?? this.preventActions,
refreshTimer: refreshTimer ?? this.refreshTimer,
refreshing: refreshing ?? this.refreshing,
backupsCredential: backupsCredential ?? this.backupsCredential,
backblazeBucket: backblazeBucket ?? this.backblazeBucket,
autobackupQuotas: autobackupQuotas ?? this.autobackupQuotas,
step: step,
// 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,
);
BackupsState finish() => BackupsState(
isInitialized: true,
autobackupPeriod: autobackupPeriod,
autobackupQuotas: autobackupQuotas,
backblazeBucket: backblazeBucket,
backups: backups,
backupsCredential: backupsCredential,
preventActions: preventActions,
refreshTimer: refreshTimer,
refreshing: refreshing,
);
}
enum BackupsInitializingStep {
hosting,
period,
rotation,
}