mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-23 09:16:54 +00:00
feat(pricing): Replace raw double with simple type Price
This commit is contained in:
parent
70330c59ab
commit
79e9334aca
|
@ -9,12 +9,15 @@ import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
|||
import 'package:selfprivacy/logic/models/json/hetzner_server_info.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||
import 'package:selfprivacy/logic/models/price.dart';
|
||||
import 'package:selfprivacy/logic/models/server_basic_info.dart';
|
||||
import 'package:selfprivacy/utils/password_generator.dart';
|
||||
|
||||
class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi {
|
||||
DigitalOceanApi(
|
||||
{final this.hasLogger = false, final this.isWithToken = true});
|
||||
DigitalOceanApi({
|
||||
this.hasLogger = false,
|
||||
this.isWithToken = true,
|
||||
});
|
||||
@override
|
||||
bool hasLogger;
|
||||
@override
|
||||
|
@ -46,7 +49,7 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi {
|
|||
final Dio client = await getClient();
|
||||
try {
|
||||
response = await client.get(
|
||||
'/servers',
|
||||
'/account',
|
||||
options: Options(
|
||||
headers: {'Authorization': 'Bearer $token'},
|
||||
),
|
||||
|
@ -71,30 +74,13 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi {
|
|||
return isValid;
|
||||
}
|
||||
|
||||
/// Hardcoded on their documentation and there is no pricing API at all
|
||||
/// Probably we should scrap the doc page manually
|
||||
@override
|
||||
RegExp getApiTokenValidation() =>
|
||||
RegExp(r'\s+|[-!$%^&*()@+|~=`{}\[\]:<>?,.\/]');
|
||||
|
||||
@override
|
||||
Future<double?> getPricePerGb() async {
|
||||
double? price;
|
||||
|
||||
final Response dbGetResponse;
|
||||
final Dio client = await getClient();
|
||||
try {
|
||||
dbGetResponse = await client.get('/pricing');
|
||||
|
||||
final volume = dbGetResponse.data['pricing']['volume'];
|
||||
final volumePrice = volume['price_per_gb_month']['gross'];
|
||||
price = double.parse(volumePrice);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
|
||||
return price;
|
||||
}
|
||||
Future<Price?> getPricePerGb() async => Price(
|
||||
value: 0.10,
|
||||
currency: 'USD',
|
||||
);
|
||||
|
||||
@override
|
||||
Future<ServerVolume?> createVolume() async {
|
||||
|
|
|
@ -9,6 +9,7 @@ import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
|||
import 'package:selfprivacy/logic/models/json/hetzner_server_info.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||
import 'package:selfprivacy/logic/models/price.dart';
|
||||
import 'package:selfprivacy/logic/models/server_basic_info.dart';
|
||||
import 'package:selfprivacy/utils/password_generator.dart';
|
||||
|
||||
|
@ -75,7 +76,7 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
|||
RegExp(r'\s+|[-!$%^&*()@+|~=`{}\[\]:<>?,.\/]');
|
||||
|
||||
@override
|
||||
Future<double?> getPricePerGb() async {
|
||||
Future<Price?> getPricePerGb() async {
|
||||
double? price;
|
||||
|
||||
final Response dbGetResponse;
|
||||
|
@ -92,7 +93,12 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi {
|
|||
client.close();
|
||||
}
|
||||
|
||||
return price;
|
||||
return price == null
|
||||
? null
|
||||
: Price(
|
||||
value: price,
|
||||
currency: 'EUR',
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
@ -22,5 +22,7 @@ abstract class ServerProviderApi extends ApiMap {
|
|||
});
|
||||
|
||||
Future<bool> isApiTokenValid(final String token);
|
||||
RegExp getApiTokenValidation();
|
||||
RegExp getApiTokenValidation() => RegExp(
|
||||
r'\s+|[-!$%^&*()@+|~=`{}\[\]:<>?,.\/]',
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:selfprivacy/logic/api_maps/rest_maps/api_map.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||
import 'package:selfprivacy/logic/models/price.dart';
|
||||
|
||||
mixin VolumeProviderApi on ApiMap {
|
||||
Future<ServerVolume?> createVolume();
|
||||
|
@ -9,5 +10,5 @@ mixin VolumeProviderApi on ApiMap {
|
|||
Future<bool> detachVolume(final int volumeId);
|
||||
Future<bool> resizeVolume(final int volumeId, final int sizeGb);
|
||||
Future<void> deleteVolume(final int id);
|
||||
Future<double?> getPricePerGb();
|
||||
Future<Price?> getPricePerGb();
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
|||
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||
import 'package:selfprivacy/logic/models/disk_status.dart';
|
||||
import 'package:selfprivacy/logic/models/price.dart';
|
||||
|
||||
part 'provider_volume_state.dart';
|
||||
|
||||
|
@ -32,7 +33,7 @@ class ApiProviderVolumeCubit
|
|||
}
|
||||
}
|
||||
|
||||
Future<double?> getPricePerGb() async =>
|
||||
Future<Price?> getPricePerGb() async =>
|
||||
providerApi!.getVolumeProvider().getPricePerGb();
|
||||
|
||||
Future<void> refresh() async {
|
||||
|
|
9
lib/logic/models/price.dart
Normal file
9
lib/logic/models/price.dart
Normal file
|
@ -0,0 +1,9 @@
|
|||
class Price {
|
||||
Price({
|
||||
required this.value,
|
||||
required this.currency,
|
||||
});
|
||||
|
||||
double value;
|
||||
String currency;
|
||||
}
|
|
@ -4,6 +4,7 @@ import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_depe
|
|||
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/price.dart';
|
||||
import 'package:selfprivacy/ui/components/brand_button/filled_button.dart';
|
||||
import 'package:selfprivacy/ui/components/brand_hero_screen/brand_hero_screen.dart';
|
||||
import 'package:selfprivacy/logic/models/disk_status.dart';
|
||||
|
@ -67,7 +68,7 @@ class _ExtendingVolumePageState extends State<ExtendingVolumePage> {
|
|||
],
|
||||
);
|
||||
}
|
||||
_euroPerGb = snapshot.data as double;
|
||||
_euroPerGb = (snapshot.data as Price).value;
|
||||
_sizeController.text = _currentSliderGbValue.truncate().toString();
|
||||
_priceController.text =
|
||||
(_euroPerGb * double.parse(_sizeController.text))
|
||||
|
|
|
@ -180,7 +180,9 @@ class ProviderSelectionPage extends StatelessWidget {
|
|||
children: [
|
||||
InkWell(
|
||||
onTap: () {
|
||||
context.read<ServerInstallationCubit>().setServerProviderType(ServerProvider.hetzner);
|
||||
context
|
||||
.read<ServerInstallationCubit>()
|
||||
.setServerProviderType(ServerProvider.hetzner);
|
||||
callback(ServerProvider.hetzner);
|
||||
},
|
||||
child: Image.asset(
|
||||
|
@ -193,7 +195,9 @@ class ProviderSelectionPage extends StatelessWidget {
|
|||
),
|
||||
InkWell(
|
||||
onTap: () {
|
||||
context.read<ServerInstallationCubit>().setServerProviderType(ServerProvider.digitalOcean);
|
||||
context
|
||||
.read<ServerInstallationCubit>()
|
||||
.setServerProviderType(ServerProvider.digitalOcean);
|
||||
callback(ServerProvider.digitalOcean);
|
||||
},
|
||||
child: Image.asset(
|
||||
|
|
Loading…
Reference in a new issue