Skip to content

Commit 8340d31

Browse files
Merge pull request #72 from dustintnguyen/master
Picker auto scroll to bottom when selecting images #71
2 parents 20745fb + 7474826 commit 8340d31

File tree

2 files changed

+131
-49
lines changed

2 files changed

+131
-49
lines changed

src/GMImagePicker/GMGridViewController.cs

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using CoreFoundation;
1818
using System.Diagnostics;
1919
using System.Runtime.CompilerServices;
20+
using System.Threading.Tasks;
2021

2122
namespace GMImagePicker
2223
{
@@ -188,55 +189,105 @@ private void Dismiss (object sender, EventArgs args)
188189
_picker.Dismiss (sender, args);
189190
}
190191

191-
public override void ViewDidLoad ()
192+
Task _viewDidLoadAsyncTask = Task.CompletedTask;
193+
194+
public virtual Task ViewDidLoadAsync()
192195
{
193-
base.ViewDidLoad ();
196+
return _viewDidLoadAsyncTask;
197+
}
194198

195-
SetupViews ();
199+
public sealed override async void ViewDidLoad ()
200+
{
201+
try
202+
{
203+
base.ViewDidLoad ();
196204

197-
if (!string.IsNullOrEmpty(_picker.CustomNavigationBarPrompt)) {
198-
NavigationItem.Prompt = _picker.CustomNavigationBarPrompt;
199-
}
205+
SetupViews ();
200206

201-
_imageManager = new PHCachingImageManager ();
202-
ResetCachedAssets ();
207+
if (!string.IsNullOrEmpty(_picker.CustomNavigationBarPrompt)) {
208+
NavigationItem.Prompt = _picker.CustomNavigationBarPrompt;
209+
}
203210

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+
}
206225
}
207226

208-
public override void ViewWillAppear(bool animated)
227+
Task _viewWillAppearAsyncTask = Task.CompletedTask;
228+
229+
public virtual Task ViewWillAppearAsync()
209230
{
210-
base.ViewWillAppear(animated);
211-
SetupButtons();
212-
SetupToolbar();
231+
return _viewWillAppearAsyncTask;
232+
}
213233

214-
if (_picker.GridSortOrder == SortOrder.Ascending)
234+
public sealed override async void ViewWillAppear(bool animated)
235+
{
236+
try
215237
{
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();
219242

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();
221247

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
224259
}
225260
}
226261

227262
NSIndexPath _newestItemPath;
228263

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)
230271
{
231-
base.ViewDidAppear (animated);
272+
try
273+
{
274+
await _viewDidLoadAsyncTask;
275+
await _viewWillAppearAsyncTask;
276+
base.ViewDidAppear(animated);
232277

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)
234288
{
235-
// Scroll to bottom (newest images are at the bottom)
236-
CollectionView.ScrollToItem(_newestItemPath, UICollectionViewScrollPosition.Bottom, false);
289+
//Handle
237290
}
238-
239-
UpdateCachedAssets();
240291
}
241292

242293
#region Asset Caching

src/GMImagePicker/GMImagePickerController.cs

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -659,35 +659,66 @@ public GMImagePickerController()
659659

660660
SetupNavigationController ();
661661
}
662+
Task _viewDidLoadAsyncTask = Task.CompletedTask;
663+
664+
public virtual Task ViewDidLoadAsync() {
665+
return _viewDidLoadAsyncTask;
666+
}
667+
668+
public sealed override async void ViewDidLoad() {
669+
try {
670+
base.ViewDidLoad();
671+
_viewDidLoadAsyncTask = ViewDidLoadAsync();
672+
await _viewDidLoadAsyncTask;
673+
}
674+
catch (Exception ex) {
675+
// Handle
676+
}
677+
}
678+
679+
Task _viewWillAppearAsyncTask = Task.CompletedTask;
680+
681+
public virtual Task ViewWillAppearAsync() {
682+
return _viewWillAppearAsyncTask;
683+
}
662684

663-
public override void ViewWillAppear (bool animated)
685+
public sealed override async void ViewWillAppear (bool animated)
664686
{
665-
base.ViewWillAppear (animated);
687+
try {
688+
await _viewDidLoadAsyncTask;
689+
base.ViewWillAppear (animated);
666690

667-
// Ensure nav and toolbar customisations are set. Defaults are in place, but the user may have changed them
668-
View.BackgroundColor = PickerBackgroundColor;
691+
// Ensure nav and toolbar customisations are set. Defaults are in place, but the user may have changed them
692+
View.BackgroundColor = PickerBackgroundColor;
669693

670-
_navigationController.Toolbar.Translucent = true;
694+
_navigationController.Toolbar.Translucent = true;
671695

672-
_navigationController.Toolbar.BarTintColor = ToolbarBarTintColor;
673-
_navigationController.Toolbar.TintColor = ToolbarTintColor;
674-
_navigationController.Toolbar.BackgroundColor = ToolbarBackgroundColor;
696+
_navigationController.Toolbar.BarTintColor = ToolbarBarTintColor;
697+
_navigationController.Toolbar.TintColor = ToolbarTintColor;
698+
_navigationController.Toolbar.BackgroundColor = ToolbarBackgroundColor;
675699

676-
_navigationController.NavigationBar.TintColor = NavigationBarTintColor;
677-
_navigationController.NavigationBar.BarTintColor = NavigationBarBarTintColor;
678-
_navigationController.NavigationBar.BackgroundColor = NavigationBarBackgroundColor;
700+
_navigationController.NavigationBar.TintColor = NavigationBarTintColor;
701+
_navigationController.NavigationBar.BarTintColor = NavigationBarBarTintColor;
702+
_navigationController.NavigationBar.BackgroundColor = NavigationBarBackgroundColor;
679703

680-
UIStringAttributes attributes;
681-
if (UseCustomFontForNavigationBar) {
682-
attributes = new UIStringAttributes { ForegroundColor = NavigationBarTextColor,
683-
Font = UIFont.FromName (PickerBoldFontName, PickerFontHeaderSize)
684-
};
685-
} else {
686-
attributes = new UIStringAttributes { ForegroundColor = NavigationBarTextColor };
687-
}
688-
_navigationController.NavigationBar.TitleTextAttributes = attributes;
704+
UIStringAttributes attributes;
705+
if (UseCustomFontForNavigationBar) {
706+
attributes = new UIStringAttributes { ForegroundColor = NavigationBarTextColor,
707+
Font = UIFont.FromName (PickerBoldFontName, PickerFontHeaderSize)
708+
};
709+
} else {
710+
attributes = new UIStringAttributes { ForegroundColor = NavigationBarTextColor };
711+
}
712+
_navigationController.NavigationBar.TitleTextAttributes = attributes;
689713

690-
UpdateToolbar ();
714+
UpdateToolbar ();
715+
_viewWillAppearAsyncTask = ViewWillAppearAsync();
716+
await _viewWillAppearAsyncTask;
717+
}
718+
catch (Exception ex)
719+
{
720+
//Handle
721+
}
691722
}
692723

693724
/// <summary>

0 commit comments

Comments
 (0)