Skip to content

Commit 9e5b053

Browse files
authored
Feature: Added support for logging in/out of GitHub (#14085)
1 parent ae07ddf commit 9e5b053

File tree

9 files changed

+171
-4
lines changed

9 files changed

+171
-4
lines changed

src/Files.App/Dialogs/SettingsDialog.xaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,20 @@
120120
<FontIcon Glyph="&#xE8EC;" />
121121
</NavigationViewItem.Icon>
122122
</NavigationViewItem>
123+
<NavigationViewItem
124+
AccessKey="G"
125+
AutomationProperties.AutomationId="SettingsItemGit"
126+
Content="{helpers:ResourceString Name=Git}"
127+
Tag="4">
128+
<NavigationViewItem.Icon>
129+
<FontIcon Glyph="&#xE794;" />
130+
</NavigationViewItem.Icon>
131+
</NavigationViewItem>
123132
<NavigationViewItem
124133
AccessKey="E"
125134
AutomationProperties.AutomationId="SettingsItemAdvanced"
126135
Content="{helpers:ResourceString Name=Advanced}"
127-
Tag="4">
136+
Tag="5">
128137
<NavigationViewItem.Icon>
129138
<FontIcon Glyph="&#xF1AD;" />
130139
</NavigationViewItem.Icon>
@@ -133,7 +142,7 @@
133142
AccessKey="B"
134143
AutomationProperties.AutomationId="SettingsItemAbout"
135144
Content="{helpers:ResourceString Name=About}"
136-
Tag="5">
145+
Tag="6">
137146
<NavigationViewItem.Icon>
138147
<FontIcon FontSize="16" Glyph="&#xE946;" />
139148
</NavigationViewItem.Icon>

src/Files.App/Dialogs/SettingsDialog.xaml.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ private void MainSettingsNavigationView_SelectionChanged(NavigationView sender,
5353
1 => SettingsContentFrame.Navigate(typeof(AppearancePage)),
5454
2 => SettingsContentFrame.Navigate(typeof(FoldersPage)),
5555
3 => SettingsContentFrame.Navigate(typeof(TagsPage)),
56-
4 => SettingsContentFrame.Navigate(typeof(AdvancedPage)),
57-
5 => SettingsContentFrame.Navigate(typeof(AboutPage)),
56+
4 => SettingsContentFrame.Navigate(typeof(GitPage)),
57+
5 => SettingsContentFrame.Navigate(typeof(AdvancedPage)),
58+
6 => SettingsContentFrame.Navigate(typeof(AboutPage)),
5859
_ => SettingsContentFrame.Navigate(typeof(AppearancePage))
5960
};
6061
}

src/Files.App/Helpers/CredentialsHelpers.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public static void SavePassword(string resourceName, string username, string pas
1313
vault.Add(credential);
1414
}
1515

16+
// Remove saved credentials from the vault
1617
public static void DeleteSavedPassword(string resourceName, string username)
1718
{
1819
var vault = new PasswordVault();

src/Files.App/Strings/en-US/Resources.resw

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3614,4 +3614,16 @@
36143614
<data name="FailedToSetBackground" xml:space="preserve">
36153615
<value>Failed to set the background wallpaper</value>
36163616
</data>
3617+
<data name="ConnectedToGitHub" xml:space="preserve">
3618+
<value>Connected to GitHub</value>
3619+
</data>
3620+
<data name="Logout" xml:space="preserve">
3621+
<value>Logout</value>
3622+
</data>
3623+
<data name="ConnectToGitHub" xml:space="preserve">
3624+
<value>Connect to GitHub</value>
3625+
</data>
3626+
<data name="Login" xml:space="preserve">
3627+
<value>Login</value>
3628+
</data>
36173629
</root>

src/Files.App/Utils/Git/GitHelpers.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,18 @@ public static GitItemModel GetGitInformationForItem(Repository repository, strin
651651
return gitItemModel;
652652
}
653653

654+
// Remove saved credentails
655+
public static void RemoveSavedCredentials()
656+
{
657+
CredentialsHelpers.DeleteSavedPassword(GIT_RESOURCE_NAME, GIT_RESOURCE_USERNAME);
658+
}
659+
660+
// Get saved credentails
661+
public static string GetSavedCredentials()
662+
{
663+
return CredentialsHelpers.GetPassword(GIT_RESOURCE_NAME, GIT_RESOURCE_USERNAME);
664+
}
665+
654666
public static async Task InitializeRepositoryAsync(string? path)
655667
{
656668
if (string.IsNullOrWhiteSpace(path))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2023 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
using System.Windows.Input;
5+
6+
namespace Files.App.ViewModels.Settings
7+
{
8+
public class GitViewModel : ObservableObject
9+
{
10+
protected readonly IFileTagsSettingsService FileTagsSettingsService = Ioc.Default.GetRequiredService<IFileTagsSettingsService>();
11+
12+
public ICommand RemoveCredentialsCommand { get; }
13+
public ICommand ConnectToGitHubCommand { get; }
14+
15+
// Enabled when there are saved credentials
16+
private bool _IsLogoutEnabled;
17+
public bool IsLogoutEnabled
18+
{
19+
get => _IsLogoutEnabled;
20+
set => SetProperty(ref _IsLogoutEnabled, value);
21+
}
22+
23+
public GitViewModel()
24+
{
25+
RemoveCredentialsCommand = new RelayCommand(DoRemoveCredentials);
26+
ConnectToGitHubCommand = new RelayCommand(DoConnectToGitHubAsync);
27+
28+
IsLogoutEnabled = GitHelpers.GetSavedCredentials() != string.Empty;
29+
}
30+
31+
public void DoRemoveCredentials()
32+
{
33+
GitHelpers.RemoveSavedCredentials();
34+
IsLogoutEnabled = false;
35+
}
36+
37+
public async void DoConnectToGitHubAsync()
38+
{
39+
UIHelpers.CloseAllDialogs();
40+
await GitHelpers.RequireGitAuthenticationAsync();
41+
}
42+
}
43+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<!-- Copyright (c) 2023 Files Community. Licensed under the MIT License. See the LICENSE. -->
2+
<Page
3+
x:Class="Files.App.Views.Settings.GitPage"
4+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:converters="using:CommunityToolkit.WinUI.UI.Converters"
7+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8+
xmlns:helpers="using:Files.App.Helpers"
9+
xmlns:i="using:Microsoft.Xaml.Interactivity"
10+
xmlns:icore="using:Microsoft.Xaml.Interactions.Core"
11+
xmlns:local="using:Files.App.UserControls.Settings"
12+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
13+
xmlns:vm="using:Files.App.ViewModels.Settings"
14+
mc:Ignorable="d">
15+
16+
<Page.Resources>
17+
<ResourceDictionary>
18+
<ResourceDictionary.MergedDictionaries>
19+
<ResourceDictionary Source="/ResourceDictionaries/RightAlignedToggleSwitchStyle.xaml" />
20+
</ResourceDictionary.MergedDictionaries>
21+
22+
<converters:BoolNegationConverter x:Key="BoolNegationConverter" />
23+
</ResourceDictionary>
24+
</Page.Resources>
25+
26+
<Page.DataContext>
27+
<vm:GitViewModel x:Name="ViewModel" />
28+
</Page.DataContext>
29+
30+
<Grid>
31+
<StackPanel
32+
HorizontalAlignment="Stretch"
33+
VerticalAlignment="Stretch"
34+
Spacing="4">
35+
<StackPanel.ChildrenTransitions>
36+
<TransitionCollection>
37+
<EntranceThemeTransition />
38+
</TransitionCollection>
39+
</StackPanel.ChildrenTransitions>
40+
41+
<!-- Title -->
42+
<TextBlock
43+
Padding="0,0,0,12"
44+
FontSize="24"
45+
FontWeight="Medium"
46+
Text="{helpers:ResourceString Name=Git}" />
47+
48+
<!-- Connect to GitHub -->
49+
<local:SettingsBlockControl
50+
x:Name="ConnectToGitHubSection"
51+
Title="{helpers:ResourceString Name=ConnectToGitHub}"
52+
HorizontalAlignment="Stretch"
53+
x:Load="{x:Bind ViewModel.IsLogoutEnabled, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}">
54+
<local:SettingsBlockControl.Icon>
55+
<FontIcon Glyph="&#xF0B9;" />
56+
</local:SettingsBlockControl.Icon>
57+
<Button Command="{x:Bind ViewModel.ConnectToGitHubCommand}" Content="{helpers:ResourceString Name=Login}" />
58+
</local:SettingsBlockControl>
59+
60+
<!-- Remove credentials -->
61+
<local:SettingsBlockControl
62+
x:Name="RemoveCredentialsSection"
63+
Title="{helpers:ResourceString Name=ConnectedToGitHub}"
64+
HorizontalAlignment="Stretch"
65+
x:Load="{x:Bind ViewModel.IsLogoutEnabled, Mode=OneWay}">
66+
<local:SettingsBlockControl.Icon>
67+
<FontIcon Glyph="&#xF0B9;" />
68+
</local:SettingsBlockControl.Icon>
69+
<Button Command="{x:Bind ViewModel.RemoveCredentialsCommand}" Content="{helpers:ResourceString Name=Logout}" />
70+
</local:SettingsBlockControl>
71+
</StackPanel>
72+
</Grid>
73+
</Page>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2023 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
using Microsoft.UI.Xaml.Controls;
5+
6+
namespace Files.App.Views.Settings
7+
{
8+
public sealed partial class GitPage : Page
9+
{
10+
public GitPage()
11+
{
12+
InitializeComponent();
13+
}
14+
}
15+
}

tests/Files.InteractionTests/Tests/SettingsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public void VerifySettingsAreAccessible()
3030
"SettingsItemAppearance",
3131
"SettingsItemFolders",
3232
"SettingsItemTags",
33+
"SettingsItemGit",
3334
"SettingsItemAdvanced",
3435
"SettingsItemAbout"
3536
};

0 commit comments

Comments
 (0)