selfprivacy.org.app/lib/ui/pages/setup/recovering/recovery_routing.dart

144 lines
5.7 KiB
Dart
Raw Normal View History

import 'package:cubit_form/cubit_form.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
import 'package:selfprivacy/logic/cubit/forms/factories/field_cubit_factory.dart';
import 'package:selfprivacy/logic/cubit/forms/setup/recovering/recovery_domain_form_cubit.dart';
2022-05-24 18:55:39 +00:00
import 'package:selfprivacy/ui/components/brand_button/filled_button.dart';
import 'package:selfprivacy/ui/components/brand_hero_screen/brand_hero_screen.dart';
2022-06-10 15:15:43 +00:00
import 'package:selfprivacy/ui/pages/root_route.dart';
import 'package:selfprivacy/ui/pages/setup/recovering/recover_by_old_token.dart';
import 'package:selfprivacy/ui/pages/setup/recovering/recover_by_recovery_key.dart';
import 'package:selfprivacy/ui/pages/setup/recovering/recover_by_new_device_key.dart';
import 'package:selfprivacy/ui/pages/setup/recovering/recovery_confirm_backblaze.dart';
import 'package:selfprivacy/ui/pages/setup/recovering/recovery_confirm_cloudflare.dart';
import 'package:selfprivacy/ui/pages/setup/recovering/recovery_confirm_server.dart';
import 'package:selfprivacy/ui/pages/setup/recovering/recovery_server_provider_connected.dart';
import 'package:selfprivacy/ui/pages/setup/recovering/recovery_method_select.dart';
2022-06-10 15:15:43 +00:00
import 'package:selfprivacy/utils/route_transitions/basic.dart';
class RecoveryRouting extends StatelessWidget {
const RecoveryRouting({super.key});
2022-05-24 18:55:39 +00:00
@override
Widget build(final BuildContext context) {
final serverInstallation = context.watch<ServerInstallationCubit>().state;
2022-05-24 18:55:39 +00:00
Widget currentPage = const SelectDomainToRecover();
if (serverInstallation is ServerInstallationRecovery) {
switch (serverInstallation.currentStep) {
2022-05-24 18:55:39 +00:00
case RecoveryStep.selecting:
if (serverInstallation.recoveryCapabilities ==
ServerRecoveryCapabilities.loginTokens) {
2022-05-24 18:55:39 +00:00
currentPage = const RecoveryMethodSelect();
}
if (serverInstallation.recoveryCapabilities ==
ServerRecoveryCapabilities.legacy) {
currentPage = const RecoveryFallbackMethodSelect();
}
break;
2022-05-24 18:55:39 +00:00
case RecoveryStep.recoveryKey:
currentPage = const RecoverByRecoveryKey();
break;
2022-05-24 18:55:39 +00:00
case RecoveryStep.newDeviceKey:
currentPage = const RecoverByNewDeviceKeyInstruction();
break;
2022-05-24 18:55:39 +00:00
case RecoveryStep.oldToken:
currentPage = const RecoverByOldToken();
break;
case RecoveryStep.serverProviderToken:
currentPage = const RecoveryServerProviderConnected();
break;
2022-05-24 18:55:39 +00:00
case RecoveryStep.serverSelection:
currentPage = const RecoveryConfirmServer();
break;
2022-05-24 18:55:39 +00:00
case RecoveryStep.cloudflareToken:
currentPage = const RecoveryConfirmCloudflare();
break;
2022-05-24 18:55:39 +00:00
case RecoveryStep.backblazeToken:
currentPage = const RecoveryConfirmBackblaze();
break;
}
}
2022-05-31 14:30:35 +00:00
return BlocListener<ServerInstallationCubit, ServerInstallationState>(
listener: (final context, final state) {
2022-05-31 14:30:35 +00:00
if (state is ServerInstallationFinished) {
Navigator.of(context).popUntil((final route) => route.isFirst);
2022-05-31 14:30:35 +00:00
}
},
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
child: currentPage,
),
);
}
}
class SelectDomainToRecover extends StatelessWidget {
const SelectDomainToRecover({super.key});
2022-05-24 18:55:39 +00:00
@override
Widget build(final BuildContext context) {
final serverInstallation = context.watch<ServerInstallationCubit>();
return BlocProvider(
create: (final context) => RecoveryDomainFormCubit(
serverInstallation,
FieldCubitFactory(context),
),
child: Builder(
builder: (final context) {
final formCubitState = context.watch<RecoveryDomainFormCubit>().state;
return BlocListener<ServerInstallationCubit, ServerInstallationState>(
listener: (final context, final state) {
if (state is ServerInstallationRecovery) {
2022-05-24 18:55:39 +00:00
if (state.currentStep == RecoveryStep.selecting) {
if (state.recoveryCapabilities ==
ServerRecoveryCapabilities.none) {
context
.read<RecoveryDomainFormCubit>()
2022-05-24 18:55:39 +00:00
.setCustomError('recovering.domain_recover_error'.tr());
}
}
}
},
child: BrandHeroScreen(
2022-05-24 18:55:39 +00:00
heroTitle: 'recovering.recovery_main_header'.tr(),
heroSubtitle: 'recovering.domain_recovery_description'.tr(),
hasBackButton: true,
hasFlashButton: false,
2022-06-10 15:15:43 +00:00
onBackButtonPressed: () {
Navigator.of(context).pushAndRemoveUntil(
materialRoute(const RootPage()),
(final predicate) => false,
);
},
children: [
CubitFormTextField(
formFieldCubit:
context.read<RecoveryDomainFormCubit>().serverDomainField,
decoration: InputDecoration(
2022-05-24 18:55:39 +00:00
border: const OutlineInputBorder(),
labelText: 'recovering.domain_recover_placeholder'.tr(),
),
),
2022-05-24 18:55:39 +00:00
const SizedBox(height: 16),
FilledButton(
title: 'basis.continue'.tr(),
onPressed: formCubitState.isSubmitting
? null
: () =>
context.read<RecoveryDomainFormCubit>().trySubmit(),
)
],
),
);
},
),
);
}
}