Skip to content

Commit

Permalink
pushNamedAndRemoveUntil
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobaraujo7 committed Aug 9, 2023
1 parent da3dbb4 commit 79e97b2
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 56 deletions.
5 changes: 5 additions & 0 deletions flutter_modular/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [6.0.3] - 2023-08-09

- fix: [#875](https://github.com/Flutterando/modular/issues/875)
- fix: [#615](https://github.com/Flutterando/modular/issues/615)

## [6.0.2] - 2023-08-01

- fix: [#867](https://github.com/Flutterando/modular/issues/867)
Expand Down
2 changes: 1 addition & 1 deletion flutter_modular/lib/src/domain/services/bind_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import 'package:result_dart/result_dart.dart';
abstract class BindService {
Result<T, ModularError> getBind<T extends Object>();
Result<bool, ModularError> disposeBind<T extends Object>();
Result<Unit, ModularError> replaceInstance<T>(T instance);
Result<Unit, ModularError> replaceInstance<T>(T instance, [Type? module]);
}
6 changes: 3 additions & 3 deletions flutter_modular/lib/src/domain/usecases/replace_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:result_dart/result_dart.dart';
import '../services/bind_service.dart';

abstract class ReplaceInstance {
Result<Unit, ModularError> call<T>(T instance);
Result<Unit, ModularError> call<T>(T instance, [Type? module]);
}

class ReplaceInstanceImpl implements ReplaceInstance {
Expand All @@ -13,7 +13,7 @@ class ReplaceInstanceImpl implements ReplaceInstance {
ReplaceInstanceImpl(this.bindService);

@override
Result<Unit, ModularError> call<T>(T instance) {
return bindService.replaceInstance<T>(instance);
Result<Unit, ModularError> call<T>(T instance, [Type? module]) {
return bindService.replaceInstance<T>(instance, module);
}
}
24 changes: 18 additions & 6 deletions flutter_modular/lib/src/infra/services/bind_service_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,26 @@ class BindServiceImpl extends BindService {
}

@override
Result<Unit, ModularError> replaceInstance<T>(T instance) {
final isAdded = injector.isAdded<T>();
if (!isAdded) {
return BindNotFoundException('$T unregistred', StackTrace.current)
.toFailure();
Result<Unit, ModularError> replaceInstance<T>(T instance, [Type? module]) {
var tag = module?.toString() ?? '';

if (tag.isEmpty) {
tag = injector.tags.firstWhere(
(innerTag) => injector.isAdded<T>(innerTag),
orElse: () => '',
);
} else {
tag = injector.isAdded<T>(tag) ? tag : '';
}

if (tag.isEmpty) {
return BindNotFoundException(
'$T unregistred',
StackTrace.current,
).toFailure();
}

injector.replaceInstance<T>(instance);
injector.replaceInstance<T>(instance, tag);
return Success.unit();
}
}
12 changes: 5 additions & 7 deletions flutter_modular/lib/src/presenter/modular_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ abstract class IModularBase {
void unbindModule<T extends Module>({String? type});

/// replace instance
void replaceInstance<T>(T instance);
void replaceInstance<T>(T instance, [Type? module]);

@visibleForTesting
String get initialRoutePath;
Expand Down Expand Up @@ -154,10 +154,8 @@ class ModularBase implements IModularBase {
@override
void init(Module module) {
if (!_moduleHasBeenStarted) {
startModule(module).fold(
(r) => printResolverFunc?.call('${module.runtimeType} started!'),
(l) => throw l,
);
startModule(module).getOrThrow();
printResolverFunc?.call('${module.runtimeType} started!');
_moduleHasBeenStarted = true;
} else {
throw ModuleStartedException(
Expand Down Expand Up @@ -219,7 +217,7 @@ class ModularBase implements IModularBase {
}

@override
void replaceInstance<T>(T instance) {
replaceInstanceUsecase.call<T>(instance).getOrThrow();
void replaceInstance<T>(T instance, [Type? module]) {
replaceInstanceUsecase.call<T>(instance, module).getOrThrow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,15 @@ class ModularRouterDelegate extends RouterDelegate<ModularBook>
@override
void popUntil(bool Function(Route) predicate) {
var isFoundedPages = currentConfiguration?.routes.where((route) {
return predicate(CustomModalRoute(ModularPage(
route: route,
args: ModularArguments.empty(),
flags: ModularFlags())));
return predicate(
CustomModalRoute(
ModularPage(
route: route,
args: ModularArguments.empty(),
flags: ModularFlags(),
),
),
);
});

isFoundedPages ??= [];
Expand All @@ -242,42 +247,45 @@ class ModularRouterDelegate extends RouterDelegate<ModularBook>

@override
Future<T?> pushNamedAndRemoveUntil<T extends Object?>(
String routeName, bool Function(Route) predicate,
{Object? arguments, bool forRoot = false}) async {
String routeName,
bool Function(Route) predicate, {
Object? arguments,
bool forRoot = false,
}) async {
final popComplete = Completer();
var book = await parser.selectBook(routeName,
arguments: arguments, popCallback: popComplete.complete);
if (forRoot) {
final list = currentConfiguration!.routes.where((route) {
return predicate(CustomModalRoute(ModularPage(
route: route,
args: ModularArguments.empty(),
flags: ModularFlags())));
}).toList();
book = currentConfiguration!
.copyWith(routes: [...list, book.routes.last.copyWith(schema: '')]);
await setNewRoutePath(book);
} else {
final list = currentConfiguration!.routes.where((route) {
return predicate(CustomModalRoute(ModularPage(
final book = await parser.selectBook(
routeName,
arguments: arguments,
popCallback: popComplete.complete,
);

final actualRoutes = currentConfiguration!.routes.toList();

final reversed = actualRoutes.reversed.toList();

for (final route in reversed) {
final result = predicate(
CustomModalRoute(
ModularPage(
route: route,
args: ModularArguments.empty(),
flags: ModularFlags())));
}).toList();
for (final route in book.routes.reversed) {
if (list
.firstWhere(
(element) => element.uri.toString() == route.uri.toString(),
orElse: ParallelRoute.empty)
.name ==
'') {
list.add(route);
}
flags: ModularFlags(),
),
),
);

if (result) {
break;
}

await setNewRoutePath(book.copyWith(routes: list));
actualRoutes.remove(route);
}

await setNewRoutePath(book.copyWith(routes: [
...actualRoutes,
...book.routes,
]));

return await popComplete.future;
}

Expand Down
4 changes: 2 additions & 2 deletions flutter_modular/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: flutter_modular
description: Smart project structure with dependency injection and route management
version: 6.0.1
version: 6.0.3
homepage: https://github.com/Flutterando/modular

environment:
sdk: ">=3.0.0 <4.0.0"

dependencies:
modular_core: ">=3.0.2+1 <4.0.0"
modular_core: ">=3.0.3+1 <4.0.0"
meta: ">=1.3.0 <2.0.0"
result_dart: ">=1.0.4 <2.0.0"
flutter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ void main() {
group('replaceInstance', () {
test('should replace instance returning unit', () {
const instance = 'String';
when(() => injector.isAdded<String>()).thenReturn(true);
when(() => injector.tags).thenReturn({'String'});
when(() => injector.isAdded<String>('String')).thenReturn(true);
when(() => injector.replaceInstance<String>(instance)).thenReturnVoid();

final result = service.replaceInstance<String>(instance);
Expand All @@ -51,7 +52,17 @@ void main() {

test('should return error if instance unregistred', () async {
const instance = 'String';
when(() => injector.isAdded<String>()).thenReturn(false);
when(() => injector.isAdded<String>('String')).thenReturn(false);

final result = service.replaceInstance<String>(instance, String);

expect(result.isError(), true);
});

test('should return error if instance unregistred without tags', () async {
const instance = 'String';
when(() => injector.tags).thenReturn({'String'});
when(() => injector.isAdded<String>('String')).thenReturn(false);

final result = service.replaceInstance<String>(instance);

Expand Down
1 change: 1 addition & 0 deletions flutter_modular/test/src/presenter/modular_base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ void main() {
});

test('init', () {
setPrintResolver((text) {});
final module = ModuleMock();
when(() => startModule.call(module)).thenReturn(const Success(unit));
modularBase.init(module);
Expand Down
4 changes: 2 additions & 2 deletions modular_core/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: modular_core
description: Smart project structure with dependency injection and route management
version: 3.0.2+1
version: 3.0.3+1
homepage: https://github.com/Flutterando/modular

environment:
sdk: ">=3.0.0 <4.0.0"

dependencies:
auto_injector: ">=1.1.1 <2.0.0"
auto_injector: ">=1.2.0 <2.0.0"
characters: ">=1.1.0 <2.0.0"
meta: ">=1.3.0 <2.0.0"

Expand Down

0 comments on commit 79e97b2

Please sign in to comment.