using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Net.NetworkInformation; using Juick.Classes; using JuickApi; using Microsoft.Phone.Net.NetworkInformation; using RestSharp; using Microsoft.Phone.Notification; using System.Diagnostics; using System.Text; using System.Windows.Controls; namespace Juick.ViewModels { public class AppViewModel : ViewModelBase { static readonly string IsNetworkAvailablePropertyName = ExpressionHelper.GetPropertyName(x => x.NetworkUnavailable); readonly HttpNotificationChannel pushChannel; // The name of our push channel. string channelName = "JuickChannel"; public void UpdateNetworkStatus() { NetworkUnavailable = !DeviceNetworkInformation.IsNetworkAvailable; } public AppViewModel() { UpdateNetworkStatus(); NetworkChange.NetworkAddressChanged += (sender, args) => UpdateNetworkStatus(); pushChannel = HttpNotificationChannel.Find(channelName); // If the channel was not found, then create a new connection to the push service. if (pushChannel == null) { pushChannel = new HttpNotificationChannel(channelName); // Register for all the events before attempting to open the channel. pushChannel.ChannelUriUpdated += (sender, e) => EnableNotifications(); pushChannel.ErrorOccurred += (sender, e) => DisableNotifications(); // Register for this notification only if you need to receive the notifications while your application is running. // Register for this notification only if you need to receive the notifications while your application is running. pushChannel.ShellToastNotificationReceived += (sender, e) => { StringBuilder message = new StringBuilder(); string relativeUri = string.Empty; message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString()); // Parse out the information that was part of the message. foreach (string key in e.Collection.Keys) { message.AppendFormat("{0}: {1}\n", key, e.Collection[key]); if (string.Compare( key, "wp:Param", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CompareOptions.IgnoreCase) == 0) { relativeUri = e.Collection[key]; } } Debug.WriteLine("Received: " + message.ToString()); }; pushChannel.Open(); } else { // The channel was already open, so just register for all the events. pushChannel.ChannelUriUpdated += (sender, e) => EnableNotifications(); pushChannel.ErrorOccurred += (sender, e) => DisableNotifications(); // Register for this notification only if you need to receive the notifications while your application is running. pushChannel.ShellToastNotificationReceived += (sender, e) => { StringBuilder message = new StringBuilder(); string relativeUri = string.Empty; message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString()); // Parse out the information that was part of the message. foreach (string key in e.Collection.Keys) { message.AppendFormat("{0}: {1}\n", key, e.Collection[key]); if (string.Compare( key, "wp:Param", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CompareOptions.IgnoreCase) == 0) { relativeUri = e.Collection[key]; } } Debug.WriteLine("Received: " + message.ToString()); }; } CheckNewDataCommand = new DelegateCommand(CheckNewData, () => Account.IsAuthenticated); } private ObservableCollection _pages; public ObservableCollection Pages { get { return _pages ?? (_pages = new ObservableCollection()); } } static readonly string IsDataLoadingPropertyName = ExpressionHelper.GetPropertyName(x => x.IsDataLoading); bool _isDataLoading; public bool IsDataLoading { get { return _isDataLoading; } set { _isDataLoading = value; NotifyPropertyChanged(IsDataLoadingPropertyName); foreach (var page in Pages) { page.LoadMessagesPageCommand.NotifyCanExecuteChanged(); } } } private bool _isNetworkUnavailable; public bool NetworkUnavailable { get { return _isNetworkUnavailable; } set { _isNetworkUnavailable = value; NotifyPropertyChanged(IsNetworkAvailablePropertyName); } } public DelegateCommand CheckNewDataCommand { get; private set; } private AccountManager _acc; public AccountManager Account { get { return _acc ?? (_acc = new AccountManager()); } } private RestClient _cl; private readonly string _juickUri = HttpsHelper.CanUseHttps ? "https://api.juick.com" : "http://api.juick.com"; public RestClient Client { get { return _cl ?? (_cl = new RestClient(_juickUri) { UserAgent = "Juick 1.1/Windows Phone " + Environment.OSVersion.Version }); } } public void EnableNotifications() { if (!Account.IsAuthenticated) return; var channelUri = pushChannel.ChannelUri.ToString(); if (channelUri == Account.NotificationUri) return; Account.NotificationUri = channelUri; if (!pushChannel.IsShellToastBound) // Bind this new channel for toast events. pushChannel.BindToShellToast(); } public void DisableNotifications() { if (pushChannel.IsShellToastBound) pushChannel.UnbindToShellToast(); Account.NotificationUri = string.Empty; } public void CheckNewData(SelectionChangedEventArgs param) { var page = param.AddedItems[0] as PageViewModel; page.RefreshData(); } } }