Last active
June 8, 2023 16:55
-
-
Save ehbc221/06e0f099421d4d877e941294c32b2a94 to your computer and use it in GitHub Desktop.
A configuration file for your Flutter environments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:expenza/config/injectable.dart'; | |
import 'package:expenza/config/my_http_overrides.dart'; | |
import 'package:expenza/constants/api.constants.dart'; | |
const String baseUrlKey = 'baseUrl'; | |
const String environmentKey = 'ENVIRONMENT'; | |
/// The application's environments. | |
enum Environment { local, dev, preprod, prod } | |
/// The application's configuration constants. | |
/// | |
/// - author - @ehbc221 | |
/// - version - 1.0.0 | |
/// - last updated - June 8th, 2023 | |
class AppConfigConstants { | |
const AppConfigConstants({ | |
required this.environment, | |
required this.baseUrl, | |
}); | |
final String environment; | |
final String baseUrl; | |
} | |
/// The local environment constants. | |
AppConfigConstants localConstants = AppConfigConstants( | |
environment: 'local', | |
baseUrl: baseApiUrlLocal, | |
); | |
/// The development environment constants. | |
AppConfigConstants devConstants = const AppConfigConstants( | |
environment: 'dev', | |
baseUrl: baseApiUrlDev, | |
); | |
/// The production environment constants. | |
AppConfigConstants preprodConstants = const AppConfigConstants( | |
environment: 'preprod', | |
baseUrl: baseApiUrlPreprod, | |
); | |
/// The production environment constants. | |
AppConfigConstants prodConstants = const AppConfigConstants( | |
environment: 'prod', | |
baseUrl: baseApiUrlProd, | |
); | |
/// The application's default configuration. | |
AppConfigConstants appConfig = prodConstants; | |
/// Check if the current environment is DEV. | |
bool isCurrentEnvironmentDev() { | |
return getEnvironment() == devConstants.environment; | |
} | |
/// Check if the current environment is LOCAL. | |
bool isCurrentEnvironmentLocal() { | |
return getEnvironment() == localConstants.environment; | |
} | |
/// Check if the current environment is PREPROD. | |
bool isCurrentEnvironmentPreprod() { | |
return getEnvironment() == preprodConstants.environment; | |
} | |
/// Check if the current environment is PROD. | |
bool isCurrentEnvironmentProd() { | |
return getEnvironment() == prodConstants.environment; | |
} | |
/// Set the application's environment. | |
void setEnvironment(Environment env) { | |
switch (env) { | |
case Environment.local: | |
appConfig = localConstants; | |
break; | |
case Environment.preprod: | |
appConfig = preprodConstants; | |
break; | |
case Environment.prod: | |
appConfig = prodConstants; | |
break; | |
default: | |
appConfig = devConstants; | |
break; | |
} | |
} | |
/// Setup the MyHttpOverrides. | |
void setupMyHttpOverrides() { | |
// Check the environment before overriding the Http global | |
// This should not be called in prod environment | |
if (getEnvironment() != prodConstants.environment) { | |
HttpOverrides.global = getIt<MyHttpOverrides>(); | |
} | |
} | |
/// Get the application's environment. | |
String getEnvironment() { | |
return appConfig.environment; | |
} | |
/// Get an environment from a [String]. | |
/// | |
/// By default, the 'prod' environment is returned. | |
Environment getEnvironmentFromString(String environment) { | |
if (environment == Environment.local.name) { | |
return Environment.local; | |
} else if (environment == Environment.dev.name) { | |
return Environment.dev; | |
} else if (environment == Environment.preprod.name) { | |
return Environment.preprod; | |
} else if (environment == Environment.prod.name) { | |
return Environment.prod; | |
} else { | |
return Environment.prod; | |
} | |
} | |
/// Get the API's base url. | |
dynamic get apiBaseUrl { | |
return appConfig.baseUrl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment