|
|
@ -39,13 +39,13 @@ public class MediaGalleryItem: Equatable, Hashable {
|
|
|
|
// MARK: Equatable
|
|
|
|
// MARK: Equatable
|
|
|
|
|
|
|
|
|
|
|
|
public static func == (lhs: MediaGalleryItem, rhs: MediaGalleryItem) -> Bool {
|
|
|
|
public static func == (lhs: MediaGalleryItem, rhs: MediaGalleryItem) -> Bool {
|
|
|
|
return lhs.message.uniqueId == rhs.message.uniqueId
|
|
|
|
return lhs.attachmentStream.uniqueId == rhs.attachmentStream.uniqueId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: Hashable
|
|
|
|
// MARK: Hashable
|
|
|
|
|
|
|
|
|
|
|
|
public var hashValue: Int {
|
|
|
|
public var hashValue: Int {
|
|
|
|
return message.hashValue
|
|
|
|
return attachmentStream.hashValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -281,10 +281,10 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
|
|
|
|
private var originRect: CGRect?
|
|
|
|
private var originRect: CGRect?
|
|
|
|
|
|
|
|
|
|
|
|
@objc
|
|
|
|
@objc
|
|
|
|
public func presentDetailView(fromViewController: UIViewController, mediaMessage: TSMessage, replacingView: UIView) {
|
|
|
|
public func presentDetailView(fromViewController: UIViewController, mediaAttachment: TSAttachment, replacingView: UIView) {
|
|
|
|
var galleryItem: MediaGalleryItem?
|
|
|
|
var galleryItem: MediaGalleryItem?
|
|
|
|
uiDatabaseConnection.read { transaction in
|
|
|
|
uiDatabaseConnection.read { transaction in
|
|
|
|
galleryItem = self.buildGalleryItem(message: mediaMessage, transaction: transaction)!
|
|
|
|
galleryItem = self.buildGalleryItem(attachment: mediaAttachment, transaction: transaction)!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
guard let initialDetailItem = galleryItem else {
|
|
|
|
guard let initialDetailItem = galleryItem else {
|
|
|
@ -414,8 +414,8 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
|
|
|
|
func pushTileView(fromNavController: OWSNavigationController) {
|
|
|
|
func pushTileView(fromNavController: OWSNavigationController) {
|
|
|
|
var mostRecentItem: MediaGalleryItem?
|
|
|
|
var mostRecentItem: MediaGalleryItem?
|
|
|
|
self.uiDatabaseConnection.read { transaction in
|
|
|
|
self.uiDatabaseConnection.read { transaction in
|
|
|
|
if let message = self.mediaGalleryFinder.mostRecentMediaMessage(transaction: transaction) {
|
|
|
|
if let attachment = self.mediaGalleryFinder.mostRecentMediaAttachment(transaction: transaction) {
|
|
|
|
mostRecentItem = self.buildGalleryItem(message: message, transaction: transaction)
|
|
|
|
mostRecentItem = self.buildGalleryItem(attachment: attachment, transaction: transaction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -633,9 +633,13 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
|
|
|
|
var hasFetchedOldest = false
|
|
|
|
var hasFetchedOldest = false
|
|
|
|
var hasFetchedMostRecent = false
|
|
|
|
var hasFetchedMostRecent = false
|
|
|
|
|
|
|
|
|
|
|
|
func buildGalleryItem(message: TSMessage, transaction: YapDatabaseReadTransaction) -> MediaGalleryItem? {
|
|
|
|
func buildGalleryItem(attachment: TSAttachment, transaction: YapDatabaseReadTransaction) -> MediaGalleryItem? {
|
|
|
|
// TODO: Support multi-image messages.
|
|
|
|
guard let attachmentStream = attachment as? TSAttachmentStream else {
|
|
|
|
guard let attachmentStream = message.attachments(with: transaction).first as? TSAttachmentStream else {
|
|
|
|
owsFailDebug("gallery doesn't yet support showing undownloaded attachments")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
guard let message = attachmentStream.fetchAlbumMessage(with: transaction) else {
|
|
|
|
owsFailDebug("attachment was unexpectedly empty")
|
|
|
|
owsFailDebug("attachment was unexpectedly empty")
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -662,7 +666,7 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
|
|
|
|
Bench(title: "fetching gallery items") {
|
|
|
|
Bench(title: "fetching gallery items") {
|
|
|
|
self.uiDatabaseConnection.read { transaction in
|
|
|
|
self.uiDatabaseConnection.read { transaction in
|
|
|
|
|
|
|
|
|
|
|
|
let initialIndex: Int = Int(self.mediaGalleryFinder.mediaIndex(message: item.message, transaction: transaction))
|
|
|
|
let initialIndex: Int = Int(self.mediaGalleryFinder.mediaIndex(attachment: item.attachmentStream, transaction: transaction))
|
|
|
|
let mediaCount: Int = Int(self.mediaGalleryFinder.mediaCount(transaction: transaction))
|
|
|
|
let mediaCount: Int = Int(self.mediaGalleryFinder.mediaCount(transaction: transaction))
|
|
|
|
|
|
|
|
|
|
|
|
let requestRange: Range<Int> = { () -> Range<Int> in
|
|
|
|
let requestRange: Range<Int> = { () -> Range<Int> in
|
|
|
@ -711,14 +715,14 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
|
|
|
|
|
|
|
|
|
|
|
|
Logger.debug("fetching set: \(unfetchedSet)")
|
|
|
|
Logger.debug("fetching set: \(unfetchedSet)")
|
|
|
|
let nsRange: NSRange = NSRange(location: unfetchedSet.min()!, length: unfetchedSet.count)
|
|
|
|
let nsRange: NSRange = NSRange(location: unfetchedSet.min()!, length: unfetchedSet.count)
|
|
|
|
self.mediaGalleryFinder.enumerateMediaMessages(range: nsRange, transaction: transaction) { (message: TSMessage) in
|
|
|
|
self.mediaGalleryFinder.enumerateMediaAttachments(range: nsRange, transaction: transaction) { (attachment: TSAttachment) in
|
|
|
|
|
|
|
|
|
|
|
|
guard !self.deletedMessages.contains(message) else {
|
|
|
|
guard !self.deletedAttachments.contains(attachment) else {
|
|
|
|
Logger.debug("skipping \(message) which has been deleted.")
|
|
|
|
Logger.debug("skipping \(attachment) which has been deleted.")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
guard let item: MediaGalleryItem = self.buildGalleryItem(message: message, transaction: transaction) else {
|
|
|
|
guard let item: MediaGalleryItem = self.buildGalleryItem(attachment: attachment, transaction: transaction) else {
|
|
|
|
owsFailDebug("unexpectedly failed to buildGalleryItem")
|
|
|
|
owsFailDebug("unexpectedly failed to buildGalleryItem")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -792,7 +796,7 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
|
|
|
|
dataSourceDelegates.append(Weak(value: dataSourceDelegate))
|
|
|
|
dataSourceDelegates.append(Weak(value: dataSourceDelegate))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var deletedMessages: Set<TSMessage> = Set()
|
|
|
|
var deletedAttachments: Set<TSAttachment> = Set()
|
|
|
|
var deletedGalleryItems: Set<MediaGalleryItem> = Set()
|
|
|
|
var deletedGalleryItems: Set<MediaGalleryItem> = Set()
|
|
|
|
|
|
|
|
|
|
|
|
func delete(items: [MediaGalleryItem], initiatedBy: MediaGalleryDataSourceDelegate) {
|
|
|
|
func delete(items: [MediaGalleryItem], initiatedBy: MediaGalleryDataSourceDelegate) {
|
|
|
@ -806,8 +810,9 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
|
|
|
|
self.editingDatabaseConnection.asyncReadWrite { transaction in
|
|
|
|
self.editingDatabaseConnection.asyncReadWrite { transaction in
|
|
|
|
for item in items {
|
|
|
|
for item in items {
|
|
|
|
let message = item.message
|
|
|
|
let message = item.message
|
|
|
|
message.remove(with: transaction)
|
|
|
|
let attachment = item.attachmentStream
|
|
|
|
self.deletedMessages.insert(message)
|
|
|
|
message.removeAttachment(attachment, transaction: transaction)
|
|
|
|
|
|
|
|
self.deletedAttachments.insert(attachment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -928,6 +933,6 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
|
|
|
|
self.uiDatabaseConnection.read { (transaction: YapDatabaseReadTransaction) in
|
|
|
|
self.uiDatabaseConnection.read { (transaction: YapDatabaseReadTransaction) in
|
|
|
|
count = self.mediaGalleryFinder.mediaCount(transaction: transaction)
|
|
|
|
count = self.mediaGalleryFinder.mediaCount(transaction: transaction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Int(count) - deletedMessages.count
|
|
|
|
return Int(count) - deletedAttachments.count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|