Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

(Hopefully) final map optimizations #93

Merged
merged 2 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions lib/ui/base/text_styles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ class TextStyles {
static const TextStyle langName =
TextStyle(fontFamily: 'OpenSans', fontSize: 14, color: Color(0xFF192123));

static const TextStyle searchBarText = TextStyle(
static const TextStyle searchBarText =
TextStyle(fontFamily: 'OpenSans', fontSize: 14, color: Color(0xFF192123));
static const TextStyle searchBarHint =
TextStyle(fontFamily: 'OpenSans', fontSize: 14, color: Colors.grey);
static const TextStyle progressbarHint = TextStyle(
fontFamily: 'OpenSans',
fontSize: 14,
color: Color(0xFF192123),
color: Colors.white,
fontWeight: FontWeight.bold);
static const TextStyle searchBarHint =
TextStyle(fontFamily: 'OpenSans', fontSize: 14, color: Colors.grey);
static const TextStyle progressbarHint =
TextStyle(fontFamily: 'OpenSans', fontSize: 14, color: Colors.white);
static const TextStyle searchResultDistance = TextStyle(
fontFamily: 'OpenSans',
fontSize: 12,
Expand Down
7 changes: 4 additions & 3 deletions lib/ui/map/map_page/map_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,13 @@ class _MapPageState extends PageStatePlante<MapPage>
builder: (context, ref, _) =>
AnimatedMapWidget(child: _mode.watch(ref).buildHeader())),
MapHintsList(controller: _hintsController),
MapPageTimedHints(
loading: _model.loading,
loadingSuggestions: _model.loadingSuggestions),
Consumer(
builder: (context, ref, _) => AnimatedMapWidget(
child: _mode.watch(ref).buildTopActions())),
IgnorePointer(
child: MapPageTimedHints(
loading: _model.loading,
loadingSuggestions: _model.loadingSuggestions)),
])),
),
Consumer(builder: (context, ref, _) => _mode.watch(ref).buildOverlay()),
Expand Down
80 changes: 40 additions & 40 deletions lib/ui/map/map_page/markers_builder.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
Expand Down Expand Up @@ -100,7 +99,7 @@ Future<BitmapDescriptor> _getMarkerBitmap(Iterable<Shop> shops,
final _imagePaint = Paint();
final _textPainter = TextPainter(textDirection: TextDirection.ltr);
final _assetsCache = <String, ui.Picture>{};
final _markersCache = <String, Map<int, ByteData>>{};
final _markersCache = <String, Map<int, BitmapDescriptor>>{};

/// Stolen from https://stackoverflow.com/a/57609840
Future<BitmapDescriptor> _bitmapDescriptorFromSvgAsset(BuildContext context,
Expand All @@ -109,6 +108,10 @@ Future<BitmapDescriptor> _bitmapDescriptorFromSvgAsset(BuildContext context,
shops = 10; // In order for cache to work better
}

if (_markersCache[assetName]?[shops] != null) {
return _markersCache[assetName]![shops]!;
}

final pictureRecorder = ui.PictureRecorder();
final Canvas canvas = Canvas(pictureRecorder);

Expand All @@ -134,46 +137,43 @@ Future<BitmapDescriptor> _bitmapDescriptorFromSvgAsset(BuildContext context,
final image = await picture.toImage(size.round(), size.round());
canvas.drawImage(image, Offset.zero, _imagePaint);

ByteData? markerData = _markersCache[assetName]?[shops];
if (markerData == null) {
// Text
if (shops != 1) {
final text = shops <= 9 ? '$shops' : '9+';
_textPainter.text = TextSpan(
text: text,
style: textStyle.copyWith(
fontSize: textStyle.fontSize! * devicePixelRatio));
_textPainter.layout();
// Magic numbers! Figured out manually, might be wrong
final double xOffset;
if (shops == 2) {
xOffset = 21 * devicePixelRatio;
} else if (shops == 4) {
xOffset = 20.5 * devicePixelRatio;
} else if (shops == 6) {
xOffset = 20.75 * devicePixelRatio;
} else if (shops == 8) {
xOffset = 21 * devicePixelRatio;
} else if (shops > 9) {
xOffset = 21 * devicePixelRatio;
} else {
xOffset = 21.5 * devicePixelRatio;
}

final yOffset = 11.5 * devicePixelRatio;
_textPainter.paint(
canvas, Offset(xOffset - _textPainter.width / 2, yOffset));
// Text
if (shops != 1) {
final text = shops <= 9 ? '$shops' : '9+';
_textPainter.text = TextSpan(
text: text,
style: textStyle.copyWith(
fontSize: textStyle.fontSize! * devicePixelRatio));
_textPainter.layout();
// Magic numbers! Figured out manually, might be wrong
final double xOffset;
if (shops == 2) {
xOffset = 21 * devicePixelRatio;
} else if (shops == 4) {
xOffset = 20.5 * devicePixelRatio;
} else if (shops == 6) {
xOffset = 20.75 * devicePixelRatio;
} else if (shops == 8) {
xOffset = 21 * devicePixelRatio;
} else if (shops > 9) {
xOffset = 21 * devicePixelRatio;
} else {
xOffset = 21.5 * devicePixelRatio;
}

final img = await pictureRecorder
.endRecording()
.toImage(size.round(), size.round());
markerData = await img.toByteData(format: ui.ImageByteFormat.png);
if (_markersCache[assetName] == null) {
_markersCache[assetName] = {};
}
_markersCache[assetName]![shops] = markerData!;
final yOffset = 11.5 * devicePixelRatio;
_textPainter.paint(
canvas, Offset(xOffset - _textPainter.width / 2, yOffset));
}

return BitmapDescriptor.fromBytes(markerData.buffer.asUint8List());
final img =
await pictureRecorder.endRecording().toImage(size.round(), size.round());
final markerData = await img.toByteData(format: ui.ImageByteFormat.png);
final result = BitmapDescriptor.fromBytes(markerData!.buffer.asUint8List());

if (_markersCache[assetName] == null) {
_markersCache[assetName] = {};
}
_markersCache[assetName]![shops] = result;
return result;
}