TweetView.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Foundation;
  5. using AppKit;
  6. using TwitterSearch.Portable.Models;
  7. using System.Threading.Tasks;
  8. namespace TwitterSearch.Mac
  9. {
  10. public partial class TweetView : AppKit.NSView
  11. {
  12. #region Constructors
  13. // Called when created from unmanaged code
  14. public TweetView (IntPtr handle) : base (handle)
  15. {
  16. Initialize ();
  17. }
  18. // Called when created directly from a XIB file
  19. [Export ("initWithCoder:")]
  20. public TweetView (NSCoder coder) : base (coder)
  21. {
  22. Initialize ();
  23. }
  24. // Shared initialization code
  25. void Initialize ()
  26. {
  27. }
  28. #endregion
  29. public Tweet Tweet { get; set; }
  30. static Dictionary<string, NSImage> cachedImages = new Dictionary<string, NSImage> ();
  31. public override void ViewDidMoveToSuperview ()
  32. {
  33. Title.StringValue = string.Format ("{0} at {1}", Tweet.ScreenName, Tweet.Date);
  34. Text.StringValue = Tweet.Text;
  35. // We don't have SDWebImage to do caching for us
  36. if (cachedImages.ContainsKey (Tweet.Image)) {
  37. Image.Image = cachedImages [Tweet.Image];
  38. }
  39. else {
  40. Task.Factory.StartNew (() => {
  41. NSData d = NSData.FromUrl (new NSUrl (Tweet.Image));
  42. BeginInvokeOnMainThread (() => {
  43. NSImage i = new NSImage (d);
  44. if (!cachedImages.ContainsKey (Tweet.Image))
  45. cachedImages.Add (Tweet.Image, i);
  46. Image.Image = i;
  47. });
  48. });
  49. }
  50. }
  51. }
  52. }