selfprivacy.org.app/lib/ui/pages/initializing/initializing.dart

498 lines
17 KiB
Dart
Raw Normal View History

2020-12-30 14:13:25 +00:00
import 'package:cubit_form/cubit_form.dart';
2020-12-10 20:33:19 +00:00
import 'package:flutter/material.dart';
2020-12-30 14:13:25 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
2020-12-10 20:33:19 +00:00
import 'package:selfprivacy/config/brand_theme.dart';
2021-02-03 19:51:07 +00:00
import 'package:selfprivacy/logic/cubit/forms/initializing/backblaze_form_cubit.dart';
2021-01-06 17:35:57 +00:00
import 'package:selfprivacy/logic/cubit/forms/initializing/cloudflare_form_cubit.dart';
2021-02-15 18:58:29 +00:00
import 'package:selfprivacy/logic/cubit/forms/initializing/domain_cloudflare.dart';
2020-12-30 14:13:25 +00:00
import 'package:selfprivacy/logic/cubit/forms/initializing/hetzner_form_cubit.dart';
2021-01-14 18:45:10 +00:00
import 'package:selfprivacy/logic/cubit/forms/initializing/root_user_form_cubit.dart';
2021-01-06 17:35:57 +00:00
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
2020-12-10 20:33:19 +00:00
import 'package:selfprivacy/logic/cubit/providers/providers_cubit.dart';
import 'package:selfprivacy/ui/components/brand_button/brand_button.dart';
import 'package:selfprivacy/ui/components/brand_card/brand_card.dart';
2021-03-18 07:26:54 +00:00
import 'package:selfprivacy/ui/components/brand_md/brand_md.dart';
2020-12-10 20:33:19 +00:00
import 'package:selfprivacy/ui/components/brand_modal_sheet/brand_modal_sheet.dart';
import 'package:selfprivacy/ui/components/brand_text/brand_text.dart';
2021-02-16 18:48:15 +00:00
import 'package:selfprivacy/ui/components/brand_timer/brand_timer.dart';
2020-12-30 14:13:25 +00:00
import 'package:selfprivacy/ui/components/progress_bar/progress_bar.dart';
import 'package:selfprivacy/ui/pages/rootRoute.dart';
import 'package:selfprivacy/utils/route_transitions/basic.dart';
2021-03-18 00:55:38 +00:00
import 'package:easy_localization/easy_localization.dart';
2020-12-10 20:33:19 +00:00
2021-01-06 17:35:57 +00:00
class InitializingPage extends StatelessWidget {
2020-12-10 20:33:19 +00:00
@override
Widget build(BuildContext context) {
2021-01-06 17:35:57 +00:00
var cubit = context.watch<AppConfigCubit>();
var actualPage = [
2021-02-16 18:48:15 +00:00
() => _stepHetzner(cubit),
() => _stepCloudflare(cubit),
() => _stepBackblaze(cubit),
() => _stepDomain(cubit),
() => _stepUser(cubit),
() => _stepServer(cubit),
() => _stepCheck(cubit),
() => _stepCheck(cubit),
() => _stepCheck(cubit),
() => Container(child: Text('Everythigng is initialized'))
][cubit.state.progress]();
2021-01-06 17:35:57 +00:00
return BlocListener<AppConfigCubit, AppConfigState>(
listener: (context, state) {
if (state.isFullyInitilized) {
Navigator.of(context).pushReplacement(materialRoute(RootPage()));
}
},
child: SafeArea(
child: Scaffold(
body: ListView(
children: [
Padding(
padding: brandPagePadding1,
child: Column(
2021-02-03 19:51:07 +00:00
crossAxisAlignment: CrossAxisAlignment.center,
2021-01-06 17:35:57 +00:00
children: [
ProgressBar(
steps: [
'Hetzner',
'CloudFlare',
2021-02-03 19:51:07 +00:00
'Backblaze',
2021-01-06 17:35:57 +00:00
'Domain',
'User',
'Server',
2021-03-18 00:55:38 +00:00
'',
'',
''
2020-12-30 14:13:25 +00:00
],
2021-01-06 17:35:57 +00:00
activeIndex: cubit.state.progress,
2020-12-30 14:13:25 +00:00
),
2021-01-06 17:35:57 +00:00
],
),
2020-12-30 14:13:25 +00:00
),
2021-01-06 17:35:57 +00:00
_addCard(
AnimatedSwitcher(
duration: Duration(milliseconds: 300),
child: actualPage,
),
2020-12-30 14:13:25 +00:00
),
2021-01-06 17:35:57 +00:00
BrandButton.text(
2021-03-18 00:55:38 +00:00
title: cubit.state.isFullyInitilized
? 'basis.close'.tr()
: 'Настрою потом',
2021-01-06 17:35:57 +00:00
onPressed: () {
Navigator.of(context).pushAndRemoveUntil(
materialRoute(RootPage()),
2021-03-15 15:39:44 +00:00
(predicate) => false,
2021-01-06 17:35:57 +00:00
);
}),
SizedBox(height: 30),
],
),
2020-12-30 14:13:25 +00:00
),
2020-12-10 20:33:19 +00:00
),
);
}
2021-01-06 17:35:57 +00:00
Widget _stepHetzner(AppConfigCubit initializingCubit) {
2020-12-30 14:13:25 +00:00
return BlocProvider(
create: (context) => HetznerFormCubit(initializingCubit),
child: Builder(builder: (context) {
2021-03-15 15:39:44 +00:00
var formCubitState = context.watch<HetznerFormCubit>().state;
2020-12-30 14:13:25 +00:00
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2021-02-15 18:58:29 +00:00
Image.asset(
'assets/images/logos/hetzner.png',
width: 150,
),
2020-12-30 14:13:25 +00:00
SizedBox(height: 10),
2021-03-18 00:55:38 +00:00
BrandText.h2('initializing.1'.tr()),
2020-12-30 14:13:25 +00:00
SizedBox(height: 10),
2021-03-18 00:55:38 +00:00
BrandText.body2('initializing.2'.tr()),
2020-12-30 14:13:25 +00:00
Spacer(),
CubitFormTextField(
2021-03-15 15:39:44 +00:00
formFieldCubit: context.read<HetznerFormCubit>().apiKey,
2020-12-30 14:13:25 +00:00
textAlign: TextAlign.center,
scrollPadding: EdgeInsets.only(bottom: 70),
decoration: InputDecoration(
hintText: 'Hetzner API Token',
),
),
2021-01-06 17:35:57 +00:00
Spacer(),
2020-12-30 14:13:25 +00:00
BrandButton.rised(
2021-03-15 15:39:44 +00:00
onPressed: formCubitState.isSubmitting
? null
: () => context.read<HetznerFormCubit>().trySubmit(),
2021-03-18 00:55:38 +00:00
title: 'basis.connect'.tr(),
2020-12-30 14:13:25 +00:00
),
SizedBox(height: 10),
BrandButton.text(
onPressed: () => _showModal(context, _HowHetzner()),
2021-03-18 00:55:38 +00:00
title: 'initializing.how'.tr(),
2020-12-30 14:13:25 +00:00
),
],
);
}),
);
}
2020-12-10 20:33:19 +00:00
void _showModal(BuildContext context, Widget widget) {
showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (BuildContext context) {
return widget;
},
);
}
2021-01-06 17:35:57 +00:00
Widget _stepCloudflare(AppConfigCubit initializingCubit) {
return BlocProvider(
create: (context) => CloudFlareFormCubit(initializingCubit),
child: Builder(builder: (context) {
2021-03-15 15:39:44 +00:00
var formCubitState = context.watch<CloudFlareFormCubit>().state;
2021-01-06 17:35:57 +00:00
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2021-02-15 18:58:29 +00:00
Image.asset(
'assets/images/logos/cloudflare.png',
width: 150,
),
SizedBox(height: 10),
2021-03-18 00:55:38 +00:00
BrandText.h2('initializing.3'.tr()),
2021-01-06 17:35:57 +00:00
SizedBox(height: 10),
2021-03-18 00:55:38 +00:00
BrandText.body2('initializing.4'.tr()),
2021-01-06 17:35:57 +00:00
Spacer(),
CubitFormTextField(
2021-03-15 15:39:44 +00:00
formFieldCubit: context.read<CloudFlareFormCubit>().apiKey,
2021-01-06 17:35:57 +00:00
textAlign: TextAlign.center,
scrollPadding: EdgeInsets.only(bottom: 70),
decoration: InputDecoration(
2021-03-18 00:55:38 +00:00
hintText: 'initializing.5'.tr(),
2021-01-06 17:35:57 +00:00
),
),
Spacer(),
BrandButton.rised(
2021-03-15 15:39:44 +00:00
onPressed: formCubitState.isSubmitting
? null
: () => context.read<CloudFlareFormCubit>().trySubmit(),
2021-03-18 00:55:38 +00:00
title: 'basis.connect'.tr(),
2021-01-06 17:35:57 +00:00
),
SizedBox(height: 10),
BrandButton.text(
onPressed: () {},
2021-03-18 00:55:38 +00:00
title: 'initializing.how'.tr(),
2021-01-06 17:35:57 +00:00
),
],
);
}),
2020-12-30 14:13:25 +00:00
);
}
2021-02-03 19:51:07 +00:00
Widget _stepBackblaze(AppConfigCubit initializingCubit) {
return BlocProvider(
create: (context) => BackblazeFormCubit(initializingCubit),
child: Builder(builder: (context) {
2021-03-15 15:39:44 +00:00
var formCubitState = context.watch<BackblazeFormCubit>().state;
2021-02-03 19:51:07 +00:00
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2021-02-15 18:58:29 +00:00
Image.asset(
'assets/images/logos/backblaze.png',
height: 50,
),
2021-02-03 19:51:07 +00:00
SizedBox(height: 10),
2021-03-18 00:55:38 +00:00
BrandText.h2('initializing.6'.tr()),
2021-02-03 19:51:07 +00:00
SizedBox(height: 10),
Spacer(),
CubitFormTextField(
2021-03-15 15:39:44 +00:00
formFieldCubit: context.read<BackblazeFormCubit>().keyId,
2021-02-03 19:51:07 +00:00
textAlign: TextAlign.center,
scrollPadding: EdgeInsets.only(bottom: 70),
decoration: InputDecoration(
hintText: 'KeyID',
),
),
Spacer(),
CubitFormTextField(
2021-03-15 15:39:44 +00:00
formFieldCubit: context.read<BackblazeFormCubit>().applicationKey,
2021-02-03 19:51:07 +00:00
textAlign: TextAlign.center,
scrollPadding: EdgeInsets.only(bottom: 70),
decoration: InputDecoration(
hintText: 'Master Application Key',
),
),
Spacer(),
BrandButton.rised(
2021-03-15 15:39:44 +00:00
onPressed: formCubitState.isSubmitting
? null
: () => context.read<BackblazeFormCubit>().trySubmit(),
2021-03-18 00:55:38 +00:00
title: 'basis.connect'.tr(),
2021-02-03 19:51:07 +00:00
),
SizedBox(height: 10),
BrandButton.text(
onPressed: () => _showModal(context, _HowHetzner()),
2021-03-18 00:55:38 +00:00
title: 'initializing.how'.tr(),
2021-02-03 19:51:07 +00:00
),
],
);
}),
);
}
2021-01-06 17:35:57 +00:00
Widget _stepDomain(AppConfigCubit initializingCubit) {
return BlocProvider(
2021-02-15 18:58:29 +00:00
create: (context) => DomainSetupCubit(initializingCubit)..load(),
2021-01-06 17:35:57 +00:00
child: Builder(builder: (context) {
2021-03-15 15:39:44 +00:00
DomainSetupState state = context.watch<DomainSetupCubit>().state;
2021-01-06 17:35:57 +00:00
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2021-02-15 18:58:29 +00:00
Image.asset(
'assets/images/logos/cloudflare.png',
width: 150,
),
SizedBox(height: 30),
2021-03-18 00:55:38 +00:00
BrandText.h2('basis.domain'.tr()),
2021-01-06 17:35:57 +00:00
SizedBox(height: 10),
2021-03-18 00:55:38 +00:00
if (state is Empty) BrandText.body2('initializing.7'.tr()),
2021-02-15 18:58:29 +00:00
if (state is Loading)
BrandText.body2(
state.type == LoadingTypes.loadingDomain
2021-03-18 00:55:38 +00:00
? 'initializing.8'.tr()
: 'basis.saving'.tr(),
2021-01-06 17:35:57 +00:00
),
2021-02-15 18:58:29 +00:00
if (state is MoreThenOne)
BrandText.body2(
2021-03-18 00:55:38 +00:00
'initializing.9'.tr(),
2021-02-15 18:58:29 +00:00
),
if (state is Loaded) ...[
SizedBox(height: 10),
Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: BrandText.h3(
'${state.domain}',
textAlign: TextAlign.center,
),
),
Container(
width: 50,
child: BrandButton.rised(
2021-03-15 15:39:44 +00:00
onPressed: () => context.read<DomainSetupCubit>().load(),
2021-02-15 18:58:29 +00:00
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.refresh,
color: Colors.white,
),
],
),
),
),
],
)
],
if (state is Empty) ...[
SizedBox(height: 30),
BrandButton.rised(
2021-03-15 15:39:44 +00:00
onPressed: () => context.read<DomainSetupCubit>().load(),
2021-02-15 18:58:29 +00:00
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.refresh,
color: Colors.white,
),
SizedBox(width: 10),
BrandText.buttonTitleText('Обновить cписок'),
],
),
),
],
if (state is Loaded) ...[
SizedBox(height: 30),
BrandButton.rised(
2021-03-15 15:39:44 +00:00
onPressed: () => context.read<DomainSetupCubit>().saveDomain(),
2021-03-18 00:55:38 +00:00
title: 'initializing.10'.tr(),
2021-02-15 18:58:29 +00:00
),
],
SizedBox(height: 10),
2021-01-06 17:35:57 +00:00
Spacer(),
SizedBox(height: 10),
BrandButton.text(
onPressed: () => _showModal(context, _HowHetzner()),
2021-03-18 00:55:38 +00:00
title: 'initializing.how'.tr(),
2021-01-06 17:35:57 +00:00
),
],
);
}),
2020-12-30 14:13:25 +00:00
);
}
2021-01-06 17:35:57 +00:00
Widget _stepUser(AppConfigCubit initializingCubit) {
return BlocProvider(
2021-01-14 18:45:10 +00:00
create: (context) => RootUserFormCubit(initializingCubit),
2021-01-06 17:35:57 +00:00
child: Builder(builder: (context) {
2021-03-15 15:39:44 +00:00
var formCubitState = context.watch<RootUserFormCubit>().state;
2021-01-06 17:35:57 +00:00
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Spacer(),
SizedBox(height: 10),
CubitFormTextField(
2021-03-15 15:39:44 +00:00
formFieldCubit: context.read<RootUserFormCubit>().userName,
2021-01-06 17:35:57 +00:00
textAlign: TextAlign.center,
scrollPadding: EdgeInsets.only(bottom: 70),
decoration: InputDecoration(
2021-03-18 00:55:38 +00:00
hintText: 'basis.nickname'.tr(),
2020-12-10 20:33:19 +00:00
),
2021-01-06 17:35:57 +00:00
),
SizedBox(height: 10),
2021-02-15 18:58:29 +00:00
BlocBuilder<FieldCubit<bool>, FieldCubitState<bool>>(
2021-03-15 15:39:44 +00:00
bloc: context.read<RootUserFormCubit>().isVisible,
2021-02-15 18:58:29 +00:00
builder: (context, state) {
var isVisible = state.value;
return CubitFormTextField(
obscureText: !isVisible,
2021-03-15 15:39:44 +00:00
formFieldCubit: context.read<RootUserFormCubit>().password,
2021-02-15 18:58:29 +00:00
textAlign: TextAlign.center,
scrollPadding: EdgeInsets.only(bottom: 70),
decoration: InputDecoration(
2021-03-18 00:55:38 +00:00
hintText: 'basis.password'.tr(),
2021-02-15 18:58:29 +00:00
suffixIcon: IconButton(
icon: Icon(
isVisible ? Icons.visibility : Icons.visibility_off,
),
2021-03-15 15:39:44 +00:00
onPressed: () => context
.read<RootUserFormCubit>()
.isVisible
.setValue(!isVisible),
2021-02-15 18:58:29 +00:00
),
suffixIconConstraints: BoxConstraints(minWidth: 60),
prefixIconConstraints: BoxConstraints(maxWidth: 85),
prefixIcon: Container(),
),
);
},
2021-01-06 17:35:57 +00:00
),
Spacer(),
BrandButton.rised(
2021-03-15 15:39:44 +00:00
onPressed: formCubitState.isSubmitting
? null
: () => context.read<RootUserFormCubit>().trySubmit(),
2021-03-18 00:55:38 +00:00
title: 'basis.connect'.tr(),
2021-01-06 17:35:57 +00:00
),
SizedBox(height: 10),
BrandButton.text(
onPressed: () => _showModal(context, _HowHetzner()),
2021-03-18 00:55:38 +00:00
title: 'initializing.how'.tr(),
2021-01-06 17:35:57 +00:00
),
],
);
}),
2020-12-30 14:13:25 +00:00
);
}
2020-12-10 20:33:19 +00:00
2021-01-06 17:35:57 +00:00
Widget _stepServer(AppConfigCubit appConfigCubit) {
var isLoading = appConfigCubit.state.isLoading;
return Builder(builder: (context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2021-01-08 12:37:28 +00:00
Spacer(flex: 2),
2021-03-18 00:55:38 +00:00
BrandText.h2('initializing.how'.tr()),
2021-01-06 17:35:57 +00:00
SizedBox(height: 10),
2021-03-18 00:55:38 +00:00
BrandText.body2('initializing.11'.tr()),
2021-01-06 17:35:57 +00:00
Spacer(),
BrandButton.rised(
2021-01-21 21:01:42 +00:00
onPressed:
2021-03-15 15:39:44 +00:00
isLoading! ? null : appConfigCubit.createServerAndSetDnsRecords,
2021-03-18 00:55:38 +00:00
title: isLoading ? 'loading' : 'initializing.11'.tr(),
2021-01-06 17:35:57 +00:00
),
2021-01-08 12:37:28 +00:00
Spacer(flex: 2),
2021-01-06 17:35:57 +00:00
BrandButton.text(
onPressed: () => _showModal(context, _HowHetzner()),
2021-03-18 00:55:38 +00:00
title: 'initializing.what'.tr(),
2021-01-06 17:35:57 +00:00
),
],
);
});
}
2021-01-06 19:25:53 +00:00
Widget _stepCheck(AppConfigCubit appConfigCubit) {
2021-02-16 18:48:15 +00:00
assert(appConfigCubit.state is TimerState, 'wronge state');
var state = appConfigCubit.state as TimerState;
2021-03-15 15:39:44 +00:00
String? text;
if (state.isServerReseted!) {
2021-03-18 00:55:38 +00:00
text = 'initializing.13'.tr();
2021-03-15 15:39:44 +00:00
} else if (state.isServerStarted!) {
2021-03-18 00:55:38 +00:00
text = 'initializing.14'.tr();
2021-02-16 18:48:15 +00:00
} else if (state.isServerCreated) {
2021-03-18 00:55:38 +00:00
text = 'initializing.15'.tr();
2021-02-16 18:48:15 +00:00
}
return Builder(builder: (context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Spacer(flex: 2),
SizedBox(height: 10),
BrandText.body2(text),
SizedBox(height: 10),
2021-03-15 15:39:44 +00:00
if (!state.isLoading!)
2021-02-16 18:48:15 +00:00
Row(
children: [
2021-03-18 00:55:38 +00:00
BrandText.body2('initializing.16'.tr()),
2021-02-16 18:48:15 +00:00
BrandTimer(
startDateTime: state.timerStart,
duration: state.duration,
)
],
),
2021-03-18 00:55:38 +00:00
if (state.isLoading!) BrandText.body2('initializing.17'.tr()),
2021-02-16 18:48:15 +00:00
Spacer(
flex: 2,
),
BrandButton.text(
onPressed: () => _showModal(context, _HowHetzner()),
2021-03-18 00:55:38 +00:00
title: 'initializing.what'.tr(),
2021-02-16 18:48:15 +00:00
),
],
);
});
2021-01-06 19:25:53 +00:00
}
2020-12-30 14:13:25 +00:00
Widget _addCard(Widget child) {
2021-01-06 17:35:57 +00:00
return Container(
height: 500,
2020-12-30 14:13:25 +00:00
padding: brandPagePadding2,
2021-01-08 12:37:28 +00:00
child: BrandCard(child: child),
2020-12-30 14:13:25 +00:00
);
2020-12-10 20:33:19 +00:00
}
}
class _HowHetzner extends StatelessWidget {
const _HowHetzner({
2021-03-15 15:39:44 +00:00
Key? key,
2020-12-10 20:33:19 +00:00
}) : super(key: key);
@override
Widget build(BuildContext context) {
return BrandModalSheet(
child: Padding(
2021-03-18 07:26:54 +00:00
padding: brandPagePadding1,
child: BrandMarkdown(
fileName: 'how_hetzner',
)),
2020-12-10 20:33:19 +00:00
);
}
}