summaryrefslogtreecommitdiff
path: root/Juick/ViewModels/AppViewModel.cs
blob: b4b287ba2d6e9d01b9936ec3d6f69cdf04fb5d1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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<AppViewModel>(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<SelectionChangedEventArgs>(CheckNewData, () => Account.IsAuthenticated);
        }

        void PushNotificationChannel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args) {
            Debug.WriteLine("PushNotificationReceived: " + args.NotificationType);
        }

        private ObservableCollection<PageViewModel> _pages;
        public ObservableCollection<PageViewModel> Pages
        {
            get { return _pages ?? (_pages = new ObservableCollection<PageViewModel>()); }
        }

        static readonly string IsDataLoadingPropertyName = ExpressionHelper.GetPropertyName<AppViewModel>(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<SelectionChangedEventArgs> 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();
        }
    }
}