Skip to content

Commit

Permalink
feat: add isValid to path and fields
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Oct 6, 2024
1 parent 886c332 commit 132d3ab
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app_navigator/lib/src/content_navigator.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:tekartik_app_navigator_flutter/content_navigator.dart';
import 'package:tekartik_app_navigator_flutter/route_aware.dart' as route_aware;
import 'package:tekartik_app_navigator_flutter/src/route_aware.dart';
Expand Down Expand Up @@ -377,6 +378,11 @@ extension ContentNavigatorBlocExt on ContentNavigatorBloc {
/// Push a path.
Future<T?> pushPath<T>(ContentPath path,
{Object? arguments, TransitionDelegate? transitionDelegate}) {
if (kDebugMode) {
if (!path.isValid()) {
throw ArgumentError('Invalid path', path.toString());
}
}
return push<T>(path.routeSettings(arguments),
transitionDelegate: transitionDelegate);
}
Expand Down
10 changes: 10 additions & 0 deletions app_navigator/lib/src/content_path.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ extension ContentPathExt on ContentPath {
/// Helper to match a string path directly
@Deprecated('Use toPathString instead')
String toPath() => toPathString();

/// Check if the path is valid and can be pushed
bool isValid() {
for (var field in fields) {
if (!field.isValid()) {
return false;
}
}
return true;
}
}

/// A field in a content path.
Expand Down
9 changes: 8 additions & 1 deletion app_navigator/lib/src/content_path_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class ContentPathField {

/// Create a field with a name and an optional value
ContentPathField(this.name, [String? value])
: _value = value == _wildcard ? null : value;
: _value = value == _wildcard ? null : value {
assert(name.isNotEmpty, 'name cannot be empty');
}

@override
int get hashCode => super.hashCode + (value?.hashCode ?? 0);
Expand Down Expand Up @@ -75,6 +77,11 @@ class ContentPathField {
return '$name/$value';
}
}

/// Check if the field is valid
bool isValid() {
return value != null && value != _wildcard;
}
}

/// content path list extension
Expand Down
4 changes: 4 additions & 0 deletions app_navigator/lib/src/content_path_part.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ import 'package:tekartik_app_navigator_flutter/content_navigator.dart';
class ContentPathPart extends ContentPathField {
/// Create a part with a name (and no value)
ContentPathPart(String name) : super(name, '');

/// Always valid
@override
bool isValid() => true;
}
8 changes: 8 additions & 0 deletions app_navigator/test/field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,13 @@ void main() {
expect(field.value, 'my_value');
expect(field.name, 'my_name');
});
test('isValid', () {
var field = ContentPathField('my_name');
expect(field.isValid(), isFalse);
field.value = 'my_value';
expect(field.isValid(), isTrue);
var part = ContentPathPart('my_name');
expect(part.isValid(), isTrue);
});
});
}
4 changes: 4 additions & 0 deletions app_navigator/test/path_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ void main() {
expect(rootContentPath.matchesString('a'), isFalse);
expect(rootContentPath.matchesString('/a'), isFalse);
});
test('isValid', () {
expect(SubPath().isValid(), isFalse);
expect((SubPath()..base.value = '123').isValid(), isTrue);
});
});
}

Expand Down

0 comments on commit 132d3ab

Please sign in to comment.