예제 #1
0
        protected override void OnAppLinkRequestReceived(Uri uri)
        {
            string token = uri.Segments[2];

            if (!WakaTimeRepo.IsTokenValid(token))
            {
                Current.MainPage.DisplayAlert("Invalid Token", "An invalid token was supplied, you have not been signed in.", "Ok");

                return;
            }

            App.cache.RemoveAll();

            Preferences.Set("token", token);

            MainPage = new MainPage();

            Current.MainPage.DisplayAlert("Successful Login", "You can always log out at the bottom of the navigation menu.", "Ok");
        }
        private async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            NavigationItem item = e.SelectedItem as NavigationItem;

            if (item == null)
            {
                return;
            }

            if (item.TargetType != null)
            {
                Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
            }
            else
            {
                switch (item.Title)
                {
                case "Profile":
                    Detail = new NavigationPage(new UserPage(MainPage.CurrentLeader));

                    break;

                case "Login":
                    if (await DisplayAlert("Login", "You are about to be redirected.", "Continue", "Cancel"))
                    {
                        try
                        {
                            await Browser.OpenAsync(WakaTimeRepo.RedirectUrl, BrowserLaunchMode.External);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Failed to start browser, the device might not have a browser installed.");

                            break;
                        }

                        string result = await DisplayPromptAsync("Token", "Go To https://wakatime.damon.sh if you didn't get redirected.\n\nPut the login token in the field below after authenticating.", "Confirm", "Cancel");

                        if (WakaTimeRepo.IsTokenValid(result))
                        {
                            App.cache.RemoveAll();

                            Preferences.Set("token", result);

                            (Application.Current).MainPage = new MainPage();
                        }
                        else
                        {
                            DisplayAlert("Invalid Token", "An invalid WakaTime token has been inserted", "Ok");
                        }
                    }

                    break;

                case "Logout":
                    if (await DisplayAlert("Logout?", "Are you sure you want to logout?", "Yes, please", "No, please take me back"))
                    {
                        App.cache.RemoveAll();

                        Preferences.Remove("token");

                        (Application.Current).MainPage = new MainPage();
                    }

                    break;

                default:
                    break;
                }
            }

            IsPresented = false;
            masterPage.lvwNavigation.SelectedItem       = null;
            masterPage.lvwBottomNavigation.SelectedItem = null;
        }