1
1
import UIKit
2
2
import Photos
3
- import MixinServices
4
3
5
4
protocol MediasPreviewViewControllerDelegate : AnyObject {
6
5
func mediasPreviewViewController( _ controller: MediasPreviewViewController , didSend assets: [ PHAsset ] )
@@ -17,14 +16,8 @@ final class MediasPreviewViewController: UIViewController {
17
16
@IBOutlet weak var sendButton : UIButton !
18
17
19
18
weak var delegate : MediasPreviewViewControllerDelegate ?
20
- weak var gridViewController : PhotoInputGridViewController ?
21
19
22
20
private var cellSizeCache = [ String: CGSize] ( )
23
- private var isAddingAsset = false
24
- private var isRemovingAsset = false
25
- private var selectedAssets : [ PHAsset ] {
26
- gridViewController? . selectedAssets ?? [ ]
27
- }
28
21
private var assets = [ PHAsset] ( ) {
29
22
didSet {
30
23
sendButton. setTitle ( R . string. localizable. chat_media_send_count ( assets. count) , for: . normal)
@@ -44,9 +37,8 @@ final class MediasPreviewViewController: UIViewController {
44
37
extension MediasPreviewViewController {
45
38
46
39
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 ) ] )
50
42
collectionView. scrollToItem ( at: IndexPath ( item: assets. count - 1 , section: 0 ) ,
51
43
at: . centeredHorizontally,
52
44
animated: true )
@@ -56,15 +48,9 @@ extension MediasPreviewViewController {
56
48
guard let index = assets. firstIndex ( of: asset) else {
57
49
return
58
50
}
51
+ assets. remove ( at: index)
59
52
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 ) ] )
68
54
}
69
55
70
56
func removeAllAssets( ) {
@@ -73,7 +59,7 @@ extension MediasPreviewViewController {
73
59
collectionView. reloadData ( )
74
60
}
75
61
76
- func updateAssets( ) {
62
+ func updateAssets( _ selectedAssets : [ PHAsset ] ) {
77
63
cellSizeCache. removeAll ( )
78
64
assets = selectedAssets
79
65
collectionView. reloadData ( )
@@ -96,7 +82,7 @@ extension MediasPreviewViewController: UICollectionViewDataSource {
96
82
guard let self = self else {
97
83
return
98
84
}
99
- self . removeMediaAnimated ( asset: asset , cell : cell )
85
+ self . remove ( asset)
100
86
self . delegate? . mediasPreviewViewController ( self , didRemove: asset)
101
87
}
102
88
}
@@ -111,12 +97,6 @@ extension MediasPreviewViewController: UICollectionViewDelegateFlowLayout, UICol
111
97
cellSizeForItemAt ( indexPath. item)
112
98
}
113
99
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
-
120
100
func collectionView( _ collectionView: UICollectionView , didSelectItemAt indexPath: IndexPath ) {
121
101
delegate? . mediasPreviewViewController ( self , didSelectAssetAt: indexPath. item)
122
102
}
@@ -149,38 +129,4 @@ extension MediasPreviewViewController {
149
129
}
150
130
}
151
131
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
-
186
132
}
0 commit comments