Skip to content

Commit

Permalink
Major cleanup and refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Amaral committed Aug 12, 2016
1 parent 0c00fe3 commit 60840fc
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 248 deletions.
44 changes: 44 additions & 0 deletions Demo/Demo Files/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
else {
self.window.rootViewController = [self generateStandardOnboardingVC];
// self.window.rootViewController = [self generateMovieOnboardingVC];
// self.window.rootViewController = [self generateContentVideoOnboardingVC];

// __weak typeof(self) weakSelf = self;
//
Expand Down Expand Up @@ -139,4 +140,47 @@ - (OnboardingViewController *)generateMovieOnboardingVC {
return onboardingVC;
}

- (OnboardingViewController *)generateContentVideoOnboardingVC {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"sun" ofType:@"mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];

OnboardingContentViewController *firstPage = [OnboardingContentViewController contentWithTitle:@"Everything Under The Sun" body:@"The temperature of the photosphere is over 10,000°F." videoURL:movieURL buttonText:nil action:nil];
firstPage.topPadding = -15;
firstPage.underTitlePadding = 160;
firstPage.titleLabel.textColor = [UIColor colorWithRed:239/255.0 green:88/255.0 blue:35/255.0 alpha:1.0];
firstPage.titleLabel.font = [UIFont fontWithName:@"SFOuterLimitsUpright" size:38.0];
firstPage.bodyLabel.textColor = [UIColor colorWithRed:239/255.0 green:88/255.0 blue:35/255.0 alpha:1.0];
firstPage.bodyLabel.font = [UIFont fontWithName:@"NasalizationRg-Regular" size:18.0];

OnboardingContentViewController *secondPage = [OnboardingContentViewController contentWithTitle:@"Every Second" body:@"600 million tons of protons are converted into helium atoms." videoURL:movieURL buttonText:nil action:nil];
secondPage.titleLabel.font = [UIFont fontWithName:@"SFOuterLimitsUpright" size:38.0];
secondPage.underTitlePadding = 170;
secondPage.topPadding = 0;
secondPage.titleLabel.textColor = [UIColor colorWithRed:251/255.0 green:176/255.0 blue:59/255.0 alpha:1.0];
secondPage.bodyLabel.textColor = [UIColor colorWithRed:251/255.0 green:176/255.0 blue:59/255.0 alpha:1.0];
secondPage.bodyLabel.font = [UIFont fontWithName:@"NasalizationRg-Regular" size:18.0];

OnboardingContentViewController *thirdPage = [OnboardingContentViewController contentWithTitle:@"We're All Star Stuff" body:@"Our very bodies consist of the same chemical elements found in the most distant nebulae, and our activities are guided by the same universal rules." videoURL:movieURL buttonText:nil action:^{
[self handleOnboardingCompletion];
}];
thirdPage.topPadding = 10;
thirdPage.underTitlePadding = 160;
thirdPage.bottomPadding = -10;
thirdPage.titleLabel.font = [UIFont fontWithName:@"SFOuterLimitsUpright" size:38.0];
thirdPage.titleLabel.textColor = [UIColor colorWithRed:58/255.0 green:105/255.0 blue:136/255.0 alpha:1.0];
thirdPage.bodyLabel.textColor = [UIColor colorWithRed:58/255.0 green:105/255.0 blue:136/255.0 alpha:1.0];
thirdPage.bodyLabel.font = [UIFont fontWithName:@"NasalizationRg-Regular" size:15.0];
[thirdPage.actionButton setTitleColor:[UIColor colorWithRed:239/255.0 green:88/255.0 blue:35/255.0 alpha:1.0] forState:UIControlStateNormal];
thirdPage.actionButton.titleLabel.font = [UIFont fontWithName:@"SpaceAge" size:17.0];

OnboardingViewController *onboardingVC = [[OnboardingViewController alloc] initWithBackgroundImage:nil contents:@[firstPage, secondPage, thirdPage]];
onboardingVC.view.backgroundColor = [UIColor blackColor];
onboardingVC.shouldFadeTransitions = YES;
onboardingVC.shouldMaskBackground = NO;
onboardingVC.pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:239/255.0 green:88/255.0 blue:35/255.0 alpha:1.0];
onboardingVC.pageControl.pageIndicatorTintColor = [UIColor whiteColor];
return onboardingVC;
}

@end
26 changes: 0 additions & 26 deletions Demo/Demo Files/OnboardTests/OnboardTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ @interface OnboardTests : XCTestCase

@interface OnboardingContentViewController (Testing)

@property (nonatomic, strong) MPMoviePlayerController *moviePlayerController;
@property (nonatomic, strong) NSURL *videoURL;
- (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL;

@end

@implementation OnboardTests
Expand All @@ -47,28 +43,6 @@ - (void)testContentViewControllers {
}
}

- (void)testContentViewControllerWithVideo {
// This tests that when we create an onboarding content view controller with a video url

OnboardingContentViewController *contentVC = [[OnboardingContentViewController alloc] initWithTitle:@"T1" body:@"B1" videoURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sun" ofType:@"mp4"]] buttonText:nil action:nil];
[contentVC view];//trick to call viewDidLoad

XCTAssertNotNil(contentVC.moviePlayerController, @"The moviePlayerController should exist.");
XCTAssertNotNil(contentVC.videoURL, @"The videoURL should exist.");
}

- (void)testGetThumbnailImageForVideo {
// This tests that when we try to get the video thumbnail on an onboarding content view controller with a video url

OnboardingContentViewController *contentVC = [[OnboardingContentViewController alloc] initWithTitle:@"T1" body:@"B1" videoURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sun" ofType:@"mp4"]] buttonText:nil action:nil];

UIImage *thumbnail = [contentVC thumbnailImageForVideo:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sun" ofType:@"mp4"]]];
UIImage *nilThumbnail = [contentVC thumbnailImageForVideo:nil];

XCTAssertNotNil(thumbnail, @"The video thumbnail should exist.");
XCTAssertNil(nilThumbnail, @"The video thumbnail should not exist for a nil URL.");
}

- (void)testDefaultValues {
// This tests that the default values on the onboarding view controller are set properly.
OnboardingViewController *onboardingVC = [[OnboardingViewController alloc] initWithBackgroundImage:nil contents:nil];
Expand Down
24 changes: 7 additions & 17 deletions Source/OnboardingContentViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import <UIKit/UIKit.h>
@import MediaPlayer;
@import AVKit;

@class OnboardingViewController;
@class OnboardingContentViewController;
Expand Down Expand Up @@ -133,6 +133,12 @@ typedef void (^action_callback)(OnboardingViewController *onboardController);
@property (nonatomic, copy) dispatch_block_t viewDidDisappearBlock;


/**
* @brief The movie player controller used to play background movies.
*/
@property (nonatomic, strong) AVPlayerViewController *moviePlayerController;


/**
* @brief Convenience class initializer for creating an onboarding content view controller.
* @return An instance of OnboardingViewController with the provided information.
Expand Down Expand Up @@ -194,20 +200,4 @@ typedef void (^action_callback)(OnboardingViewController *onboardController);
*/
- (void)updateAlphas:(CGFloat)newAlpha;


// The following properties are all deprecated, and will be removed in the next release of Onboard.
//
//
@property (nonatomic, strong) UIColor *titleTextColor __attribute__((deprecated("Set titleLabel.textColor instead. This property will be removed in the next update.")));
@property (nonatomic, strong) NSString *titleFontName __attribute__((deprecated("Set titleLabel.font instead. This property will be removed in the next update.")));
@property (nonatomic) CGFloat titleFontSize __attribute__((deprecated("Set titleLabel.font instead. This property will be removed in the next update.")));

@property (nonatomic, strong) UIColor *bodyTextColor __attribute__((deprecated("Set bodyLabel.textColor instead. This property will be removed in the next update.")));
@property (nonatomic, strong) NSString *bodyFontName __attribute__((deprecated("Set bodyLabel.font instead. This property will be removed in the next update.")));
@property (nonatomic) CGFloat bodyFontSize __attribute__((deprecated("Set bodyLabel.font instead. This property will be removed in the next update.")));

@property (nonatomic, strong) UIColor *buttonTextColor __attribute__((deprecated("Modify the actionButton property directly. This property will be removed in the next update.")));
@property (nonatomic, strong) NSString *buttonFontName __attribute__((deprecated("Modify the actionButton property directly. This property will be removed in the next update.")));
@property (nonatomic) CGFloat buttonFontSize __attribute__((deprecated("Modify the actionButton property directly. This property will be removed in the next update.")));

@end
104 changes: 40 additions & 64 deletions Source/OnboardingContentViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import "OnboardingContentViewController.h"
#import "OnboardingViewController.h"
#import <AVFoundation/AVFoundation.h>
@import AVFoundation;

static NSString * const kDefaultOnboardingFont = @"Helvetica-Light";

Expand All @@ -34,10 +34,11 @@

@interface OnboardingContentViewController ()

@property (nonatomic, strong) UIImageView *thumbnailImageView;
@property (nonatomic, strong) MPMoviePlayerController *moviePlayerController;
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) NSURL *videoURL;

@property (nonatomic) BOOL wasPreviouslyVisible;

@end

@implementation OnboardingContentViewController
Expand All @@ -46,6 +47,9 @@ - (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark - Initializers

+ (instancetype)contentWithTitle:(NSString *)title body:(NSString *)body image:(UIImage *)image buttonText:(NSString *)buttonText action:(dispatch_block_t)action {
return [[self alloc] initWithTitle:title body:body image:image buttonText:buttonText action:action];
}
Expand Down Expand Up @@ -123,18 +127,8 @@ - (instancetype)initWithTitle:(NSString *)title body:(NSString *)body image:(UII
self.buttonActionHandler = actionBlock ?: ^(OnboardingViewController *controller){};

// Movie player
if (videoURL) {
self.videoURL = videoURL;
self.videoURL = videoURL;

self.moviePlayerController = [MPMoviePlayerController new];
self.moviePlayerController.contentURL = self.videoURL;
self.moviePlayerController.repeatMode = MPMovieRepeatModeOne;
self.moviePlayerController.controlStyle = MPMovieControlStyleNone;

self.thumbnailImageView = [[UIImageView alloc] initWithImage:[self thumbnailImageForVideo:self.videoURL]];
self.thumbnailImageView.contentMode = UIViewContentModeScaleAspectFit;
}

// Auto-navigation
self.movesToNextViewController = NO;

Expand All @@ -160,14 +154,18 @@ - (instancetype)initWithTitle:(NSString *)title body:(NSString *)body image:(UII
- (void)viewDidLoad {
[super viewDidLoad];

// Handle when the app enters the foreground.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAppEnteredForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAppEnteredForeground) name:UIApplicationDidBecomeActiveNotification object:nil];

self.view.backgroundColor = [UIColor clearColor];

// Add all our subviews
if (self.videoURL != nil) {
[self.moviePlayerController.backgroundView addSubview:self.thumbnailImageView];
if (self.videoURL) {
self.player = [[AVPlayer alloc] initWithURL:self.videoURL];

self.moviePlayerController = [AVPlayerViewController new];
self.moviePlayerController.player = self.player;
self.moviePlayerController.showsPlaybackControls = NO;

[self.view addSubview:self.moviePlayerController.view];
}

Expand All @@ -185,13 +183,20 @@ - (void)viewWillAppear:(BOOL)animated {
if (self.delegate) {
[self.delegate setNextPage:self];
}

// If we have a video URL, start playing
if (self.videoURL) {
[self.player play];
}

// Call our view will appear block
if (self.viewWillAppearBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
self.viewWillAppearBlock();
});
}

self.wasPreviouslyVisible = YES;
}

- (void)viewDidAppear:(BOOL)animated {
Expand All @@ -209,12 +214,6 @@ - (void)viewDidAppear:(BOOL)animated {
self.viewDidAppearBlock();
});
}

// If we have a video, start playing
if (self.moviePlayerController.playbackState != MPMoviePlaybackStatePlaying) {
self.moviePlayerController.currentPlaybackTime = 0.0;
[self.moviePlayerController play];
}
}

- (void)viewWillDisappear:(BOOL)animated {
Expand All @@ -226,6 +225,8 @@ - (void)viewWillDisappear:(BOOL)animated {
self.viewWillDisappearBlock();
});
}

self.wasPreviouslyVisible = NO;
}

- (void)viewDidDisappear:(BOOL)animated {
Expand All @@ -237,10 +238,21 @@ - (void)viewDidDisappear:(BOOL)animated {
self.viewDidDisappearBlock();
});
}

// If we have a video, stop playing
if (self.moviePlayerController.playbackState != MPMoviePlaybackStateStopped) {
[self.moviePlayerController stop];

// Pause our video if we have one.
if ((self.player.rate != 0.0) && !self.player.error) {
[self.player pause];
}
}


#pragma mark - App life cycle

- (void)handleAppEnteredForeground {
// If we have a video URL and this view controller was previously on screen
// restart it as it will be paused when the app enters the foreground.
if (self.videoURL && self.wasPreviouslyVisible) {
[self.player play];
}
}

Expand All @@ -254,10 +266,6 @@ - (void)viewWillLayoutSubviews {
self.moviePlayerController.view.frame = self.view.frame;
}

if (self.thumbnailImageView) {
self.thumbnailImageView.frame = self.view.frame;
}

CGFloat viewWidth = CGRectGetWidth(self.view.frame);
CGFloat contentWidth = viewWidth * kContentWidthMultiplier;
CGFloat xPadding = (viewWidth - contentWidth) / 2.0;
Expand All @@ -280,16 +288,6 @@ - (void)viewWillLayoutSubviews {
}


#pragma mark - App state

- (void)handleAppEnteredForeground {
//If the movie player is paused, as it does by default when backgrounded, start playing again.
if (self.moviePlayerController.playbackState == MPMoviePlaybackStatePaused) {
[self.moviePlayerController play];
}
}


#pragma mark - Transition alpha

- (void)updateAlphas:(CGFloat)newAlpha {
Expand All @@ -315,26 +313,4 @@ - (void)handleButtonPressed {
}
}


#pragma mark - Utils

- (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL {

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetImageGenerator *assetIG = [[AVAssetImageGenerator alloc] initWithAsset:asset];
assetIG.appliesPreferredTrackTransform = YES;
assetIG.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;

NSError *error = nil;
CGImageRef thumbnailImageRef = [assetIG copyCGImageAtTime:CMTimeMake(0, 60) actualTime:NULL error:&error];

if (!error) {
UIImage *thumbnailImage = [[UIImage alloc] initWithCGImage:thumbnailImageRef];
return thumbnailImage;
} else {
NSLog(@"thumbnailImageGenerationError %@", error);
return nil;
}
}

@end
29 changes: 0 additions & 29 deletions Source/OnboardingViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,33 +135,4 @@
*/
- (void)moveNextPage;


// The following properties are all deprecated, and will be removed in the next release of Onboard.
//
//
@property (nonatomic) BOOL hidePageControl __attribute__((deprecated("Modify the pageControl property directly. This property will be removed in the next update.")));

@property (nonatomic) CGFloat iconSize __attribute__((deprecated("Modify the content view controller's iconSize directly. This property will be removed in the next update.")));
@property (nonatomic) CGFloat iconHeight __attribute__((deprecated("Modify the content view controller's iconHeight directly. This property will be removed in the next update.")));
@property (nonatomic) CGFloat iconWidth __attribute__((deprecated("Modify the content view controller's iconWidth directly. This property will be removed in the next update.")));

@property (nonatomic, strong) UIColor *titleTextColor __attribute__((deprecated("Modify the content view controller's titleLabel directly. This property will be removed in the next update.")));
@property (nonatomic, strong) NSString *titleFontName __attribute__((deprecated("Modify the content view controller's titleLabel directly. This property will be removed in the next update.")));
@property (nonatomic) CGFloat titleFontSize __attribute__((deprecated("Modify the content view controller's titleLabel directly. This property will be removed in the next update.")));

@property (nonatomic, strong) UIColor *bodyTextColor __attribute__((deprecated("Modify the content view controller's bodyLabel directly. This property will be removed in the next update.")));
@property (nonatomic, strong) NSString *bodyFontName __attribute__((deprecated("Modify the content view controller's bodyLabel directly. This property will be removed in the next update.")));
@property (nonatomic) CGFloat bodyFontSize __attribute__((deprecated("Modify the content view controller's bodyLabel directly.")));

@property (nonatomic, strong) UIColor *buttonTextColor __attribute__((deprecated("Modify the content view controller's actionButton directly. This property will be removed in the next update.")));
@property (nonatomic, strong) NSString *buttonFontName __attribute__((deprecated("Modify the content view controller's actionButton directly. This property will be removed in the next update.")));
@property (nonatomic) CGFloat buttonFontSize __attribute__((deprecated("Modify the content view controller's actionButton directly. This property will be removed in the next update.")));

@property (nonatomic, strong) NSString *fontName __attribute__((deprecated("Modify the content view controller's labels directly. This property will be removed in the next update.")));

@property (nonatomic) CGFloat topPadding __attribute__((deprecated("Modify the content view controller's topPadding directly. This property will be removed in the next update.")));
@property (nonatomic) CGFloat underIconPadding __attribute__((deprecated("Modify the content view controller's underIconPadding directly. This property will be removed in the next update.")));
@property (nonatomic) CGFloat underTitlePadding __attribute__((deprecated("Modify the content view controller's underTitlePadding directly. This property will be removed in the next update.")));
@property (nonatomic) CGFloat bottomPadding __attribute__((deprecated("Modify the content view controller's bottomPadding directly. This property will be removed in the next update.")));

@end
Loading

0 comments on commit 60840fc

Please sign in to comment.