Skip to content

Instantly share code, notes, and snippets.

@EyMaddis
Created February 5, 2021 18:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EyMaddis/273d365e78dfff112a3eed07ab07c55b to your computer and use it in GitHub Desktop.
Save EyMaddis/273d365e78dfff112a3eed07ab07c55b to your computer and use it in GitHub Desktop.
React Native Google Cast MiniPlayer Base Implementation
function ExampleComp() {
// height is necessary otherwise it will be 0
return <View style={{ height: 60 }}>
<RNGoogleCastMiniPlayer style={{ flex: 1 }} />
</View>
}
#import <React/RCTView.h>
@interface RNGoogleCastMiniPlayer : RCTView
@end
#import <GoogleCast/GoogleCast.h>
#import "RNGoogleCastMiniPlayer.h"
#import "RNGoogleCastMiniPlayerControllerViewController.h"
#import <React/RCTView.h>
@implementation RNGoogleCastMiniPlayer : RCTView
{
RNGoogleCastMiniPlayerControllerViewController *_viewController;
}
- (instancetype)init
{
self = [super init];
if (self) {
_viewController = [[RNGoogleCastMiniPlayerControllerViewController alloc] init];
_viewController.view.frame = self.bounds;
[self addSubview:_viewController.view];
}
return self;
}
-(void)layoutSubviews {
// GCKCastContext *castContext = [GCKCastContext sharedInstance];
// GCKUIMiniMediaControlsViewController *miniPlayer = [castContext createMiniMediaControlsViiewController];
// _miniplayer = [[GCKUIMiniMediaControlsViewController alloc] initWithFrame:self.bounds];
_viewController.view.frame = self.bounds;
}
@end
import { requireNativeComponent, View } from 'react-native'
const RNGoogleCastMiniPlayer: typeof View = requireNativeComponent(
'RNGoogleCastMiniPlayer'
)
if (!RNGoogleCastMiniPlayer) {
throw new Error('RNGoogleCastMiniPlayer not accessible')
}
export { RNGoogleCastMiniPlayer }
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface RNGoogleCastMiniPlayerControllerViewController : UIViewController
@end
NS_ASSUME_NONNULL_END
#import "RNGoogleCastMiniPlayerControllerViewController.h"
#import <GoogleCast/GoogleCast.h>
static const NSTimeInterval kCastControlBarsAnimationDuration = 0.20;
@interface RNGoogleCastMiniPlayerControllerViewController () <GCKUIMiniMediaControlsViewControllerDelegate> {
__weak IBOutlet UIView *_miniMediaControlsContainerView;
__weak IBOutlet NSLayoutConstraint *_miniMediaControlsHeightConstraint;
GCKUIMiniMediaControlsViewController *_miniMediaControlsViewController;
}
@property(nonatomic, weak, readwrite) UINavigationController *navigationController;
@property(nonatomic, assign, readwrite) BOOL miniMediaControlsViewEnabled;
@property(nonatomic, assign, readwrite) BOOL miniMediaControlsItemEnabled;
@end
@implementation RNGoogleCastMiniPlayerControllerViewController
- (void)viewDidLoad {
[super viewDidLoad];
// this line in missing in the docs from Google Cast
_miniMediaControlsContainerView = self.view;
self.miniMediaControlsViewEnabled = YES;
GCKCastContext *castContext = [GCKCastContext sharedInstance];
_miniMediaControlsViewController =
[castContext createMiniMediaControlsViewController];
_miniMediaControlsViewController.delegate = self;
[self updateControlBarsVisibility];
[self installViewController:_miniMediaControlsViewController
inContainerView:_miniMediaControlsContainerView];
}
- (void)setMiniMediaControlsViewEnabled:(BOOL)miniMediaControlsViewEnabled {
_miniMediaControlsViewEnabled = miniMediaControlsViewEnabled;
if (self.isViewLoaded) {
[self updateControlBarsVisibility];
}
}
- (void)updateControlBarsVisibility {
if (self.miniMediaControlsViewEnabled &&
_miniMediaControlsViewController.active) {
_miniMediaControlsHeightConstraint.constant =
_miniMediaControlsViewController.minHeight;
[self.view bringSubviewToFront:_miniMediaControlsContainerView];
} else {
_miniMediaControlsHeightConstraint.constant = 0;
}
[UIView animateWithDuration:kCastControlBarsAnimationDuration
animations:^{
[self.view layoutIfNeeded];
}];
[self.view setNeedsLayout];
}
- (void)installViewController:(UIViewController *)viewController
inContainerView:(UIView *)containerView {
if (viewController) {
[self addChildViewController:viewController];
viewController.view.frame = containerView.bounds;
[containerView addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
}
}
- (void)uninstallViewController:(UIViewController *)viewController {
[viewController willMoveToParentViewController:nil];
[viewController.view removeFromSuperview];
[viewController removeFromParentViewController];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"NavigationVCEmbedSegue"]) {
self.navigationController =
(UINavigationController *)segue.destinationViewController;
}
}
- (void)miniMediaControlsViewController:(nonnull GCKUIMiniMediaControlsViewController *)miniMediaControlsViewController shouldAppear:(BOOL)shouldAppear {
[self updateControlBarsVisibility];
}
@end
#import "RNGoogleCastMiniPlayer.h"
#import <GoogleCast/GoogleCast.h>
#import <GoogleCast/GCKCastContext.h>
#import <React/RCTViewManager.h>
@interface RNGoogleCastMiniPlayerManager : RCTViewManager
@end
@implementation RNGoogleCastMiniPlayerManager
{
GCKUIMiniMediaControlsViewController *_miniplayer;
}
RCT_EXPORT_MODULE(RNGoogleCastMiniPlayer)
- (UIView *)view {
return [[RNGoogleCastMiniPlayer alloc] init];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment