Skip to content
Merged
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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");
}
}