This commit is contained in:
gitea
2025-11-05 19:01:26 +01:00
commit 5adc1af4ec
59 changed files with 2875 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
/// Configuration class that holds application settings.
///
/// This class contains environment-specific configuration values
/// such as API base URL and logging settings.
class AppConfig {
/// The base URL for API requests.
final String apiBaseUrl;
/// Whether logging is enabled in the application.
final bool enableLogging;
/// Creates an [AppConfig] instance with the provided values.
///
/// [apiBaseUrl] - The base URL for API requests.
/// [enableLogging] - Whether logging should be enabled.
const AppConfig({
required this.apiBaseUrl,
required this.enableLogging,
});
@override
String toString() {
return 'AppConfig(apiBaseUrl: $apiBaseUrl, enableLogging: $enableLogging)';
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is AppConfig &&
other.apiBaseUrl == apiBaseUrl &&
other.enableLogging == enableLogging;
}
@override
int get hashCode => apiBaseUrl.hashCode ^ enableLogging.hashCode;
}
+51
View File
@@ -0,0 +1,51 @@
import 'app_config.dart';
/// Exception thrown when an invalid environment is provided to [ConfigLoader].
class InvalidEnvironmentException implements Exception {
/// The invalid environment that was provided.
final String environment;
/// Creates an [InvalidEnvironmentException] with the provided environment.
InvalidEnvironmentException(this.environment);
@override
String toString() {
return 'InvalidEnvironmentException: Invalid environment "$environment". '
'Valid environments are: dev, prod';
}
}
/// Loads application configuration based on the specified environment.
///
/// This class provides a factory method to load configuration for either
/// 'dev' or 'prod' environments. It throws [InvalidEnvironmentException]
/// if an invalid environment is provided.
class ConfigLoader {
/// Private constructor to prevent instantiation.
ConfigLoader._();
/// Loads configuration for the specified environment.
///
/// [environment] - The environment to load ('dev' or 'prod').
///
/// Returns an [AppConfig] instance for the specified environment.
///
/// Throws [InvalidEnvironmentException] if [environment] is not 'dev' or 'prod'.
static AppConfig load(String environment) {
switch (environment.toLowerCase()) {
case 'dev':
return const AppConfig(
apiBaseUrl: 'https://api-dev.example.com',
enableLogging: true,
);
case 'prod':
return const AppConfig(
apiBaseUrl: 'https://api.example.com',
enableLogging: false,
);
default:
throw InvalidEnvironmentException(environment);
}
}
}