@@ -15,23 +15,15 @@ import Photos
15
15
16
16
class MTImagePickerAssetsController : UIViewController , UICollectionViewDataSource , UICollectionViewDelegate {
17
17
18
- weak var delegate : MTImagePickerControllerDelegate ?
19
- var maxCount : Int = Int . max
18
+ weak var delegate : MTImagePickerDataSourceDelegate !
20
19
var groupModel : MTImagePickerAlbumModel !
21
- var source : MTImagePickerSource = . ALAsset
22
20
23
21
@IBOutlet weak var collectionView : MTImagePickerCollectionView !
24
22
@IBOutlet weak var lbSelected : UILabel !
25
23
@IBOutlet weak var btnPreview : UIButton !
26
24
27
25
private var dataSource = [ MTImagePickerModel] ( )
28
- private var selectedSource = Set < MTImagePickerModel > ( )
29
26
private var initialScrollDone : Bool = false
30
- private var navigation : MTImagePickerController {
31
- get {
32
- return self . navigationController as! MTImagePickerController
33
- }
34
- }
35
27
36
28
class var instance : MTImagePickerAssetsController {
37
29
get {
@@ -44,26 +36,24 @@ class MTImagePickerAssetsController :UIViewController,UICollectionViewDataSource
44
36
//MARK: Lifecycle
45
37
override func viewDidLoad( ) {
46
38
super. viewDidLoad ( )
47
- let loading = LoadingViewController ( )
48
- loading. show ( text: " Loading... " . localized)
49
39
if let title = self . groupModel. getAlbumName ( ) {
50
40
self . title = title
51
41
}
42
+ let loading = LoadingViewController ( )
43
+ loading. show ( text: " Loading... " . localized)
52
44
self . groupModel? . getMTImagePickerModelsListAsync { ( models) in
53
45
loading. dismiss ( )
54
46
self . dataSource = models
55
47
self . collectionView. reloadData ( )
56
48
self . scrollToBottom ( )
57
49
}
58
-
59
- self . initUI ( )
60
50
}
61
51
62
52
override func viewWillAppear( _ animated: Bool ) {
63
53
super. viewWillAppear ( animated)
64
54
self . collectionView. reloadData ( )
65
- self . lbSelected. text = String ( self . selectedSource. count)
66
- self . btnPreview. isEnabled = !( self . selectedSource. count == 0 )
55
+ self . lbSelected. text = String ( delegate . selectedSource. count)
56
+ self . btnPreview. isEnabled = !( delegate . selectedSource. count == 0 )
67
57
}
68
58
69
59
override func viewDidLayoutSubviews( ) {
@@ -97,7 +87,7 @@ class MTImagePickerAssetsController :UIViewController,UICollectionViewDataSource
97
87
}
98
88
cell. imageView. image = model. getThumbImage ( size: cell. imageView. frame. size)
99
89
cell. indexPath = indexPath
100
- cell. btnCheck. isSelected = self . selectedSource. contains ( model)
90
+ cell. btnCheck. isSelected = delegate . selectedSource. contains ( model)
101
91
cell. btnCheck. addTarget ( self , action: #selector( MTImagePickerAssetsController . btnCheckTouch ( _: ) ) , for: . touchUpInside)
102
92
cell. leading. constant = self . collectionView. leading. constant
103
93
cell. trailing. constant = self . collectionView. leading. constant
@@ -111,31 +101,26 @@ class MTImagePickerAssetsController :UIViewController,UICollectionViewDataSource
111
101
}
112
102
113
103
@objc func btnCheckTouch( _ sender: UIButton ) {
114
- if self . selectedSource. count < self . maxCount || sender. isSelected == true {
104
+ if delegate . selectedSource. count < delegate . maxCount || sender. isSelected == true {
115
105
sender. isSelected = !sender. isSelected
116
- let indexPath = ( sender. superview? . superview as! MTImagePickerCell ) . indexPath
106
+ let index = ( sender. superview? . superview as! MTImagePickerCell ) . indexPath. row
117
107
if sender. isSelected {
118
- self . selectedSource. insert ( self . dataSource [ ( indexPath ? . row ) ! ] )
108
+ delegate . selectedSource. append ( self . dataSource [ index ] )
119
109
sender. heartbeatsAnimation ( duration: 0.15 )
120
110
} else {
121
- self . selectedSource. remove ( self . dataSource [ ( indexPath? . row) !] )
111
+ if let removeIndex = delegate. selectedSource. index ( of: self . dataSource [ index] ) {
112
+ delegate. selectedSource. remove ( at: removeIndex)
113
+ }
122
114
}
123
- self . lbSelected. text = String ( self . selectedSource. count)
115
+ self . lbSelected. text = String ( delegate . selectedSource. count)
124
116
self . lbSelected. heartbeatsAnimation ( duration: 0.15 )
125
- self . btnPreview. isEnabled = !( self . selectedSource. count == 0 )
117
+ self . btnPreview. isEnabled = !( delegate . selectedSource. count == 0 )
126
118
} else {
127
119
let alertView = FlashAlertView ( message: " Maxium selected " . localized, delegate: nil )
128
120
alertView. show ( )
129
121
}
130
122
}
131
123
132
- func showUnAuthorize( ) {
133
- DispatchQueue . main. async {
134
- let alertView = UIAlertView ( title: " Notice " . localized, message: " 照片访问权限被禁用,请前往系统设置->隐私->照片中,启用本程序对照片的访问权限 " , delegate: nil , cancelButtonTitle: " OK " . localized)
135
- alertView. show ( )
136
- }
137
- }
138
-
139
124
//旋转处理
140
125
override func willRotate( to toInterfaceOrientation: UIInterfaceOrientation , duration: TimeInterval ) {
141
126
if self . interfaceOrientation. isPortrait != toInterfaceOrientation. isPortrait {
@@ -154,54 +139,25 @@ class MTImagePickerAssetsController :UIViewController,UICollectionViewDataSource
154
139
}
155
140
}
156
141
157
- private func initUI( ) {
158
- self . title = " All Photos " . localized
159
- }
160
-
161
142
private func pushToImageSelectorPreviewController( initialIndexPath: IndexPath ? , dataSource: [ MTImagePickerModel ] ) {
162
143
let vc = MTImagePickerPreviewController . instance
163
144
vc. dataSource = dataSource
164
- vc. selectedSource = self . selectedSource
145
+ vc. delegate = self . delegate
165
146
vc. initialIndexPath = initialIndexPath
166
- vc. maxCount = self . maxCount
167
- vc. dismiss = {
168
- selectedSource in
169
- self . selectedSource = selectedSource
170
- self . collectionView. reloadData ( )
171
- }
172
147
self . navigationController? . pushViewController ( vc, animated: true )
173
148
}
174
149
175
- private func getSelectedSortedSource( ) -> [ MTImagePickerModel ] {
176
- var dataSource = [ MTImagePickerModel] ( )
177
- for model in self . selectedSource. sorted ( by: { return $0. sortNumber < $1. sortNumber} ) {
178
- dataSource. append ( model)
179
- }
180
- return dataSource
181
- }
182
-
183
150
//MARK: IBActions
184
151
@IBAction func btnFinishTouch( _ sender: AnyObject ) {
185
- let dataSource = self . getSelectedSortedSource ( )
186
- if self . source == . Photos {
187
- if #available( iOS 8 . 0 , * ) {
188
- self . delegate? . imagePickerController ? ( picker: self . navigation, didFinishPickingWithPhotosModels: dataSource as! [ MTImagePickerPhotosModel ] )
189
- } else {
190
- // Fallback on earlier versions
191
- }
192
- } else {
193
- self . delegate? . imagePickerController ? ( picker: self . navigation, didFinishPickingWithAssetsModels: dataSource as! [ MTImagePickerAssetsModel ] )
194
- }
195
- self . dismiss ( animated: true , completion: nil )
152
+ delegate. didFinishPicking ( )
196
153
}
197
154
198
155
@IBAction func btnPreviewTouch( _ sender: AnyObject ) {
199
- let dataSource = self . getSelectedSortedSource ( )
156
+ let dataSource = delegate . selectedSource
200
157
self . pushToImageSelectorPreviewController ( initialIndexPath: nil , dataSource: dataSource)
201
158
}
202
159
@IBAction func btnCancelTouch( _ sender: AnyObject ) {
203
- self . delegate? . imagePickerControllerDidCancel ? ( picker: self . navigation)
204
- self . dismiss ( animated: true , completion: nil )
160
+ delegate. didCancel ( )
205
161
}
206
162
}
207
163
0 commit comments