Skip to content

Commit

Permalink
Feature: Added support for logging in/out of GitHub (#14085)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaira2 authored Nov 26, 2023
1 parent ae07ddf commit 9e5b053
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/Files.App/Dialogs/SettingsDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,20 @@
<FontIcon Glyph="&#xE8EC;" />
</NavigationViewItem.Icon>
</NavigationViewItem>
<NavigationViewItem
AccessKey="G"
AutomationProperties.AutomationId="SettingsItemGit"
Content="{helpers:ResourceString Name=Git}"
Tag="4">
<NavigationViewItem.Icon>
<FontIcon Glyph="&#xE794;" />
</NavigationViewItem.Icon>
</NavigationViewItem>
<NavigationViewItem
AccessKey="E"
AutomationProperties.AutomationId="SettingsItemAdvanced"
Content="{helpers:ResourceString Name=Advanced}"
Tag="4">
Tag="5">
<NavigationViewItem.Icon>
<FontIcon Glyph="&#xF1AD;" />
</NavigationViewItem.Icon>
Expand All @@ -133,7 +142,7 @@
AccessKey="B"
AutomationProperties.AutomationId="SettingsItemAbout"
Content="{helpers:ResourceString Name=About}"
Tag="5">
Tag="6">
<NavigationViewItem.Icon>
<FontIcon FontSize="16" Glyph="&#xE946;" />
</NavigationViewItem.Icon>
Expand Down
5 changes: 3 additions & 2 deletions src/Files.App/Dialogs/SettingsDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ private void MainSettingsNavigationView_SelectionChanged(NavigationView sender,
1 => SettingsContentFrame.Navigate(typeof(AppearancePage)),
2 => SettingsContentFrame.Navigate(typeof(FoldersPage)),
3 => SettingsContentFrame.Navigate(typeof(TagsPage)),
4 => SettingsContentFrame.Navigate(typeof(AdvancedPage)),
5 => SettingsContentFrame.Navigate(typeof(AboutPage)),
4 => SettingsContentFrame.Navigate(typeof(GitPage)),
5 => SettingsContentFrame.Navigate(typeof(AdvancedPage)),
6 => SettingsContentFrame.Navigate(typeof(AboutPage)),
_ => SettingsContentFrame.Navigate(typeof(AppearancePage))
};
}
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Helpers/CredentialsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static void SavePassword(string resourceName, string username, string pas
vault.Add(credential);
}

// Remove saved credentials from the vault
public static void DeleteSavedPassword(string resourceName, string username)
{
var vault = new PasswordVault();
Expand Down
12 changes: 12 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -3614,4 +3614,16 @@
<data name="FailedToSetBackground" xml:space="preserve">
<value>Failed to set the background wallpaper</value>
</data>
<data name="ConnectedToGitHub" xml:space="preserve">
<value>Connected to GitHub</value>
</data>
<data name="Logout" xml:space="preserve">
<value>Logout</value>
</data>
<data name="ConnectToGitHub" xml:space="preserve">
<value>Connect to GitHub</value>
</data>
<data name="Login" xml:space="preserve">
<value>Login</value>
</data>
</root>
12 changes: 12 additions & 0 deletions src/Files.App/Utils/Git/GitHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,18 @@ public static GitItemModel GetGitInformationForItem(Repository repository, strin
return gitItemModel;
}

// Remove saved credentails
public static void RemoveSavedCredentials()
{
CredentialsHelpers.DeleteSavedPassword(GIT_RESOURCE_NAME, GIT_RESOURCE_USERNAME);
}

// Get saved credentails
public static string GetSavedCredentials()
{
return CredentialsHelpers.GetPassword(GIT_RESOURCE_NAME, GIT_RESOURCE_USERNAME);
}

public static async Task InitializeRepositoryAsync(string? path)
{
if (string.IsNullOrWhiteSpace(path))
Expand Down
43 changes: 43 additions & 0 deletions src/Files.App/ViewModels/Settings/GitViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using System.Windows.Input;

namespace Files.App.ViewModels.Settings
{
public class GitViewModel : ObservableObject
{
protected readonly IFileTagsSettingsService FileTagsSettingsService = Ioc.Default.GetRequiredService<IFileTagsSettingsService>();

public ICommand RemoveCredentialsCommand { get; }
public ICommand ConnectToGitHubCommand { get; }

// Enabled when there are saved credentials
private bool _IsLogoutEnabled;
public bool IsLogoutEnabled
{
get => _IsLogoutEnabled;
set => SetProperty(ref _IsLogoutEnabled, value);
}

public GitViewModel()
{
RemoveCredentialsCommand = new RelayCommand(DoRemoveCredentials);
ConnectToGitHubCommand = new RelayCommand(DoConnectToGitHubAsync);

IsLogoutEnabled = GitHelpers.GetSavedCredentials() != string.Empty;
}

public void DoRemoveCredentials()
{
GitHelpers.RemoveSavedCredentials();
IsLogoutEnabled = false;
}

public async void DoConnectToGitHubAsync()
{
UIHelpers.CloseAllDialogs();
await GitHelpers.RequireGitAuthenticationAsync();
}
}
}
73 changes: 73 additions & 0 deletions src/Files.App/Views/Settings/GitPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<!-- Copyright (c) 2023 Files Community. Licensed under the MIT License. See the LICENSE. -->
<Page
x:Class="Files.App.Views.Settings.GitPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.UI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:helpers="using:Files.App.Helpers"
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:icore="using:Microsoft.Xaml.Interactions.Core"
xmlns:local="using:Files.App.UserControls.Settings"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:Files.App.ViewModels.Settings"
mc:Ignorable="d">

<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ResourceDictionaries/RightAlignedToggleSwitchStyle.xaml" />
</ResourceDictionary.MergedDictionaries>

<converters:BoolNegationConverter x:Key="BoolNegationConverter" />
</ResourceDictionary>
</Page.Resources>

<Page.DataContext>
<vm:GitViewModel x:Name="ViewModel" />
</Page.DataContext>

<Grid>
<StackPanel
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Spacing="4">
<StackPanel.ChildrenTransitions>
<TransitionCollection>
<EntranceThemeTransition />
</TransitionCollection>
</StackPanel.ChildrenTransitions>

<!-- Title -->
<TextBlock
Padding="0,0,0,12"
FontSize="24"
FontWeight="Medium"
Text="{helpers:ResourceString Name=Git}" />

<!-- Connect to GitHub -->
<local:SettingsBlockControl
x:Name="ConnectToGitHubSection"
Title="{helpers:ResourceString Name=ConnectToGitHub}"
HorizontalAlignment="Stretch"
x:Load="{x:Bind ViewModel.IsLogoutEnabled, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}">
<local:SettingsBlockControl.Icon>
<FontIcon Glyph="&#xF0B9;" />
</local:SettingsBlockControl.Icon>
<Button Command="{x:Bind ViewModel.ConnectToGitHubCommand}" Content="{helpers:ResourceString Name=Login}" />
</local:SettingsBlockControl>

<!-- Remove credentials -->
<local:SettingsBlockControl
x:Name="RemoveCredentialsSection"
Title="{helpers:ResourceString Name=ConnectedToGitHub}"
HorizontalAlignment="Stretch"
x:Load="{x:Bind ViewModel.IsLogoutEnabled, Mode=OneWay}">
<local:SettingsBlockControl.Icon>
<FontIcon Glyph="&#xF0B9;" />
</local:SettingsBlockControl.Icon>
<Button Command="{x:Bind ViewModel.RemoveCredentialsCommand}" Content="{helpers:ResourceString Name=Logout}" />
</local:SettingsBlockControl>
</StackPanel>
</Grid>
</Page>
15 changes: 15 additions & 0 deletions src/Files.App/Views/Settings/GitPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Microsoft.UI.Xaml.Controls;

namespace Files.App.Views.Settings
{
public sealed partial class GitPage : Page
{
public GitPage()
{
InitializeComponent();
}
}
}
1 change: 1 addition & 0 deletions tests/Files.InteractionTests/Tests/SettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void VerifySettingsAreAccessible()
"SettingsItemAppearance",
"SettingsItemFolders",
"SettingsItemTags",
"SettingsItemGit",
"SettingsItemAdvanced",
"SettingsItemAbout"
};
Expand Down

0 comments on commit 9e5b053

Please sign in to comment.