Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 4602e4e

Browse files
committed
wip fixing pr comments
1 parent c69e18b commit 4602e4e

7 files changed

Lines changed: 117 additions & 43 deletions

File tree

AsyncDisplayKit/ASButtonNode.mm

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,6 @@ - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
212212
return stack;
213213
}
214214

215-
- (void)layoutDidFinish
216-
{
217-
218-
}
219-
220215
- (void)layout
221216
{
222217
[super layout];

AsyncDisplayKit/ASVideoNode.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ typedef NS_ENUM(NSUInteger, ASVideoGravity) {
77
ASVideoGravityResize
88
};
99

10-
@interface ASVideoNode : ASDisplayNode<_ASDisplayLayerDelegate>
10+
@interface ASVideoNode : ASControlNode<_ASDisplayLayerDelegate>
1111
@property (atomic, strong, readwrite) AVAsset *asset;
12-
@property (nonatomic, assign, readwrite) BOOL shouldAutoPlay;
12+
@property (nonatomic, assign, readwrite) BOOL shouldAutoplay;
1313
@property (atomic) ASVideoGravity gravity;
1414
@property (atomic) BOOL autorepeat;
1515
@property (atomic) ASButtonNode *playButton;
16+
@property (atomic) AVPlayer *player;
1617

1718
- (void)play;
1819
- (void)pause;
@@ -22,9 +23,9 @@ typedef NS_ENUM(NSUInteger, ASVideoGravity) {
2223
@protocol ASVideoNodeDelegate <NSObject>
2324
@end
2425

25-
@protocol ASVideoNodeDatasource <NSObject>
26+
@protocol ASVideoNodeDataSource <NSObject>
2627
@optional
27-
- (ASDisplayNode *)playButtonForVideoNode:(ASVideoNode *) videoNode;
28+
- (ASButtonNode *)playButtonForVideoNode:(ASVideoNode *)videoNode;
2829
- (UIImage *)thumbnailForVideoNode:(ASVideoNode *) videoNode;
2930
- (NSURL *)thumbnailURLForVideoNode:(ASVideoNode *)videoNode;
3031
@end

AsyncDisplayKit/ASVideoNode.mm

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
#import "ASVideoNode.h"
44

5-
@interface ASVideoNode () {
5+
@interface ASVideoNode ()
6+
{
67
ASDN::RecursiveMutex _lock;
78

8-
__weak id<ASVideoNodeDatasource> _datasource;
9+
__weak id<ASVideoNodeDataSource> _dataSource;
910

1011
BOOL _shouldBePlaying;
1112
AVAsset *_asset;
@@ -23,7 +24,8 @@ - (void)setInterfaceState:(ASInterfaceState)newState;
2324

2425
@implementation ASVideoNode
2526

26-
- (instancetype)init {
27+
- (instancetype)init
28+
{
2729
if (!(self = [super init])) { return nil; }
2830

2931
_playerNode = [[ASDisplayNode alloc] initWithLayerBlock:^CALayer *{
@@ -35,11 +37,14 @@ - (instancetype)init {
3537

3638
self.gravity = ASVideoGravityResizeAspect;
3739

38-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(restartVideo:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
40+
[self addTarget:self action:@selector(tapped) forControlEvents:ASControlNodeEventTouchUpInside];
41+
42+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didPlayToEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
3943

4044
return self;
4145
}
4246

47+
// FIXME: Adopt interfaceStateDidChange API
4348
- (void)setInterfaceState:(ASInterfaceState)newState
4449
{
4550
[super setInterfaceState:newState];
@@ -51,9 +56,6 @@ - (void)setInterfaceState:(ASInterfaceState)newState
5156
if (_shouldBePlaying) {
5257
[self play];
5358
}
54-
if (_spinner) {
55-
[self addSubnode:_spinner];
56-
}
5759
}
5860
}
5961

@@ -71,9 +73,9 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
7173
}
7274
}
7375

74-
- (void)restartVideo:(NSNotification *)notification
76+
- (void)didPlayToEnd:(NSNotification *)notification
7577
{
76-
if ( [[[notification object] asset] isEqual:_asset]) {
78+
if (ASObjectIsEqual([[notification object] asset], _asset)) {
7779
[[((AVPlayerLayer *)_playerNode.layer) player] seekToTime:CMTimeMakeWithSeconds(0, 1)];
7880

7981
if (_autorepeat) {
@@ -84,18 +86,12 @@ - (void)restartVideo:(NSNotification *)notification
8486
}
8587
}
8688

87-
- (void)layoutDidFinish
89+
- (void)layout
8890
{
91+
[super layout];
8992
_playerNode.frame = self.bounds;
9093
}
9194

92-
- (void)didLoad {
93-
[super didLoad];
94-
95-
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];
96-
[self.view addGestureRecognizer:tap];
97-
}
98-
9995
- (void)tapped
10096
{
10197
if (_shouldBePlaying) {
@@ -134,7 +130,7 @@ - (void)fetchData
134130
}
135131

136132
}
137-
if (_shouldAutoPlay) {
133+
if (_shouldAutoplay) {
138134
[self play];
139135
}
140136
}
@@ -189,7 +185,8 @@ - (AVAsset *)asset
189185
return _asset;
190186
}
191187

192-
- (void)setGravity:(ASVideoGravity)gravity {
188+
- (void)setGravity:(ASVideoGravity)gravity
189+
{
193190
ASDN::MutexLocker l(_lock);
194191

195192
switch (gravity) {
@@ -208,50 +205,56 @@ - (void)setGravity:(ASVideoGravity)gravity {
208205
}
209206
}
210207

211-
- (ASVideoGravity)gravity;
208+
- (ASVideoGravity)gravity
212209
{
213210
ASDN::MutexLocker l(_lock);
214211

215-
if ([((AVPlayerLayer *)_playerNode.layer).contentsGravity isEqualToString:AVLayerVideoGravityResize]) {
212+
if (ASObjectIsEqual(((AVPlayerLayer *)_playerNode.layer).contentsGravity, AVLayerVideoGravityResize)) {
216213
return ASVideoGravityResize;
217214
}
218-
if ([((AVPlayerLayer *)_playerNode.layer).contentsGravity isEqualToString:AVLayerVideoGravityResizeAspectFill]) {
215+
if (ASObjectIsEqual(((AVPlayerLayer *)_playerNode.layer).contentsGravity, AVLayerVideoGravityResizeAspectFill)) {
219216
return ASVideoGravityResizeAspectFill;
220217
}
221218

222219
return ASVideoGravityResizeAspect;
223220
}
224221

225-
- (void)play;
222+
- (void)play
226223
{
227224
ASDN::MutexLocker l(_lock);
228225

229-
[[((AVPlayerLayer *)_playerNode.layer) player] play];
230-
_shouldBePlaying = YES;
231-
_playButton.alpha = 0.0;
232-
if ([self ready] && ![self.subnodes containsObject:_spinner]) {
226+
if (!_spinner) {
233227
_spinner = [[ASDisplayNode alloc] initWithViewBlock:^UIView *{
234228
UIActivityIndicatorView *spinnnerView = [[UIActivityIndicatorView alloc] initWithFrame:_playButton.frame];
235229
spinnnerView.color = [UIColor whiteColor];
236230
[spinnnerView startAnimating];
237231

238232
return spinnnerView;
239233
}];
240-
234+
}
235+
236+
if (![self ready]) {
241237
[self addSubnode:_spinner];
242238
}
239+
240+
[[((AVPlayerLayer *)_playerNode.layer) player] play];
241+
_shouldBePlaying = YES;
242+
_playButton.alpha = 0.0;
243+
// if ([self ready] && ![self.subnodes containsObject:_spinner]) {
244+
// }
243245
}
244246

245-
- (BOOL)ready;
247+
- (BOOL)ready
246248
{
247-
return [((AVPlayerLayer *)_playerNode.layer) player].currentItem.status != AVPlayerItemStatusReadyToPlay;
249+
return [((AVPlayerLayer *)_playerNode.layer) player].currentItem.status == AVPlayerItemStatusReadyToPlay;
248250
}
249251

250-
- (void)pause;
252+
- (void)pause
251253
{
252254
ASDN::MutexLocker l(_lock);
253255

254256
[[((AVPlayerLayer *)_playerNode.layer) player] pause];
257+
[((UIActivityIndicatorView *)_spinner.view) stopAnimating];
255258
_shouldBePlaying = NO;
256259
_playButton.alpha = 1.0;
257260
}
@@ -261,6 +264,16 @@ - (AVPlayerItem *)currentItem
261264
return _currentItem;
262265
}
263266

267+
- (ASDisplayNode *)spinner
268+
{
269+
return _spinner;
270+
}
271+
272+
- (AVPlayerItem *)curentItem
273+
{
274+
return _currentItem;
275+
}
276+
264277
- (void)dealloc
265278
{
266279
[[NSNotificationCenter defaultCenter] removeObserver:self];

AsyncDisplayKitTests/ASVideoNodeTests.m

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ @interface ASVideoNodeTests : XCTestCase
1616
@interface ASVideoNode ()
1717
@property (atomic, readonly) AVPlayerItem *currentItem;
1818
@property (atomic) ASInterfaceState interfaceState;
19+
@property (atomic) ASDisplayNode *spinner;
20+
@end
21+
22+
@interface AVPlayerItem ()
23+
@property (nonatomic) AVPlayerItemStatus status;
1924
@end
2025

2126
@implementation ASVideoNodeTests
2227

23-
- (void)testVideoNodeReplacesAVPlayerItemWhenNewURLIsSet {
28+
- (void)testVideoNodeReplacesAVPlayerItemWhenNewURLIsSet
29+
{
2430
ASVideoNode *videoNode = [[ASVideoNode alloc] init];
2531
videoNode.interfaceState = ASInterfaceStateFetchData;
2632
videoNode.asset = [AVAsset assetWithURL:[NSURL URLWithString:@"firstURL"]];
@@ -33,7 +39,8 @@ - (void)testVideoNodeReplacesAVPlayerItemWhenNewURLIsSet {
3339
XCTAssertNotEqualObjects(item, secondItem);
3440
}
3541

36-
- (void)testVideoNodeDoesNotReplaceAVPlayerItemWhenSameURLIsSet {
42+
- (void)testVideoNodeDoesNotReplaceAVPlayerItemWhenSameURLIsSet
43+
{
3744
ASVideoNode *videoNode = [[ASVideoNode alloc] init];
3845
videoNode.interfaceState = ASInterfaceStateFetchData;
3946
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:@"firstURL"]];
@@ -47,4 +54,57 @@ - (void)testVideoNodeDoesNotReplaceAVPlayerItemWhenSameURLIsSet {
4754
XCTAssertEqualObjects(item, secondItem);
4855
}
4956

57+
//Touch Handling
58+
59+
- (void)testSpinnerDefaultsToNil
60+
{
61+
ASVideoNode *videoNode = [[ASVideoNode alloc] init];
62+
63+
XCTAssertNil(videoNode.spinner);
64+
}
65+
66+
- (void)testOnPlayIfVideoIsNotReadyInitializeSpinnerAndAddAsSubnode
67+
{
68+
ASVideoNode *videoNode = [[ASVideoNode alloc] init];
69+
videoNode.interfaceState = ASInterfaceStateFetchData;
70+
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:@"firstURL"]];
71+
videoNode.asset = asset;
72+
73+
[videoNode play];
74+
75+
XCTAssertNotNil(videoNode.spinner);
76+
}
77+
78+
- (void)testOnPauseSpinnerIsPausedIfPresent
79+
{
80+
ASVideoNode *videoNode = [[ASVideoNode alloc] init];
81+
videoNode.interfaceState = ASInterfaceStateFetchData;
82+
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:@"firstURL"]];
83+
videoNode.asset = asset;
84+
85+
[videoNode play];
86+
87+
[videoNode pause];
88+
89+
XCTAssertFalse(((UIActivityIndicatorView *)videoNode.spinner.view).isAnimating);
90+
}
91+
92+
- (void)testOnVideoReadySpinnerIsStoppedAndRemoved
93+
{
94+
ASVideoNode *videoNode = [[ASVideoNode alloc] init];
95+
videoNode.interfaceState = ASInterfaceStateFetchData;
96+
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:@"firstURL"]];
97+
videoNode.asset = asset;
98+
99+
[videoNode play];
100+
[videoNode observeValueForKeyPath:@"status" ofObject:[videoNode currentItem] change:@{@"new" : @(AVPlayerItemStatusReadyToPlay)} context:NULL];
101+
102+
XCTAssertFalse(((UIActivityIndicatorView *)videoNode.spinner.view).isAnimating);
103+
}
104+
105+
- (void)testPlayButtonUserInteractionIsNotEnabled
106+
{
107+
108+
}
109+
50110
@end

examples/Videos/Sample.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
AE8E41191C228A4A00913AC4 /* bearacrat@2x.jpg in Resources */ = {isa = PBXBuildFile; fileRef = AE8E41181C228A4A00913AC4 /* bearacrat@2x.jpg */; };
1919
AE8E411B1C23634C00913AC4 /* simon.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = AE8E411A1C235A6000913AC4 /* simon.mp4 */; };
2020
AED850671C22679200183ED3 /* playButton@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = AED850661C22679200183ED3 /* playButton@2x.png */; };
21+
AEE1F2C01C293CF1005E0577 /* playButtonSelected@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = AEE1F2BF1C293CF1005E0577 /* playButtonSelected@2x.png */; };
2122
/* End PBXBuildFile section */
2223

2324
/* Begin PBXFileReference section */
@@ -38,6 +39,7 @@
3839
AE8E41181C228A4A00913AC4 /* bearacrat@2x.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = "bearacrat@2x.jpg"; sourceTree = "<group>"; };
3940
AE8E411A1C235A6000913AC4 /* simon.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = simon.mp4; sourceTree = "<group>"; };
4041
AED850661C22679200183ED3 /* playButton@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "playButton@2x.png"; sourceTree = "<group>"; };
42+
AEE1F2BF1C293CF1005E0577 /* playButtonSelected@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "playButtonSelected@2x.png"; sourceTree = "<group>"; };
4143
C068F1D3F0CC317E895FCDAB /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
4244
/* End PBXFileReference section */
4345

@@ -78,6 +80,7 @@
7880
isa = PBXGroup;
7981
children = (
8082
AED850661C22679200183ED3 /* playButton@2x.png */,
83+
AEE1F2BF1C293CF1005E0577 /* playButtonSelected@2x.png */,
8184
AE8E41181C228A4A00913AC4 /* bearacrat@2x.jpg */,
8285
AE8E411A1C235A6000913AC4 /* simon.mp4 */,
8386
05E2128819D4DB510098F589 /* AppDelegate.h */,
@@ -186,6 +189,7 @@
186189
6C2C82AC19EE274300767484 /* Default-667h@2x.png in Resources */,
187190
AE8E411B1C23634C00913AC4 /* simon.mp4 in Resources */,
188191
6C2C82AD19EE274300767484 /* Default-736h@3x.png in Resources */,
192+
AEE1F2C01C293CF1005E0577 /* playButtonSelected@2x.png in Resources */,
189193
);
190194
runOnlyForDeploymentPostprocessing = 0;
191195
};

examples/Videos/Sample/ViewController.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ - (ASVideoNode *)simonVideo;
7878
simonVideo.backgroundColor = [UIColor lightGrayColor];
7979
simonVideo.autorepeat = YES;
8080
simonVideo.playButton = [self playButton];
81-
simonVideo.shouldAutoPlay = YES;
81+
simonVideo.shouldAutoplay = YES;
8282

8383
return simonVideo;
8484
}
@@ -92,6 +92,7 @@ - (ASButtonNode *)playButton;
9292
[playButton measure:CGSizeMake(50, 50)];
9393
playButton.bounds = CGRectMake(0, 0, playButton.calculatedSize.width, playButton.calculatedSize.height);
9494
playButton.position = CGPointMake([UIScreen mainScreen].bounds.size.width/4, ([UIScreen mainScreen].bounds.size.height/3)/2);
95+
[playButton setImage:[UIImage imageNamed:@"playButtonSelected@2x.png"] forState:ASButtonStateHighlighted];
9596

9697
return playButton;
9798
}
15.7 KB
Loading

0 commit comments

Comments
 (0)