1#import <UIKit/UIKit.h>
2
3#import "EXManagedAppSplashScreenConfigurationBuilder.h"
4#import "EXUtil.h"
5
6
7static const NSString *kManifestIosKey = @"ios";
8static const NSString *kManifestSplashKey = @"splash";
9static const NSString *kManifestResizeModeKey = @"resizeMode";
10static const NSString *kManifestBackgroundColorKey = @"backgroundColor";
11static const NSString *kManifestTabletImageUrlKey = @"tabletImageUrl";
12static const NSString *kManifestImageUrlKey = @"imageUrl";
13
14static const NSString *kImageResizeModeContain = @"contain";
15static const NSString *kImageResizeModeCover = @"cover";
16
17@implementation EXManagedAppSplashScreenConfigurationBuilder
18
19+ (EXManagedAppSplashScreenConfiguration *)parseManifest:(NSDictionary *)manifest
20{
21  UIColor *backgroundColor = [[self class] parseBackgroundColor:manifest];
22  NSString *imageUrl = [[self class] parseImageUrl:manifest];
23  EXSplashScreenImageResizeMode imageResizeMode = [[self class] parseImageResizeMode:manifest];
24  return [[EXManagedAppSplashScreenConfiguration alloc] initWithBackgroundColor:backgroundColor
25                                                                       imageUrl:imageUrl
26                                                                imageResizeMode:imageResizeMode];
27}
28
29+ (UIColor * _Nonnull)parseBackgroundColor:(NSDictionary *)manifest
30{
31  // TODO: (@bbarthec) backgroundColor is recommended to be in HEX format for now, but it should be any css-valid format
32  NSString *hexString = [[self class] getStringFromManifest:manifest
33                                                      paths:@[
34                                                        @[kManifestIosKey, kManifestSplashKey, kManifestBackgroundColorKey],
35                                                        @[kManifestSplashKey, kManifestBackgroundColorKey],
36                                                      ]];
37  UIColor *color = [EXUtil colorWithHexString:hexString];
38  if (color) {
39    return color;
40  }
41
42  return [UIColor whiteColor];
43}
44
45+ (NSString * _Nullable)parseImageUrl:(NSDictionary *)manifest
46{
47  return [[self class] getStringFromManifest:manifest
48                                       paths:@[
49                                         [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad
50                                           ? @[kManifestIosKey, kManifestSplashKey, kManifestTabletImageUrlKey]
51                                           : @[],
52                                         @[kManifestIosKey, kManifestSplashKey, kManifestImageUrlKey],
53                                         @[kManifestSplashKey, kManifestImageUrlKey],
54                                       ]];
55}
56
57+ (EXSplashScreenImageResizeMode)parseImageResizeMode:(NSDictionary *)manifest
58{
59  NSString *resizeMode = [[self class] getStringFromManifest:manifest
60                                                       paths:@[
61                                                         @[kManifestIosKey, kManifestSplashKey, kManifestResizeModeKey],
62                                                         @[kManifestSplashKey, kManifestResizeModeKey],
63                                                       ]];
64  if ([kImageResizeModeCover isEqualToString:resizeMode]) {
65    return EXSplashScreenImageResizeModeCover;
66  }
67  return EXSplashScreenImageResizeModeContain;
68}
69
70+ (NSString * _Nullable)getStringFromManifest:(NSDictionary *)manifest
71                                        paths:(NSArray<NSArray<const NSString *> *> *)paths
72{
73  for (NSArray<const NSString *> *path in paths) {
74    NSString *result = [[self class] getStringFromManifest:manifest path:path];
75    if (result) {
76      return result;
77    }
78  }
79  return nil;
80}
81
82+ (NSString * _Nullable)getStringFromManifest:(NSDictionary *)manifest
83                                         path:(NSArray<const NSString *> *)path
84{
85  NSDictionary *json = manifest;
86  for (int i = 0; i < path.count; i++) {
87    BOOL isLastKey = i == path.count - 1;
88    const NSString *key = path[i];
89    id value = json[key];
90    if (isLastKey && [value isKindOfClass:[NSString class]]) {
91      return value;
92    }
93    json = value;
94  }
95  return nil;
96}
97
98@end
99