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

244 lines
8.3 KiB
Dart
Raw Normal View History

import 'package:easy_localization/easy_localization.dart';
2020-12-03 16:52:53 +00:00
import 'package:flutter/material.dart';
import 'package:selfprivacy/config/brand_theme.dart';
2021-12-06 18:31:19 +00:00
import 'package:selfprivacy/logic/cubit/backups/backups_cubit.dart';
2022-02-16 07:09:53 +00:00
import 'package:selfprivacy/logic/cubit/dns_records/dns_records_cubit.dart';
2020-12-10 20:33:19 +00:00
import 'package:selfprivacy/logic/cubit/providers/providers_cubit.dart';
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
2022-08-24 23:45:02 +00:00
import 'package:selfprivacy/logic/cubit/provider_volumes/provider_volume_cubit.dart';
import 'package:selfprivacy/logic/cubit/server_volumes/server_volume_cubit.dart';
import 'package:selfprivacy/logic/models/disk_size.dart';
import 'package:selfprivacy/logic/models/hive/server_details.dart';
import 'package:selfprivacy/logic/models/json/server_disk_volume.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';
2022-02-16 07:09:53 +00:00
import 'package:selfprivacy/ui/pages/dns_details/dns_details.dart';
import 'package:selfprivacy/ui/pages/providers/storage_card.dart';
import 'package:selfprivacy/ui/pages/server_details/server_details_screen.dart';
import 'package:selfprivacy/ui/pages/server_storage/disk_status.dart';
import 'package:selfprivacy/utils/route_transitions/basic.dart';
2021-03-26 13:38:39 +00:00
2022-06-05 19:36:32 +00:00
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
2020-12-03 16:52:53 +00:00
class ProvidersPage extends StatefulWidget {
const ProvidersPage({final super.key});
2020-12-03 16:52:53 +00:00
@override
State<ProvidersPage> createState() => _ProvidersPageState();
2020-12-03 16:52:53 +00:00
}
class _ProvidersPageState extends State<ProvidersPage> {
@override
2022-06-05 19:36:32 +00:00
Widget build(final BuildContext context) {
final bool isReady = context.watch<ServerInstallationCubit>().state
is ServerInstallationFinished;
final bool isBackupInitialized =
context.watch<BackupsCubit>().state.isInitialized;
final DnsRecordsStatus dnsStatus =
context.watch<DnsRecordsCubit>().state.dnsState;
2022-02-16 07:09:53 +00:00
StateType getDnsStatus() {
if (dnsStatus == DnsRecordsStatus.uninitialized ||
dnsStatus == DnsRecordsStatus.refreshing) {
return StateType.uninitialized;
}
if (dnsStatus == DnsRecordsStatus.error) {
return StateType.warning;
}
return StateType.stable;
}
2021-01-06 17:35:57 +00:00
2022-06-05 19:36:32 +00:00
final List<Padding> cards = ProviderType.values
2021-03-25 08:54:39 +00:00
.map(
2022-06-05 19:36:32 +00:00
(final ProviderType type) => Padding(
2022-05-24 18:55:39 +00:00
padding: const EdgeInsets.only(bottom: 30),
2021-03-25 08:54:39 +00:00
child: _Card(
provider: ProviderModel(
2021-12-06 18:31:19 +00:00
state: isReady
? (type == ProviderType.backup && !isBackupInitialized
? StateType.uninitialized
2022-02-16 07:09:53 +00:00
: (type == ProviderType.domain)
? getDnsStatus()
: StateType.stable)
2021-12-06 18:31:19 +00:00
: StateType.uninitialized,
2021-03-25 08:54:39 +00:00
type: type,
),
),
),
)
2020-12-06 07:28:31 +00:00
.toList();
cards.add(
Padding(
padding: const EdgeInsets.only(bottom: 30),
2022-08-24 23:45:02 +00:00
child: StorageCard(
diskStatus: toDiskStatus(
context.read<ApiServerVolumeCubit>().state.volumes,
context.read<ApiProviderVolumeCubit>().state.volumes,
),
),
),
);
2020-12-03 16:52:53 +00:00
return Scaffold(
appBar: PreferredSize(
2022-05-24 18:55:39 +00:00
preferredSize: const Size.fromHeight(52),
2021-05-25 21:53:54 +00:00
child: BrandHeader(
title: 'providers.page_title'.tr(),
),
2020-12-03 16:52:53 +00:00
),
body: ListView(
2021-05-25 21:53:54 +00:00
padding: paddingH15V0,
2021-01-06 17:35:57 +00:00
children: [
if (!isReady) ...[
2022-05-24 18:55:39 +00:00
const NotReadyCard(),
const SizedBox(height: 24),
2021-01-06 17:35:57 +00:00
],
...cards,
],
2020-12-03 16:52:53 +00:00
),
);
}
DiskStatus toDiskStatus(
final List<ServerDiskVolume> serverVolumes,
final List<ServerVolume> providerVolumes,
) {
final DiskStatus diskStatus = DiskStatus();
diskStatus.isDiskOkay = true;
if (providerVolumes.isEmpty || serverVolumes.isEmpty) {
diskStatus.isDiskOkay = false;
}
diskStatus.diskVolumes = serverVolumes.map((
final ServerDiskVolume volume,
) {
final DiskVolume diskVolume = DiskVolume();
diskVolume.sizeUsed = DiskSize(
byte: volume.usedSpace == 'None' ? 0 : int.parse(volume.usedSpace),
);
diskVolume.sizeTotal = DiskSize(
byte: volume.totalSpace == 'None' ? 0 : int.parse(volume.totalSpace),
);
diskVolume.serverDiskVolume = volume;
for (final ServerVolume providerVolume in providerVolumes) {
if (providerVolume.linuxDevice == null ||
volume.model == null ||
volume.serial == null) {
continue;
}
final String deviceId = providerVolume.linuxDevice!.split('/').last;
if (deviceId.contains(volume.model!) &&
deviceId.contains(volume.serial!)) {
diskVolume.providerVolume = providerVolume;
break;
}
}
diskVolume.name = volume.name;
diskVolume.root = volume.root;
diskVolume.percentage =
volume.usedSpace != 'None' && volume.totalSpace != 'None'
? 1.0 / diskVolume.sizeTotal.byte * diskVolume.sizeUsed.byte
: 0.0;
if (diskVolume.percentage >= 0.8 ||
diskVolume.sizeTotal.asGb() - diskVolume.sizeUsed.asGb() <= 2.0) {
diskStatus.isDiskOkay = false;
}
return diskVolume;
}).toList();
return diskStatus;
}
2020-12-03 16:52:53 +00:00
}
class _Card extends StatelessWidget {
const _Card({required this.provider});
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
2022-06-05 19:36:32 +00:00
Widget build(final 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;
2022-06-05 19:36:32 +00:00
final bool isReady = context.watch<ServerInstallationCubit>().state
is ServerInstallationFinished;
2022-06-05 19:36:32 +00:00
final ServerInstallationState appConfig =
context.watch<ServerInstallationCubit>().state;
2021-01-27 18:33:00 +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,
2022-06-05 19:36:32 +00:00
builder: (final BuildContext context) => const BrandBottomSheet(
2021-06-20 21:08:52 +00:00
isExpended: true,
child: ServerDetailsScreen(),
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:
2022-02-16 07:09:53 +00:00
title = 'providers.domain.screen_title'.tr();
message = appConfig.isDomainSelected
? appConfig.serverDomain!.domainName
: '';
2021-03-26 13:38:39 +00:00
stableText = 'providers.domain.status'.tr();
onTap = () => Navigator.of(context).push(
materialRoute(
const DnsDetailsPage(),
),
);
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();
onTap = () => Navigator.of(context).push(
materialRoute(
const BackupDetails(),
),
);
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),
),
const SizedBox(height: 16),
Text(
title,
style: Theme.of(context).textTheme.titleLarge,
),
2020-12-06 07:28:31 +00:00
if (message != null) ...[
Text(
message,
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 16),
2020-12-06 07:28:31 +00:00
],
2020-12-10 20:33:19 +00:00
if (provider.state == StateType.stable) BrandText.body2(stableText),
2020-12-06 07:28:31 +00:00
],
),
),
);
}
}