using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using Hammock; using MahTweets.Core; using MahTweets.Core.Extensions; using MahTweets.Core.Extensions.MahTweets.Core.Extensions; using MahTweets.Core.Interfaces; using MahTweets.Core.LaterReadingProviders; using MahTweets.Core.Models; using MahTweets.Core.Repositories; using MahTweets.Core.Services; using MahTweets.Core.UpdateTypes; using MahTweets.Core.Utilities; using MahTweets.Extensions.MediaProviders; using MahTweets.Extensions.Twitter.Extensions; using MahTweets.Extensions.Twitter.Models; namespace MahTweets.Extensions.Twitter.ViewModels { public class SingleTweetViewModel : MahViewModelBase { private readonly AccountRepository _accountRepository; private readonly ContactRepository _contactRepo; private readonly LocationService _locationService; private readonly object _lockObject = new object(); private readonly IStatusService _statusService; private readonly List providers = new List { new EmbedlyProvider() }; private ThreadSafeObservableCollection _contacts; private string _id = ""; private Twitter _parent; private Tweet _update; public SingleTweetViewModel( IStatusService statusService, ContactRepository contactRepository, AccountRepository accountRepository, LocationService locationService) { _statusService = statusService; _contactRepo = contactRepository; _accountRepository = accountRepository; _locationService = locationService; Conversation = new ThreadSafeObservableCollection(); Contacts = new ThreadSafeObservableCollection(); } public string Id { get { return _id; } set { _id = value; RaisePropertyChanged("Id"); Update = _statusService.GetById(_id); } } public string Parent { set { if (_update == null) return; _parent = (Twitter)_accountRepository .Where(p => p.Credentials.AccountName == value) .First(); if (_update.InReplyToID != null && Conversation.Count == 0) FetchTweet(_update.InReplyToID); } } public Tweet Update { get { return _update; } set { _update = value; RaisePropertyChanged("Update"); } } public ThreadSafeObservableCollection Images { get { if (_update == null || !_update.HasLinks) return null; var images = new ThreadSafeObservableCollection(); string[] words = Update.Text.Split(' '); foreach (string word in words) { if (word.ContainsHyperlink()) { foreach (IImageProvider p in providers) { if (!p.IsMatch(word)) continue; p.GetImage(word, images.Add); break; } } } return images; } } public ThreadSafeObservableCollection Contacts { get { return _contacts; } set { _contacts = value; RaisePropertyChanged("Contacts"); } } public ThreadSafeObservableCollection Conversation { get; set; } private void FetchTweet(string latestId) { var firstTweet = _statusService.GetById(latestId); if (firstTweet != null) { Conversation.Add(firstTweet); LockAddContact(firstTweet.Contact); if (!string.IsNullOrEmpty(firstTweet.InReplyToID) && firstTweet.InReplyToID != "0") { FetchTweet(firstTweet.InReplyToID); } } else { _parent.TwitterClient.Statuses.BeginGetTweet(latestId, BeginGetTweetCallback); } } private void BeginGetTweetCallback(RestRequest req, RestResponse res, object obj) { try { if (obj != null && obj is MahApps.Twitter.Models.Tweet) { var tweet = (MahApps.Twitter.Models.Tweet)obj; var foundContact = Contacts.FirstOrDefault(x => x.Name == tweet.User.ScreenName); if (foundContact == null) { foundContact = _contactRepo.FetchOrCreate(tweet.User.ScreenName); LockAddContact(foundContact); } var t = tweet.ToStatus(_contactRepo); t.Types.AddUpdate(_parent); t.AddParent(_parent); Conversation.Add(t); if (!string.IsNullOrEmpty(t.InReplyToID) && t.InReplyToID != "0") { FetchTweet(t.InReplyToID); } } } catch (Exception ex) { //TODO: Logging Debug.WriteLine(ex.Message); } } public void LockAddContact(Contact contact) { lock (_lockObject) { if (!Contacts.Contains(contact)) { Contacts.Add(contact); } } } public void InstapaperTweet() { Instapaper.AddWithToast(String.Format("http://twitter.com/{0}/status/{1}", Update.Contact.Name, Update.ID)); } public void Retweet() { var parent = ((Twitter)Update.Parents.FirstOrDefault()); if (parent != null) parent.Retweet(Update.ID); } public void Favourite() { var parent = ((Twitter)Update.Parents.FirstOrDefault()); if (parent != null) parent.Favourite(Update.ID); } public void Block() { var parent = ((Twitter)Update.Parents.FirstOrDefault()); if (parent != null) parent.Block(Update.Contact.Name); } } }