Repository URL to install this package:
//
// DESplashScreenHandler.m
// HelloCordova
//
// Created by Marco Bonati on 23/02/2017.
//
//
#import <Foundation/Foundation.h>
#import "DESplashScreenManager.h"
#import "DESplashScreenProvider.h"
#import "DEDefaultSplashScreenProvider.h"
#import <Cordova/CDVViewController.h>
#import <DEUtility/DEAppPreferencesUtils.h>
#import "DESplashScreenDefines.h"
#import <DEUtility/DELogger.h>
@interface DESplashScreenManager()
{
NSDictionary* _appConfig;
BOOL _isFirstTime;
}
@end
@interface DESplashScreenManager ()
{
id<DESplashScreenProvider> _splashScreenProvider;
UIView* _splashView;
CDVViewController* _mainViewController;
BOOL _splashVisible;
}
@end
@implementation DESplashScreenManager
+ (id)sharedInstance {
static DESplashScreenManager *_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
- (id)init
{
DDLogDebug(@"Initializing Splash Screen Manager...");
self = [super init];
_appConfig = [[DEAppPreferencesUtils sharedInstance]appConfiguration];
_isFirstTime = YES;
return self;
}
- (double)fadeDuration
{
double fadeDuration = [[DEAppPreferencesUtils sharedInstance] doublePreferenceForKey:DESPLASH_CFG_PARAM_FADE_DURATION withDefaultValue:0.4f];
return fadeDuration;
}
- (void)installWithProvider:(id<DESplashScreenProvider>)splashProvider onViewController:(CDVViewController*)mainViewController
{
DDLogDebug(@"installWithProvidercalled for %@", splashProvider);
_splashVisible = NO;
_mainViewController = mainViewController;
_splashScreenProvider = splashProvider;
BOOL autostartEnabled = [[DEAppPreferencesUtils sharedInstance] booleanPreferenceForKey:DESPLASH_CFG_PARAM_ENABLED withDefaultValue:YES];
if (autostartEnabled){
[self showSplash];
}
}
- (void)installDefault:(CDVViewController*)mainViewController
{
[self installWithProvider:[[DEDefaultSplashScreenProvider alloc]init:mainViewController] onViewController:mainViewController];
}
- (void)showSplash
{
DDLogDebug(@"showSplash called");
if (_splashView==nil){
_splashView = [_splashScreenProvider createSplashScreenView];
if (!_isFirstTime){
[_splashView setAlpha:0.0f];
} else {
[_splashView setAlpha:1.0f];
}
}
UIView* parentView = _mainViewController.view;
parentView.userInteractionEnabled = NO; // disable user interaction while splashscreen is shown
/*
//parentView.autoresizesSubviews = YES;
_splashView.frame = parentView.frame;
_splashView.bounds = parentView.bounds;
*/
[parentView addSubview:_splashView];
if (!_isFirstTime){
[UIView transitionWithView:_splashView
duration:[self fadeDuration]
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[_splashView setAlpha:1.0f];
}
completion:NULL];
} else {
//the first time shows without animation in
[_splashView setAlpha:1.0f];
}
[parentView addObserver:self forKeyPath:@"frame" options:0 context:nil];
[parentView addObserver:self forKeyPath:@"bounds" options:0 context:nil];
_isFirstTime = NO;
_splashVisible = YES;
DDLogDebug(@"showSplash done");
}
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
if (_splashView!=nil){
// [_splashView updateUI];
}
}
- (void)hideSplash
{
DDLogDebug(@"hideSplash called");
if (!_splashVisible){
return ;
}
BOOL autodestroy = [[DEAppPreferencesUtils sharedInstance] booleanPreferenceForKey:DESPLASH_CFG_PARAM_AUTODESTROY withDefaultValue:YES];
if (autodestroy){
[self destroySplash];
return;
}
if (_splashView!=nil){
[self doFadeOut:_splashView andRemove:NO];
}
_mainViewController.view.userInteractionEnabled = YES;
_splashVisible = NO;
DDLogDebug(@"hideSplash done");
}
- (void)destroySplash
{
DDLogDebug(@"destroySplash called");
@try {
[_mainViewController.view removeObserver:self forKeyPath:@"frame"];
[_mainViewController.view removeObserver:self forKeyPath:@"bounds"];
}
@catch (NSException *exception) {
// When reloading the page from a remotely connected Safari, there
// are no observers, so the removeObserver method throws an exception,
// that we can safely ignore.
// Alternatively we can check whether there are observers before calling removeObserver
}
_mainViewController.view.userInteractionEnabled = YES;
if (_splashView!=nil){
[self doFadeOut:_splashView andRemove:YES];
}
_splashView = nil;
_splashVisible = NO;
DDLogDebug(@"destroySplash done");
}
- (void)doFadeOut:(UIView*)view andRemove:(BOOL)doRemove
{
[UIView transitionWithView:view
duration:[self fadeDuration]
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[view setAlpha:0.0f];
}
completion:^(BOOL finished){
if (doRemove){
[view removeFromSuperview];
}
}];
}
- (BOOL)isSplashVisible
{
return _splashVisible;
}
@end