Skip to content

Commit 02e5927

Browse files
committed
add JSQVideoMediaitem. close jessesquires#504.
1 parent 41d295a commit 02e5927

File tree

4 files changed

+228
-1
lines changed

4 files changed

+228
-1
lines changed

JSQMessages.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
8846442719EB189E001EBA43 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 8846442619EB189E001EBA43 /* [email protected] */; };
4242
8846442A19EB18A5001EBA43 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 8846442819EB18A5001EBA43 /* [email protected] */; };
4343
8846442B19EB18A5001EBA43 /* bubble_tailless.png in Resources */ = {isa = PBXBuildFile; fileRef = 8846442919EB18A5001EBA43 /* bubble_tailless.png */; };
44+
8846442E19EB2BF4001EBA43 /* JSQVideoMediaitem.m in Sources */ = {isa = PBXBuildFile; fileRef = 8846442D19EB2BF4001EBA43 /* JSQVideoMediaitem.m */; };
4445
886FFD2E19E9A65D00EB8485 /* UIDevice+JSQMessages.m in Sources */ = {isa = PBXBuildFile; fileRef = 886FFD2D19E9A65D00EB8485 /* UIDevice+JSQMessages.m */; };
4546
8885734A19DE540400E89D20 /* DemoSettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8885734919DE540400E89D20 /* DemoSettingsViewController.m */; };
4647
8885734D19DE55D000E89D20 /* NSUserDefaults+DemoSettings.m in Sources */ = {isa = PBXBuildFile; fileRef = 8885734C19DE55D000E89D20 /* NSUserDefaults+DemoSettings.m */; };
@@ -171,6 +172,8 @@
171172
8846442619EB189E001EBA43 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
172173
8846442819EB18A5001EBA43 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
173174
8846442919EB18A5001EBA43 /* bubble_tailless.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = bubble_tailless.png; sourceTree = "<group>"; };
175+
8846442C19EB2BF4001EBA43 /* JSQVideoMediaitem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSQVideoMediaitem.h; sourceTree = "<group>"; };
176+
8846442D19EB2BF4001EBA43 /* JSQVideoMediaitem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSQVideoMediaitem.m; sourceTree = "<group>"; };
174177
886FFD2C19E9A65D00EB8485 /* UIDevice+JSQMessages.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIDevice+JSQMessages.h"; sourceTree = "<group>"; };
175178
886FFD2D19E9A65D00EB8485 /* UIDevice+JSQMessages.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIDevice+JSQMessages.m"; sourceTree = "<group>"; };
176179
8885734819DE540400E89D20 /* DemoSettingsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoSettingsViewController.h; sourceTree = "<group>"; };
@@ -582,6 +585,8 @@
582585
88A25F8619D8E01A00924534 /* JSQPhotoMediaItem.m */,
583586
88A25F8719D8E01A00924534 /* JSQTextMessage.h */,
584587
88A25F8819D8E01A00924534 /* JSQTextMessage.m */,
588+
8846442C19EB2BF4001EBA43 /* JSQVideoMediaitem.h */,
589+
8846442D19EB2BF4001EBA43 /* JSQVideoMediaitem.m */,
585590
);
586591
path = Model;
587592
sourceTree = "<group>";
@@ -924,6 +929,7 @@
924929
88A25FC919D8E01A00924534 /* JSQTextMessage.m in Sources */,
925930
88A25FC419D8E01A00924534 /* JSQMediaMessage.m in Sources */,
926931
88A25FC219D8E01A00924534 /* JSQMessagesCollectionViewFlowLayoutInvalidationContext.m in Sources */,
932+
8846442E19EB2BF4001EBA43 /* JSQVideoMediaitem.m in Sources */,
927933
88A25FE119D8E0C400924534 /* TableViewController.m in Sources */,
928934
88A25FBD19D8E01A00924534 /* JSQMessagesAvatarImageFactory.m in Sources */,
929935
88A25FB519D8E01A00924534 /* JSQSystemSoundPlayer+JSQMessages.m in Sources */,

JSQMessagesViewController/Model/JSQMessageMediaData.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
*
2929
* This library provides a few concrete classes that conform to this protocol. You may use them as-is,
3030
* but they will likely require some modifications or extensions to conform to your particular data models.
31-
* These concrete media items are: `JSQPhotoMediaItem`, `JSQLocationMediaItem`.
31+
* These concrete media items are: `JSQPhotoMediaItem`, `JSQLocationMediaItem`, `JSQVideoMediaItem`.
3232
*
3333
* @see JSQPhotoMediaItem.
3434
* @see JSQLocationMediaItem.
35+
* @see JSQVideoMediaItem.
3536
*/
3637
@protocol JSQMessageMediaData <NSObject>
3738

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// Created by Jesse Squires
3+
// http://www.jessesquires.com
4+
//
5+
//
6+
// Documentation
7+
// http://cocoadocs.org/docsets/JSQMessagesViewController
8+
//
9+
//
10+
// GitHub
11+
// https://github.com/jessesquires/JSQMessagesViewController
12+
//
13+
//
14+
// License
15+
// Copyright (c) 2014 Jesse Squires
16+
// Released under an MIT license: http://opensource.org/licenses/MIT
17+
//
18+
19+
#import "JSQMessageMediaData.h"
20+
21+
/**
22+
* The `JSQVideoMediaitem` class is a concrete class that implements the `JSQMessageMediaData` protocol
23+
* and represents a video media message. An initialized `JSQVideoMediaitem` object can be passed
24+
* to a `JSQMediaMessage` object during its initialization to construct a valid media message object.
25+
* You may wish to subclass `JSQVideoMediaitem` to provide additional functionality or behavior.
26+
*/
27+
@interface JSQVideoMediaitem : NSObject <JSQMessageMediaData, NSCoding, NSCopying>
28+
29+
/**
30+
* The URL that identifies a video resource.
31+
*/
32+
@property (nonatomic, strong) NSURL *fileURL;
33+
34+
/**
35+
* A boolean value that specifies whether or not the video is ready to be played.
36+
*
37+
* @discussion When set to `YES`, the video is ready. When set to `NO` it is not ready.
38+
*/
39+
@property (nonatomic, assign) BOOL isReadyToPlay;
40+
41+
/**
42+
* Initializes and returns a video media item having the given fileURL.
43+
*
44+
* @param fileURL The URL that identifies the video resource.
45+
* @param isReadyToPlay A boolean value that specifies if the video is ready to play.
46+
*
47+
* @return An initialized `JSQVideoMediaitem` if successful, `nil` otherwise.
48+
*
49+
* @discussion If the video must be downloaded from the network,
50+
* you may initialize a `JSQVideoMediaitem` with a `nil` fileURL or specify `NO` for
51+
* isReadyToPlay. Once the video has been saved to disk, or is ready to stream, you can
52+
* set the fileURL property or isReadyToPlay property, respectively.
53+
*/
54+
- (instancetype)initWithFileURL:(NSURL *)fileURL isReadyToPlay:(BOOL)isReadyToPlay;
55+
56+
@end
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
//
2+
// Created by Jesse Squires
3+
// http://www.jessesquires.com
4+
//
5+
//
6+
// Documentation
7+
// http://cocoadocs.org/docsets/JSQMessagesViewController
8+
//
9+
//
10+
// GitHub
11+
// https://github.com/jessesquires/JSQMessagesViewController
12+
//
13+
//
14+
// License
15+
// Copyright (c) 2014 Jesse Squires
16+
// Released under an MIT license: http://opensource.org/licenses/MIT
17+
//
18+
19+
#import "JSQVideoMediaitem.h"
20+
21+
#import "JSQMessagesMediaPlaceholderView.h"
22+
23+
#import "UIImage+JSQMessages.h"
24+
25+
26+
@interface JSQVideoMediaitem ()
27+
28+
@property (strong, nonatomic) UIImageView *cachedVideoImageView;
29+
30+
@end
31+
32+
33+
@implementation JSQVideoMediaitem
34+
35+
#pragma mark - Initialization
36+
37+
- (instancetype)initWithFileURL:(NSURL *)fileURL isReadyToPlay:(BOOL)isReadyToPlay
38+
{
39+
self = [super init];
40+
if (self) {
41+
_fileURL = [fileURL copy];
42+
_isReadyToPlay = isReadyToPlay;
43+
_cachedVideoImageView = nil;
44+
}
45+
return self;
46+
}
47+
48+
- (void)dealloc
49+
{
50+
_fileURL = nil;
51+
_cachedVideoImageView = nil;
52+
}
53+
54+
#pragma mark - Setters
55+
56+
- (void)setFileURL:(NSURL *)fileURL
57+
{
58+
_fileURL = [fileURL copy];
59+
_cachedVideoImageView = nil;
60+
}
61+
62+
- (void)setIsReadyToPlay:(BOOL)isReadyToPlay
63+
{
64+
_isReadyToPlay = isReadyToPlay;
65+
_cachedVideoImageView = nil;
66+
}
67+
68+
#pragma mark - JSQMessageMediaData protocol
69+
70+
- (UIView *)mediaView
71+
{
72+
if (self.fileURL == nil || !self.isReadyToPlay) {
73+
return nil;
74+
}
75+
76+
if (self.cachedVideoImageView == nil) {
77+
CGSize size = [self mediaViewDisplaySize];
78+
UIImage *playIcon = [[UIImage imageNamed:@"play"] jsq_imageMaskedWithColor:[UIColor lightGrayColor]];
79+
80+
UIImageView *imageView = [[UIImageView alloc] initWithImage:playIcon];
81+
imageView.backgroundColor = [UIColor blackColor];
82+
imageView.frame = CGRectMake(0.0f, 0.0f, size.width, size.height);
83+
imageView.contentMode = UIViewContentModeCenter;
84+
imageView.clipsToBounds = YES;
85+
imageView.layer.cornerRadius = 20.0f;
86+
self.cachedVideoImageView = imageView;
87+
}
88+
89+
return self.cachedVideoImageView;
90+
}
91+
92+
- (CGSize)mediaViewDisplaySize
93+
{
94+
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
95+
return CGSizeMake(300.0f, 180.0f);
96+
}
97+
return CGSizeMake(200.0f, 120.0f);
98+
}
99+
100+
- (UIView *)mediaPlaceholderView
101+
{
102+
return [JSQMessagesMediaPlaceholderView viewWithActivityIndicator];
103+
}
104+
105+
#pragma mark - NSObject
106+
107+
- (BOOL)isEqual:(id)object
108+
{
109+
if (self == object) {
110+
return YES;
111+
}
112+
113+
if (![object isKindOfClass:[self class]]) {
114+
return NO;
115+
}
116+
117+
JSQVideoMediaitem *videoItem = (JSQVideoMediaitem *)object;
118+
119+
return [self.fileURL isEqual:videoItem.fileURL]
120+
&& self.isReadyToPlay == videoItem.isReadyToPlay;
121+
}
122+
123+
- (NSUInteger)hash
124+
{
125+
return self.fileURL.hash;
126+
}
127+
128+
- (NSString *)description
129+
{
130+
return [NSString stringWithFormat:@"<%@: fileURL=%@, isReadyToPlay=%@>", [self class], self.fileURL, @(self.isReadyToPlay)];
131+
}
132+
133+
- (id)debugQuickLookObject
134+
{
135+
return [self mediaView] ?: [self mediaPlaceholderView];
136+
}
137+
138+
#pragma mark - NSCoding
139+
140+
- (instancetype)initWithCoder:(NSCoder *)aDecoder
141+
{
142+
self = [super init];
143+
if (self) {
144+
_fileURL = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(fileURL))];
145+
_isReadyToPlay = [aDecoder decodeBoolForKey:NSStringFromSelector(@selector(isReadyToPlay))];
146+
}
147+
return self;
148+
}
149+
150+
- (void)encodeWithCoder:(NSCoder *)aCoder
151+
{
152+
[aCoder encodeObject:self.fileURL forKey:NSStringFromSelector(@selector(fileURL))];
153+
[aCoder encodeBool:self.isReadyToPlay forKey:NSStringFromSelector(@selector(isReadyToPlay))];
154+
}
155+
156+
#pragma mark - NSCopying
157+
158+
- (instancetype)copyWithZone:(NSZone *)zone
159+
{
160+
return [[[self class] allocWithZone:zone] initWithFileURL:self.fileURL
161+
isReadyToPlay:self.isReadyToPlay];
162+
}
163+
164+
@end

0 commit comments

Comments
 (0)