selfprivacy.org.app/lib/ui/pages/server_storage/data_migration.dart

166 lines
5.5 KiB
Dart
Raw Normal View History

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:selfprivacy/logic/models/disk_size.dart';
2022-09-14 16:46:38 +00:00
import 'package:selfprivacy/logic/models/service.dart';
import 'package:selfprivacy/ui/components/brand_button/filled_button.dart';
import 'package:selfprivacy/ui/components/brand_header/brand_header.dart';
import 'package:selfprivacy/ui/components/info_box/info_box.dart';
import 'package:selfprivacy/ui/pages/server_storage/disk_status.dart';
import 'package:selfprivacy/ui/pages/server_storage/server_storage_list_item.dart';
2022-09-14 16:46:38 +00:00
import 'package:selfprivacy/ui/pages/server_storage/service_migration_list_item.dart';
class DataMigrationPage extends StatefulWidget {
const DataMigrationPage({
2022-09-14 16:46:38 +00:00
required this.services,
required this.diskStatus,
final super.key,
});
final DiskStatus diskStatus;
2022-09-14 16:46:38 +00:00
final List<Service> services;
@override
State<DataMigrationPage> createState() => _DataMigrationPageState();
}
class _DataMigrationPageState extends State<DataMigrationPage> {
2022-09-14 16:46:38 +00:00
/// Service id to target migration disk name
final Map<String, String> serviceToDisk = {};
static const headerHeight = 52.0;
static const headerVerticalPadding = 8.0;
static const listItemHeight = 62.0;
@override
void initState() {
super.initState();
for (final Service service in widget.services) {
if (service.storageUsage.volume != null) {
serviceToDisk[service.id] = service.storageUsage.volume!;
}
}
}
void onChange(final String volumeName, final String serviceId) {
setState(() {
serviceToDisk[serviceId] = volumeName;
});
}
/// Check the services and if a service is moved (in a serviceToDisk entry)
/// subtract the used storage from the old volume and add it to the new volume.
/// The old volume is the volume the service is currently on, shown in services list.
2022-09-15 21:08:32 +00:00
DiskVolume recalculatedDiskUsages(
final DiskVolume volume, final List<Service> services) {
2022-09-14 16:46:38 +00:00
DiskSize used = volume.sizeUsed;
for (final Service service in services) {
if (service.storageUsage.volume != null) {
if (service.storageUsage.volume == volume.name) {
2022-09-15 21:08:32 +00:00
if (serviceToDisk[service.id] != null &&
serviceToDisk[service.id] != volume.name) {
2022-09-14 16:46:38 +00:00
used -= service.storageUsage.used;
}
} else {
2022-09-15 21:08:32 +00:00
if (serviceToDisk[service.id] != null &&
serviceToDisk[service.id] == volume.name) {
2022-09-14 16:46:38 +00:00
used += service.storageUsage.used;
}
}
}
}
return volume.copyWith(sizeUsed: used);
}
@override
2022-09-14 16:46:38 +00:00
Widget build(final BuildContext context) {
final Size appBarHeight = Size.fromHeight(
headerHeight +
headerVerticalPadding * 2 +
listItemHeight * widget.diskStatus.diskVolumes.length +
headerVerticalPadding * widget.diskStatus.diskVolumes.length,
);
return SafeArea(
child: Scaffold(
appBar: PreferredSize(
preferredSize: appBarHeight,
child: Column(
children: [
BrandHeader(
title: 'providers.storage.data_migration_title'.tr(),
hasBackButton: true,
),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: headerVerticalPadding,
),
child: Column(
2022-08-29 18:18:07 +00:00
children: [
2022-09-14 16:46:38 +00:00
...widget.diskStatus.diskVolumes
.map(
(final volume) => Column(
children: [
ServerStorageListItem(
2022-09-15 21:08:32 +00:00
volume: recalculatedDiskUsages(
volume, widget.services),
2022-09-14 16:46:38 +00:00
dense: true,
),
const SizedBox(height: headerVerticalPadding),
],
),
)
.toList(),
2022-08-29 18:18:07 +00:00
],
),
2022-09-14 16:46:38 +00:00
),
const Divider(height: 0),
],
),
),
body: ListView(
padding: const EdgeInsets.all(16.0),
children: <Widget>[
2022-09-15 21:08:32 +00:00
if (widget.services.isEmpty)
const Center(child: CircularProgressIndicator()),
2022-09-14 16:46:38 +00:00
...widget.services
.map(
(final service) => Column(
children: [
const SizedBox(height: 8),
ServiceMigrationListItem(
service: service,
diskStatus: widget.diskStatus,
selectedVolume: serviceToDisk[service.id]!,
onChange: onChange,
),
const SizedBox(height: 4),
const Divider(),
],
),
)
.toList(),
Padding(
padding: const EdgeInsets.all(8.0),
2022-09-15 21:08:32 +00:00
child: InfoBox(
text: 'providers.storage.data_migration_notice'.tr(),
isWarning: true,
),
2022-09-14 16:46:38 +00:00
),
const SizedBox(height: 16),
FilledButton(
title: 'providers.storage.start_migration_button'.tr(),
onPressed: () {
// TODO: Implement migration
},
),
const SizedBox(height: 32),
],
),
),
);
}
}