refactor: Move backup business logic to BackupsCubit

This commit is contained in:
NaiJi 2023-09-26 22:20:42 -03:00
parent 08988ed10b
commit 8f6cb77cc4
9 changed files with 38 additions and 126 deletions

View file

@ -29,6 +29,8 @@ class BackupsCubit extends ServerInstallationDependendCubit<BackupsState> {
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));
emit(
@ -37,6 +39,7 @@ class BackupsCubit extends ServerInstallationDependendCubit<BackupsState> {
isInitialized: backupConfig?.isInitialized,
autobackupPeriod: backupConfig?.autobackupPeriod ?? Duration.zero,
autobackupQuotas: backupConfig?.autobackupQuotas,
backupsCredential: backupsCredential,
backups: backups,
preventActions: false,
refreshing: false,
@ -45,6 +48,19 @@ class BackupsCubit extends ServerInstallationDependendCubit<BackupsState> {
}
}
Future<void> setBackupsKey(
final String keyId,
final String applicationKey,
) async {
final BackupsCredential backupsCredential = BackupsCredential(
keyId: keyId,
applicationKey: applicationKey,
provider: BackupsProviderType.backblaze,
);
await getIt<ApiConfigModel>().storeBackblazeCredential(backupsCredential);
emit(state.copyWith(backupsCredential: backupsCredential));
}
Future<void> initializeBackups() async {
emit(state.copyWith(preventActions: true));
final String? encryptionKey =

View file

@ -10,6 +10,7 @@ class BackupsState extends ServerInstallationDependendState {
this.autobackupPeriod,
this.backblazeBucket,
this.autobackupQuotas,
this.backupsCredential,
});
final bool isInitialized;
@ -20,6 +21,7 @@ class BackupsState extends ServerInstallationDependendState {
final Duration? autobackupPeriod;
final BackblazeBucket? backblazeBucket;
final AutobackupQuotas? autobackupQuotas;
final BackupsCredential? backupsCredential;
List<Backup> serviceBackups(final String serviceId) => backups
.where((final backup) => backup.serviceId == serviceId)
@ -43,6 +45,7 @@ class BackupsState extends ServerInstallationDependendState {
final Duration? autobackupPeriod,
final BackblazeBucket? backblazeBucket,
final AutobackupQuotas? autobackupQuotas,
final BackupsCredential? backupsCredential,
}) =>
BackupsState(
isInitialized: isInitialized ?? this.isInitialized,
@ -50,12 +53,13 @@ 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,
// 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,
autobackupQuotas: autobackupQuotas ?? this.autobackupQuotas,
);
}

View file

@ -2,12 +2,12 @@ import 'dart:async';
import 'package:cubit_form/cubit_form.dart';
import 'package:selfprivacy/config/get_it_config.dart';
import 'package:selfprivacy/logic/api_maps/rest_maps/backblaze.dart';
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
import 'package:selfprivacy/logic/cubit/backups/backups_cubit.dart';
import 'package:selfprivacy/logic/models/hive/backups_credential.dart';
import 'package:easy_localization/easy_localization.dart';
class BackblazeFormCubit extends FormCubit {
BackblazeFormCubit(this.serverInstallationCubit) {
BackblazeFormCubit(this.backupsCubit) {
keyId = FieldCubit(
initalValue: '',
validations: [
@ -27,13 +27,13 @@ class BackblazeFormCubit extends FormCubit {
@override
FutureOr<void> onSubmit() async {
serverInstallationCubit.setBackblazeKey(
await backupsCubit.setBackupsKey(
keyId.state.value,
applicationKey.state.value,
);
}
final ServerInstallationCubit serverInstallationCubit;
final BackupsCubit backupsCubit;
late final FieldCubit<String> keyId;
late final FieldCubit<String> applicationKey;

View file

@ -5,11 +5,8 @@ import 'package:easy_localization/easy_localization.dart';
import 'package:equatable/equatable.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/api_maps/tls_options.dart';
import 'package:selfprivacy/logic/models/disk_size.dart';
import 'package:selfprivacy/logic/models/hive/backblaze_bucket.dart';
import 'package:selfprivacy/logic/models/hive/backups_credential.dart';
import 'package:selfprivacy/logic/models/callback_dialogue_branching.dart';
import 'package:selfprivacy/logic/models/hive/server_details.dart';
import 'package:selfprivacy/logic/models/hive/user.dart';
@ -214,33 +211,6 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
);
}
void setBackblazeKey(final String keyId, final String applicationKey) async {
final BackupsCredential backblazeCredential = BackupsCredential(
keyId: keyId,
applicationKey: applicationKey,
provider: BackupsProviderType.backblaze,
);
final BackblazeBucket? bucket;
await repository.saveBackblazeKey(backblazeCredential);
if (state is ServerInstallationRecovery) {
final configuration = await ServerApi(
customToken:
(state as ServerInstallationRecovery).serverDetails!.apiToken,
isWithToken: true,
).getBackupsConfiguration();
if (configuration != null) {
try {
bucket = await BackblazeApi()
.fetchBucket(backblazeCredential, configuration);
await getIt<ApiConfigModel>().storeBackblazeBucket(bucket!);
} catch (e) {
print(e);
}
}
return;
}
}
void setDomain(final ServerDomain serverDomain) async {
await repository.saveDomain(serverDomain);
emit(

View file

@ -75,7 +75,7 @@ class BackupDetailsPage extends StatelessWidget {
BrandButton.rised(
onPressed: preventActions
? null
: () => context.pushRoute(const BackupProviderPicker()),
: () => context.pushRoute(const BackupProviderPickerRoute()),
text: 'backup.initialize'.tr(),
),
],

View file

@ -8,8 +8,8 @@ import 'package:selfprivacy/ui/components/buttons/brand_button.dart';
import 'package:selfprivacy/ui/layouts/responsive_layout_with_infobox.dart';
@RoutePage()
class BackupProviderPicker extends StatelessWidget {
const BackupProviderPicker({
class BackupProviderPickerPage extends StatelessWidget {
const BackupProviderPickerPage({
super.key,
});

View file

@ -1,79 +0,0 @@
import 'package:cubit_form/cubit_form.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:selfprivacy/logic/cubit/forms/setup/initializing/backblaze_form_cubit.dart';
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
import 'package:selfprivacy/logic/cubit/support_system/support_system_cubit.dart';
import 'package:selfprivacy/ui/components/buttons/brand_button.dart';
import 'package:selfprivacy/ui/layouts/brand_hero_screen.dart';
class RecoveryConfirmBackblaze extends StatelessWidget {
const RecoveryConfirmBackblaze({super.key});
@override
Widget build(final BuildContext context) {
final ServerInstallationCubit appConfig =
context.watch<ServerInstallationCubit>();
return BlocProvider(
create: (final BuildContext context) => BackblazeFormCubit(appConfig),
child: Builder(
builder: (final BuildContext context) {
final FormCubitState formCubitState =
context.watch<BackblazeFormCubit>().state;
return BrandHeroScreen(
heroTitle: 'recovering.provider_connected'.tr(args: ['Backblaze']),
heroSubtitle: 'recovering.provider_connected_description'.tr(
args: ['Backblaze'],
),
hasBackButton: true,
ignoreBreakpoints: true,
hasSupportDrawer: true,
onBackButtonPressed: () {
Navigator.of(context).popUntil((final route) => route.isFirst);
},
hasFlashButton: false,
children: [
CubitFormTextField(
autofocus: true,
formFieldCubit: context.read<BackblazeFormCubit>().keyId,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'KeyID',
),
),
const SizedBox(height: 16),
CubitFormTextField(
formFieldCubit:
context.read<BackblazeFormCubit>().applicationKey,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Master Application Key',
),
),
const SizedBox(height: 16),
BrandButton.rised(
onPressed: formCubitState.isSubmitting
? null
: () => context.read<BackblazeFormCubit>().trySubmit(),
text: 'basis.connect'.tr(),
),
const SizedBox(height: 16),
Builder(
builder: (final context) => BrandButton.text(
onPressed: () =>
context.read<SupportSystemCubit>().showArticle(
article: 'how_backblaze',
context: context,
),
title: 'initializing.how'.tr(),
),
),
],
);
},
),
);
}
}

View file

@ -25,6 +25,7 @@ import 'package:selfprivacy/ui/pages/services/services.dart';
import 'package:selfprivacy/ui/pages/setup/initializing/initializing.dart';
import 'package:selfprivacy/ui/pages/setup/recovering/recovery_routing.dart';
import 'package:selfprivacy/ui/pages/users/users.dart';
import 'package:selfprivacy/ui/pages/backups/backup_provider_picker_page.dart';
part 'router.gr.dart';
@ -100,7 +101,7 @@ class RootRouter extends _$RootRouter {
AutoRoute(page: BackupsListRoute.page),
AutoRoute(page: ServerStorageRoute.page),
AutoRoute(page: ExtendingVolumeRoute.page),
AutoRoute(page: BackupProviderPicker.page),
AutoRoute(page: BackupProviderPickerRoute.page),
],
),
AutoRoute(page: ServicesMigrationRoute.page),

View file

@ -186,10 +186,10 @@ abstract class _$RootRouter extends RootStackRouter {
child: const RecoveryRouting(),
);
},
BackupProviderPicker.name: (routeData) {
BackupProviderPickerRoute.name: (routeData) {
return AutoRoutePage<dynamic>(
routeData: routeData,
child: const BackupProviderPicker(),
child: const BackupProviderPickerPage(),
);
},
};
@ -691,15 +691,15 @@ class RecoveryRoute extends PageRouteInfo<void> {
}
/// generated route for
/// [BackupProviderPicker]
class BackupProviderPicker extends PageRouteInfo<void> {
const BackupProviderPicker({List<PageRouteInfo>? children})
/// [BackupProviderPickerPage]
class BackupProviderPickerRoute extends PageRouteInfo<void> {
const BackupProviderPickerRoute({List<PageRouteInfo>? children})
: super(
BackupProviderPicker.name,
BackupProviderPickerRoute.name,
initialChildren: children,
);
static const String name = 'BackupProviderPicker';
static const String name = 'BackupProviderPickerRoute';
static const PageInfo<void> page = PageInfo<void>(name);
}