summaryrefslogtreecommitdiff
path: root/Juick/ViewModels
diff options
context:
space:
mode:
authorGravatar k0st1x2015-12-26 01:11:34 +0300
committerGravatar k0st1x2015-12-26 01:11:34 +0300
commit7d98c0b7de38999cfc0b81540d0ce425c7a0e15e (patch)
treebcf100fcf152d4675c64b9ebb399cd90366fbab2 /Juick/ViewModels
parent73443e2fd916747e9d6c0bc5b0365e50813207e0 (diff)
migrate to WNS
Diffstat (limited to 'Juick/ViewModels')
-rw-r--r--Juick/ViewModels/AppViewModel.cs117
-rw-r--r--Juick/ViewModels/LoginViewModel.cs3
2 files changed, 28 insertions, 92 deletions
diff --git a/Juick/ViewModels/AppViewModel.cs b/Juick/ViewModels/AppViewModel.cs
index 4aa9321..b4b287b 100644
--- a/Juick/ViewModels/AppViewModel.cs
+++ b/Juick/ViewModels/AppViewModel.cs
@@ -1,26 +1,19 @@
using System;
-using System.Collections.Generic;
using System.Collections.ObjectModel;
-using System.Linq;
+using System.Diagnostics;
using System.Net.NetworkInformation;
+using System.Windows.Controls;
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;
+using Windows.Networking.PushNotifications;
namespace Juick.ViewModels
{
public class AppViewModel : ViewModelBase
{
static readonly string IsNetworkAvailablePropertyName = ExpressionHelper.GetPropertyName<AppViewModel>(x => x.NetworkUnavailable);
- readonly HttpNotificationChannel pushChannel;
-
- // The name of our push channel.
- string channelName = "JuickChannel";
+ PushNotificationChannel pushNotificationChannel;
public void UpdateNetworkStatus()
{
@@ -31,77 +24,23 @@ namespace Juick.ViewModels
{
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];
- }
+ PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync()
+ .AsTask()
+ .ContinueWith(x => {
+ if(x.IsFaulted) {
+ Debug.WriteLine(x.Exception.GetBaseException().ToString());
+ } else {
+ pushNotificationChannel = x.Result;
+ pushNotificationChannel.PushNotificationReceived += PushNotificationChannel_PushNotificationReceived;
}
- 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<SelectionChangedEventArgs>(CheckNewData, () => Account.IsAuthenticated);
}
+
+ void PushNotificationChannel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args) {
+ Debug.WriteLine("PushNotificationReceived: " + args.NotificationType);
+ }
+
private ObservableCollection<PageViewModel> _pages;
public ObservableCollection<PageViewModel> Pages
{
@@ -144,10 +83,8 @@ namespace Juick.ViewModels
get { return _acc ?? (_acc = new AccountManager()); }
}
- private RestClient _cl;
-
private readonly string _juickUri = HttpsHelper.CanUseHttps ? "https://api.juick.com" : "http://api.juick.com";
-
+ private RestClient _cl;
public RestClient Client
{
get
@@ -162,21 +99,19 @@ namespace Juick.ViewModels
public void EnableNotifications()
{
if (!Account.IsAuthenticated) return;
- if (pushChannel.ChannelUri == null) return;
- var channelUri = pushChannel.ChannelUri.ToString();
+ if(pushNotificationChannel == null || pushNotificationChannel.Uri == null)
+ return;
+ var channelUri = pushNotificationChannel.Uri;
if (channelUri == Account.NotificationUri)
return;
- Account.NotificationUri = channelUri;
- if (!pushChannel.IsShellToastBound)
- // Bind this new channel for toast events.
- pushChannel.BindToShellToast();
+ Account.NotificationUri = channelUri;
}
public void DisableNotifications()
{
- if (pushChannel.IsShellToastBound)
- pushChannel.UnbindToShellToast();
- Account.NotificationUri = string.Empty;
+ if(pushNotificationChannel != null)
+ pushNotificationChannel.Close();
+ Account.NotificationUri = string.Empty;
}
public void CheckNewData(SelectionChangedEventArgs param)
diff --git a/Juick/ViewModels/LoginViewModel.cs b/Juick/ViewModels/LoginViewModel.cs
index f6f50ec..c593c06 100644
--- a/Juick/ViewModels/LoginViewModel.cs
+++ b/Juick/ViewModels/LoginViewModel.cs
@@ -3,10 +3,11 @@ using System.Net;
using System.Windows;
using Juick.Classes;
using RestSharp;
+using RestSharp.Authenticators;
namespace Juick.ViewModels
{
- public class LoginViewModel : ViewModelBase
+ public class LoginViewModel : ViewModelBase
{
private string _username;
private string _password;