1// Copyright 2018-present 650 Industries. All rights reserved.
2
3#import <EXSplashScreen/EXSplashScreenModule.h>
4#import <EXSplashScreen/EXSplashScreenService.h>
5#import <UMCore/UMAppLifecycleService.h>
6#import <UMCore/UMUtilities.h>
7
8@interface EXSplashScreenModule ()
9
10@property (nonatomic, weak) UMModuleRegistry *moduleRegistry;
11@property (nonatomic, weak) id<UMUtilitiesInterface> utilities;
12
13@end
14
15@implementation EXSplashScreenModule
16
17UM_EXPORT_MODULE(ExpoSplashScreen);
18
19- (void)setModuleRegistry:(UMModuleRegistry *)moduleRegistry
20{
21  _moduleRegistry = moduleRegistry;
22  _utilities = [moduleRegistry getModuleImplementingProtocol:@protocol(UMUtilitiesInterface)];
23  [[moduleRegistry getModuleImplementingProtocol:@protocol(UMAppLifecycleService)] registerAppLifecycleListener:self];
24}
25
26UM_EXPORT_METHOD_AS(hideAsync,
27                    hideWithResolve:(UMPromiseResolveBlock)resolve
28                    reject:(UMPromiseRejectBlock)reject)
29{
30  UM_WEAKIFY(self);
31  dispatch_async(dispatch_get_main_queue(), ^{
32    UM_ENSURE_STRONGIFY(self);
33    UIViewController *currentViewController = self.utilities.currentViewController;
34    [[self splashScreenService] hideSplashScreenFor:currentViewController
35                                    successCallback:^(BOOL hasEffect){ resolve(@(hasEffect)); }
36                                    failureCallback:^(NSString *message){ reject(@"ERR_SPLASH_SCREEN_CANNOT_HIDE", message, nil); }];
37  });
38}
39
40UM_EXPORT_METHOD_AS(preventAutoHideAsync,
41                    preventAutoHideWithResolve:(UMPromiseResolveBlock)resolve
42                    reject:(UMPromiseRejectBlock)reject)
43{
44  UM_WEAKIFY(self);
45  dispatch_async(dispatch_get_main_queue(), ^{
46    UM_ENSURE_STRONGIFY(self);
47    UIViewController *currentViewController = self.utilities.currentViewController;
48    [[self splashScreenService] preventSplashScreenAutoHideFor:currentViewController
49                                               successCallback:^(BOOL hasEffect){ resolve(@(hasEffect)); }
50                                               failureCallback:^(NSString *message){ reject(@"ERR_SPLASH_SCREEN_CANNOT_PREVENT_AUTOHIDE", message, nil); }];
51  });
52}
53
54# pragma mark - UMAppLifecycleListener
55
56- (void)onAppBackgrounded {}
57
58- (void)onAppForegrounded {}
59
60- (void)onAppContentDidAppear
61{
62  UM_WEAKIFY(self);
63  dispatch_async(dispatch_get_main_queue(), ^{
64    UM_ENSURE_STRONGIFY(self);
65    UIViewController* currentViewController = self.utilities.currentViewController;
66    [[self splashScreenService] onAppContentDidAppear:currentViewController];
67  });
68}
69
70- (void)onAppContentWillReload
71{
72  UM_WEAKIFY(self);
73  dispatch_async(dispatch_get_main_queue(), ^{
74    UM_ENSURE_STRONGIFY(self);
75    UIViewController* currentViewController = self.utilities.currentViewController;
76    [[self splashScreenService] onAppContentWillReload:currentViewController];
77  });
78}
79
80# pragma mark - internals
81
82/**
83 * Tries to obtain singleton module that is registered as "SplashScreen".
84 * Silent agreement is that registered module conforms to "EXSplashScreenService" protocol.
85 */
86- (id<EXSplashScreenService>)splashScreenService
87{
88  return [self.moduleRegistry getSingletonModuleForName:@"SplashScreen"];
89}
90
91@end
92