summaryrefslogtreecommitdiff
path: root/Juick/ViewModels/AppViewModel.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Juick/ViewModels/AppViewModel.cs')
-rw-r--r--Juick/ViewModels/AppViewModel.cs63
1 files changed, 25 insertions, 38 deletions
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<PageViewModel> _pages;
public ObservableCollection<PageViewModel> 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));
- }
+ }
}
}