summaryrefslogtreecommitdiff
path: root/Juick/Classes/AccountManager.cs
blob: 82fc44689d41547bec7211fd75ba3dd48528e8bf (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
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Xml;
using System.Xml.Serialization;

namespace Juick.Classes
{
    public class AccountManager
    {
        const string FileName = "account.xml";
        public NetworkCredential Credentials
        {
            get
            {
                return new NetworkCredential
                           {
                               UserName = IsolatedStorageSettings.ApplicationSettings.Contains("user") ?
                               IsolatedStorageSettings.ApplicationSettings["user"] as string : null,
                               Password = IsolatedStorageSettings.ApplicationSettings.Contains("password") ?
                               IsolatedStorageSettings.ApplicationSettings["password"] as string : null,
                           };
            }
            set
            {
                var creds = value;
                if (creds != null)
                {
                    IsolatedStorageSettings.ApplicationSettings["user"] = value.UserName;
                    IsolatedStorageSettings.ApplicationSettings["password"] = value.Password;
                }
                else
                {
                    IsolatedStorageSettings.ApplicationSettings["user"] = null;
                    IsolatedStorageSettings.ApplicationSettings["password"] = null;
                }
            }
        }

        public void SignIn(Page page, string login, string pass, Uri nextUri)
        {
            Credentials = new NetworkCredential(login, pass);
            page.NavigationService.Navigate(nextUri);
            page.Dispatcher.BeginInvoke(() => page.NavigationService.RemoveBackEntry());
        }

        public void SignOut(Page page)
        {
            Credentials = null;
            page.NavigationService.Navigate(new Uri("/LoginView.xaml", UriKind.Relative));
            page.Dispatcher.BeginInvoke(() => page.NavigationService.RemoveBackEntry());
        }
    }
}