Refactor environment variable handling and update build instructions

- Introduced a Makefile for building the release APK with `make build release`.
- Updated README to reflect new build command and usage of `--dart-define-from-file=.env`.
- Removed dotenv dependency and switched to using Dart's `--dart-define` for API key management.
- Adjusted ForexRateAPI key loading logic to improve error handling and clarity.

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-20 09:55:07 +01:00
parent 529d4eaa3f
commit 2e703c6722
7 changed files with 77 additions and 14 deletions
+58 -2
View File
@@ -41,7 +41,7 @@ class ForexRateApiService {
final parsed = jsonDecode(response.body) as Map<String, dynamic>;
final error = parsed['error'] as Map<String, dynamic>?;
final code = error?['code']?.toString().toLowerCase();
return code == 'rate_limit_reached';
return code == '104' || code == 'rate_limit_reached';
} catch (_) {
return false;
}
@@ -136,6 +136,62 @@ class ForexRateApiService {
if (apiKey == 'REPLACE_WITH_YOUR_FOREXRATE_API_KEY') {
throw StateError('ForexRateAPI key is not configured.');
}
return null;
final start = DateTime(year, 1, 1);
final end = DateTime(year, 12, 31);
final uri =
Uri.https('api.forexrateapi.com', '/v1/timeframe', <String, String>{
'api_key': apiKey,
'base': 'USD',
'currencies': 'SEK',
'start_date': DateFormat('yyyy-MM-dd').format(start),
'end_date': DateFormat('yyyy-MM-dd').format(end),
});
_log('Timeframe request: ${_redactedUri(uri)}');
final response = await _getWithRateLimitRetry(uri, operation: 'Timeframe');
if (response.statusCode != 200) {
_log(
'Timeframe endpoint unavailable for this account or request; falling back to per-day requests. status=${response.statusCode}',
);
return null;
}
final json = jsonDecode(response.body) as Map<String, dynamic>;
final success = json['success'] as bool? ?? false;
if (!success) {
_log(
'Timeframe success=false; falling back to per-day requests. error=${json['error']}',
);
return null;
}
final rates = json['rates'] as Map<String, dynamic>?;
if (rates == null) {
_log(
'Timeframe response missing rates; falling back to per-day requests.',
);
return null;
}
final results = <String, double>{};
for (final entry in rates.entries) {
final dayRates = entry.value as Map<String, dynamic>?;
final sek = dayRates?['SEK'];
if (sek is num) {
results[entry.key] = sek.toDouble();
}
}
if (results.isEmpty) {
_log(
'Timeframe parsed no SEK entries; falling back to per-day requests.',
);
return null;
}
_log('Timeframe parsed ${results.length} daily SEK quotes.');
return results;
}
}