feat: Implement backups wizard cubit

This commit is contained in:
NaiJi 2023-09-28 22:05:06 -03:00
parent a19f0dc2ef
commit a7b11779eb
4 changed files with 110 additions and 1 deletions

View file

@ -287,6 +287,11 @@
"other": "Last {} yearly backups will be kept" "other": "Last {} yearly backups will be kept"
}, },
"yearly_infinite": "All yearly backups will be kept" "yearly_infinite": "All yearly backups will be kept"
},
"steps": {
"hosting": "Storage Provider",
"period": "Automatic backups",
"rotation": "Rotation settings"
} }
}, },
"storage": { "storage": {
@ -462,7 +467,6 @@
"hosting": "Hosting", "hosting": "Hosting",
"server_type": "Server type", "server_type": "Server type",
"dns_provider": "DNS provider", "dns_provider": "DNS provider",
"backups_provider": "Backups",
"domain": "Domain", "domain": "Domain",
"master_account": "Master account", "master_account": "Master account",
"server": "Server", "server": "Server",

View file

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:selfprivacy/logic/cubit/backups_wizard/backups_wizard_cubit.dart';
import 'package:selfprivacy/logic/cubit/devices/devices_cubit.dart'; import 'package:selfprivacy/logic/cubit/devices/devices_cubit.dart';
import 'package:selfprivacy/logic/cubit/recovery_key/recovery_key_cubit.dart'; import 'package:selfprivacy/logic/cubit/recovery_key/recovery_key_cubit.dart';
import 'package:selfprivacy/logic/cubit/server_detailed_info/server_detailed_info_cubit.dart'; import 'package:selfprivacy/logic/cubit/server_detailed_info/server_detailed_info_cubit.dart';
@ -27,6 +28,7 @@ class BlocAndProviderConfig extends StatelessWidget {
const isAutoDark = true; const isAutoDark = true;
final serverInstallationCubit = ServerInstallationCubit()..load(); final serverInstallationCubit = ServerInstallationCubit()..load();
final supportSystemCubit = SupportSystemCubit(); final supportSystemCubit = SupportSystemCubit();
final backupsWizardCubit = BackupsWizardCubit();
final usersCubit = UsersCubit(serverInstallationCubit); final usersCubit = UsersCubit(serverInstallationCubit);
final servicesCubit = ServicesCubit(serverInstallationCubit); final servicesCubit = ServicesCubit(serverInstallationCubit);
final backupsCubit = BackupsCubit(serverInstallationCubit); final backupsCubit = BackupsCubit(serverInstallationCubit);
@ -97,6 +99,9 @@ class BlocAndProviderConfig extends StatelessWidget {
servicesCubit: servicesCubit, servicesCubit: servicesCubit,
), ),
), ),
BlocProvider(
create: (final _) => backupsWizardCubit..load(),
),
], ],
child: child, child: child,
); );

View file

@ -0,0 +1,52 @@
import 'dart:async';
import 'package:cubit_form/cubit_form.dart';
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server_api.dart';
import 'package:selfprivacy/logic/models/backup.dart';
import 'package:selfprivacy/logic/models/hive/backups_credential.dart';
part 'backups_wizard_state.dart';
class BackupsWizardCubit extends Cubit<BackupsWizardState> {
BackupsWizardCubit() : super(const BackupsWizardState());
Future<void> load() async {
final BackupConfiguration? backupConfig =
await ServerApi().getBackupsConfiguration();
/// If config already exists, then user only lacks credentials,
/// we don't need full re-initialization
if (backupConfig != null) {
emit(state.copyWith(currentStep: BackupsWizardStep.hostingRecovery));
}
}
void setBackupsCredential(final BackupsCredential backupsCredential) {
emit(
state.copyWith(
backupsCredential: backupsCredential,
currentStep: state.currentStep == BackupsWizardStep.hostingRecovery
? BackupsWizardStep.finished
: BackupsWizardStep.period,
),
);
}
void setAutobackupPeriod(final Duration? autobackupPeriod) {
emit(
state.copyWith(
autobackupPeriod: autobackupPeriod,
currentStep: BackupsWizardStep.quotas,
),
);
}
void setAutobackupQuotas(final AutobackupQuotas autobackupQuotas) {
emit(
state.copyWith(
autobackupQuotas: autobackupQuotas,
currentStep: BackupsWizardStep.confirmation,
),
);
}
}

View file

@ -0,0 +1,48 @@
part of 'backups_wizard_cubit.dart';
/// Temporary attributes of backups configuration.
///
/// After backups wizard is finished, this data is to be converted
/// to actual backups state and set to the app config and its server permanently.
class BackupsWizardState {
const BackupsWizardState({
this.autobackupPeriod,
this.autobackupQuotas,
this.backupsCredential,
this.currentStep = BackupsWizardStep.hostingInitialization,
});
final BackupsWizardStep currentStep;
final Duration? autobackupPeriod;
final AutobackupQuotas? autobackupQuotas;
final BackupsCredential? backupsCredential;
BackupsWizardState copyWith({
final Duration? autobackupPeriod,
final AutobackupQuotas? autobackupQuotas,
final BackupsCredential? backupsCredential,
final BackupsWizardStep? currentStep,
}) =>
BackupsWizardState(
backupsCredential: backupsCredential ?? this.backupsCredential,
autobackupQuotas: autobackupQuotas ?? this.autobackupQuotas,
autobackupPeriod: autobackupPeriod ?? this.autobackupPeriod,
currentStep: currentStep ?? this.currentStep,
);
}
/// The state machine is expected to follow:
///
/// (Initialize backups) -> hostingInitialization -> period -> rotation -> confirmation
///
/// or
///
/// (Recovery access) -> hostingRecovery
enum BackupsWizardStep {
hostingRecovery,
hostingInitialization,
period,
quotas,
confirmation,
finished,
}