Skip to content

Commit

Permalink
Merge fd38bd3 into 625c853
Browse files Browse the repository at this point in the history
  • Loading branch information
cq-z committed Nov 3, 2020
2 parents 625c853 + fd38bd3 commit 4cdbaf6
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 18 deletions.
4 changes: 3 additions & 1 deletion flutter_modular/lib/src/inject/bind.dart
Expand Up @@ -10,7 +10,9 @@ class Bind<T> {
///When 'false', the object is instantiated along with the module.
final bool lazy;

Bind(this.inject, {this.singleton = true, this.lazy = true})
final String alias;

Bind(this.inject, {this.singleton = true, this.lazy = true, this.alias })
: assert((singleton || lazy),
r"'singleton' can't be false if 'lazy' is also false");
}
Expand Down
2 changes: 1 addition & 1 deletion flutter_modular/lib/src/inject/inject.dart
Expand Up @@ -10,7 +10,7 @@ class Inject<T> {
@deprecated
Map<String, dynamic> params = {};
final String tag;
final List<Type> typesInRequest;
final List<String> typesInRequest;

Inject({this.params, this.tag, this.typesInRequest});

Expand Down
13 changes: 7 additions & 6 deletions flutter_modular/lib/src/interfaces/child_module.dart
Expand Up @@ -18,18 +18,19 @@ abstract class ChildModule {

final List<String> paths = <String>[];

final Map<Type, dynamic> _singletonBinds = {};
final Map<String, dynamic> _singletonBinds = {};

T getBind<T>(Map<String, dynamic> params, {List<Type> typesInRequest}) {
T getBind<T>(Map<String, dynamic> params, {List<String> typesInRequest, String alias}) {
T bindValue;
var type = _getInjectType<T>();
var type = alias ?? _getInjectType<T>().toString();
if (_singletonBinds.containsKey(type)) {
bindValue = _singletonBinds[type];
return bindValue;
}

var bind = _binds.firstWhere((b) => b.inject is T Function(Inject),
var bind = _binds.firstWhere((b) => b.alias == type || ( b.alias == null && T.toString() != 'dynamic' && b.inject is T Function(Inject)),
orElse: () => null);

if (bind == null) {
typesInRequest.remove(type);
return null;
Expand All @@ -51,7 +52,7 @@ ${typesInRequest.join('\n')}
bindValue =
bind.inject(Inject(params: params, typesInRequest: typesInRequest));
if (bind.singleton) {
_singletonBinds[type] = bindValue;
_singletonBinds[bind.alias ?? type] = bindValue;
}

typesInRequest.remove(type);
Expand Down Expand Up @@ -105,7 +106,7 @@ ${typesInRequest.join('\n')}
for (final bindElement in _binds) {
if (!bindElement.lazy) {
var b = bindElement.inject(Inject());
_singletonBinds[b.runtimeType] = b;
_singletonBinds[bindElement.alias ?? b.runtimeType.toString()] = b;
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions flutter_modular/lib/src/modular_base.dart
Expand Up @@ -169,24 +169,26 @@ class Modular {
static B get<B>(
{Map<String, dynamic> params,
String module,
List<Type> typesInRequest,
List<String> typesInRequest,
String alias,
B defaultValue}) {
if (B.toString() == 'dynamic') {
if (B.toString() == 'dynamic' && alias == null) {
throw ModularError('not allow for dynamic values');
}

typesInRequest ??= [];

if (module != null) {
return _getInjectableObject<B>(module,
params: params, typesInRequest: typesInRequest);
params: params, typesInRequest: typesInRequest, alias: alias);
}

for (var key in _injectMap.keys) {
final value = _getInjectableObject<B>(key,
params: params,
disableError: true,
typesInRequest: typesInRequest,
alias: alias,
checkKey: false);
if (value != null) {
return value;
Expand All @@ -203,15 +205,16 @@ class Modular {
static B _getInjectableObject<B>(String tag,
{Map<String, dynamic> params,
bool disableError = false,
List<Type> typesInRequest,
List<String> typesInRequest,
String alias,
bool checkKey = true}) {
B value;
if (!checkKey) {
value =
_injectMap[tag].getBind<B>(params, typesInRequest: typesInRequest);
_injectMap[tag].getBind<B>(params, typesInRequest: typesInRequest, alias: alias);
} else if (_injectMap.containsKey(tag)) {
value =
_injectMap[tag].getBind<B>(params, typesInRequest: typesInRequest);
_injectMap[tag].getBind<B>(params, typesInRequest: typesInRequest, alias: alias);
}
if (value == null && !disableError) {
throw ModularError('${B.toString()} not found in module $tag');
Expand Down
4 changes: 2 additions & 2 deletions flutter_modular/lib/src/widgets/module_widget.dart
Expand Up @@ -30,8 +30,8 @@ abstract class WidgetModule extends StatelessWidget implements ChildModule {
}

@override
T getBind<T>(Map<String, dynamic> params, {List<Type> typesInRequest}) {
return _fakeModule.getBind<T>(params, typesInRequest: typesInRequest);
T getBind<T>(Map<String, dynamic> params, {List<String> typesInRequest, String alias}) {
return _fakeModule.getBind<T>(params, typesInRequest: typesInRequest, alias: alias);
}

@override
Expand Down
4 changes: 2 additions & 2 deletions flutter_modular/lib/src/widgets/widget_module.dart
Expand Up @@ -30,8 +30,8 @@ abstract class WidgetModule extends StatelessWidget implements ChildModule {
}

@override
T getBind<T>(Map<String, dynamic> params, {List<Type> typesInRequest}) {
return _fakeModule.getBind<T>(params, typesInRequest: typesInRequest);
T getBind<T>(Map<String, dynamic> params, {List<String> typesInRequest, String alias}) {
return _fakeModule.getBind<T>(params, typesInRequest: typesInRequest, alias: alias);
}

@override
Expand Down
2 changes: 2 additions & 0 deletions flutter_modular/test/app/app_module.dart
Expand Up @@ -9,12 +9,14 @@ import 'modules/home/home_module.dart';
import 'modules/product/product_module.dart';
import 'shared/ilocal_repository.dart';
import 'shared/local_storage_shared.dart';
import 'shared/local_storage_shared_alias.dart';

class AppModule extends MainModule {
@override
List<Bind> get binds => [
Bind((i) => AppBloc()),
Bind<ILocalStorage>((i) => LocalStorageSharePreference()),
Bind((i) => LocalStorageSharePreferenceAlias(),alias: 'test'),
];

@override
Expand Down
18 changes: 18 additions & 0 deletions flutter_modular/test/app/shared/local_storage_shared_alias.dart
@@ -0,0 +1,18 @@
import 'ilocal_repository.dart';

class LocalStorageSharePreferenceAlias implements ILocalStorage {
@override
Future delete(String key) {
return null;
}

@override
Future get(String key) {
return null;
}

@override
Future put(String key, String value) {
return null;
}
}
1 change: 1 addition & 0 deletions flutter_modular/test/bind_test.dart
Expand Up @@ -8,6 +8,7 @@ void main() {
expect(Bind((i) => 'obs', singleton: true, lazy: true), isA<Bind>());
expect(Bind((i) => 'obs', singleton: true, lazy: false), isA<Bind>());
expect(Bind((i) => 'obs', singleton: false, lazy: true), isA<Bind>());
expect(Bind((i) => 'obs', singleton: false, lazy: true, alias: 'test'), isA<Bind>());
});
test('error', () {
expect(
Expand Down
5 changes: 5 additions & 0 deletions flutter_modular/test/modular_inject_test.dart
Expand Up @@ -10,6 +10,7 @@ import 'app/modules/home/home_module.dart';
import 'app/shared/app_info.state.dart';
import 'app/shared/ilocal_repository.dart';
import 'app/shared/local_storage_shared.dart';
import 'app/shared/local_storage_shared_alias.dart';

void main() {
setUpAll(() {
Expand All @@ -23,11 +24,15 @@ void main() {
test('Get withless module', () {
expect(Modular.get<AppBloc>(), isA<AppBloc>());
expect(Modular.get<HomeBloc>(), isA<HomeBloc>());
expect(Modular.get(alias: 'test'), isA<LocalStorageSharePreferenceAlias>());

});

test('Get with module', () {
expect(Modular.get<AppBloc>(module: 'AppModule'), isA<AppBloc>());
expect(Modular.get<HomeBloc>(module: 'HomeModule'), isA<HomeBloc>());
expect(Modular.get(module: 'AppModule', alias: 'test'), isA<LocalStorageSharePreferenceAlias>());

});

test('Inject not found with module', () {
Expand Down

0 comments on commit 4cdbaf6

Please sign in to comment.