Skip to content

Commit

Permalink
Remove cell animation
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyu committed Mar 3, 2022
1 parent a3562c7 commit 3c8d2f6
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import UIKit
import Photos
import MixinServices

protocol MediasPreviewViewControllerDelegate: AnyObject {
func mediasPreviewViewController(_ controller: MediasPreviewViewController, didSend assets: [PHAsset])
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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() {
Expand All @@ -73,7 +59,7 @@ extension MediasPreviewViewController {
collectionView.reloadData()
}

func updateAssets() {
func updateAssets(_ selectedAssets: [PHAsset]) {
cellSizeCache.removeAll()
assets = selectedAssets
collectionView.reloadData()
Expand All @@ -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)
}
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -288,7 +288,7 @@ extension PhotoInputViewController: MediasPreviewWindowDelegate {
dismissMediasPreviewControllerAnimated()
} else {
gridViewController.updateSelectdAssets(assets)
mediasPreviewViewController.updateAssets()
mediasPreviewViewController.updateAssets(assets)
}
}

Expand Down
3 changes: 1 addition & 2 deletions Mixin/UserInterface/Windows/Cells/MediaPreviewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Mixin/UserInterface/Windows/Cells/MediaPreviewCell.xib
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="gTV-IL-0wX" customClass="MediaPreviewCell" customModule="Mixin" customModuleProvider="target">
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="media_preview" id="gTV-IL-0wX" customClass="MediaPreviewCell" customModule="Mixin" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="312" height="312"/>
<autoresizingMask key="autoresizingMask"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
Expand Down
4 changes: 2 additions & 2 deletions Mixin/UserInterface/Windows/MediasPreviewWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3c8d2f6

Please sign in to comment.