@ -175,6 +175,8 @@ protocol MediaGalleryDataSource: class {
func showAllMedia ( focusedItem : MediaGalleryItem )
func showAllMedia ( focusedItem : MediaGalleryItem )
func dismissMediaDetailViewController ( _ mediaDetailViewController : MediaPageViewController , animated isAnimated : Bool , completion : ( ( ) -> Void ) ? )
func dismissMediaDetailViewController ( _ mediaDetailViewController : MediaPageViewController , animated isAnimated : Bool , completion : ( ( ) -> Void ) ? )
func delete ( message : TSMessage )
}
}
class MediaGalleryViewController : UINavigationController , MediaGalleryDataSource , MediaTileViewControllerDelegate {
class MediaGalleryViewController : UINavigationController , MediaGalleryDataSource , MediaTileViewControllerDelegate {
@ -182,6 +184,7 @@ class MediaGalleryViewController: UINavigationController, MediaGalleryDataSource
private var pageViewController : MediaPageViewController ?
private var pageViewController : MediaPageViewController ?
private let uiDatabaseConnection : YapDatabaseConnection
private let uiDatabaseConnection : YapDatabaseConnection
private let editingDatabaseConnection : YapDatabaseConnection
private let mediaGalleryFinder : OWSMediaGalleryFinder
private let mediaGalleryFinder : OWSMediaGalleryFinder
private var initialDetailItem : MediaGalleryItem ?
private var initialDetailItem : MediaGalleryItem ?
@ -199,6 +202,9 @@ class MediaGalleryViewController: UINavigationController, MediaGalleryDataSource
self . thread = thread
self . thread = thread
assert ( uiDatabaseConnection . isInLongLivedReadTransaction ( ) )
assert ( uiDatabaseConnection . isInLongLivedReadTransaction ( ) )
self . uiDatabaseConnection = uiDatabaseConnection
self . uiDatabaseConnection = uiDatabaseConnection
self . editingDatabaseConnection = OWSPrimaryStorage . shared ( ) . newDatabaseConnection ( )
self . options = options
self . options = options
self . mediaGalleryFinder = OWSMediaGalleryFinder ( thread : thread )
self . mediaGalleryFinder = OWSMediaGalleryFinder ( thread : thread )
@ -631,6 +637,12 @@ class MediaGalleryViewController: UINavigationController, MediaGalleryDataSource
Logger . debug ( " \( self . logTag ) in \( #function ) fetching set: \( unfetchedSet ) " )
Logger . debug ( " \( self . logTag ) in \( #function ) 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 . enumerateMediaMessages ( range : nsRange , transaction : transaction ) { ( message : TSMessage ) in
guard ! self . deletedMessages . contains ( message ) else {
Logger . debug ( " \( self . logTag ) skipping \( message ) which has been deleted. " )
return
}
guard let item : MediaGalleryItem = self . buildGalleryItem ( message : message , transaction : transaction ) else {
guard let item : MediaGalleryItem = self . buildGalleryItem ( message : message , transaction : transaction ) else {
owsFail ( " \( self . logTag ) in \( #function ) unexpectedly failed to buildGalleryItem " )
owsFail ( " \( self . logTag ) in \( #function ) unexpectedly failed to buildGalleryItem " )
return
return
@ -700,6 +712,65 @@ class MediaGalleryViewController: UINavigationController, MediaGalleryDataSource
}
}
}
}
var deletedMessages : Set < TSMessage > = Set ( )
func delete ( message : TSMessage ) {
Logger . info ( " \( logTag ) in \( #function ) with message: \( String ( describing : message . uniqueId ) ) attachmentId: \( String ( describing : message . attachmentIds . firstObject ) ) " )
// T O D O p u t t h i s s o m e w h e r e r e a s o n a b l e . . .
self . mediaTileViewController . collectionView ! . layoutIfNeeded ( )
self . editingDatabaseConnection . asyncReadWrite { transaction in
message . remove ( with : transaction )
}
self . deletedMessages . insert ( message )
var deletedSections : IndexSet = IndexSet ( )
var deletedIndexPaths : [ IndexPath ] = [ ]
guard let itemIndex = galleryItems . index ( where : { $0 . message = = message } ) else {
owsFail ( " \( logTag ) in \( #function ) removing unknown item. " )
return
}
let item : MediaGalleryItem = galleryItems [ itemIndex ]
self . galleryItems . remove ( at : itemIndex )
guard let sectionIndex = sectionDates . index ( where : { $0 = = item . galleryDate } ) else {
owsFail ( " \( logTag ) in \( #function ) item with unknown date. " )
return
}
guard var sectionItems = self . sections [ item . galleryDate ] else {
owsFail ( " \( logTag ) in \( #function ) item with unknown section " )
return
}
if sectionItems = = [ item ] {
// L a s t i t e m i n s e c t i o n . D e l e t e s e c t i o n .
self . sections [ item . galleryDate ] = nil
self . sectionDates . remove ( at : sectionIndex )
deletedSections . insert ( sectionIndex + 1 )
deletedIndexPaths . append ( IndexPath ( row : 0 , section : sectionIndex + 1 ) )
} else {
guard let sectionRowIndex = sectionItems . index ( of : item ) else {
owsFail ( " \( logTag ) in \( #function ) item with unknown sectionRowIndex " )
return
}
sectionItems . remove ( at : sectionRowIndex )
self . sections [ item . galleryDate ] = sectionItems
deletedIndexPaths . append ( IndexPath ( row : sectionRowIndex , section : sectionIndex + 1 ) )
}
// T O D O ? n o t i f y p a g e r v i e w
// n o t i f y t i l e v i e w
self . mediaTileViewController . updatedDataSource ( deletedSections : deletedSections , deletedItems : deletedIndexPaths )
}
let kGallerySwipeLoadBatchSize : UInt = 5
let kGallerySwipeLoadBatchSize : UInt = 5
internal func galleryItem ( after currentItem : MediaGalleryItem ) -> MediaGalleryItem ? {
internal func galleryItem ( after currentItem : MediaGalleryItem ) -> MediaGalleryItem ? {