Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable get instance by tag #880

Closed
wants to merge 4 commits into from
Closed
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
9 changes: 8 additions & 1 deletion doc/docs/flutter_modular/dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ class AppModule extends Module {
void binds(i) {
i.add(XPTOEmail.new);
i.add<EmailService>(XPTOEmailService.new);
i.addSingleton(Client.new)
i.addSingleton(Client.new);

//You could register bind by tag
i.addSingleton<IHomeController>(HomeController.new, tag: 'home');
i.addSingleton<IHomeController>(HomeController2.new, tag: 'home2');
}

...
Expand All @@ -129,6 +133,9 @@ final client = Modular.get<Client>();

// or set a default value
final client = Modular.get<Client>(defaultValue: Client());

//if you wanna get a instance by tag name
final client = Modular.get<IHomeController>(tag: 'home');
```

## Auto Dispose
Expand Down
4 changes: 2 additions & 2 deletions flutter_modular/lib/src/domain/services/bind_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:modular_core/modular_core.dart';
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<T, ModularError> getBind<T extends Object>({String? tag});
Result<bool, ModularError> disposeBind<T extends Object>({String? tag});
Result<Unit, ModularError> replaceInstance<T>(T instance, [Type? module]);
}
6 changes: 3 additions & 3 deletions flutter_modular/lib/src/domain/usecases/dispose_bind.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 DisposeBind {
Result<bool, ModularError> call<T extends Object>();
Result<bool, ModularError> call<T extends Object>({String? tag});
}

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

@override
Result<bool, ModularError> call<T extends Object>() {
return bindService.disposeBind<T>();
Result<bool, ModularError> call<T extends Object>({String? tag}) {
return bindService.disposeBind<T>(tag: tag);
}
}
6 changes: 3 additions & 3 deletions flutter_modular/lib/src/domain/usecases/get_bind.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 GetBind {
Result<T, ModularError> call<T extends Object>();
Result<T, ModularError> call<T extends Object>({String? tag});
}

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

@override
Result<T, ModularError> call<T extends Object>() {
return bindService.getBind<T>();
Result<T, ModularError> call<T extends Object>({String? tag}) {
return bindService.getBind<T>(tag: tag);
}
}
10 changes: 7 additions & 3 deletions flutter_modular/lib/src/infra/services/bind_service_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ class BindServiceImpl extends BindService {
BindServiceImpl(this.injector);

@override
Result<bool, ModularError> disposeBind<T extends Object>() {
Result<bool, ModularError> disposeBind<T extends Object>({String? tag}) {
if (tag?.isNotEmpty ?? false) {
injector.disposeSingletonsByTag(tag!);
return const Success(true);
}
final result = injector.disposeSingleton<T>();
return Success(result != null);
}

@override
Result<T, ModularError> getBind<T extends Object>() {
Result<T, ModularError> getBind<T extends Object>({String? tag}) {
try {
final result = injector.get<T>();
final result = injector.get<T>(tag: tag);
return Success(result);
} on AutoInjectorException catch (e, s) {
return Failure(BindNotFoundException(e.toString(), s));
Expand Down
16 changes: 8 additions & 8 deletions flutter_modular/lib/src/presenter/modular_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ abstract class IModularBase {
IModularNavigator? navigatorDelegate;

/// Request an instance by [Type]
B get<B extends Object>();
B get<B extends Object>({String? tag});

/// Request an instance by [Type]
/// <br>
/// Return null if not found instance
B? tryGet<B extends Object>();
B? tryGet<B extends Object>({String? tag});

/// Dispose a bind by [Type]
bool dispose<B extends Object>();
Expand Down Expand Up @@ -132,17 +132,17 @@ class ModularBase implements IModularBase {
});

@override
bool dispose<B extends Object>() =>
disposeBind<B>().getOrElse((left) => false);
bool dispose<B extends Object>({String? tag}) =>
disposeBind<B>(tag: tag).getOrElse((left) => false);

@override
B get<B extends Object>() {
return getBind<B>().getOrThrow();
B get<B extends Object>({String? tag}) {
return getBind<B>(tag: tag).getOrThrow();
}

@override
B? tryGet<B extends Object>() {
return getBind<B>().getOrNull();
B? tryGet<B extends Object>({String? tag}) {
return getBind<B>(tag: tag).getOrNull();
}

@override
Expand Down
Loading