Merge branch 'charlesmchen/tapOnAlbumItem'

pull/1/head
Matthew Chen 7 years ago
commit 8acca4d56a

@ -4,7 +4,7 @@
import Foundation import Foundation
@objc @objc(OWSConversationMediaView)
public class ConversationMediaView: UIView { public class ConversationMediaView: UIView {
// MARK: - Dependencies // MARK: - Dependencies
@ -16,7 +16,8 @@ public class ConversationMediaView: UIView {
// MARK: - // MARK: -
private let mediaCache: NSCache<NSString, AnyObject> private let mediaCache: NSCache<NSString, AnyObject>
private let attachment: TSAttachment @objc
public let attachment: TSAttachment
private let isOutgoing: Bool private let isOutgoing: Bool
private let maxMessageWidth: CGFloat private let maxMessageWidth: CGFloat
private var loadBlock : (() -> Void)? private var loadBlock : (() -> Void)?

@ -261,4 +261,20 @@ public class MediaAlbumCellView: UIStackView {
return CGSize(width: maxMessageWidth, height: bigImageSize + smallImageSize + kSpacingPts) return CGSize(width: maxMessageWidth, height: bigImageSize + smallImageSize + kSpacingPts)
} }
} }
@objc
public func mediaView(forLocation location: CGPoint) -> ConversationMediaView? {
var bestMediaView: ConversationMediaView?
var bestDistance: CGFloat = 0
for itemView in itemViews {
let itemCenter = convert(itemView.center, from: itemView.superview)
let distance = CGPointDistance(location, itemCenter)
if bestMediaView != nil && distance > bestDistance {
continue
}
bestMediaView = itemView
bestDistance = distance
}
return bestMediaView
}
} }

@ -1307,7 +1307,7 @@ const UIDataDetectorTypes kOWSAllowedDataDetectorTypes
[self.delegate didTapTruncatedTextMessage:self.viewItem]; [self.delegate didTapTruncatedTextMessage:self.viewItem];
return; return;
case OWSMessageGestureLocation_Media: case OWSMessageGestureLocation_Media:
[self handleMediaTapGesture]; [self handleMediaTapGesture:locationInMessageBubble];
break; break;
case OWSMessageGestureLocation_QuotedReply: case OWSMessageGestureLocation_QuotedReply:
if (self.viewItem.quotedReply) { if (self.viewItem.quotedReply) {
@ -1319,7 +1319,7 @@ const UIDataDetectorTypes kOWSAllowedDataDetectorTypes
} }
} }
- (void)handleMediaTapGesture - (void)handleMediaTapGesture:(CGPoint)locationInMessageBubble
{ {
OWSAssertDebug(self.delegate); OWSAssertDebug(self.delegate);
@ -1358,35 +1358,25 @@ const UIDataDetectorTypes kOWSAllowedDataDetectorTypes
[self.delegate didTapFailedIncomingAttachment:self.viewItem]; [self.delegate didTapFailedIncomingAttachment:self.viewItem];
return; return;
} }
if (![self.bodyMediaView isKindOfClass:[OWSMediaAlbumCellView class]]) {
// TODO: We might be able to get rid of this. OWSFailDebug(@"Unexpected body media view: %@", self.bodyMediaView.class);
if (self.viewItem.mediaAlbumItems.count == 1) {
ConversationMediaAlbumItem *mediaAlbumItem = self.viewItem.mediaAlbumItems.firstObject;
if (!mediaAlbumItem.attachmentStream.isValidVisualMedia) {
// Do nothing.
} else if (mediaAlbumItem.attachmentStream.isAnimated || mediaAlbumItem.attachmentStream.isImage) {
[self.delegate didTapImageViewItem:self.viewItem
attachmentStream:mediaAlbumItem.attachmentStream
imageView:self.bodyMediaView];
return;
} else if (mediaAlbumItem.attachmentStream.isVideo) {
[self.delegate didTapVideoViewItem:self.viewItem
attachmentStream:mediaAlbumItem.attachmentStream
imageView:self.bodyMediaView];
return; return;
} }
OWSMediaAlbumCellView *_Nullable mediaAlbumCellView = (OWSMediaAlbumCellView *)self.bodyMediaView;
CGPoint location = [self convertPoint:locationInMessageBubble toView:self.bodyMediaView];
OWSConversationMediaView *_Nullable mediaView = [mediaAlbumCellView mediaViewForLocation:location];
if (!mediaView) {
OWSFailDebug(@"Missing media view.");
return;
} }
// For now, use first valid attachment. TSAttachment *attachment = mediaView.attachment;
TSAttachmentStream *_Nullable attachmentStream = self.viewItem.firstValidAlbumAttachment; if (![attachment isKindOfClass:[TSAttachmentStream class]]) {
if (!attachmentStream) { OWSLogWarn(@"Media attachment not yet downloaded.");
OWSLogInfo(@"Ignoring tap on album without any valid attachments.");
return; return;
} }
TSAttachmentStream *attachmentStream = (TSAttachmentStream *)attachment;
[self.delegate didTapImageViewItem:self.viewItem [self.delegate didTapImageViewItem:self.viewItem attachmentStream:attachmentStream imageView:mediaView];
attachmentStream:attachmentStream
imageView:self.bodyMediaView];
break; break;
} }
} }

@ -264,10 +264,7 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
self.options = options self.options = options
self.mediaGalleryFinder = OWSMediaGalleryFinder(thread: thread) self.mediaGalleryFinder = OWSMediaGalleryFinder(thread: thread)
let navController = MediaGalleryNavigationController()
self.navigationController = navController
super.init() super.init()
navController.retainUntilDismissed = self
} }
// MARK: Present/Dismiss // MARK: Present/Dismiss
@ -307,6 +304,11 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
self.addDataSourceDelegate(pageViewController) self.addDataSourceDelegate(pageViewController)
self.pageViewController = pageViewController self.pageViewController = pageViewController
let navController = MediaGalleryNavigationController()
self.navigationController = navController
navController.retainUntilDismissed = self
navigationController.setViewControllers([pageViewController], animated: false) navigationController.setViewControllers([pageViewController], animated: false)
self.replacingView = replacingView self.replacingView = replacingView

@ -188,6 +188,12 @@ CG_INLINE CGPoint CGPointScale(CGPoint point, CGFloat factor)
return CGPointMake(point.x * factor, point.y * factor); return CGPointMake(point.x * factor, point.y * factor);
} }
CG_INLINE CGFloat CGPointDistance(CGPoint left, CGPoint right)
{
CGPoint delta = CGPointSubtract(left, right);
return sqrt(delta.x * delta.x + delta.y * delta.y);
}
CG_INLINE CGSize CGSizeScale(CGSize size, CGFloat factor) CG_INLINE CGSize CGSizeScale(CGSize size, CGFloat factor)
{ {
return CGSizeMake(size.width * factor, size.height * factor); return CGSizeMake(size.width * factor, size.height * factor);

Loading…
Cancel
Save