summaryrefslogtreecommitdiff
path: root/Juick/ViewModels
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2013-04-26 22:04:18 +0400
committerGravatar Vitaly Takmazov2013-04-26 22:04:18 +0400
commitee46fb9228fd6e70893270703d814a400bb8ca22 (patch)
tree32bbe9e4b43c1fb6906ac708009020cc1990bbf0 /Juick/ViewModels
parente7fb2a3794bc7fb3c6592312d2628d9d11eedf3b (diff)
Fixed crash when network is unavailable, added warning message
Diffstat (limited to 'Juick/ViewModels')
-rw-r--r--Juick/ViewModels/AppViewModel.cs52
-rw-r--r--Juick/ViewModels/PageViewModel.cs2
2 files changed, 39 insertions, 15 deletions
diff --git a/Juick/ViewModels/AppViewModel.cs b/Juick/ViewModels/AppViewModel.cs
index 7d5006c..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,29 +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(e.ChannelUri.ToString());
- 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());
@@ -57,20 +67,20 @@ namespace Juick.ViewModels
Debug.WriteLine("Received: " + message.ToString());
};
- pushChannel.Open();
+ _pushChannel.Open();
// Bind this new channel for toast events.
- pushChannel.BindToShellToast();
+ _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();
+ _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;
@@ -94,7 +104,7 @@ namespace Juick.ViewModels
Debug.WriteLine("Received: " + message.ToString());
};
- EnableNotifications(pushChannel.ChannelUri.ToString());
+ EnableNotifications(_pushChannel.ChannelUri.ToString());
}
}
private ObservableCollection<PageViewModel> _pages;
@@ -120,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
diff --git a/Juick/ViewModels/PageViewModel.cs b/Juick/ViewModels/PageViewModel.cs
index e398caf..ce48449 100644
--- a/Juick/ViewModels/PageViewModel.cs
+++ b/Juick/ViewModels/PageViewModel.cs
@@ -72,6 +72,8 @@ namespace Juick.ViewModels
_context.Client.ExecuteAsync<List<Message>>(request, response =>
{
_context.IsDataLoading = false;
+ if (response.Data == null)
+ return;
response.Data.Select(x => new PostItem(x)).ToList().ForEach(i => Items.Add(i));
});
_context.IsDataLoading = true;