// Copyright © 2018 650 Industries. All rights reserved. #import #import #import @interface EXSplashScreenService () @property (nonatomic, strong) NSMapTable *splashScreenControllers; @end @implementation EXSplashScreenService UM_REGISTER_SINGLETON_MODULE(SplashScreen); - (instancetype)init { if (self = [super init]) { _splashScreenControllers = [NSMapTable weakToStrongObjectsMapTable]; } return self; } - (void)showSplashScreenFor:(UIViewController *)viewController { id splashScreenViewProvider = [EXSplashScreenViewNativeProvider new]; return [self showSplashScreenFor:viewController splashScreenViewProvider:splashScreenViewProvider successCallback:^{} failureCallback:^(NSString *message){ UMLogWarn(@"%@", message); }]; } - (void)showSplashScreenFor:(UIViewController *)viewController splashScreenViewProvider:(id)splashScreenViewProvider successCallback:(void (^)(void))successCallback failureCallback:(void (^)(NSString * _Nonnull))failureCallback { if ([self.splashScreenControllers objectForKey:viewController]) { return failureCallback(@"'SplashScreen.show' has already been called for given view controller."); } EXSplashScreenController *splashScreenController = [[EXSplashScreenController alloc] initWithViewController:viewController splashScreenViewProvider:splashScreenViewProvider]; [self.splashScreenControllers setObject:splashScreenController forKey:viewController]; [[self.splashScreenControllers objectForKey:viewController] showWithCallback:successCallback failureCallback:failureCallback]; } - (void)preventSplashScreenAutoHideFor:(UIViewController *)viewController successCallback:(void (^)(BOOL hasEffect))successCallback failureCallback:(void (^)(NSString * _Nonnull))failureCallback { if (![self.splashScreenControllers objectForKey:viewController]) { return failureCallback(@"No native splash screen registered for given view controller. Call 'SplashScreen.show' for given view controller first."); } return [[self.splashScreenControllers objectForKey:viewController] preventAutoHideWithCallback:successCallback failureCallback:failureCallback]; } - (void)hideSplashScreenFor:(UIViewController *)viewController successCallback:(void (^)(BOOL hasEffect))successCallback failureCallback:(void (^)(NSString * _Nonnull))failureCallback { if (![self.splashScreenControllers objectForKey:viewController]) { return failureCallback(@"No native splash screen registered for given view controller. Call 'SplashScreen.show' for given view controller first."); } return [[self.splashScreenControllers objectForKey:viewController] hideWithCallback:successCallback failureCallback:failureCallback]; } - (void)onAppContentDidAppear:(UIViewController *)viewController { if (![self.splashScreenControllers objectForKey:viewController]) { UMLogWarn(@"No native splash screen registered for given view controller. Call 'SplashScreen.show' for given view controller first."); } [[self.splashScreenControllers objectForKey:viewController] onAppContentDidAppear]; } - (void)onAppContentWillReload:(UIViewController *)viewController { if (![self.splashScreenControllers objectForKey:viewController]) { UMLogWarn(@"No native splash screen registered for given view controller. Call 'SplashScreen.show' for given view controller first."); } [[self.splashScreenControllers objectForKey:viewController] onAppContentWillReload]; } # pragma mark - UIApplicationDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIViewController *rootViewController = [[application keyWindow] rootViewController]; if (rootViewController) { [self showSplashScreenFor:rootViewController]; } return YES; } @end