using System; using System.Collections.Generic; using System.IO.IsolatedStorage; using System.Linq; using System.Windows; using System.Windows.Controls; using Autofac; using GalaSoft.MvvmLight.Messaging; using MahTweets.Core; using MahTweets.Core.Events; using MahTweets.Core.Extensions; using MahTweets.Core.Services; using Microsoft.Phone.Tasks; using Phone.Controls; namespace MahTweets.Extensions.Twitter { public class TweetActionBase : UserControl { public static DependencyProperty ShowProperty = DependencyProperty.RegisterAttached("Show", typeof (bool), typeof (TweetActionBase), new PropertyMetadata( ShowChanged)); public AbstractStatus Update { get { return DataContext as AbstractStatus; } } public INavigationService NavigationService { get; set; } private static void ShowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var source = (TweetActionBase) d; if (!(bool) e.NewValue) VisualStateManager.GoToState(source, "DefaultState", true); else { VisualStateManager.GoToState(source, "ActionState", true); Messenger.Default.Send(new SelectedItemEvent(source.Update)); } } public static void SetShow(DependencyObject o, bool value) { o.SetValue(ShowProperty, value); } public static bool GetShow(DependencyObject o) { return (bool) o.GetValue(ShowProperty); } public void ViewLinks() { var links = new List(); string[] words = Update.Text.Split(' '); foreach (string word in words) { if (word.ContainsHyperlink()) links.Add(word); else if ((word.StartsWith("@") || word.StartsWith(".@") || word.StartsWith("-@") || word.StartsWith("r@") || word.StartsWith("rt@")) && word != "@") { links.Add(word); } else if (word.StartsWith("#") && word != "#") { links.Add(word); } } var dialog = new PickerBoxDialog { SelectedIndex = -1, Title = "Links", ItemSource = links }; dialog.Closed += DialogClosed; dialog.Show(); } private void DialogClosed(object sender, EventArgs e) { var nav = IoC.Container.Resolve(); var link = (string) ((PickerBoxDialog) sender).SelectedItem; if (link.ContainsHyperlink()) { bool UseInternalBrowser = true; if (IsolatedStorageSettings.ApplicationSettings.Contains("UseInternalBrowser")) UseInternalBrowser = (bool) IsolatedStorageSettings.ApplicationSettings["UseInternalBrowser"]; if (UseInternalBrowser) nav.Navigate(new Uri("/Views/BrowserView.xaml?Url=" + Uri.EscapeUriString(link), UriKind.RelativeOrAbsolute)); else { var task = new WebBrowserTask {URL = link}; task.Show(); } } else if (link.StartsWith("@")) { nav.Navigate( new Uri( "/MahTweets.Extensions.Twitter;component/Views/ProfileView.xaml?Name=" + link.Substring(1), UriKind.RelativeOrAbsolute)); } else if (link.StartsWith("#")) { nav.Navigate( new Uri("/Views/SearchView.xaml?Term=" + link.Substring(1), UriKind.RelativeOrAbsolute)); } } public void More() { if (!Update.Parents.Any()) { MessageBox.Show("You can't reply to a tweet if you dont have a Twitter account setup!"); return; } ToggleOpen(); var nav = IoC.Container.Resolve(); nav.Navigate( new Uri( "/MahTweets.Extensions.Twitter;component/Views/SingleTweetView.xaml?Rand="+DateTime.Now+"&Id=" + Update.ID + "&Parent=" + ((Twitter) Update.Parents.First()).Credentials.AccountName, UriKind.RelativeOrAbsolute)); } public void Reply() { if (!Update.Parents.Any()) { MessageBox.Show("You can't reply to a tweet if you dont have a Twitter account setup!"); return; } ToggleOpen(); Messenger.Default.Send(new OpenCompositionEvent(CompostionMode.All, Update)); } public void Retweet() { if (!Update.Parents.Any()) { MessageBox.Show("You can't retweet to a tweet if you dont have a Twitter account setup!"); return; } ((Twitter) Update.Parents.First()).Retweet(Update.ID); ToggleOpen(); } public void Favourite() { if (!Update.Parents.Any()) { MessageBox.Show("You can't favourite to a tweet if you dont have a Twitter account setup!"); return; } ((Twitter) Update.Parents.First()).Favourite(Update.ID); ToggleOpen(); } public void ToggleOpen() { Update.Open = !Update.Open; } } }