1*be199093STomasz Sapeta#import <MBProgressHUD/MBProgressHUD.h>
2*be199093STomasz Sapeta
39fcddb20Sandy#import "EXManagedAppSplashScreenViewController.h"
49fcddb20Sandy#import "EXSplashScreenHUDButton.h"
59fcddb20Sandy
69fcddb20Sandy@interface EXManagedAppSplashScreenViewController()
79fcddb20Sandy
89fcddb20Sandy@property (nonatomic, weak) NSTimer *warningTimer;
99fcddb20Sandy@property (nonatomic, weak) MBProgressHUD *warningHud;
109fcddb20Sandy
119fcddb20Sandy@end
129fcddb20Sandy
139fcddb20Sandy@implementation EXManagedAppSplashScreenViewController
149fcddb20Sandy
159fcddb20Sandy- (instancetype)initWithRootView:(UIView *)rootView splashScreenView:(UIView *)splashScreenView
169fcddb20Sandy{
179fcddb20Sandy  if (self = [super initWithRootView:rootView splashScreenView:splashScreenView]) {
189fcddb20Sandy    self.splashScreenView.userInteractionEnabled = YES;
199fcddb20Sandy  }
209fcddb20Sandy
219fcddb20Sandy  return self;
229fcddb20Sandy}
239fcddb20Sandy
249fcddb20Sandy- (void)showWithCallback:(void (^)(void))successCallback failureCallback:(void (^)(NSString * _Nonnull))failureCallback
259fcddb20Sandy{
269fcddb20Sandy  [super showWithCallback:^{
279fcddb20Sandy    [self startSplashScreenVisibleTimer];
289fcddb20Sandy    if (successCallback) {
299fcddb20Sandy      successCallback();
309fcddb20Sandy    }
319fcddb20Sandy  } failureCallback:failureCallback];
329fcddb20Sandy}
339fcddb20Sandy
349fcddb20Sandy- (void)hideWithCallback:(void (^)(BOOL))successCallback failureCallback:(void (^)(NSString * _Nonnull))failureCallback
359fcddb20Sandy{
369fcddb20Sandy  [super hideWithCallback:^(BOOL isSuccess){
379fcddb20Sandy    if (self.warningTimer) {
389fcddb20Sandy      [self.warningTimer invalidate];
399fcddb20Sandy    }
409fcddb20Sandy
419fcddb20Sandy    if (successCallback) {
429fcddb20Sandy      successCallback(YES);
439fcddb20Sandy    }
449fcddb20Sandy  } failureCallback:failureCallback];
459fcddb20Sandy}
469fcddb20Sandy
479fcddb20Sandy
489fcddb20Sandy-(void)startSplashScreenVisibleTimer
499fcddb20Sandy{
509fcddb20Sandy  self.warningTimer = [NSTimer scheduledTimerWithTimeInterval:20.0
519fcddb20Sandy                                                       target:self
529fcddb20Sandy                                                     selector:@selector(showSplashScreenVisibleWarning)
539fcddb20Sandy                                                     userInfo:nil
549fcddb20Sandy                                                      repeats:NO];
559fcddb20Sandy}
569fcddb20Sandy
579fcddb20Sandy- (void)showSplashScreenVisibleWarning
589fcddb20Sandy{
599fcddb20Sandy#if DEBUG
609fcddb20Sandy  _warningHud = [MBProgressHUD showHUDAddedTo: self.splashScreenView animated:YES];
619fcddb20Sandy  _warningHud.mode = MBProgressHUDModeCustomView;
629fcddb20Sandy
639fcddb20Sandy  EXSplashScreenHUDButton *button = [EXSplashScreenHUDButton buttonWithType: UIButtonTypeSystem];
649fcddb20Sandy  [button addTarget:self action:@selector(navigateToFYI) forControlEvents:UIControlEventTouchUpInside];
659fcddb20Sandy
669fcddb20Sandy  _warningHud.customView = button;
679fcddb20Sandy  _warningHud.offset = CGPointMake(0.f, MBProgressMaxOffset);
689fcddb20Sandy
699fcddb20Sandy  [_warningHud hideAnimated:YES afterDelay:8.f];
709fcddb20Sandy#endif
719fcddb20Sandy}
729fcddb20Sandy
739fcddb20Sandy- (void)navigateToFYI
749fcddb20Sandy{
759fcddb20Sandy  NSURL *fyiURL = [[NSURL alloc] initWithString:@"https://expo.fyi/splash-screen-hanging"];
769fcddb20Sandy  [[UIApplication sharedApplication] openURL:fyiURL];
779fcddb20Sandy  [_warningHud hideAnimated: YES];
789fcddb20Sandy}
799fcddb20Sandy
809fcddb20Sandy@end
81