using System; using System.Collections.Generic; using System.Diagnostics; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Navigation; using GalaSoft.MvvmLight.Messaging; using MahTweets.Mobile.Core; using MahTweets.Mobile.Core.Interfaces; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; namespace MahTweets.Mobile { public partial class App { private static List _updateTypes; // Avoid double-initialization private bool _phoneApplicationInitialized; public PhoneApplicationFrame RootFrame { get; private set; } public App() { UnhandledException += ApplicationUnhandledException; if (Debugger.IsAttached) { Current.Host.Settings.EnableFrameRateCounter = true; } InitializeComponent(); InitializePhoneApplication(); Startup += AppStartup; // MahTweets specific stuff GlobalDispatcher.Dispatcher = Deployment.Current.Dispatcher; } // Code to execute when the application is launching (eg, from Start) // This code will not execute when the application is reactivated private void ApplicationLaunching(object sender, LaunchingEventArgs e) { ViewModelLocator.Current.Initialise(); ViewModelLocator.Current.MainViewModel.LoadTimeline(); ViewModelLocator.Current.MainViewModel.HandleNotifications(); } // Code to execute when the application is activated (brought to foreground) // This code will not execute when the application is first launched private void ApplicationActivated(object sender, ActivatedEventArgs e) { ViewModelLocator.Current.Initialise(); ViewModelLocator.Current.MainViewModel.LoadTimeline(); ViewModelLocator.Current.MainViewModel.HandleNotifications(); } // Code to execute when the application is deactivated (sent to background) // This code will not execute when the application is closing private void ApplicationDeactivated(object sender, DeactivatedEventArgs e) { ViewModelLocator.Current.MainViewModel.SaveTimeline(); } // Code to execute when the application is closing (eg, user hit Back) // This code will not execute when the application is deactivated private void ApplicationClosing(object sender, ClosingEventArgs e) { ViewModelLocator.Current.MainViewModel.SaveTimeline(); } // Do not add any additional code to this method private void InitializePhoneApplication() { if (_phoneApplicationInitialized) return; RootFrame = new TransitionFrame(); RootFrame.Navigated += CompleteInitializePhoneApplication; RootFrame.NavigationFailed += RootFrameNavigationFailed; RootFrame.Obscured += RootFrameObscured; RootFrame.Unobscured += RootFrameUnobscured; // Ensure we don't initialize again _phoneApplicationInitialized = true; } private void RootFrameUnobscured(object sender, EventArgs e) { ViewModelLocator.Current.MainViewModel.RestartTimer(); Messenger.Default.Send(new ResumeTimerEvent()); } private void RootFrameObscured(object sender, ObscuredEventArgs e) { ViewModelLocator.Current.MainViewModel.StopTimer(); Messenger.Default.Send(new PauseTimerEvent()); } private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e) { if (RootVisual != RootFrame) RootVisual = RootFrame; RootFrame.Navigated -= CompleteInitializePhoneApplication; } private static void RootFrameNavigationFailed(object sender, NavigationFailedEventArgs e) { if (Debugger.IsAttached) { Debugger.Break(); } ViewModelLocator.Current.Feedback.SendException(e.Exception); } // Code to execute on Unhandled Exceptions private static void ApplicationUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { if (Debugger.IsAttached) { // An unhandled exception has occurred; break into the debugger Debugger.Break(); } ViewModelLocator.Current.Feedback.SendException(e.ExceptionObject); } static void AppStartup(object sender, StartupEventArgs e) { } public static IEnumerable UpdateTypes { get { return _updateTypes ?? (_updateTypes = new List { new NormalUpdate(), new MentionUpdate(), new DirectMessageUpdate() }); } } //HACK: THIS SHOULD NOT BE HERE BUT ARGH STUPID PERFORMANCE OVERHEADS private void ImageMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { ViewModelLocator.Current.NavigationService.Navigate( new Uri( "/MahTweets.Mobile.TwitterPlugin;component/Views/ProfileView.xaml?Name=" + ((IStatusUpdate)((Image)sender).DataContext).Contact.Name, UriKind.RelativeOrAbsolute)); } } }