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

235 lines
8.3 KiB
Dart
Raw Normal View History

2021-09-02 22:32:07 +03:00
import 'dart:ui';
2020-12-01 21:08:19 +02:00
import 'package:flutter/material.dart';
import 'package:selfprivacy/config/brand_theme.dart';
2021-08-18 13:44:46 +03:00
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
2022-08-30 06:09:09 +03:00
import 'package:selfprivacy/logic/cubit/client_jobs/client_jobs_cubit.dart';
2021-08-29 18:02:51 +03:00
import 'package:selfprivacy/logic/cubit/services/services_cubit.dart';
import 'package:selfprivacy/logic/models/job.dart';
2021-03-18 02:55:38 +02:00
import 'package:selfprivacy/logic/models/state_types.dart';
2021-05-26 00:53:54 +03:00
import 'package:selfprivacy/ui/components/brand_cards/brand_cards.dart';
2020-12-03 18:52:53 +02:00
import 'package:selfprivacy/ui/components/brand_header/brand_header.dart';
2021-08-29 18:02:51 +03:00
import 'package:selfprivacy/ui/components/brand_switch/brand_switch.dart';
2020-12-08 21:26:51 +02:00
import 'package:selfprivacy/ui/components/brand_text/brand_text.dart';
2020-12-01 21:08:19 +02:00
import 'package:selfprivacy/ui/components/icon_status_mask/icon_status_mask.dart';
2021-01-06 19:35:57 +02:00
import 'package:selfprivacy/ui/components/not_ready_card/not_ready_card.dart';
2021-03-18 02:55:38 +02:00
import 'package:easy_localization/easy_localization.dart';
import 'package:selfprivacy/ui/pages/services/service_page.dart';
import 'package:selfprivacy/utils/route_transitions/basic.dart';
2021-03-26 15:38:39 +02:00
import 'package:selfprivacy/utils/ui_helpers.dart';
import 'package:url_launcher/url_launcher.dart';
2020-12-01 21:08:19 +02:00
2021-09-02 22:32:07 +03:00
const switchableServices = [
ServiceTypes.bitwarden,
ServiceTypes.nextcloud,
ServiceTypes.pleroma,
ServiceTypes.gitea,
ServiceTypes.ocserv,
2021-09-02 22:32:07 +03:00
];
2020-12-01 21:08:19 +02:00
class ServicesPage extends StatefulWidget {
const ServicesPage({final super.key});
2020-12-01 21:08:19 +02:00
@override
State<ServicesPage> createState() => _ServicesPageState();
2020-12-01 21:08:19 +02:00
}
void _launchURL(final url) async {
try {
final Uri uri = Uri.parse(url);
await launchUrl(
uri,
mode: LaunchMode.externalApplication,
);
} catch (e) {
print(e);
2021-12-23 15:52:12 +02:00
}
}
2020-12-01 21:08:19 +02:00
class _ServicesPageState extends State<ServicesPage> {
@override
Widget build(final BuildContext context) {
final isReady = context.watch<ServerInstallationCubit>().state
is ServerInstallationFinished;
2021-01-06 19:35:57 +02:00
2020-12-01 21:08:19 +02:00
return Scaffold(
2020-12-03 18:52:53 +02:00
appBar: PreferredSize(
2022-05-24 21:55:39 +03:00
preferredSize: const Size.fromHeight(52),
2021-05-26 00:53:54 +03:00
child: BrandHeader(
title: 'basis.services'.tr(),
),
2020-12-03 18:52:53 +02:00
),
body: RefreshIndicator(
onRefresh: () async {
context.read<ServicesCubit>().reload();
},
child: ListView(
padding: paddingH15V0,
children: [
BrandText.body1('basis.services_title'.tr()),
const SizedBox(height: 24),
if (!isReady) ...[const NotReadyCard(), const SizedBox(height: 24)],
...ServiceTypes.values
.map(
(final t) => Padding(
padding: const EdgeInsets.only(
bottom: 30,
),
child: _Card(serviceType: t),
),
)
.toList()
],
),
2020-12-01 21:08:19 +02:00
),
);
}
}
class _Card extends StatelessWidget {
const _Card({required this.serviceType});
2020-12-01 21:08:19 +02:00
2021-03-18 02:55:38 +02:00
final ServiceTypes serviceType;
2020-12-01 21:08:19 +02:00
@override
Widget build(final BuildContext context) {
final isReady = context.watch<ServerInstallationCubit>().state
is ServerInstallationFinished;
2021-08-29 18:02:51 +03:00
final serviceState = context.watch<ServicesCubit>().state;
final jobsCubit = context.watch<JobsCubit>();
final jobState = jobsCubit.state;
2021-09-02 22:32:07 +03:00
final switchableService = switchableServices.contains(serviceType);
final hasSwitchJob = switchableService &&
2021-09-02 22:32:07 +03:00
jobState is JobsStateWithJobs &&
jobState.clientJobList.any(
(final el) => el is ServiceToggleJob && el.type == serviceType,
);
2021-09-02 22:32:07 +03:00
final isSwitchOn = isReady &&
2021-09-02 22:32:07 +03:00
(!switchableServices.contains(serviceType) ||
serviceState.isEnableByType(serviceType));
2021-08-29 18:02:51 +03:00
final config = context.watch<ServerInstallationCubit>().state;
final domainName = UiHelpers.getDomainName(config);
2021-12-23 15:52:12 +02:00
2021-03-18 02:55:38 +02:00
return GestureDetector(
2022-09-16 17:14:29 +03:00
onTap: isReady
? () => Navigator.of(context)
.push(materialRoute(ServicePage(serviceId: serviceType.name)))
: null,
2021-05-26 00:53:54 +03:00
child: BrandCards.big(
2021-03-18 02:55:38 +02:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2021-08-29 18:02:51 +03:00
Row(
children: [
IconStatusMask(
2021-09-02 22:32:07 +03:00
status:
2022-02-16 09:28:29 +02:00
isSwitchOn ? StateType.stable : StateType.uninitialized,
2021-08-29 18:02:51 +03:00
child: Icon(serviceType.icon, size: 30, color: Colors.white),
),
2022-02-16 09:28:29 +02:00
if (isReady && switchableService) ...[
2022-05-24 21:55:39 +03:00
const Spacer(),
2021-08-29 18:02:51 +03:00
Builder(
builder: (final context) {
2021-08-29 18:02:51 +03:00
late bool isActive;
2021-09-02 22:32:07 +03:00
if (hasSwitchJob) {
isActive = (jobState.clientJobList.firstWhere(
(final el) =>
el is ServiceToggleJob && el.type == serviceType,
) as ServiceToggleJob)
2021-08-29 18:02:51 +03:00
.needToTurnOn;
} else {
isActive = serviceState.isEnableByType(serviceType);
}
return BrandSwitch(
value: isActive,
onChanged: (final value) =>
2021-09-02 22:32:07 +03:00
jobsCubit.createOrRemoveServiceToggleJob(
2021-08-29 18:02:51 +03:00
ServiceToggleJob(
type: serviceType,
needToTurnOn: value,
),
),
);
},
),
]
],
2021-03-18 02:55:38 +02:00
),
2021-09-02 22:32:07 +03:00
ClipRect(
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2022-05-24 21:55:39 +03:00
const SizedBox(height: 10),
2021-09-02 22:32:07 +03:00
BrandText.h2(serviceType.title),
2022-05-24 21:55:39 +03:00
const SizedBox(height: 10),
2021-12-23 15:52:12 +02:00
if (serviceType.subdomain != '')
Column(
children: [
GestureDetector(
onTap: () => _launchURL(
'https://${serviceType.subdomain}.$domainName',
),
2021-12-23 15:52:12 +02:00
child: Text(
'${serviceType.subdomain}.$domainName',
style: TextStyle(
color:
Theme.of(context).colorScheme.secondary,
decoration: TextDecoration.underline,
),
2021-12-23 15:52:12 +02:00
),
),
2022-05-24 21:55:39 +03:00
const SizedBox(height: 10),
2021-12-23 15:52:12 +02:00
],
),
if (serviceType == ServiceTypes.mailserver)
Column(
children: [
Text(
domainName,
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
decoration: TextDecoration.underline,
),
),
const SizedBox(height: 10),
],
),
2021-12-23 15:52:12 +02:00
BrandText.body2(serviceType.loginInfo),
2022-05-24 21:55:39 +03:00
const SizedBox(height: 10),
2021-09-02 22:32:07 +03:00
BrandText.body2(serviceType.subtitle),
2022-05-24 21:55:39 +03:00
const SizedBox(height: 10),
2021-09-02 22:32:07 +03:00
],
),
if (hasSwitchJob)
Positioned(
2021-09-29 21:28:47 +03:00
bottom: 24,
2021-09-02 22:32:07 +03:00
left: 0,
right: 0,
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: 3,
sigmaY: 2,
),
child: BrandText.h2(
'jobs.run_jobs'.tr(),
2021-09-02 22:32:07 +03:00
textAlign: TextAlign.center,
),
),
)
],
),
)
2020-12-01 21:08:19 +02:00
],
2021-03-18 02:55:38 +02:00
),
),
);
}
}