diff --git a/lib/config/hive_config.dart b/lib/config/hive_config.dart index 7d8e59aa..93bce6ee 100644 --- a/lib/config/hive_config.dart +++ b/lib/config/hive_config.dart @@ -87,6 +87,9 @@ class BNames { /// A String field of [serverInstallationBox] box. static String hetznerKey = 'hetznerKey'; + /// A String field of [serverInstallationBox] box. + static String serverProvider = 'serverProvider'; + /// A String field of [serverLocation] box. static String serverLocation = 'serverLocation'; diff --git a/lib/logic/api_maps/rest_maps/api_factory_creator.dart b/lib/logic/api_maps/rest_maps/api_factory_creator.dart index 0dbe1e90..25518f3c 100644 --- a/lib/logic/api_maps/rest_maps/api_factory_creator.dart +++ b/lib/logic/api_maps/rest_maps/api_factory_creator.dart @@ -18,9 +18,9 @@ class ApiFactoryCreator { ) { switch (settings.provider) { case ServerProvider.hetzner: - return HetznerApiFactory(); + return HetznerApiFactory(region: settings.location); case ServerProvider.digitalOcean: - return DigitalOceanApiFactory(); + return DigitalOceanApiFactory(region: settings.location); case ServerProvider.unknown: throw UnknownApiProviderException('Unknown server provider'); } diff --git a/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_factory.dart b/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_factory.dart index 6864c7ab..73a1e647 100644 --- a/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_factory.dart +++ b/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean_factory.dart @@ -6,13 +6,17 @@ import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/volume_pro class DigitalOceanApiFactory extends ServerProviderApiFactory with VolumeProviderApiFactory { + DigitalOceanApiFactory({this.region}); + + final String? region; + @override ServerProviderApi getServerProvider({ final ServerProviderApiSettings settings = const ServerProviderApiSettings(), }) => DigitalOceanApi( - region: settings.region, + region: settings.region ?? region, hasLogger: settings.hasLogger, isWithToken: settings.isWithToken, ); @@ -23,7 +27,7 @@ class DigitalOceanApiFactory extends ServerProviderApiFactory const ServerProviderApiSettings(), }) => DigitalOceanApi( - region: settings.region, + region: settings.region ?? region, hasLogger: settings.hasLogger, isWithToken: settings.isWithToken, ); diff --git a/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner.dart b/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner.dart index 7edb4cf1..c5360a2d 100644 --- a/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner.dart +++ b/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner.dart @@ -187,8 +187,9 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi { return volumes; } - @override - Future getVolume(final String volumeId) async { + Future getVolume( + final String volumeId, + ) async { ServerVolume? volume; final Response dbGetResponse; @@ -230,7 +231,9 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi { @override Future attachVolume( - final ServerVolume volume, final int serverId) async { + final ServerVolume volume, + final int serverId, + ) async { bool success = false; final Response dbPostResponse; @@ -576,14 +579,16 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi { '/locations', ); - locations = response.data!['locations'].map( - (final location) => ServerProviderLocation( - title: location['city'], - description: location['description'], - flag: getEmojiFlag(location['country']), - identifier: location['name'], - ), - ); + locations = response.data!['locations'] + .map( + (final location) => ServerProviderLocation( + title: location['city'], + description: location['description'], + flag: getEmojiFlag(location['country']), + identifier: location['name'], + ), + ) + .toList(); } catch (e) { print(e); } finally { @@ -600,36 +605,36 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi { final List types = []; final Dio client = await getClient(); - try { - final Response response = await client.get( - '/server_types', - ); - final rawTypes = response.data!['server_types']; - for (final rawType in rawTypes) { - for (final rawPrice in rawType['prices']) { - if (rawPrice['location'].toString() == location.identifier) { - types.add( - ServerType( - title: rawType['description'], - identifier: rawType['name'], - ram: rawType['memory'], - cores: rawType['cores'], - disk: DiskSize(byte: rawType['disk'] * 1024 * 1024 * 1024), - price: Price( - value: rawPrice['price_monthly']['gross'], - currency: 'EUR', - ), - location: location, + //try { + final Response response = await client.get( + '/server_types', + ); + final rawTypes = response.data!['server_types']; + for (final rawType in rawTypes) { + for (final rawPrice in rawType['prices']) { + if (rawPrice['location'].toString() == location.identifier) { + types.add( + ServerType( + title: rawType['description'], + identifier: rawType['name'], + ram: rawType['memory'], + cores: rawType['cores'], + disk: DiskSize(byte: rawType['disk'] * 1024 * 1024 * 1024), + price: Price( + value: double.parse(rawPrice['price_monthly']['gross']), + currency: 'EUR', ), - ); - } + location: location, + ), + ); } } - } catch (e) { - print(e); - } finally { - close(client); } + //} catch (e) { + //print(e); + //} finally { + close(client); + //} return types; } diff --git a/lib/logic/cubit/server_installation/server_installation_cubit.dart b/lib/logic/cubit/server_installation/server_installation_cubit.dart index 499ff49a..e298d7ad 100644 --- a/lib/logic/cubit/server_installation/server_installation_cubit.dart +++ b/lib/logic/cubit/server_installation/server_installation_cubit.dart @@ -55,7 +55,8 @@ class ServerInstallationCubit extends Cubit { } } - void setServerProviderType(final ServerProvider providerType) { + void setServerProviderType(final ServerProvider providerType) async { + await repository.saveServerProviderType(providerType); repository.serverProviderApiFactory = ApiFactoryCreator.createServerProviderApiFactory( ServerProviderApiFactorySettings( @@ -117,7 +118,6 @@ class ServerInstallationCubit extends Cubit { void setServerProviderKey(final String serverProviderKey) async { await repository.saveServerProviderKey(serverProviderKey); - if (state is ServerInstallationRecovery) { emit( (state as ServerInstallationRecovery).copyWith( @@ -141,7 +141,7 @@ class ServerInstallationCubit extends Cubit { repository.serverProviderApiFactory = ApiFactoryCreator.createServerProviderApiFactory( ServerProviderApiFactorySettings( - provider: getIt().serverDetails!.provider, + provider: getIt().serverProvider!, location: serverType.location.identifier, ), ); diff --git a/lib/logic/cubit/server_installation/server_installation_repository.dart b/lib/logic/cubit/server_installation/server_installation_repository.dart index 5b33ef28..f6120cbb 100644 --- a/lib/logic/cubit/server_installation/server_installation_repository.dart +++ b/lib/logic/cubit/server_installation/server_installation_repository.dart @@ -683,6 +683,10 @@ class ServerInstallationRepository { getIt().init(); } + Future saveServerProviderType(final ServerProvider type) async { + await getIt().storeServerProviderType(type); + } + Future saveServerProviderKey(final String key) async { await getIt().storeServerProviderKey(key); } diff --git a/lib/logic/get_it/api_config.dart b/lib/logic/get_it/api_config.dart index 302a37b1..4f64af8f 100644 --- a/lib/logic/get_it/api_config.dart +++ b/lib/logic/get_it/api_config.dart @@ -13,6 +13,7 @@ class ApiConfigModel { String? get serverLocation => _serverLocation; String? get serverType => _serverType; String? get cloudFlareKey => _cloudFlareKey; + ServerProvider? get serverProvider => _serverProvider; BackblazeCredential? get backblazeCredential => _backblazeCredential; ServerDomain? get serverDomain => _serverDomain; BackblazeBucket? get backblazeBucket => _backblazeBucket; @@ -21,11 +22,17 @@ class ApiConfigModel { String? _serverLocation; String? _cloudFlareKey; String? _serverType; + ServerProvider? _serverProvider; ServerHostingDetails? _serverDetails; BackblazeCredential? _backblazeCredential; ServerDomain? _serverDomain; BackblazeBucket? _backblazeBucket; + Future storeServerProviderType(final ServerProvider value) async { + await _box.put(BNames.serverProvider, value); + _serverProvider = value; + } + Future storeServerProviderKey(final String value) async { await _box.put(BNames.hetznerKey, value); _serverProviderKey = value; diff --git a/lib/ui/pages/setup/initializing/initializing.dart b/lib/ui/pages/setup/initializing/initializing.dart index b5c7851c..217bfc86 100644 --- a/lib/ui/pages/setup/initializing/initializing.dart +++ b/lib/ui/pages/setup/initializing/initializing.dart @@ -72,7 +72,8 @@ class InitializingPage extends StatelessWidget { ) : ProgressBar( steps: const [ - 'Hetzner', + 'Hosting', + 'Server Type', 'CloudFlare', 'Backblaze', 'Domain', diff --git a/lib/ui/pages/setup/initializing/server_provider_picker.dart b/lib/ui/pages/setup/initializing/server_provider_picker.dart index 3984b856..4934d1e3 100644 --- a/lib/ui/pages/setup/initializing/server_provider_picker.dart +++ b/lib/ui/pages/setup/initializing/server_provider_picker.dart @@ -114,9 +114,7 @@ class ProviderInputDataPage extends StatelessWidget { const Spacer(), FilledButton( title: 'basis.connect'.tr(), - onPressed: () => providerCubit.state.isSubmitting - ? null - : () => providerCubit.trySubmit(), + onPressed: () => providerCubit.trySubmit(), ), const SizedBox(height: 10), OutlinedButton( diff --git a/lib/ui/pages/setup/initializing/server_type_picker.dart b/lib/ui/pages/setup/initializing/server_type_picker.dart index 7a56ac2e..ce21b5aa 100644 --- a/lib/ui/pages/setup/initializing/server_type_picker.dart +++ b/lib/ui/pages/setup/initializing/server_type_picker.dart @@ -147,14 +147,14 @@ class SelectTypePage extends StatelessWidget { children: [ Text(type.title), const SizedBox(height: 8), - Text('cores: $type.cores'), + Text('cores: $type.cores.toString()'), const SizedBox(height: 8), - Text('ram: $type.ram'), + Text('ram: $type.ram.toString()'), const SizedBox(height: 8), - Text('disk: $type.disk.gibibyte'), + Text('disk: $type.disk.gibibyte.toString()'), const SizedBox(height: 8), Text( - 'price: $type.price.value $type.price.currency', + 'price: $type.price.value.toString() $type.price.currency', ), ], ),