summaryrefslogtreecommitdiff
path: root/Juick/Classes/AccountManager.cs
blob: f7fcc75ee86e1c589f6f4c955daaaf79446093b9 (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
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)
        {
            Credentials = new NetworkCredential(login, pass);
            page.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            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()); 
        }
    }
}