using System; using System.Collections.ObjectModel; using System.Diagnostics; using System.Net.NetworkInformation; using System.Windows.Controls; using Juick.Classes; using Microsoft.Phone.Net.NetworkInformation; using RestSharp; using Windows.Networking.PushNotifications; namespace Juick.ViewModels { public class AppViewModel : ViewModelBase { static readonly string IsNetworkAvailablePropertyName = ExpressionHelper.GetPropertyName(x => x.NetworkUnavailable); PushNotificationChannel pushNotificationChannel; public void UpdateNetworkStatus() { NetworkUnavailable = !DeviceNetworkInformation.IsNetworkAvailable; } public AppViewModel() { UpdateNetworkStatus(); NetworkChange.NetworkAddressChanged += (sender, args) => UpdateNetworkStatus(); PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync() .AsTask() .ContinueWith(x => { if(x.IsFaulted) { Debug.WriteLine(x.Exception.GetBaseException().ToString()); } else { pushNotificationChannel = x.Result; pushNotificationChannel.PushNotificationReceived += PushNotificationChannel_PushNotificationReceived; } }); CheckNewDataCommand = new DelegateCommand(CheckNewData, () => Account.IsAuthenticated); } void PushNotificationChannel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args) { Debug.WriteLine("PushNotificationReceived: " + args.NotificationType); } private ObservableCollection _pages; public ObservableCollection Pages { get { return _pages ?? (_pages = new ObservableCollection()); } } static readonly string IsDataLoadingPropertyName = ExpressionHelper.GetPropertyName(x => x.IsDataLoading); bool _isDataLoading; public bool IsDataLoading { get { return _isDataLoading; } set { _isDataLoading = value; NotifyPropertyChanged(IsDataLoadingPropertyName); foreach (var page in Pages) { page.LoadMessagesPageCommand.NotifyCanExecuteChanged(); } } } private bool _isNetworkUnavailable; public bool NetworkUnavailable { get { return _isNetworkUnavailable; } set { _isNetworkUnavailable = value; NotifyPropertyChanged(IsNetworkAvailablePropertyName); } } public DelegateCommand CheckNewDataCommand { get; private set; } private AccountManager _acc; public AccountManager Account { get { return _acc ?? (_acc = new AccountManager()); } } private readonly string _juickUri = HttpsHelper.CanUseHttps ? "https://api.juick.com" : "http://api.juick.com"; private RestClient _cl; public RestClient Client { get { return _cl ?? (_cl = new RestClient(_juickUri) { UserAgent = "Juick 1.1/Windows Phone " + Environment.OSVersion.Version }); } } public void EnableNotifications() { if (!Account.IsAuthenticated) return; if(pushNotificationChannel == null || pushNotificationChannel.Uri == null) return; var channelUri = pushNotificationChannel.Uri; if (channelUri == Account.NotificationUri) return; Account.NotificationUri = channelUri; } public void DisableNotifications() { if(pushNotificationChannel != null) pushNotificationChannel.Close(); Account.NotificationUri = string.Empty; } public void CheckNewData(SelectionChangedEventArgs param) { var page = param.AddedItems[0] as PageViewModel; page.RefreshData(); } } }