Skip to content

Commit 3234db1

Browse files
committed
Remove cell animation
1 parent a3562c7 commit 3234db1

File tree

6 files changed

+13
-68
lines changed

6 files changed

+13
-68
lines changed

Mixin/UserInterface/Controllers/Chat/Cells/SelectedMediaCell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class SelectedMediaCell: UICollectionViewCell {
2828
if asset.mediaType == .video {
2929
mediaTypeView.style = .video(duration: asset.duration)
3030
} else {
31-
if let uti = asset.value(forKey: "uniformTypeIdentifier") as? String, UTTypeConformsTo(uti as CFString, kUTTypeGIF) {
31+
if let uti = asset.uniformTypeIdentifier, UTTypeConformsTo(uti as CFString, kUTTypeGIF) {
3232
mediaTypeView.style = .gif
3333
} else {
3434
mediaTypeView.style = .hidden

Mixin/UserInterface/Controllers/Chat/MediasPreviewViewController.swift

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import UIKit
22
import Photos
3-
import MixinServices
43

54
protocol MediasPreviewViewControllerDelegate: AnyObject {
65
func mediasPreviewViewController(_ controller: MediasPreviewViewController, didSend assets: [PHAsset])
@@ -17,14 +16,8 @@ final class MediasPreviewViewController: UIViewController {
1716
@IBOutlet weak var sendButton: UIButton!
1817

1918
weak var delegate: MediasPreviewViewControllerDelegate?
20-
weak var gridViewController: PhotoInputGridViewController?
2119

2220
private var cellSizeCache = [String: CGSize]()
23-
private var isAddingAsset = false
24-
private var isRemovingAsset = false
25-
private var selectedAssets: [PHAsset] {
26-
gridViewController?.selectedAssets ?? []
27-
}
2821
private var assets = [PHAsset]() {
2922
didSet {
3023
sendButton.setTitle(R.string.localizable.chat_media_send_count(assets.count), for: .normal)
@@ -44,9 +37,8 @@ final class MediasPreviewViewController: UIViewController {
4437
extension MediasPreviewViewController {
4538

4639
func add(_ asset: PHAsset) {
47-
isAddingAsset = true
48-
assets = selectedAssets
49-
UIView.performWithoutAnimation(collectionView.reloadData)
40+
assets.append(asset)
41+
collectionView.insertItems(at: [IndexPath(item: assets.count - 1, section: 0)])
5042
collectionView.scrollToItem(at: IndexPath(item: assets.count - 1, section: 0),
5143
at: .centeredHorizontally,
5244
animated: true)
@@ -56,15 +48,9 @@ extension MediasPreviewViewController {
5648
guard let index = assets.firstIndex(of: asset) else {
5749
return
5850
}
51+
assets.remove(at: index)
5952
cellSizeCache.removeValue(forKey: asset.localIdentifier)
60-
let indexPath = IndexPath(item: index, section: 0)
61-
let removeInvisibleCell = !collectionView.indexPathsForVisibleItems.contains(indexPath)
62-
if isRemovingAsset || removeInvisibleCell {
63-
assets = selectedAssets
64-
collectionView.reloadData()
65-
} else if let cell = collectionView.cellForItem(at: indexPath) {
66-
removeMediaAnimated(asset: asset, cell: cell)
67-
}
53+
collectionView.deleteItems(at: [IndexPath(item: index, section: 0)])
6854
}
6955

7056
func removeAllAssets() {
@@ -73,7 +59,7 @@ extension MediasPreviewViewController {
7359
collectionView.reloadData()
7460
}
7561

76-
func updateAssets() {
62+
func updateAssets(_ selectedAssets: [PHAsset]) {
7763
cellSizeCache.removeAll()
7864
assets = selectedAssets
7965
collectionView.reloadData()
@@ -96,7 +82,7 @@ extension MediasPreviewViewController: UICollectionViewDataSource {
9682
guard let self = self else {
9783
return
9884
}
99-
self.removeMediaAnimated(asset: asset, cell: cell)
85+
self.remove(asset)
10086
self.delegate?.mediasPreviewViewController(self, didRemove: asset)
10187
}
10288
}
@@ -111,12 +97,6 @@ extension MediasPreviewViewController: UICollectionViewDelegateFlowLayout, UICol
11197
cellSizeForItemAt(indexPath.item)
11298
}
11399

114-
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
115-
if isAddingAsset && indexPath.item == assets.count - 1 {
116-
addedMediaWillDisplay(cell)
117-
}
118-
}
119-
120100
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
121101
delegate?.mediasPreviewViewController(self, didSelectAssetAt: indexPath.item)
122102
}
@@ -149,38 +129,4 @@ extension MediasPreviewViewController {
149129
}
150130
}
151131

152-
private func removeMediaAnimated(asset: PHAsset, cell: UICollectionViewCell) {
153-
isRemovingAsset = true
154-
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) {
155-
cell.alpha = 0
156-
cell.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
157-
} completion: { _ in
158-
if let index = self.assets.firstIndex(of: asset) {
159-
self.collectionView.performBatchUpdates {
160-
self.assets.remove(at: index)
161-
self.collectionView.deleteItems(at: [IndexPath(item: index, section: 0)])
162-
} completion: { _ in
163-
self.assets = self.selectedAssets
164-
self.collectionView.reloadData()
165-
self.isRemovingAsset = false
166-
}
167-
} else {
168-
self.assets = self.selectedAssets
169-
self.collectionView.reloadData()
170-
self.isRemovingAsset = false
171-
}
172-
}
173-
}
174-
175-
private func addedMediaWillDisplay(_ cell: UICollectionViewCell) {
176-
cell.alpha = 0
177-
cell.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
178-
UIView.animate(withDuration: 0.3, delay: 0.2, options: .curveEaseOut) {
179-
cell.transform = .identity
180-
cell.alpha = 1
181-
} completion: { _ in
182-
self.isAddingAsset = false
183-
}
184-
}
185-
186132
}

Mixin/UserInterface/Controllers/Chat/PhotoInputViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class PhotoInputViewController: UIViewController, ConversationInputAccessible {
2424
private weak var mediasPreviewControllerIfLoaded: MediasPreviewViewController?
2525
private lazy var mediasPreviewViewController: MediasPreviewViewController = {
2626
let controller = R.storyboard.chat.selected_medias()!
27-
controller.gridViewController = gridViewController
2827
controller.delegate = self
2928
mediasPreviewControllerIfLoaded = controller
3029
return controller
@@ -191,6 +190,7 @@ extension PhotoInputViewController: PHPhotoLibraryChangeObserver {
191190

192191
func photoLibraryDidChange(_ changeInstance: PHChange) {
193192
DispatchQueue.main.sync {
193+
self.dismissMediasPreviewControllerIfNeeded()
194194
if let allPhotos = self.allPhotos, let changeDetails = changeInstance.changeDetails(for: allPhotos) {
195195
self.allPhotos = changeDetails.fetchResultAfterChanges
196196
}
@@ -288,7 +288,7 @@ extension PhotoInputViewController: MediasPreviewWindowDelegate {
288288
dismissMediasPreviewControllerAnimated()
289289
} else {
290290
gridViewController.updateSelectdAssets(assets)
291-
mediasPreviewViewController.updateAssets()
291+
mediasPreviewViewController.updateAssets(assets)
292292
}
293293
}
294294

Mixin/UserInterface/Windows/Cells/MediaPreviewCell.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import MixinServices
66

77
class MediaPreviewCell: UICollectionViewCell {
88

9-
static let reuseIdentifier = "cell_identifier_media_preview_cell"
109
static let cellSize = CGSize(width: 312, height: 312)
1110

1211
@IBOutlet weak var badge: BadgeView!
@@ -46,7 +45,7 @@ class MediaPreviewCell: UICollectionViewCell {
4645
if asset.mediaType == .video {
4746
mediaTypeView.style = .video(duration: asset.duration)
4847
} else {
49-
if let uti = asset.value(forKey: "uniformTypeIdentifier") as? String, UTTypeConformsTo(uti as CFString, kUTTypeGIF) {
48+
if let uti = asset.uniformTypeIdentifier, UTTypeConformsTo(uti as CFString, kUTTypeGIF) {
5049
mediaTypeView.style = .gif
5150
} else {
5251
mediaTypeView.style = .hidden

Mixin/UserInterface/Windows/Cells/MediaPreviewCell.xib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<objects>
1111
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
1212
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
13-
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="gTV-IL-0wX" customClass="MediaPreviewCell" customModule="Mixin" customModuleProvider="target">
13+
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="media_preview" id="gTV-IL-0wX" customClass="MediaPreviewCell" customModule="Mixin" customModuleProvider="target">
1414
<rect key="frame" x="0.0" y="0.0" width="312" height="312"/>
1515
<autoresizingMask key="autoresizingMask"/>
1616
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">

Mixin/UserInterface/Windows/MediasPreviewWindow.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class MediasPreviewWindow: BottomSheetView {
3333
collectionView.delegate = self
3434
collectionView.dataSource = self
3535
collectionView.allowsMultipleSelection = true
36-
collectionView.register(UINib(resource: R.nib.mediaPreviewCell), forCellWithReuseIdentifier: MediaPreviewCell.reuseIdentifier)
36+
collectionView.register(R.nib.mediaPreviewCell)
3737
}
3838

3939
override func layoutSubviews() {
@@ -93,7 +93,7 @@ extension MediasPreviewWindow: UICollectionViewDataSource {
9393
}
9494

9595
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
96-
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MediaPreviewCell.reuseIdentifier, for: indexPath) as! MediaPreviewCell
96+
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: R.reuseIdentifier.media_preview, for: indexPath)!
9797
if indexPath.item < assets.count {
9898
let asset = assets[indexPath.item]
9999
cell.load(asset: asset)

0 commit comments

Comments
 (0)