Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix caching background images for tiles and remove url logs with access token #369

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions lib/common/map/image_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,25 @@ class MapboxTileImageCache {

// the background image that should be cached and displayed in the all rides view.
final cacheUrl =
"https://api.mapbox.com/styles/v1/$styleId/static/$bboxStr/${(1000 * widthRatio).toInt()}x${(1000 * heightRatio).toInt()}/?attribution=false&logo=false&$accessToken";
final endpoint = Uri.parse(cacheUrl);
"https://api.mapbox.com/styles/v1/$styleId/static/$bboxStr/1000x1000/?attribution=false&logo=false&$accessToken";

// Only run if ratios differ. Otherwise we would make the same request.
if (heightRatio != 1 && widthRatio != 1) {
final cacheResponse = await Http.get(endpoint).timeout(const Duration(seconds: 4));
if (cacheResponse.statusCode != 200) {
final err = "Error while fetching background image status from Mapbox: ${cacheResponse.statusCode}";
throw Exception(err);
}
// Use Cache url when height or width ratio is given.
final endpoint = Uri.parse(heightRatio != 1 || widthRatio != 1 ? cacheUrl : feedbackUrl);

final cachedImage = MemoryImage(cacheResponse.bodyBytes);
log.i("Fetched background image from Mapbox: $cacheUrl");
await saveImage(bbox, cachedImage, brightness, styleUri);
// Only run if ratios differ. Otherwise we would make the same request.
final cacheResponse = await Http.get(endpoint).timeout(const Duration(seconds: 4));
if (cacheResponse.statusCode != 200) {
final err = "Error while fetching background image status from Mapbox: ${cacheResponse.statusCode}";
throw Exception(err);
}

final cachedImage = MemoryImage(cacheResponse.bodyBytes);
log.i("Fetched background image from Mapbox");
await saveImage(bbox, cachedImage, brightness, styleUri);

final MemoryImage feedbackImage = MemoryImage(feedbackResponse.bodyBytes);

log.i("Fetched background image from Mapbox: $feedbackUrl");
log.i("Fetched background image from Mapbox");

// save timestamp of last fetch to shared preferences, used in pruning of old images
final prefs = await SharedPreferences.getInstance();
Expand Down
Loading