|
17 | 17 | using CoreFoundation; |
18 | 18 | using System.Diagnostics; |
19 | 19 | using System.Runtime.CompilerServices; |
| 20 | +using System.Threading.Tasks; |
20 | 21 |
|
21 | 22 | namespace GMImagePicker |
22 | 23 | { |
@@ -188,55 +189,105 @@ private void Dismiss (object sender, EventArgs args) |
188 | 189 | _picker.Dismiss (sender, args); |
189 | 190 | } |
190 | 191 |
|
191 | | - public override void ViewDidLoad () |
| 192 | + Task _viewDidLoadAsyncTask = Task.CompletedTask; |
| 193 | + |
| 194 | + public virtual Task ViewDidLoadAsync() |
192 | 195 | { |
193 | | - base.ViewDidLoad (); |
| 196 | + return _viewDidLoadAsyncTask; |
| 197 | + } |
194 | 198 |
|
195 | | - SetupViews (); |
| 199 | + public sealed override async void ViewDidLoad () |
| 200 | + { |
| 201 | + try |
| 202 | + { |
| 203 | + base.ViewDidLoad (); |
196 | 204 |
|
197 | | - if (!string.IsNullOrEmpty(_picker.CustomNavigationBarPrompt)) { |
198 | | - NavigationItem.Prompt = _picker.CustomNavigationBarPrompt; |
199 | | - } |
| 205 | + SetupViews (); |
200 | 206 |
|
201 | | - _imageManager = new PHCachingImageManager (); |
202 | | - ResetCachedAssets (); |
| 207 | + if (!string.IsNullOrEmpty(_picker.CustomNavigationBarPrompt)) { |
| 208 | + NavigationItem.Prompt = _picker.CustomNavigationBarPrompt; |
| 209 | + } |
203 | 210 |
|
204 | | - // Register for changes |
205 | | - PHPhotoLibrary.SharedPhotoLibrary.RegisterChangeObserver (this); |
| 211 | + _imageManager = new PHCachingImageManager (); |
| 212 | + ResetCachedAssets (); |
| 213 | + |
| 214 | + // Register for changes |
| 215 | + PHPhotoLibrary.SharedPhotoLibrary.RegisterChangeObserver (this); |
| 216 | + |
| 217 | + _viewDidLoadAsyncTask = ViewDidLoadAsync(); |
| 218 | + await _viewDidLoadAsyncTask; |
| 219 | + |
| 220 | + } |
| 221 | + catch(Exception ex) |
| 222 | + { |
| 223 | + //Handle |
| 224 | + } |
206 | 225 | } |
207 | 226 |
|
208 | | - public override void ViewWillAppear(bool animated) |
| 227 | + Task _viewWillAppearAsyncTask = Task.CompletedTask; |
| 228 | + |
| 229 | + public virtual Task ViewWillAppearAsync() |
209 | 230 | { |
210 | | - base.ViewWillAppear(animated); |
211 | | - SetupButtons(); |
212 | | - SetupToolbar(); |
| 231 | + return _viewWillAppearAsyncTask; |
| 232 | + } |
213 | 233 |
|
214 | | - if (_picker.GridSortOrder == SortOrder.Ascending) |
| 234 | + public sealed override async void ViewWillAppear(bool animated) |
| 235 | + { |
| 236 | + try |
215 | 237 | { |
216 | | - // Scroll to bottom (newest images are at the bottom) |
217 | | - CollectionView.SetNeedsLayout(); |
218 | | - CollectionView.LayoutIfNeeded(); |
| 238 | + await _viewDidLoadAsyncTask; |
| 239 | + base.ViewWillAppear(animated); |
| 240 | + SetupButtons(); |
| 241 | + SetupToolbar(); |
219 | 242 |
|
220 | | - CollectionView.SetContentOffset(new CGPoint(0, CollectionView.CollectionViewLayout.CollectionViewContentSize.Height), false); |
| 243 | + if (_picker.GridSortOrder == SortOrder.Ascending) { |
| 244 | + // Scroll to bottom (newest images are at the bottom) |
| 245 | + CollectionView.SetNeedsLayout(); |
| 246 | + CollectionView.LayoutIfNeeded(); |
221 | 247 |
|
222 | | - var item = CollectionView.NumberOfItemsInSection(0) - 1; |
223 | | - _newestItemPath = NSIndexPath.FromItemSection(item, 0); |
| 248 | + CollectionView.SetContentOffset(new CGPoint(0, CollectionView.CollectionViewLayout.CollectionViewContentSize.Height), false); |
| 249 | + |
| 250 | + var item = CollectionView.NumberOfItemsInSection(0) - 1; |
| 251 | + _newestItemPath = NSIndexPath.FromItemSection(item, 0); |
| 252 | + } |
| 253 | + _viewWillAppearAsyncTask = ViewWillAppearAsync(); |
| 254 | + await _viewWillAppearAsyncTask; |
| 255 | + } |
| 256 | + catch(Exception ex) |
| 257 | + { |
| 258 | + //Handle |
224 | 259 | } |
225 | 260 | } |
226 | 261 |
|
227 | 262 | NSIndexPath _newestItemPath; |
228 | 263 |
|
229 | | - public override void ViewDidAppear (bool animated) |
| 264 | + Task _viewDidAppearAsyncTask = Task.CompletedTask; |
| 265 | + public virtual Task ViewDidAppearAsync() |
| 266 | + { |
| 267 | + return _viewDidAppearAsyncTask; |
| 268 | + } |
| 269 | + |
| 270 | + public sealed override async void ViewDidAppear (bool animated) |
230 | 271 | { |
231 | | - base.ViewDidAppear (animated); |
| 272 | + try |
| 273 | + { |
| 274 | + await _viewDidLoadAsyncTask; |
| 275 | + await _viewWillAppearAsyncTask; |
| 276 | + base.ViewDidAppear(animated); |
232 | 277 |
|
233 | | - if (_newestItemPath != null && _newestItemPath.Section >= 0 && _newestItemPath.Row >= 0 && _newestItemPath.Item >= 0 && _picker.GridSortOrder == SortOrder.Ascending) |
| 278 | + if (_newestItemPath != null && _newestItemPath.Section >= 0 && _newestItemPath.Row >= 0 && _newestItemPath.Item >= 0 && _picker.GridSortOrder == SortOrder.Ascending) { |
| 279 | + // Scroll to bottom (newest images are at the bottom) |
| 280 | + CollectionView.ScrollToItem(_newestItemPath, UICollectionViewScrollPosition.Bottom, false); |
| 281 | + } |
| 282 | + |
| 283 | + UpdateCachedAssets(); |
| 284 | + _viewDidAppearAsyncTask = ViewDidAppearAsync(); |
| 285 | + await _viewDidAppearAsyncTask; |
| 286 | + } |
| 287 | + catch(Exception ex) |
234 | 288 | { |
235 | | - // Scroll to bottom (newest images are at the bottom) |
236 | | - CollectionView.ScrollToItem(_newestItemPath, UICollectionViewScrollPosition.Bottom, false); |
| 289 | + //Handle |
237 | 290 | } |
238 | | - |
239 | | - UpdateCachedAssets(); |
240 | 291 | } |
241 | 292 |
|
242 | 293 | #region Asset Caching |
|
0 commit comments