Skip to content

Commit

Permalink
refactor!: Make CDVViewController more API friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
dpogue committed Aug 15, 2024
1 parent c6fd16f commit 9b635b2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 27 deletions.
40 changes: 31 additions & 9 deletions CordovaLib/Classes/Public/CDVViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,23 @@ Licensed to the Apache Software Foundation (ASF) under one
#import <Cordova/NSDictionary+CordovaPreferences.h>
#import "CDVCommandDelegateImpl.h"

UIColor* defaultBackgroundColor(void) {
static UIColor* defaultBackgroundColor(void) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
if (@available(iOS 13.0, *)) {
return UIColor.systemBackgroundColor;
} else {
return UIColor.whiteColor;
}
#endif

return UIColor.whiteColor;
}

@interface CDVViewController () <CDVWebViewEngineConfigurationDelegate> { }
@interface CDVViewController () <CDVWebViewEngineConfigurationDelegate> {
id <CDVWebViewEngineProtocol> _webViewEngine;
id <CDVCommandDelegate> _commandDelegate;
CDVCommandQueue* _commandQueue;
UIColor* _backgroundColor;
UIColor* _splashBackgroundColor;
}

@property (nonatomic, readwrite, strong) NSXMLParser* configParser;
@property (nonatomic, readwrite, strong) NSMutableDictionary* settings;
Expand All @@ -62,6 +70,8 @@ @implementation CDVViewController
@synthesize commandDelegate = _commandDelegate;
@synthesize commandQueue = _commandQueue;
@synthesize webViewEngine = _webViewEngine;
@synthesize backgroundColor = _backgroundColor;
@synthesize splashBackgroundColor = _splashBackgroundColor;
@dynamic webView;

- (void)__init
Expand All @@ -85,6 +95,8 @@ - (void)__init
name:CDVPageDidLoadNotification object:nil];


self.showInitialSplashScreen = YES;

self.initialized = YES;
}
}
Expand All @@ -110,6 +122,16 @@ - (id)init
return self;
}

- (void)setBackgroundColor:(UIColor *)color
{
_backgroundColor = color ?: defaultBackgroundColor();
}

- (void)setSplashBackgroundColor:(UIColor *)color
{
_splashBackgroundColor = color ?: self.backgroundColor;
}

-(NSString*)configFilePath{
NSString* path = self.configFile ?: @"config.xml";

Expand Down Expand Up @@ -298,12 +320,12 @@ - (void)viewDidLoad
}
// /////////////////

UIColor* bgDefault = defaultBackgroundColor();
UIColor* bgColor = [UIColor colorNamed:@"BackgroundColor"] ?: bgDefault;
UIColor* bgSplash = [UIColor colorNamed:@"SplashScreenBackgroundColor"] ?: bgColor;
[self.webView setBackgroundColor:self.backgroundColor];
[self.launchView setBackgroundColor:self.splashBackgroundColor];

[self.webView setBackgroundColor:bgColor];
[self.launchView setBackgroundColor:bgSplash];
if (self.showInitialSplashScreen) {
[self.launchView setAlpha:1];
}
}

-(void)viewWillAppear:(BOOL)animated
Expand Down
51 changes: 39 additions & 12 deletions CordovaLib/include/Cordova/CDVViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,11 @@

@class CDVPlugin;

/*!
@abstract The main view controller for Cordova web content.
*/
@interface CDVViewController : UIViewController {
@protected
id <CDVWebViewEngineProtocol> _webViewEngine;
@protected
id <CDVCommandDelegate> _commandDelegate;
@protected
CDVCommandQueue* _commandQueue;
}
@interface CDVViewController : UIViewController

NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, readonly, weak) IBOutlet UIView* webView;
@property (nonatomic, readonly, strong) UIView* launchView;

@property (nullable, nonatomic, readonly, strong) NSMutableDictionary* pluginObjects;
@property (nonatomic, readonly, strong) NSDictionary* pluginsMap;
Expand All @@ -60,6 +49,38 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly, strong) id <CDVCommandDelegate> commandDelegate;


/**
A boolean value indicating whether to show the splash screen while the webview
is initially loading.
The default value is `YES`.
This can be set in the storyboard file as a view controller attribute.
*/
@property (nonatomic) IBInspectable BOOL showInitialSplashScreen;

/**
The color drawn behind the web content.
This is used as the background color for the web view behind any HTML content
and during loading before web content has been rendered. The default value is
the system background color.
This can be set in the storyboard file as a view controller attribute.
*/
@property (nonatomic, null_resettable, copy) IBInspectable UIColor *backgroundColor;

/**
The color drawn behind the splash screen content.
This is used as the background color for the splash screen while the web
content is loading. If a page background color has been specified, that will
be used as the default value, otherwise the system background color is used.
This can be set in the storyboard file as a view controller attribute.
*/
@property (nonatomic, null_resettable, copy) IBInspectable UIColor *splashBackgroundColor;

- (UIView*)newCordovaViewWithFrame:(CGRect)bounds;

- (nullable NSString*)appURLScheme;
Expand All @@ -71,6 +92,12 @@ NS_ASSUME_NONNULL_BEGIN

- (void)parseSettingsWithParser:(NSObject <NSXMLParserDelegate>*)delegate;

/**
Toggles the display of the splash screen overtop of the web view.
- Parameters:
- visible: Whether to make the splash screen visible or not.
*/
- (void)showLaunchScreen:(BOOL)visible;

NS_ASSUME_NONNULL_END
Expand Down
6 changes: 0 additions & 6 deletions templates/project/__PROJECT_NAME__/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,4 @@ Licensed to the Apache Software Foundation (ASF) under one

@implementation MainViewController

- (void)viewDidLoad
{
[super viewDidLoad];
[self.launchView setAlpha:1];
}

@end

0 comments on commit 9b635b2

Please sign in to comment.