From f845109f6679e78501c1f3363315a0cf39af29e9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 23 Apr 2013 16:09:43 +0400 Subject: Fix enable/disable notifications flow --- Juick/Classes/AccountManager.cs | 40 ++++++++++++++++++------ Juick/MainPage.xaml.cs | 2 +- Juick/ViewModels/AppViewModel.cs | 63 +++++++++++++++----------------------- Juick/ViewModels/LoginViewModel.cs | 3 +- 4 files changed, 59 insertions(+), 49 deletions(-) (limited to 'Juick') diff --git a/Juick/Classes/AccountManager.cs b/Juick/Classes/AccountManager.cs index 1f9ffe5..eb99d92 100644 --- a/Juick/Classes/AccountManager.cs +++ b/Juick/Classes/AccountManager.cs @@ -3,6 +3,8 @@ using System.IO.IsolatedStorage; using System.Net; using System.Windows; using System.Windows.Controls; +using RestSharp; +using System.Diagnostics; namespace Juick.Classes { @@ -10,14 +12,14 @@ namespace Juick.Classes { private string _userName; private string _password; - + public string UserName { get { if (_userName == null) { - IsolatedStorageSettings.ApplicationSettings.TryGetValue("user", out _userName); + IsolatedStorageSettings.ApplicationSettings.TryGetValue("user", out _userName); } return _userName; } @@ -34,7 +36,7 @@ namespace Juick.Classes { if (_password == null) { - IsolatedStorageSettings.ApplicationSettings.TryGetValue("password", out _password); + IsolatedStorageSettings.ApplicationSettings.TryGetValue("password", out _password); } return _password; } @@ -47,7 +49,8 @@ namespace Juick.Classes public bool IsAuthenticated { - get { + get + { bool authenticated; IsolatedStorageSettings.ApplicationSettings.TryGetValue("authenticated", out authenticated); return authenticated; @@ -55,26 +58,45 @@ namespace Juick.Classes set { IsolatedStorageSettings.ApplicationSettings["authenticated"] = value; } } + + public string NotificationUri { get { - string savedUri; - IsolatedStorageSettings.ApplicationSettings.TryGetValue("notification_uri", out savedUri); - return savedUri; + string _notificationUri; + IsolatedStorageSettings.ApplicationSettings.TryGetValue("notification_uri", out _notificationUri); + return _notificationUri; } set { + var oldValue = NotificationUri; + if (!string.IsNullOrEmpty(oldValue)) + UnregisterNotificationUrl(oldValue); IsolatedStorageSettings.ApplicationSettings["notification_uri"] = value; + if (!string.IsNullOrEmpty(value)) + RegisterNotificationUrl(value); } } - - + + public void SignOut(Page page) { IsAuthenticated = false; + App.AppContext.DisableNotifications(); page.NavigationService.Navigate(new Uri("/LoginView.xaml", UriKind.Relative)); page.Dispatcher.BeginInvoke(() => page.NavigationService.RemoveBackEntry()); } + + void RegisterNotificationUrl(string newUrl) + { + App.AppContext.Client.ExecuteAsync(new RestRequest("/winphone/register?url=" + newUrl), + response => Debug.WriteLine("Registering push url, status {0}: {1}", response.Request.Resource, response.StatusCode)); + } + void UnregisterNotificationUrl(string oldUrl) + { + App.AppContext.Client.ExecuteAsync(new RestRequest("/winphone/unregister?url=" + oldUrl), + response => Debug.WriteLine("Unregistered push url, status {0}: {1}", response.Request.Resource, response.StatusCode)); + } } } diff --git a/Juick/MainPage.xaml.cs b/Juick/MainPage.xaml.cs index 20beef5..7baa6dd 100644 --- a/Juick/MainPage.xaml.cs +++ b/Juick/MainPage.xaml.cs @@ -70,7 +70,7 @@ namespace Juick } if (!string.IsNullOrEmpty(FileId) || navigateUri.StartsWith(loginUriPart)) { - NavigationService.Navigate(new Uri(navigateUri, UriKind.Relative)); + ((App)Application.Current).NavigateTo(new Uri(navigateUri, UriKind.Relative), true); } if (queryStrings.ContainsKey("mid")) { diff --git a/Juick/ViewModels/AppViewModel.cs b/Juick/ViewModels/AppViewModel.cs index 7d5006c..ab00cb0 100644 --- a/Juick/ViewModels/AppViewModel.cs +++ b/Juick/ViewModels/AppViewModel.cs @@ -28,8 +28,13 @@ namespace Juick.ViewModels 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(); + 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. @@ -56,17 +61,15 @@ namespace Juick.ViewModels } Debug.WriteLine("Received: " + message.ToString()); }; - - pushChannel.Open(); - - // Bind this new channel for toast events. - pushChannel.BindToShellToast(); - + pushChannel.Open(); } else { // The channel was already open, so just register for all the events. - pushChannel.ChannelUriUpdated += (sender, e) => EnableNotifications(e.ChannelUri.ToString()); + 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. @@ -92,10 +95,8 @@ namespace Juick.ViewModels } } Debug.WriteLine("Received: " + message.ToString()); - }; - - EnableNotifications(pushChannel.ChannelUri.ToString()); - } + }; + } } private ObservableCollection _pages; public ObservableCollection Pages @@ -139,37 +140,23 @@ namespace Juick.ViewModels } } - public void EnableNotifications(string Url) + public void EnableNotifications() { 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); - } + 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() { - UnregisterNotificationUrl(Account.NotificationUri); + if (pushChannel.IsShellToastBound) + pushChannel.UnbindToShellToast(); 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)); - } + } } } diff --git a/Juick/ViewModels/LoginViewModel.cs b/Juick/ViewModels/LoginViewModel.cs index 2bc5bda..c0bf1f9 100644 --- a/Juick/ViewModels/LoginViewModel.cs +++ b/Juick/ViewModels/LoginViewModel.cs @@ -59,11 +59,12 @@ namespace Juick.ViewModels App.AppContext.Account.UserName = Username; App.AppContext.Account.Password = Password; App.AppContext.Account.IsAuthenticated = true; + App.AppContext.EnableNotifications(); ((App)Application.Current).NavigateTo(NextUri, true); } else { - //AddError("Username", "Invalid username or password", false); + MessageBox.Show("Invalid username or password", "Error", MessageBoxButton.OK); } }); } -- cgit v1.2.3