diff options
Diffstat (limited to 'Juick/ViewModels/AppViewModel.cs')
-rw-r--r-- | Juick/ViewModels/AppViewModel.cs | 103 |
1 files changed, 69 insertions, 34 deletions
diff --git a/Juick/ViewModels/AppViewModel.cs b/Juick/ViewModels/AppViewModel.cs index ab00cb0..07ba7cf 100644 --- a/Juick/ViewModels/AppViewModel.cs +++ b/Juick/ViewModels/AppViewModel.cs @@ -2,8 +2,10 @@ 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; @@ -13,34 +15,37 @@ namespace Juick.ViewModels { public class AppViewModel : ViewModelBase { - readonly HttpNotificationChannel pushChannel; + static readonly string IsNetworkAvailablePropertyName = ExpressionHelper.GetPropertyName<AppViewModel>(x => x.NetworkUnavailable); + readonly HttpNotificationChannel _pushChannel; // The name of our push channel. - string channelName = "JuickChannel"; + private const string channelName = "JuickChannel"; + + public void UpdateNetworkStatus() + { + NetworkUnavailable = !DeviceNetworkInformation.IsNetworkAvailable; + } public AppViewModel() { - pushChannel = HttpNotificationChannel.Find(channelName); + 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) + if (_pushChannel == null) { - pushChannel = new HttpNotificationChannel(channelName); + _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(); - }; + _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) => + _pushChannel.ShellToastNotificationReceived += (sender, e) => { - StringBuilder message = new StringBuilder(); + var message = new StringBuilder(); string relativeUri = string.Empty; message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString()); @@ -61,19 +66,21 @@ namespace Juick.ViewModels } Debug.WriteLine("Received: " + message.ToString()); }; - pushChannel.Open(); + + _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(); - }; - pushChannel.ErrorOccurred += (sender, e) => DisableNotifications(); + _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) => + _pushChannel.ShellToastNotificationReceived += (sender, e) => { StringBuilder message = new StringBuilder(); string relativeUri = string.Empty; @@ -95,8 +102,10 @@ namespace Juick.ViewModels } } Debug.WriteLine("Received: " + message.ToString()); - }; - } + }; + + EnableNotifications(_pushChannel.ChannelUri.ToString()); + } } private ObservableCollection<PageViewModel> _pages; public ObservableCollection<PageViewModel> Pages @@ -121,6 +130,18 @@ namespace Juick.ViewModels } } + private bool _isNetworkUnavailable; + public bool NetworkUnavailable + { + get { return _isNetworkUnavailable; } + set + { + _isNetworkUnavailable = value; + NotifyPropertyChanged(IsNetworkAvailablePropertyName); + } + } + + private AccountManager _acc; public AccountManager Account @@ -140,23 +161,37 @@ namespace Juick.ViewModels } } - public void EnableNotifications() + public void EnableNotifications(string Url) { 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(); + 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() { - if (pushChannel.IsShellToastBound) - pushChannel.UnbindToShellToast(); + 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)); + } } } |