Skip to content

Commit

Permalink
refactor: add assertions, remove redundant constructor, unnecessary p…
Browse files Browse the repository at this point in the history
…rivate field (#1832)

* remove private constructor from Bounds

* make SourceAttribution.onTap public since it gets provided by the user anyway

* add assertions
  • Loading branch information
josxha authored Feb 20, 2024
1 parent f22d3a4 commit 95ff019
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
10 changes: 5 additions & 5 deletions lib/src/layer/attribution_layer/rich/source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import 'package:meta/meta.dart';
/// Extended by [TextSourceAttribution] & [LogoSourceAttribution].
@immutable
sealed class SourceAttribution extends StatelessWidget {
const SourceAttribution._({super.key, VoidCallback? onTap}) : _onTap = onTap;
const SourceAttribution._({super.key, this.onTap});

final VoidCallback? _onTap;
final VoidCallback? onTap;

Widget _render(BuildContext context);

@override
@nonVirtual
Widget build(BuildContext context) {
if (_onTap == null) return _render(context);
if (onTap == null) return _render(context);

return GestureDetector(
onTap: _onTap,
onTap: onTap,
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: _render(context),
Expand All @@ -33,7 +33,7 @@ sealed class SourceAttribution extends StatelessWidget {
@immutable
class TextSourceAttribution extends SourceAttribution {
/// Default style used to display the [text], only when
/// [SourceAttribution._onTap] is not `null`
/// [SourceAttribution.onTap] is not `null`
static const defaultLinkTextStyle =
TextStyle(decoration: TextDecoration.underline);

Expand Down
5 changes: 4 additions & 1 deletion lib/src/layer/polygon_layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ class PolygonLayer<R extends Object> extends StatefulWidget {
this.polygonLabels = true,
this.drawLabelsLast = false,
this.hitNotifier,
});
}) : assert(
simplificationTolerance >= 0,
'simplificationTolerance cannot be negative: $simplificationTolerance',
);

@override
State<PolygonLayer<R>> createState() => _PolygonLayerState<R>();
Expand Down
5 changes: 4 additions & 1 deletion lib/src/layer/polyline_layer/polyline_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ class PolylineLayer<R extends Object> extends StatefulWidget {
this.simplificationTolerance = 0.4,
this.hitNotifier,
this.minimumHitbox = 10,
});
}) : assert(
simplificationTolerance >= 0,
'simplificationTolerance cannot be negative: $simplificationTolerance',
);

@override
State<PolylineLayer<R>> createState() => _PolylineLayerState<R>();
Expand Down
10 changes: 4 additions & 6 deletions lib/src/misc/bounds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ class Bounds<T extends num> {
minY = a.y;
maxY = b.y;
}
return Bounds._(Point<T>(minX, minY), Point<T>(maxX, maxY));
return Bounds.unsafe(Point<T>(minX, minY), Point<T>(maxX, maxY));
}

/// Create a [Bounds] instance **without** checking if [min] is actually the
/// minimum and [max] is actually the maximum.
const Bounds.unsafe(this.min, this.max);

const Bounds._(this.min, this.max);

/// Create a [Bounds] as bounding box of a list of points.
static Bounds<double> containing(Iterable<Point<double>> points) {
var maxX = double.negativeInfinity;
Expand All @@ -53,13 +51,13 @@ class Bounds<T extends num> {
minY = math.min(point.y, minY);
}

return Bounds._(Point(minX, minY), Point(maxX, maxY));
return Bounds.unsafe(Point(minX, minY), Point(maxX, maxY));
}

/// Creates a new [Bounds] obtained by expanding the current ones with a new
/// point.
Bounds<T> extend(Point<T> point) {
return Bounds._(
return Bounds.unsafe(
Point(math.min(point.x, min.x), math.min(point.y, min.y)),
Point(math.max(point.x, max.x), math.max(point.y, max.y)),
);
Expand Down Expand Up @@ -147,7 +145,7 @@ class Bounds<T extends num> {
final bottomY = math.min(max.y, b.max.y);

if (leftX <= rightX && topY <= bottomY) {
return Bounds._(Point(leftX, topY), Point(rightX, bottomY));
return Bounds.unsafe(Point(leftX, topY), Point(rightX, bottomY));
}

return null;
Expand Down

0 comments on commit 95ff019

Please sign in to comment.