Skip to content

Commit

Permalink
Merge 7ba0dc1 into 598809e
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderShirokih committed Jul 27, 2021
2 parents 598809e + 7ba0dc1 commit 305cae3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
Expand Up @@ -18,7 +18,7 @@ abstract class ModularInterface {
@visibleForTesting
void overrideBinds(List<Bind> binds);
void init(Module module);
void bindModule(Module module, [String path]);
void bindModule(Module module, {String path, bool rebindDuplicates});
void debugPrintModular(String text);
T bind<T extends Object>(Bind<T> bind);
Future<void> isModuleReady<M>();
Expand Down
45 changes: 36 additions & 9 deletions flutter_modular/lib/src/presenters/modular_impl.dart
Expand Up @@ -56,24 +56,41 @@ class ModularImpl implements ModularInterface {
}

@override
void bindModule(Module module, [String path = '']) {
void bindModule(Module module,
{String path = '', bool rebindDuplicates = false}) {
final name = module.runtimeType.toString();
if (!injectMap.containsKey(name)) {
module.paths.add(path);
injectMap[name] = module;
module.instance(_getAllSingletons());
debugPrintModular("-- ${module.runtimeType.toString()} INITIALIZED");
} else {
// The same module with default path, so rebind the module
if (path.isEmpty && rebindDuplicates) {
final oldModule = injectMap[name];

// Remove old injects
oldModule?.cleanInjects();
injectMap.remove(name);

// Install new injects
bindModule(module, path: name, rebindDuplicates: false);
return;
}

// Add the new path only if the last path in paths list is different from the current one
final _paths = injectMap[name]?.paths;
if (_paths?.isNotEmpty == true && _paths?.last != path) _paths?.add(path);

if (_paths?.isNotEmpty == true && _paths?.last != path) {
_paths?.add(path);
}
}
}

@override
void init(Module module) {
_initialModule = module;
bindModule(module, module.runtimeType.toString());
bindModule(module, path: module.runtimeType.toString());
}

@override
Expand Down Expand Up @@ -105,14 +122,22 @@ class ModularImpl implements ModularInterface {
B get<B extends Object>({List<Type>? typesInRequestList, B? defaultValue}) {
var typesInRequest = typesInRequestList ?? [];
if (Modular.flags.experimentalNotAllowedParentBinds) {
final module = routerDelegate.currentConfiguration?.currentModule?.runtimeType.toString() ?? 'AppModule';
var bind = injectMap[module]!.binds.firstWhere((b) => b.inject is B Function(Inject), orElse: () => BindEmpty());
final module = routerDelegate
.currentConfiguration?.currentModule?.runtimeType
.toString() ??
'AppModule';
var bind = injectMap[module]!.binds.firstWhere(
(b) => b.inject is B Function(Inject),
orElse: () => BindEmpty());
if (bind is BindEmpty) {
bind = injectMap[module]!.binds.firstWhere((b) => b.inject is Future<B> Function(Inject), orElse: () => BindEmpty());
bind = injectMap[module]!.binds.firstWhere(
(b) => b.inject is Future<B> Function(Inject),
orElse: () => BindEmpty());
}

if (bind is BindEmpty) {
throw ModularError('\"${B.toString()}\" not found in \"$module\" module');
throw ModularError(
'\"${B.toString()}\" not found in \"$module\" module');
}
}
var result = _findExistingInstance<B>();
Expand All @@ -122,7 +147,8 @@ class ModularImpl implements ModularInterface {
}

for (var key in injectMap.keys) {
final value = _getInjectableObject<B>(key, typesInRequestList: typesInRequest, checkKey: false);
final value = _getInjectableObject<B>(key,
typesInRequestList: typesInRequest, checkKey: false);
if (value != null) {
return value;
}
Expand Down Expand Up @@ -187,7 +213,8 @@ class ModularImpl implements ModularInterface {
}

@override
T bind<T extends Object>(Bind<T> bind) => Inject(overrideBinds: _overrideBinds ?? []).get(bind);
T bind<T extends Object>(Bind<T> bind) =>
Inject(overrideBinds: _overrideBinds ?? []).get(bind);

@override
Future<void> isModuleReady<M>() {
Expand Down
Expand Up @@ -112,7 +112,7 @@ class ModularRouteInformationParser extends RouteInformationParser<ModularRoute>
);
}
if (route.module != null) {
Modular.bindModule(route.module!, uri.path);
Modular.bindModule(route.module!, path: uri.path);
}
return router;
}
Expand All @@ -139,7 +139,7 @@ class ModularRouteInformationParser extends RouteInformationParser<ModularRoute>
}

if (parseRoute.currentModule != null) {
Modular.bindModule(parseRoute.currentModule!, uri.path);
Modular.bindModule(parseRoute.currentModule!, path: uri.path);
}
return parseRoute;
}
Expand Down
8 changes: 4 additions & 4 deletions flutter_modular/lib/src/presenters/widgets/widget_module.dart
Expand Up @@ -12,6 +12,7 @@ _debugPrintModular(String text) {
}

abstract class WidgetModule extends StatelessWidget implements Module {

Widget get view;

@override
Expand All @@ -28,7 +29,7 @@ abstract class WidgetModule extends StatelessWidget implements Module {

final _FakeModule _fakeModule = _FakeModule();

WidgetModule() {
WidgetModule({Key? key}): super(key: key) {
// ignore: invalid_use_of_visible_for_testing_member
_fakeModule.changeBinds(binds);
}
Expand Down Expand Up @@ -107,8 +108,7 @@ class _ModularProviderState extends State<ModularProvider> {
@override
void initState() {
super.initState();
// Modular.addCoreInit(widget.module);
_debugPrintModular("-- ${widget.module.runtimeType} INITIALIZED");
Modular.bindModule(widget.module, rebindDuplicates: true);
}

@override
Expand All @@ -118,8 +118,8 @@ class _ModularProviderState extends State<ModularProvider> {

@override
void dispose() {
widget.module.cleanInjects();
super.dispose();
// Modular.removeModule(widget.module);
_debugPrintModular("-- ${widget.module.runtimeType} DISPOSED");
}
}

0 comments on commit 305cae3

Please sign in to comment.