2023-02-24 16:45:32 +00:00
|
|
|
import 'package:auto_route/auto_route.dart';
|
2022-08-03 02:25:33 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2024-01-26 14:46:09 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-02-01 14:30:06 +00:00
|
|
|
import 'package:selfprivacy/logic/bloc/volumes/volumes_bloc.dart';
|
2022-08-24 23:45:02 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/provider_volumes/provider_volume_cubit.dart';
|
2022-08-24 05:35:49 +00:00
|
|
|
import 'package:selfprivacy/logic/models/disk_size.dart';
|
2024-01-31 05:14:23 +00:00
|
|
|
import 'package:selfprivacy/logic/models/disk_status.dart';
|
2022-10-12 01:42:45 +00:00
|
|
|
import 'package:selfprivacy/logic/models/price.dart';
|
2023-03-27 17:02:44 +00:00
|
|
|
import 'package:selfprivacy/ui/components/buttons/brand_button.dart';
|
2023-07-28 23:42:41 +00:00
|
|
|
import 'package:selfprivacy/ui/components/info_box/info_box.dart';
|
2023-10-07 21:49:55 +00:00
|
|
|
import 'package:selfprivacy/ui/helpers/modals.dart';
|
2023-02-23 14:49:14 +00:00
|
|
|
import 'package:selfprivacy/ui/layouts/brand_hero_screen.dart';
|
2022-08-03 02:25:33 +00:00
|
|
|
|
2023-03-22 11:38:18 +00:00
|
|
|
@RoutePage()
|
2022-08-03 02:25:33 +00:00
|
|
|
class ExtendingVolumePage extends StatefulWidget {
|
2022-08-24 05:35:49 +00:00
|
|
|
const ExtendingVolumePage({
|
|
|
|
required this.diskVolumeToResize,
|
|
|
|
required this.diskStatus,
|
2022-10-26 16:26:09 +00:00
|
|
|
super.key,
|
2022-08-24 05:35:49 +00:00
|
|
|
});
|
2022-08-03 02:25:33 +00:00
|
|
|
|
2022-08-24 05:35:49 +00:00
|
|
|
final DiskVolume diskVolumeToResize;
|
|
|
|
final DiskStatus diskStatus;
|
2022-08-03 02:25:33 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<ExtendingVolumePage> createState() => _ExtendingVolumePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ExtendingVolumePageState extends State<ExtendingVolumePage> {
|
2022-09-06 10:27:18 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
2022-09-18 22:11:26 +00:00
|
|
|
minSize = widget.diskVolumeToResize.sizeTotal + DiskSize.fromGibibyte(3);
|
2022-09-06 10:27:18 +00:00
|
|
|
_currentSliderGbValue = minSize.gibibyte;
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2022-10-06 14:46:29 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_sizeController.dispose();
|
|
|
|
_priceController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:35:49 +00:00
|
|
|
bool _isError = false;
|
2022-08-03 02:25:33 +00:00
|
|
|
|
2022-09-06 10:27:18 +00:00
|
|
|
late double _currentSliderGbValue;
|
2023-06-22 19:02:49 +00:00
|
|
|
double _pricePerGb = 1.0;
|
2022-08-03 02:25:33 +00:00
|
|
|
|
2022-09-06 10:27:18 +00:00
|
|
|
final DiskSize maxSize = const DiskSize(byte: 500000000000);
|
|
|
|
late DiskSize minSize;
|
2022-08-03 02:25:33 +00:00
|
|
|
|
|
|
|
final TextEditingController _sizeController = TextEditingController();
|
2022-08-24 05:35:49 +00:00
|
|
|
final TextEditingController _priceController = TextEditingController();
|
2022-08-03 02:25:33 +00:00
|
|
|
|
|
|
|
void _updateErrorStatuses() {
|
2022-09-06 09:17:44 +00:00
|
|
|
_isError = minSize.gibibyte > _currentSliderGbValue;
|
2022-08-03 02:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(final BuildContext context) => FutureBuilder(
|
2024-02-01 14:30:06 +00:00
|
|
|
future: context.read<ProviderVolumeCubit>().getPricePerGb(),
|
2022-08-03 02:25:33 +00:00
|
|
|
builder: (
|
|
|
|
final BuildContext context,
|
|
|
|
final AsyncSnapshot<void> snapshot,
|
|
|
|
) {
|
2022-08-24 05:35:49 +00:00
|
|
|
if (!snapshot.hasData) {
|
|
|
|
return BrandHeroScreen(
|
|
|
|
hasBackButton: true,
|
2022-10-03 23:32:35 +00:00
|
|
|
heroTitle: 'storage.extending_volume_title'.tr(),
|
|
|
|
heroSubtitle: 'storage.extending_volume_description'.tr(),
|
2022-08-24 05:35:49 +00:00
|
|
|
children: const [
|
|
|
|
SizedBox(height: 16),
|
2022-09-06 10:27:18 +00:00
|
|
|
Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
),
|
2022-08-24 05:35:49 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2023-06-22 19:02:49 +00:00
|
|
|
final price = snapshot.data as Price;
|
|
|
|
_pricePerGb = price.value;
|
2023-09-16 04:41:02 +00:00
|
|
|
final currentSizeValue = _currentSliderGbValue.truncate().toString();
|
|
|
|
_sizeController.text = 'storage.gb'.tr(args: [currentSizeValue]);
|
2022-08-03 02:25:33 +00:00
|
|
|
_priceController.text =
|
2023-09-16 04:41:02 +00:00
|
|
|
'${(_pricePerGb * double.parse(currentSizeValue)).toStringAsFixed(2)}'
|
|
|
|
' '
|
|
|
|
'${price.currency.shortcode}';
|
2022-09-06 11:03:21 +00:00
|
|
|
minSize =
|
2022-09-18 22:11:26 +00:00
|
|
|
widget.diskVolumeToResize.sizeTotal + DiskSize.fromGibibyte(3);
|
2022-08-24 05:35:49 +00:00
|
|
|
if (_currentSliderGbValue < 0) {
|
2022-09-06 09:17:44 +00:00
|
|
|
_currentSliderGbValue = minSize.gibibyte;
|
2022-08-24 05:35:49 +00:00
|
|
|
}
|
2022-08-03 02:25:33 +00:00
|
|
|
|
2022-09-18 22:11:26 +00:00
|
|
|
final isAlreadyResizing =
|
2024-02-01 14:30:06 +00:00
|
|
|
context.watch<ProviderVolumeCubit>().state.isResizing;
|
2022-09-18 22:11:26 +00:00
|
|
|
|
2022-08-03 02:25:33 +00:00
|
|
|
return BrandHeroScreen(
|
|
|
|
hasBackButton: true,
|
2022-10-03 23:32:35 +00:00
|
|
|
heroTitle: 'storage.extending_volume_title'.tr(),
|
|
|
|
heroSubtitle: 'storage.extending_volume_description'.tr(),
|
2022-08-03 02:25:33 +00:00
|
|
|
children: [
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
Row(
|
2022-08-24 05:35:49 +00:00
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
mainAxisSize: MainAxisSize.max,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
2022-08-03 02:25:33 +00:00
|
|
|
children: [
|
2022-08-24 05:35:49 +00:00
|
|
|
ConstrainedBox(
|
|
|
|
constraints: const BoxConstraints(maxWidth: 130),
|
|
|
|
child: TextField(
|
|
|
|
readOnly: true,
|
|
|
|
textAlign: TextAlign.start,
|
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
enabled: true,
|
|
|
|
controller: _sizeController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
errorText: _isError ? ' ' : null,
|
2022-10-03 23:32:35 +00:00
|
|
|
labelText: 'storage.size'.tr(),
|
2022-08-24 05:35:49 +00:00
|
|
|
),
|
2022-08-03 02:25:33 +00:00
|
|
|
),
|
|
|
|
),
|
2022-08-24 05:35:49 +00:00
|
|
|
const SizedBox(width: 16),
|
|
|
|
ConstrainedBox(
|
|
|
|
constraints: const BoxConstraints(maxWidth: 130),
|
|
|
|
child: TextField(
|
|
|
|
readOnly: true,
|
|
|
|
textAlign: TextAlign.start,
|
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
enabled: true,
|
|
|
|
controller: _priceController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
errorText: _isError ? ' ' : null,
|
2023-09-16 04:41:02 +00:00
|
|
|
labelText: 'storage.price'.tr(),
|
2022-08-24 05:35:49 +00:00
|
|
|
),
|
2022-08-03 02:25:33 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
Slider(
|
2022-09-06 09:17:44 +00:00
|
|
|
min: minSize.gibibyte,
|
2022-08-24 05:35:49 +00:00
|
|
|
value: _currentSliderGbValue,
|
2022-09-06 09:17:44 +00:00
|
|
|
max: maxSize.gibibyte,
|
2022-08-03 02:25:33 +00:00
|
|
|
onChanged: (final double value) {
|
|
|
|
setState(() {
|
|
|
|
_currentSliderGbValue = value;
|
|
|
|
_updateErrorStatuses();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
2023-02-05 13:24:37 +00:00
|
|
|
BrandButton.filled(
|
|
|
|
onPressed: _isError || isAlreadyResizing
|
2022-09-18 16:26:55 +00:00
|
|
|
? null
|
2022-09-18 16:31:22 +00:00
|
|
|
: () {
|
2023-10-07 21:49:55 +00:00
|
|
|
showPopUpAlert(
|
|
|
|
alertTitle: 'storage.extending_volume_title'.tr(),
|
|
|
|
description:
|
|
|
|
'storage.extending_volume_modal_description'.tr(
|
|
|
|
args: [_sizeController.text, _priceController.text],
|
|
|
|
),
|
|
|
|
actionButtonTitle: 'basis.continue'.tr(),
|
|
|
|
actionButtonOnPressed: () {
|
2024-02-01 14:30:06 +00:00
|
|
|
context.read<ProviderVolumeCubit>().resizeVolume(
|
2023-10-07 21:49:55 +00:00
|
|
|
widget.diskVolumeToResize,
|
|
|
|
DiskSize.fromGibibyte(
|
|
|
|
_currentSliderGbValue.truncate().toDouble(),
|
|
|
|
),
|
2024-02-01 14:30:06 +00:00
|
|
|
context.read<VolumesBloc>().invalidateCache,
|
2023-10-07 21:49:55 +00:00
|
|
|
);
|
|
|
|
context.router.popUntilRoot();
|
|
|
|
},
|
|
|
|
);
|
2022-09-18 16:31:22 +00:00
|
|
|
},
|
2023-02-05 13:24:37 +00:00
|
|
|
child: Text('storage.extend_volume_button.title'.tr()),
|
2022-08-03 02:25:33 +00:00
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
const Divider(
|
|
|
|
height: 1.0,
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
2023-07-28 23:42:41 +00:00
|
|
|
InfoBox(
|
|
|
|
text: 'storage.extending_volume_price_info'.tr(),
|
|
|
|
isWarning: false,
|
2022-08-24 05:35:49 +00:00
|
|
|
),
|
2022-08-03 02:25:33 +00:00
|
|
|
const SizedBox(height: 16),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|