Skip to content
This repository
branch: master
file 111 lines (101 sloc) 4.038 kb
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DropNet.Samples.WP7.ViewModels;

namespace DropNet.Samples.WP7
{
    public partial class MainPage : PhoneApplicationPage
    {
        private MainViewModel _model;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            //Basic MVVM stuff
            _model = new MainViewModel();
            _model.ShowLogin = true;
            this.DataContext = _model;
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            App.DropNetClient.GetTokenAsync(userToken =>
                {
                    //Dont really need to do anything with the usertoken yet as its stored inside the client for the session

                    //Now we want to use the new Request token we have to generate an Authorize Url
                    var tokenUrl = App.DropNetClient.BuildAuthorizeUrl("http://dkdevelopment.net/BoxShotLogin.htm"); //Spelt correctly in v1.8.1
                    //Capture the LoadCompleted event from the browser so we can check when the user has logged in
                    loginBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(loginBrowser_LoadCompleted);
                    //Open a browser with the URL
                    loginBrowser.Navigate(new Uri(tokenUrl));
                },
                (error) =>
                {
                    //OH DEAR GOD WHAT HAPPENED?!
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            MessageBox.Show(error.Message);
                        });
                });
        }

        void loginBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            //Check for the callback path here (or just check it against "/1/oauth/authorize")
            if (e.Uri.AbsolutePath == "/BoxShotLogin.htm")
            {
                //The User has logged in!
                //Now to convert the Request Token into an Access Token
                App.DropNetClient.GetAccessTokenAsync(response =>
                    {
                        //GREAT SUCCESS!
                        //Now we should save the Token and Secret so the user doesnt have to login next time
                        //response.Token;
                        //response.Secret;

                        //Now lets load the root contents and hide the login on the page
                        _model.ShowContents = true;
                        _model.ShowLogin = false;

                        LoadContents();
                    },
                    (error) =>
                    {
                        //OH DEAR GOD WHAT HAPPENED?!
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            MessageBox.Show(error.Message);
                        });
                    });
            }
            else
            {
                //Probably the login page loading, ignore
            }
        }

        private void LoadContents()
        {
            App.DropNetClient.GetMetaDataAsync("/", (response) =>
                {
                    _model.Meta = response;
                },
                (error) =>
                {
                    //OH DEAR GOD WHAT HAPPENED?!
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show(error.Message);
                    });
                });
        }
    }
}
Something went wrong with that request. Please try again.