1#import <EXSplashScreen/EXSplashScreenConfiguration.h>
2#import <React/RCTImageSource.h>
3#import <React/RCTImageView.h>
4#import <UMCore/UMDefines.h>
5
6#import "EXManagedAppSplashScreenViewProvider.h"
7#import "EXManagedAppSplashScreenConfigurationBuilder.h"
8#import "EXKernel.h"
9#import "EXReactAppManager.h"
10
11@interface RCTImageView (EXAppLoadingView)
12
13@property (nonatomic, copy) RCTDirectEventBlock onLoadEnd;
14
15@end
16
17@interface EXManagedAppSplashScreenViewProvider ()
18
19@property (nonatomic, weak) UIView *splashScreenView;
20@property (nonatomic, strong) EXSplashScreenConfiguration *configuration;
21@property (nonatomic, strong, nullable) RCTImageView *splashImageView;
22
23@end
24
25@implementation EXManagedAppSplashScreenViewProvider
26
27- (instancetype)initWithManifest:(NSDictionary *)manifest
28{
29  if (self = [super init]) {
30    _configuration = [EXManagedAppSplashScreenConfigurationBuilder parseManifest:manifest];
31  }
32  return self;
33}
34
35- (void)updateSplashScreenViewWithManifest:(NSDictionary *)manifest
36{
37  _configuration = [EXManagedAppSplashScreenConfigurationBuilder parseManifest:manifest];
38  if (_splashScreenView) {
39    [self configureSplashScreenView:_splashScreenView];
40  }
41}
42
43- (UIView *)createSplashScreenView
44{
45  UIView *splashScreenView = [UIView new];
46  [self configureSplashScreenView:splashScreenView];
47  _splashScreenView = splashScreenView;
48  return splashScreenView;
49}
50
51- (void)configureSplashScreenView:(UIView *)splashScreenView
52{
53  UM_WEAKIFY(self);
54  dispatch_async(dispatch_get_main_queue(), ^{
55    UM_ENSURE_STRONGIFY(self);
56    splashScreenView.backgroundColor = self.configuration.backgroundColor;
57
58    if (self.configuration.imageUrl) {
59      EXKernelAppRecord *homeAppRecord = [EXKernel sharedInstance].appRegistry.homeAppRecord;
60
61      if (homeAppRecord.appManager.reactBridge) {
62        // remove old splashImageView
63        if (self.splashImageView) {
64          [self.splashImageView removeFromSuperview];
65        }
66
67        RCTImageSource *imageSource = [RCTConvert RCTImageSource:@{ @"uri": self.configuration.imageUrl }];
68
69        self.splashImageView = [[RCTImageView alloc] initWithBridge:homeAppRecord.appManager.reactBridge];
70        self.splashImageView.frame = splashScreenView.bounds;
71        self.splashImageView.imageSources = @[imageSource];
72        self.splashImageView.resizeMode = self.configuration.imageResizeMode == EXSplashScreenImageResizeModeCover ? RCTResizeModeCover : RCTResizeModeContain;;
73        UM_WEAKIFY(self);
74        [self.splashImageView setOnLoadEnd:^(NSDictionary *dict) {
75          UM_ENSURE_STRONGIFY(self);
76        }];
77        [splashScreenView addSubview:self.splashImageView];
78      }
79    }
80  });
81}
82
83@end
84
85