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; 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. private const 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(e.ChannelUri.ToString()); _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) => { var 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(); // Bind this new channel for toast events. _pushChannel.BindToShellToast(); } else { // The channel was already open, so just register for all the events. _pushChannel.ChannelUriUpdated += (sender, e) => EnableNotifications(e.ChannelUri.ToString()); _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()); }; EnableNotifications(_pushChannel.ChannelUri.ToString()); } } 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); } } private AccountManager _acc; public AccountManager Account { get { return _acc ?? (_acc = new AccountManager()); } } private RestClient _cl; public RestClient Client { get { return _cl ?? (_cl = new RestClient("http://api.juick.com") { UserAgent = "Juick 1.1/Windows Phone " + Environment.OSVersion.Version }); } } public void EnableNotifications(string Url) { if (!Account.IsAuthenticated) return; Client.Authenticator = new HttpBasicAuthenticator(Account.UserName, Account.Password); Debug.WriteLine(Url.ToString()); if (string.IsNullOrEmpty(Account.NotificationUri) || Account.NotificationUri == Url) { Account.NotificationUri = Url; RegisterNotificationUrl(Url); } else { UnregisterNotificationUrl(Account.NotificationUri); Account.NotificationUri = Url; RegisterNotificationUrl(Url); } } public void DisableNotifications() { UnregisterNotificationUrl(Account.NotificationUri); Account.NotificationUri = string.Empty; } void RegisterNotificationUrl(string newUrl) { Client.ExecuteAsync(new RestRequest("/winphone/register?url=" + newUrl), response => Debug.WriteLine(response.StatusCode)); } void UnregisterNotificationUrl(string oldUrl) { Client.ExecuteAsync(new RestRequest("/winphone/unregister?url=" + oldUrl), response => Debug.WriteLine(response.StatusCode)); } } }