Skip to content

Commit

Permalink
Improved code quality (refactoring & stricter linting) (#1594)
Browse files Browse the repository at this point in the history
* add lints, code style improvements, add assert messages

* use documentation comments for methods

https://dart.dev/language/comments#documentation-comments

* better callback parameter names, dart format

* fix lazy fields in `MapCamera`

* use documentation comments part 2

* use `@immutable` for immutables, const constructors where possible

* refactor loops

* use math min() / max()

* remove omit_local_variable_types lint

* use full method bodies instead of `=>`

* typo

* Fixed conflict resolution bugs

* Fix lint warning

* remove cupertino imports

fix
https://github.com/fleaflet/flutter_map/pull/1594/files/a50be82dfbfc92fff753c7b704d4b98bb63a6d81#r1275362713
https://github.com/fleaflet/flutter_map/pull/1594/files/a50be82dfbfc92fff753c7b704d4b98bb63a6d81#r1275360404

* remove non null cast

* revert list mapping

https://github.com/fleaflet/flutter_map/pull/1594/files/38d7250da5524779449cab91559d1b25234641da..df4b2053e79c71704b02fd72dcfe52bfd28d6888#r1270268326
  • Loading branch information
josxha authored Aug 3, 2023
1 parent 991f53c commit 9907251
Show file tree
Hide file tree
Showing 60 changed files with 561 additions and 464 deletions.
16 changes: 16 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,19 @@ linter:
type_annotate_public_apis: true
unnecessary_statements: true
use_named_constants: true
use_super_parameters: true
cast_nullable_to_non_nullable: true
only_throw_errors: true
sort_unnamed_constructors_first: true
prefer_asserts_with_message: true
missing_whitespace_between_adjacent_strings: true
avoid_multiple_declarations_per_line: true
noop_primitive_operations: true
avoid_void_async: true
avoid_redundant_argument_values: true
avoid_types_on_closure_parameters: true
unnecessary_null_checks: true
prefer_single_quotes: true
unnecessary_parenthesis: true
use_if_null_to_convert_nulls_to_bools: true
matching_super_parameters: true
4 changes: 2 additions & 2 deletions example/lib/pages/custom_crs/custom_crs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class _CustomCrsPageState extends State<CustomCrsPage> {
// Some goeserver changes origin based on zoom level
// and some are not at all (use explicit/implicit null or use [Point(0, 0)])
// @see https://github.com/kartena/Proj4Leaflet/pull/171
origins: [const Point(0, 0)],
origins: const [Point(0, 0)],
// Scale factors (pixels per projection unit, for example pixels/meter) for zoom levels;
// specify either scales or resolutions, not both
scales: null,
Expand Down Expand Up @@ -167,7 +167,7 @@ class _CustomCrsPageState extends State<CustomCrsPage> {
format: 'image/jpeg',
baseUrl:
'https://www.gebco.net/data_and_products/gebco_web_services/north_polar_view_wms/mapserv?',
layers: ['gebco_north_polar_view'],
layers: const ['gebco_north_polar_view'],
),
),
),
Expand Down
8 changes: 4 additions & 4 deletions example/lib/pages/epsg3413_crs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class _EPSG3413PageState extends State<EPSG3413Page> {
proj4Projection: epsg3413,
resolutions: resolutions,
bounds: epsg3413Bounds,
origins: [const Point(0, 0)],
origins: const [Point(0, 0)],
scales: null,
transformation: null,
);
Expand All @@ -67,8 +67,8 @@ class _EPSG3413PageState extends State<EPSG3413Page> {
Widget build(BuildContext context) {
// These circles should have the same pixel radius on the map
final circles = [
CircleMarker(
point: const LatLng(90, 0),
const CircleMarker(
point: LatLng(90, 0),
radius: 20000,
useRadiusInMeter: true,
color: Colors.yellow,
Expand Down Expand Up @@ -162,7 +162,7 @@ class _EPSG3413PageState extends State<EPSG3413Page> {
format: 'image/jpeg',
baseUrl:
'https://www.gebco.net/data_and_products/gebco_web_services/north_polar_view_wms/mapserv?',
layers: ['gebco_north_polar_view'],
layers: const ['gebco_north_polar_view'],
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion example/lib/pages/epsg4326_crs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EPSG4326Page extends StatelessWidget {
wmsOptions: WMSTileLayerOptions(
crs: const Epsg4326(),
baseUrl: 'https://ows.mundialis.de/services/service?',
layers: ['TOPO-OSM-WMS'],
layers: const ['TOPO-OSM-WMS'],
),
userAgentPackageName: 'dev.fleaflet.flutter_map.example',
),
Expand Down
2 changes: 1 addition & 1 deletion example/lib/pages/wms_tile_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class WMSLayerPage extends StatelessWidget {
TileLayer(
wmsOptions: WMSTileLayerOptions(
baseUrl: 'https://{s}.s2maps-tiles.eu/wms/?',
layers: ['s2cloudless-2021_3857'],
layers: const ['s2cloudless-2021_3857'],
),
subdomains: const ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
userAgentPackageName: 'dev.fleaflet.flutter_map.example',
Expand Down
41 changes: 25 additions & 16 deletions lib/src/geo/crs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:proj4dart/proj4dart.dart' as proj4;
///
/// The main objective of a CRS is to handle the conversion between surface
/// points of objects of different dimensions. In our case 3D and 2D objects.
@immutable
abstract class Crs {
const Crs();

Expand All @@ -26,15 +27,15 @@ abstract class Crs {
try {
final projectedPoint = projection.project(latlng);
final scale = this.scale(zoom);
return transformation.transform(projectedPoint, scale.toDouble());
} catch (e) {
return transformation.transform(projectedPoint, scale);
} catch (_) {
return const Point(0, 0);
}
}

/// Converts a map point to the sphere coordinate (at a certain zoom).
LatLng pointToLatLng(Point point, double zoom) => projection
.unproject(transformation.untransform(point, scale(zoom).toDouble()));
LatLng pointToLatLng(Point point, double zoom) =>
projection.unproject(transformation.untransform(point, scale(zoom)));

/// Zoom to Scale function.
double scale(double zoom) => 256.0 * math.pow(2, zoom);
Expand All @@ -48,8 +49,8 @@ abstract class Crs {

final b = projection.bounds!;
final s = scale(zoom);
final min = transformation.transform(b.min, s.toDouble());
final max = transformation.transform(b.max, s.toDouble());
final min = transformation.transform(b.min, s);
final max = transformation.transform(b.max, s);
return Bounds(min, max);
}

Expand All @@ -61,6 +62,7 @@ abstract class Crs {
}

// Custom CRS for non geographical maps
@immutable
class CrsSimple extends Crs {
@override
final String code = 'CRS.SIMPLE';
Expand All @@ -71,7 +73,7 @@ class CrsSimple extends Crs {
@override
final Transformation transformation;

CrsSimple()
const CrsSimple()
: projection = const _LonLat(),
transformation = const Transformation(1, 0, -1, 0),
super();
Expand All @@ -86,6 +88,7 @@ class CrsSimple extends Crs {
(double, double)? get wrapLng => null;
}

@immutable
abstract class Earth extends Crs {
@override
bool get infinite => false;
Expand All @@ -100,6 +103,7 @@ abstract class Earth extends Crs {
}

/// The most common CRS used for rendering maps.
@immutable
class Epsg3857 extends Earth {
@override
final String code = 'EPSG:3857';
Expand All @@ -123,6 +127,7 @@ class Epsg3857 extends Earth {
}

/// A common CRS among GIS enthusiasts. Uses simple Equirectangular projection.
@immutable
class Epsg4326 extends Earth {
@override
final String code = 'EPSG:4326';
Expand All @@ -140,6 +145,7 @@ class Epsg4326 extends Earth {
}

/// Custom CRS
@immutable
class Proj4Crs extends Crs {
@override
final String code;
Expand All @@ -163,7 +169,7 @@ class Proj4Crs extends Crs {

final List<double> _scales;

Proj4Crs._({
const Proj4Crs._({
required this.code,
required this.projection,
required this.transformation,
Expand Down Expand Up @@ -229,17 +235,16 @@ class Proj4Crs extends Crs {
final scale = this.scale(zoom);
final transformation = _getTransformationByZoom(zoom);

return transformation.transform(projectedPoint, scale.toDouble());
} catch (e) {
return transformation.transform(projectedPoint, scale);
} catch (_) {
return const Point(0, 0);
}
}

/// Converts a map point to the sphere coordinate (at a certain zoom).
@override
LatLng pointToLatLng(Point point, double zoom) =>
projection.unproject(_getTransformationByZoom(zoom)
.untransform(point, scale(zoom).toDouble()));
LatLng pointToLatLng(Point point, double zoom) => projection.unproject(
_getTransformationByZoom(zoom).untransform(point, scale(zoom)));

/// Rescales the bounds to a given zoom value.
@override
Expand All @@ -251,8 +256,8 @@ class Proj4Crs extends Crs {

final transformation = _getTransformationByZoom(zoom);

final min = transformation.transform(b.min, s.toDouble());
final max = transformation.transform(b.max, s.toDouble());
final min = transformation.transform(b.min, s);
final max = transformation.transform(b.max, s);
return Bounds(min, max);
}

Expand All @@ -267,7 +272,7 @@ class Proj4Crs extends Crs {
final baseScale = _scales[iZoom];
final nextScale = _scales[iZoom + 1];
final scaleDiff = nextScale - baseScale;
final zDiff = (zoom - iZoom);
final zDiff = zoom - iZoom;
return baseScale + scaleDiff * zDiff;
}
}
Expand Down Expand Up @@ -319,6 +324,7 @@ class Proj4Crs extends Crs {
}
}

@immutable
abstract class Projection {
const Projection();

Expand Down Expand Up @@ -367,6 +373,7 @@ class _LonLat extends Projection {
}
}

@immutable
class SphericalMercator extends Projection {
static const int r = 6378137;
static const double maxLatitude = 85.0511287798;
Expand Down Expand Up @@ -402,6 +409,7 @@ class SphericalMercator extends Projection {
}
}

@immutable
class _Proj4Projection extends Projection {
final proj4.Projection epsg4326;

Expand Down Expand Up @@ -432,6 +440,7 @@ class _Proj4Projection extends Projection {
}
}

@immutable
class Transformation {
final num a;
final num b;
Expand Down
28 changes: 9 additions & 19 deletions lib/src/geo/latlng_bounds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,21 @@ class LatLngBounds {
LatLng corner2,
) : this.fromPoints([corner1, corner2]);

LatLngBounds.fromPoints(List<LatLng> points) : assert(points.isNotEmpty) {
LatLngBounds.fromPoints(List<LatLng> points)
: assert(
points.isNotEmpty,
'LatLngBounds cannot be created with an empty List of LatLng',
) {
double minX = 180;
double maxX = -180;
double minY = 90;
double maxY = -90;

for (final point in points) {
final double x = point.longitude;
final double y = point.latitude;

if (minX > x) {
minX = x;
}

if (minY > y) {
minY = y;
}

if (maxX < x) {
maxX = x;
}

if (maxY < y) {
maxY = y;
}
minX = math.min<double>(minX, point.longitude);
minY = math.min<double>(minY, point.latitude);
maxX = math.max<double>(maxX, point.longitude);
maxY = math.max<double>(maxY, point.latitude);
}

_sw = LatLng(minY, minX);
Expand Down
Loading

0 comments on commit 9907251

Please sign in to comment.