From 3c8d2f643b89bf112d71c75e6ee777ece2e37341 Mon Sep 17 00:00:00 2001 From: fanyu Date: Thu, 3 Mar 2022 17:04:20 +0800 Subject: [PATCH] Remove cell animation --- .../Chat/Cells/SelectedMediaCell.swift | 2 +- .../Chat/MediasPreviewViewController.swift | 66 ++----------------- .../Chat/Model/ConversationDataSource.swift | 11 ---- .../Chat/PhotoInputViewController.swift | 4 +- .../Windows/Cells/MediaPreviewCell.swift | 3 +- .../Windows/Cells/MediaPreviewCell.xib | 2 +- .../Windows/MediasPreviewWindow.swift | 4 +- 7 files changed, 13 insertions(+), 79 deletions(-) diff --git a/Mixin/UserInterface/Controllers/Chat/Cells/SelectedMediaCell.swift b/Mixin/UserInterface/Controllers/Chat/Cells/SelectedMediaCell.swift index cbe294e8d1..e0ce2c4985 100644 --- a/Mixin/UserInterface/Controllers/Chat/Cells/SelectedMediaCell.swift +++ b/Mixin/UserInterface/Controllers/Chat/Cells/SelectedMediaCell.swift @@ -28,7 +28,7 @@ class SelectedMediaCell: UICollectionViewCell { if asset.mediaType == .video { mediaTypeView.style = .video(duration: asset.duration) } else { - if let uti = asset.value(forKey: "uniformTypeIdentifier") as? String, UTTypeConformsTo(uti as CFString, kUTTypeGIF) { + if let uti = asset.uniformTypeIdentifier, UTTypeConformsTo(uti as CFString, kUTTypeGIF) { mediaTypeView.style = .gif } else { mediaTypeView.style = .hidden diff --git a/Mixin/UserInterface/Controllers/Chat/MediasPreviewViewController.swift b/Mixin/UserInterface/Controllers/Chat/MediasPreviewViewController.swift index d8a2e11526..0de6d8e176 100644 --- a/Mixin/UserInterface/Controllers/Chat/MediasPreviewViewController.swift +++ b/Mixin/UserInterface/Controllers/Chat/MediasPreviewViewController.swift @@ -1,6 +1,5 @@ import UIKit import Photos -import MixinServices protocol MediasPreviewViewControllerDelegate: AnyObject { func mediasPreviewViewController(_ controller: MediasPreviewViewController, didSend assets: [PHAsset]) @@ -17,14 +16,8 @@ final class MediasPreviewViewController: UIViewController { @IBOutlet weak var sendButton: UIButton! weak var delegate: MediasPreviewViewControllerDelegate? - weak var gridViewController: PhotoInputGridViewController? private var cellSizeCache = [String: CGSize]() - private var isAddingAsset = false - private var isRemovingAsset = false - private var selectedAssets: [PHAsset] { - gridViewController?.selectedAssets ?? [] - } private var assets = [PHAsset]() { didSet { sendButton.setTitle(R.string.localizable.chat_media_send_count(assets.count), for: .normal) @@ -44,9 +37,8 @@ final class MediasPreviewViewController: UIViewController { extension MediasPreviewViewController { func add(_ asset: PHAsset) { - isAddingAsset = true - assets = selectedAssets - UIView.performWithoutAnimation(collectionView.reloadData) + assets.append(asset) + collectionView.insertItems(at: [IndexPath(item: assets.count - 1, section: 0)]) collectionView.scrollToItem(at: IndexPath(item: assets.count - 1, section: 0), at: .centeredHorizontally, animated: true) @@ -56,15 +48,9 @@ extension MediasPreviewViewController { guard let index = assets.firstIndex(of: asset) else { return } + assets.remove(at: index) cellSizeCache.removeValue(forKey: asset.localIdentifier) - let indexPath = IndexPath(item: index, section: 0) - let removeInvisibleCell = !collectionView.indexPathsForVisibleItems.contains(indexPath) - if isRemovingAsset || removeInvisibleCell { - assets = selectedAssets - collectionView.reloadData() - } else if let cell = collectionView.cellForItem(at: indexPath) { - removeMediaAnimated(asset: asset, cell: cell) - } + collectionView.deleteItems(at: [IndexPath(item: index, section: 0)]) } func removeAllAssets() { @@ -73,7 +59,7 @@ extension MediasPreviewViewController { collectionView.reloadData() } - func updateAssets() { + func updateAssets(_ selectedAssets: [PHAsset]) { cellSizeCache.removeAll() assets = selectedAssets collectionView.reloadData() @@ -96,7 +82,7 @@ extension MediasPreviewViewController: UICollectionViewDataSource { guard let self = self else { return } - self.removeMediaAnimated(asset: asset, cell: cell) + self.remove(asset) self.delegate?.mediasPreviewViewController(self, didRemove: asset) } } @@ -111,12 +97,6 @@ extension MediasPreviewViewController: UICollectionViewDelegateFlowLayout, UICol cellSizeForItemAt(indexPath.item) } - func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { - if isAddingAsset && indexPath.item == assets.count - 1 { - addedMediaWillDisplay(cell) - } - } - func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { delegate?.mediasPreviewViewController(self, didSelectAssetAt: indexPath.item) } @@ -149,38 +129,4 @@ extension MediasPreviewViewController { } } - private func removeMediaAnimated(asset: PHAsset, cell: UICollectionViewCell) { - isRemovingAsset = true - UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) { - cell.alpha = 0 - cell.transform = CGAffineTransform(scaleX: 0.1, y: 0.1) - } completion: { _ in - if let index = self.assets.firstIndex(of: asset) { - self.collectionView.performBatchUpdates { - self.assets.remove(at: index) - self.collectionView.deleteItems(at: [IndexPath(item: index, section: 0)]) - } completion: { _ in - self.assets = self.selectedAssets - self.collectionView.reloadData() - self.isRemovingAsset = false - } - } else { - self.assets = self.selectedAssets - self.collectionView.reloadData() - self.isRemovingAsset = false - } - } - } - - private func addedMediaWillDisplay(_ cell: UICollectionViewCell) { - cell.alpha = 0 - cell.transform = CGAffineTransform(scaleX: 0.1, y: 0.1) - UIView.animate(withDuration: 0.3, delay: 0.2, options: .curveEaseOut) { - cell.transform = .identity - cell.alpha = 1 - } completion: { _ in - self.isAddingAsset = false - } - } - } diff --git a/Mixin/UserInterface/Controllers/Chat/Model/ConversationDataSource.swift b/Mixin/UserInterface/Controllers/Chat/Model/ConversationDataSource.swift index c51681f639..85631402e3 100644 --- a/Mixin/UserInterface/Controllers/Chat/Model/ConversationDataSource.swift +++ b/Mixin/UserInterface/Controllers/Chat/Model/ConversationDataSource.swift @@ -1158,17 +1158,6 @@ extension ConversationDataSource { } messagesToStack.removeAll() } - - //TODO: ‼️ delete - let totalCount = result.reduce(0) { partialResult, item in - if item.category == MessageCategory.STACKED_PHOTO.rawValue { - return partialResult + item.messageItems!.count - } else { - return partialResult + 1 - } - } - assert(totalCount == messages.count) - return result } diff --git a/Mixin/UserInterface/Controllers/Chat/PhotoInputViewController.swift b/Mixin/UserInterface/Controllers/Chat/PhotoInputViewController.swift index e354f46533..f98db41145 100644 --- a/Mixin/UserInterface/Controllers/Chat/PhotoInputViewController.swift +++ b/Mixin/UserInterface/Controllers/Chat/PhotoInputViewController.swift @@ -24,7 +24,6 @@ class PhotoInputViewController: UIViewController, ConversationInputAccessible { private weak var mediasPreviewControllerIfLoaded: MediasPreviewViewController? private lazy var mediasPreviewViewController: MediasPreviewViewController = { let controller = R.storyboard.chat.selected_medias()! - controller.gridViewController = gridViewController controller.delegate = self mediasPreviewControllerIfLoaded = controller return controller @@ -191,6 +190,7 @@ extension PhotoInputViewController: PHPhotoLibraryChangeObserver { func photoLibraryDidChange(_ changeInstance: PHChange) { DispatchQueue.main.sync { + self.dismissMediasPreviewControllerIfNeeded() if let allPhotos = self.allPhotos, let changeDetails = changeInstance.changeDetails(for: allPhotos) { self.allPhotos = changeDetails.fetchResultAfterChanges } @@ -288,7 +288,7 @@ extension PhotoInputViewController: MediasPreviewWindowDelegate { dismissMediasPreviewControllerAnimated() } else { gridViewController.updateSelectdAssets(assets) - mediasPreviewViewController.updateAssets() + mediasPreviewViewController.updateAssets(assets) } } diff --git a/Mixin/UserInterface/Windows/Cells/MediaPreviewCell.swift b/Mixin/UserInterface/Windows/Cells/MediaPreviewCell.swift index f637563f4b..ff05c6a065 100644 --- a/Mixin/UserInterface/Windows/Cells/MediaPreviewCell.swift +++ b/Mixin/UserInterface/Windows/Cells/MediaPreviewCell.swift @@ -6,7 +6,6 @@ import MixinServices class MediaPreviewCell: UICollectionViewCell { - static let reuseIdentifier = "cell_identifier_media_preview_cell" static let cellSize = CGSize(width: 312, height: 312) @IBOutlet weak var badge: BadgeView! @@ -46,7 +45,7 @@ class MediaPreviewCell: UICollectionViewCell { if asset.mediaType == .video { mediaTypeView.style = .video(duration: asset.duration) } else { - if let uti = asset.value(forKey: "uniformTypeIdentifier") as? String, UTTypeConformsTo(uti as CFString, kUTTypeGIF) { + if let uti = asset.uniformTypeIdentifier, UTTypeConformsTo(uti as CFString, kUTTypeGIF) { mediaTypeView.style = .gif } else { mediaTypeView.style = .hidden diff --git a/Mixin/UserInterface/Windows/Cells/MediaPreviewCell.xib b/Mixin/UserInterface/Windows/Cells/MediaPreviewCell.xib index 606c12a8f7..3e8d9fd020 100644 --- a/Mixin/UserInterface/Windows/Cells/MediaPreviewCell.xib +++ b/Mixin/UserInterface/Windows/Cells/MediaPreviewCell.xib @@ -10,7 +10,7 @@ - + diff --git a/Mixin/UserInterface/Windows/MediasPreviewWindow.swift b/Mixin/UserInterface/Windows/MediasPreviewWindow.swift index db73db71ec..0a326d8461 100644 --- a/Mixin/UserInterface/Windows/MediasPreviewWindow.swift +++ b/Mixin/UserInterface/Windows/MediasPreviewWindow.swift @@ -33,7 +33,7 @@ final class MediasPreviewWindow: BottomSheetView { collectionView.delegate = self collectionView.dataSource = self collectionView.allowsMultipleSelection = true - collectionView.register(UINib(resource: R.nib.mediaPreviewCell), forCellWithReuseIdentifier: MediaPreviewCell.reuseIdentifier) + collectionView.register(R.nib.mediaPreviewCell) } override func layoutSubviews() { @@ -93,7 +93,7 @@ extension MediasPreviewWindow: UICollectionViewDataSource { } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { - let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MediaPreviewCell.reuseIdentifier, for: indexPath) as! MediaPreviewCell + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: R.reuseIdentifier.media_preview, for: indexPath)! if indexPath.item < assets.count { let asset = assets[indexPath.item] cell.load(asset: asset)