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

230 lines
7.2 KiB
Dart
Raw Normal View History

2020-12-03 16:52:53 +00:00
import 'package:flutter/material.dart';
import 'package:selfprivacy/config/brand_theme.dart';
2021-01-06 17:35:57 +00:00
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
2021-12-06 18:31:19 +00:00
import 'package:selfprivacy/logic/cubit/backups/backups_cubit.dart';
2020-12-10 20:33:19 +00:00
import 'package:selfprivacy/logic/cubit/providers/providers_cubit.dart';
2020-12-06 07:28:31 +00:00
import 'package:selfprivacy/logic/models/provider.dart';
2021-06-08 18:52:44 +00:00
import 'package:selfprivacy/ui/components/brand_bottom_sheet/brand_bottom_sheet.dart';
2021-05-25 21:53:54 +00:00
import 'package:selfprivacy/ui/components/brand_cards/brand_cards.dart';
2020-12-03 16:52:53 +00:00
import 'package:selfprivacy/ui/components/brand_header/brand_header.dart';
2020-12-08 19:26:51 +00:00
import 'package:selfprivacy/ui/components/brand_text/brand_text.dart';
2020-12-03 16:52:53 +00:00
import 'package:selfprivacy/ui/components/icon_status_mask/icon_status_mask.dart';
2021-01-06 17:35:57 +00:00
import 'package:selfprivacy/ui/components/not_ready_card/not_ready_card.dart';
2021-06-20 21:08:52 +00:00
import 'package:selfprivacy/ui/helpers/modals.dart';
2021-12-06 18:31:19 +00:00
import 'package:selfprivacy/ui/pages/backup_details/backup_details.dart';
2021-03-26 13:38:39 +00:00
import 'package:selfprivacy/ui/pages/server_details/server_details.dart';
2021-03-15 15:39:44 +00:00
import 'package:easy_localization/easy_localization.dart';
2021-03-26 13:38:39 +00:00
import 'package:selfprivacy/utils/ui_helpers.dart';
var navigatorKey = GlobalKey<NavigatorState>();
2020-12-03 16:52:53 +00:00
class ProvidersPage extends StatefulWidget {
2021-03-15 15:39:44 +00:00
ProvidersPage({Key? key}) : super(key: key);
2020-12-03 16:52:53 +00:00
@override
_ProvidersPageState createState() => _ProvidersPageState();
}
class _ProvidersPageState extends State<ProvidersPage> {
@override
Widget build(BuildContext context) {
2021-09-29 18:28:47 +00:00
var isReady = context.watch<AppConfigCubit>().state is AppConfigFinished;
2021-12-06 18:31:19 +00:00
var isBackupInitialized = context.watch<BackupsCubit>().state.isInitialized;
2021-01-06 17:35:57 +00:00
2020-12-10 20:33:19 +00:00
final cards = ProviderType.values
2021-03-25 08:54:39 +00:00
.map(
(type) => Padding(
padding: EdgeInsets.only(bottom: 30),
child: _Card(
provider: ProviderModel(
2021-12-06 18:31:19 +00:00
state: isReady
? (type == ProviderType.backup && !isBackupInitialized
? StateType.uninitialized
: StateType.stable)
: StateType.uninitialized,
2021-03-25 08:54:39 +00:00
type: type,
),
),
),
)
2020-12-06 07:28:31 +00:00
.toList();
2020-12-03 16:52:53 +00:00
return Scaffold(
appBar: PreferredSize(
2021-05-25 21:53:54 +00:00
child: BrandHeader(
title: 'providers.page_title'.tr(),
hasFlashButton: true,
),
2020-12-03 16:52:53 +00:00
preferredSize: Size.fromHeight(52),
),
body: ListView(
2021-05-25 21:53:54 +00:00
padding: paddingH15V0,
2021-01-06 17:35:57 +00:00
children: [
if (!isReady) ...[
NotReadyCard(),
SizedBox(height: 24),
],
...cards,
],
2020-12-03 16:52:53 +00:00
),
);
}
}
class _Card extends StatelessWidget {
2021-03-15 15:39:44 +00:00
const _Card({Key? key, required this.provider}) : super(key: key);
2020-12-03 16:52:53 +00:00
2020-12-06 07:28:31 +00:00
final ProviderModel provider;
2020-12-03 16:52:53 +00:00
@override
Widget build(BuildContext context) {
2021-03-26 13:38:39 +00:00
late String title;
2021-03-15 15:39:44 +00:00
String? message;
2021-03-26 13:38:39 +00:00
late String stableText;
late VoidCallback onTap;
2021-09-29 18:28:47 +00:00
var isReady = context.watch<AppConfigCubit>().state is AppConfigFinished;
2021-03-15 15:39:44 +00:00
AppConfigState appConfig = context.watch<AppConfigCubit>().state;
2021-01-27 18:33:00 +00:00
var domainName =
2021-03-15 15:39:44 +00:00
appConfig.isDomainFilled ? appConfig.cloudFlareDomain!.domainName : '';
2020-12-03 16:52:53 +00:00
2020-12-06 07:28:31 +00:00
switch (provider.type) {
2020-12-10 20:33:19 +00:00
case ProviderType.server:
2021-03-15 15:39:44 +00:00
title = 'providers.server.card_title'.tr();
2021-03-26 13:38:39 +00:00
stableText = 'providers.server.status'.tr();
2021-06-20 21:08:52 +00:00
onTap = () => showBrandBottomSheet(
2021-06-08 18:52:44 +00:00
context: context,
builder: (context) => BrandBottomSheet(
2021-06-20 21:08:52 +00:00
isExpended: true,
2021-06-08 18:52:44 +00:00
child: ServerDetails(),
2021-03-26 13:38:39 +00:00
),
);
2021-06-08 18:52:44 +00:00
2020-12-03 16:52:53 +00:00
break;
2020-12-10 20:33:19 +00:00
case ProviderType.domain:
2021-03-15 15:39:44 +00:00
title = 'providers.domain.card_title'.tr();
2021-01-27 18:33:00 +00:00
message = domainName;
2021-03-26 13:38:39 +00:00
stableText = 'providers.domain.status'.tr();
2021-06-20 21:08:52 +00:00
onTap = () => showBrandBottomSheet<void>(
2021-03-26 13:38:39 +00:00
context: context,
builder: (BuildContext context) {
return _ProviderDetails(
provider: provider,
statusText: stableText,
);
},
);
2020-12-03 16:52:53 +00:00
break;
2020-12-10 20:33:19 +00:00
case ProviderType.backup:
2021-03-15 15:39:44 +00:00
title = 'providers.backup.card_title'.tr();
2021-03-26 13:38:39 +00:00
stableText = 'providers.backup.status'.tr();
2021-12-06 18:31:19 +00:00
onTap = () => showBrandBottomSheet(
2021-03-26 13:38:39 +00:00
context: context,
builder: (BuildContext context) {
2021-12-06 18:31:19 +00:00
return BrandBottomSheet(
isExpended: true,
child: BackupDetails(),
2021-03-26 13:38:39 +00:00
);
},
);
2020-12-06 07:28:31 +00:00
break;
}
return GestureDetector(
2021-06-08 18:52:44 +00:00
onTap: isReady ? onTap : null,
2021-05-25 21:53:54 +00:00
child: BrandCards.big(
2020-12-06 07:28:31 +00:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2021-01-06 17:35:57 +00:00
IconStatusMask(
2020-12-06 07:28:31 +00:00
status: provider.state,
child: Icon(provider.icon, size: 30, color: Colors.white),
),
SizedBox(height: 10),
2020-12-08 19:26:51 +00:00
BrandText.h2(title),
2020-12-06 07:28:31 +00:00
SizedBox(height: 10),
if (message != null) ...[
2020-12-08 19:26:51 +00:00
BrandText.body2(message),
2020-12-06 07:28:31 +00:00
SizedBox(height: 10),
],
2020-12-10 20:33:19 +00:00
if (provider.state == StateType.stable) BrandText.body2(stableText),
2020-12-06 07:28:31 +00:00
],
),
),
);
}
}
class _ProviderDetails extends StatelessWidget {
const _ProviderDetails({
2021-03-15 15:39:44 +00:00
Key? key,
required this.provider,
required this.statusText,
2020-12-06 07:28:31 +00:00
}) : super(key: key);
final ProviderModel provider;
2021-03-15 15:39:44 +00:00
final String? statusText;
2020-12-06 07:28:31 +00:00
@override
Widget build(BuildContext context) {
2021-03-15 15:39:44 +00:00
late String title;
2021-03-18 00:55:38 +00:00
late List<Widget> children;
2020-12-06 07:28:31 +00:00
2021-03-18 00:55:38 +00:00
var config = context.watch<AppConfigCubit>().state;
2021-03-26 13:38:39 +00:00
var domainName = UiHelpers.getDomainName(config);
2020-12-06 07:28:31 +00:00
switch (provider.type) {
2020-12-10 20:33:19 +00:00
case ProviderType.server:
2021-03-26 13:38:39 +00:00
throw ('wrong type');
2020-12-10 20:33:19 +00:00
case ProviderType.domain:
2021-03-15 15:39:44 +00:00
title = 'providers.domain.card_title'.tr();
2021-03-18 00:55:38 +00:00
children = [
BrandText.body1('providers.domain.bottom_sheet.1'.tr()),
SizedBox(height: 10),
2021-10-05 20:43:12 +00:00
BrandText.body1(domainName),
2021-03-18 00:55:38 +00:00
SizedBox(height: 10),
2021-03-26 13:38:39 +00:00
BrandText.body1('providers.domain.status'.tr()),
2021-03-18 00:55:38 +00:00
];
2020-12-03 16:52:53 +00:00
break;
2020-12-10 20:33:19 +00:00
case ProviderType.backup:
2021-03-15 15:39:44 +00:00
title = 'providers.backup.card_title'.tr();
2021-03-18 00:55:38 +00:00
children = [
BrandText.body1('providers.backup.bottom_sheet.1'.tr()),
SizedBox(height: 10),
BrandText.body1(
'providers.backup.bottom_sheet.2'.tr(args: [domainName, 'Time'])),
SizedBox(height: 10),
2021-03-26 13:53:21 +00:00
BrandText.body1('providers.backup.status'.tr()),
2021-03-18 00:55:38 +00:00
];
2020-12-03 16:52:53 +00:00
break;
}
2021-06-20 21:08:52 +00:00
return BrandBottomSheet(
child: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 40),
Padding(
padding: paddingH15V0,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
IconStatusMask(
status: provider.state,
child: Icon(provider.icon, size: 40, color: Colors.white),
2020-12-10 20:33:19 +00:00
),
2021-06-20 21:08:52 +00:00
SizedBox(height: 10),
BrandText.h1(title),
SizedBox(height: 10),
...children,
SizedBox(height: 30),
],
),
)
],
),
2020-12-03 16:52:53 +00:00
),
);
}
}