1// Copyright © 2018 650 Industries. All rights reserved.
2
3#import <EXSplashScreen/EXSplashScreenViewController.h>
4#import <ExpoModulesCore/EXDefines.h>
5#import <ExpoModulesCore/EXUtilities.h>
6
7@interface EXSplashScreenViewController ()
8
9@property (nonatomic, weak) UIView *rootView;
10
11@property (nonatomic, assign) BOOL autoHideEnabled;
12@property (nonatomic, assign) BOOL splashScreenShown;
13@property (nonatomic, assign) BOOL appContentAppeared;
14
15@end
16
17@implementation EXSplashScreenViewController
18
19- (instancetype)initWithRootView:(UIView *)rootView splashScreenView:(nonnull UIView *)splashScreenView
20{
21  if (self = [super init]) {
22    _rootView = rootView;
23    _autoHideEnabled = YES;
24    _splashScreenShown = NO;
25    _appContentAppeared = NO;
26    _splashScreenView = splashScreenView;
27  }
28  return self;
29}
30
31# pragma mark public methods
32
33- (void)showWithCallback:(void (^)(void))successCallback failureCallback:(void (^)(NSString * _Nonnull))failureCallback
34{
35  [self showWithCallback:successCallback];
36}
37
38- (void)showWithCallback:(nullable void(^)(void))successCallback
39{
40  [EXUtilities performSynchronouslyOnMainThread:^{
41    UIView *rootView = self.rootView;
42    self.splashScreenView.frame = rootView.bounds;
43    [rootView addSubview:self.splashScreenView];
44    self.splashScreenShown = YES;
45    if (successCallback) {
46      successCallback();
47    }
48  }];
49}
50
51- (void)preventAutoHideWithCallback:(void (^)(BOOL))successCallback failureCallback:(void (^)(NSString * _Nonnull))failureCallback
52{
53  if (!_autoHideEnabled) {
54    return successCallback(NO);
55  }
56
57  _autoHideEnabled = NO;
58  successCallback(YES);
59}
60
61- (void)hideWithCallback:(void (^)(BOOL))successCallback failureCallback:(void (^)(NSString * _Nonnull))failureCallback
62{
63  if (!_splashScreenShown) {
64    return successCallback(NO);
65  }
66
67  [self hideWithCallback:successCallback];
68}
69
70- (void)hideWithCallback:(nullable void(^)(BOOL))successCallback
71{
72  EX_WEAKIFY(self);
73  dispatch_async(dispatch_get_main_queue(), ^{
74    EX_ENSURE_STRONGIFY(self);
75    [self.splashScreenView removeFromSuperview];
76    self.splashScreenShown = NO;
77    self.autoHideEnabled = YES;
78    if (successCallback) {
79      successCallback(YES);
80    }
81  });
82}
83
84- (BOOL)needsHideOnAppContentDidAppear
85{
86  if (!_appContentAppeared && _autoHideEnabled) {
87    _appContentAppeared = YES;
88    return YES;
89  }
90  return NO;
91}
92
93- (BOOL)needsShowOnAppContentWillReload
94{
95  if (!_appContentAppeared) {
96    _autoHideEnabled = YES;
97    _appContentAppeared = NO;
98    return YES;
99  }
100  return NO;
101}
102
103@end
104