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

Support storyboard embed modules #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -9,6 +9,7 @@
#import <objc/runtime.h>
#import "RamblerViperOpenModulePromise.h"
#import "RamblerViperModuleFactory.h"
#import "RamblerViperModuleInput.h"

static IMP originalPrepareForSegueMethodImp;

Expand Down Expand Up @@ -100,29 +101,41 @@ + (void)swizzlePrepareForSegue {
});
}

static UIViewController *sourceViewControllerFromSegue(UIStoryboardSegue * segue) {
return segue.sourceViewController;
}

static UIViewController *destinationViewControllerFromSegue(UIStoryboardSegue * segue) {
UIViewController *destinationViewController = segue.destinationViewController;
if ([destinationViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = destinationViewController;
destinationViewController = navigationController.topViewController;
}
return destinationViewController;
}

void RamblerViperPrepareForSegueSender(id self, SEL selector, UIStoryboardSegue * segue, id sender) {

((void(*)(id,SEL,UIStoryboardSegue*,id))originalPrepareForSegueMethodImp)(self,selector,segue,sender);

if (![sender isKindOfClass:[RamblerViperOpenModulePromise class]]) {
return;
}

id<RamblerViperModuleInput> moduleInput = nil;

UIViewController *destinationViewController = segue.destinationViewController;
if ([destinationViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = segue.destinationViewController;
destinationViewController = navigationController.topViewController;
}

id<RamblerViperModuleTransitionHandlerProtocol> targetModuleTransitionHandler = destinationViewController;
id<RamblerViperModuleTransitionHandlerProtocol> targetModuleTransitionHandler = destinationViewControllerFromSegue(segue);
if ([targetModuleTransitionHandler respondsToSelector:@selector(moduleInput)]) {
moduleInput = [targetModuleTransitionHandler moduleInput];
} else {
return;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really sure that "return" is needed there.

In case sender is RamblerViperOpenModulePromise and targetModuleTransitionHandler does not respond to selector "moduleInput", variable moduleInput will be nil.
Value of this variable will be assigned to property moduleInput of RamblerViperOpenModulePromise instance and in setter (setModuleInput:) will be performed some inner setup which will allow to perform linkBlock but whith moduleInput == nil. If we do not call setModuleInput: inner setup won`t be performed and linkBlock will be never called too.

}

RamblerViperOpenModulePromise *openModulePromise = sender;
openModulePromise.moduleInput = moduleInput;
if ([sender isKindOfClass:[RamblerViperOpenModulePromise class]]) {
RamblerViperOpenModulePromise *openModulePromise = sender;
openModulePromise.moduleInput = moduleInput;
}

if ([moduleInput respondsToSelector:@selector(setModuleOutput:)]) {
id sourceOutput = [sourceViewControllerFromSegue(segue) moduleInput];
[moduleInput setModuleOutput:sourceOutput];
}
}

@end