ViewController.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using Foundation;
  3. using UIKit;
  4. using ImageSearch.ViewModel;
  5. using SDWebImage;
  6. namespace ImageSearch.iOS
  7. {
  8. public partial class ViewController : UIViewController, IUICollectionViewDataSource
  9. {
  10. ImageSearchViewModel viewModel;
  11. public ViewController (IntPtr handle) : base (handle)
  12. {
  13. }
  14. public override void ViewDidLoad ()
  15. {
  16. base.ViewDidLoad ();
  17. viewModel = new ImageSearchViewModel();
  18. CollectionViewImages.WeakDataSource = this;
  19. //Button Click event to get images
  20. ButtonSearch.TouchUpInside += async (sender, args) =>
  21. {
  22. ButtonSearch.Enabled = false;
  23. ActivityIsLoading.StartAnimating();
  24. await viewModel.SearchForImagesAsync(TextFieldQuery.Text);
  25. CollectionViewImages.ReloadData();
  26. ButtonSearch.Enabled = true;
  27. ActivityIsLoading.StopAnimating();
  28. };
  29. }
  30. public override void DidReceiveMemoryWarning ()
  31. {
  32. base.DidReceiveMemoryWarning ();
  33. // Release any cached data, images, etc that aren't in use.
  34. }
  35. public nint GetItemsCount(UICollectionView collectionView, nint section) =>
  36. viewModel.Images.Count;
  37. public UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
  38. {
  39. var cell = collectionView.DequeueReusableCell("imagecell", indexPath) as ImageCell;
  40. var item = viewModel.Images[indexPath.Row];
  41. cell.Caption.Text = item.Title;
  42. cell.Image.SetImage(new NSUrl(item.ThumbnailLink));
  43. return cell;
  44. }
  45. }
  46. }