Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Git Integration - Phase 4 #12439

Merged
merged 14 commits into from
May 30, 2023
36 changes: 36 additions & 0 deletions src/Files.App/Actions/Git/GitFetchAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Files.App.Contexts;

namespace Files.App.Actions
{
internal class GitFetchAction : ObservableObject, IAction
{
private readonly IContentPageContext _context;

public string Label { get; } = "GitFetch".GetLocalizedResource();

public string Description { get; } = "GitFetchDescription".GetLocalizedResource();

public bool IsExecutable
=> _context.CanExecuteGitAction;

public GitFetchAction()
{
_context = Ioc.Default.GetRequiredService<IContentPageContext>();

_context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync()
{
GitHelpers.FetchOrigin(_context.ShellPage!.InstanceViewModel.GitRepositoryPath);

return Task.CompletedTask;
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IContentPageContext.CanExecuteGitAction))
OnPropertyChanged(nameof(IsExecutable));
}
}
}
39 changes: 39 additions & 0 deletions src/Files.App/Actions/Git/GitPullAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Files.App.Commands;
using Files.App.Contexts;

namespace Files.App.Actions
{
internal class GitPullAction : ObservableObject, IAction
{
private readonly IContentPageContext _context;

public string Label { get; } = "GitPull".GetLocalizedResource();

public string Description { get; } = "GitPullDescription".GetLocalizedResource();

public RichGlyph Glyph { get; } = new("\uE74B");

public bool IsExecutable
=> _context.CanExecuteGitAction;

public GitPullAction()
{
_context = Ioc.Default.GetRequiredService<IContentPageContext>();

_context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync()
{
GitHelpers.PullOrigin(_context.ShellPage!.InstanceViewModel.GitRepositoryPath);

return Task.CompletedTask;
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IContentPageContext.CanExecuteGitAction))
OnPropertyChanged(nameof(IsExecutable));
}
}
}
4 changes: 4 additions & 0 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,9 @@ public enum CommandCodes

// Play
PlayAll,

// Git
GitFetch,
GitPull,
}
}
4 changes: 4 additions & 0 deletions src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ public IRichCommand this[HotKey hotKey]
public IRichCommand ClosePane => commands[CommandCodes.ClosePane];
public IRichCommand OpenFileLocation => commands[CommandCodes.OpenFileLocation];
public IRichCommand PlayAll => commands[CommandCodes.PlayAll];
public IRichCommand GitFetch => commands[CommandCodes.GitFetch];
public IRichCommand GitPull => commands[CommandCodes.GitPull];

public CommandManager()
{
Expand Down Expand Up @@ -302,6 +304,8 @@ public CommandManager()
[CommandCodes.ClosePane] = new ClosePaneAction(),
[CommandCodes.OpenFileLocation] = new OpenFileLocationAction(),
[CommandCodes.PlayAll] = new PlayAllAction(),
[CommandCodes.GitFetch] = new GitFetchAction(),
[CommandCodes.GitPull] = new GitPullAction(),
};

private void UpdateHotKeys()
Expand Down
3 changes: 3 additions & 0 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,8 @@ public interface ICommandManager : IEnumerable<IRichCommand>
IRichCommand ClosePane { get; }

IRichCommand PlayAll { get; }

IRichCommand GitFetch { get; }
IRichCommand GitPull { get; }
}
}
22 changes: 13 additions & 9 deletions src/Files.App/Contexts/ContentPage/ContentPageContext.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Filesystem;
using Files.App.UserControls.MultitaskingControl;
using Files.App.ViewModels;
using Files.App.Views.LayoutModes;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Linq;

namespace Files.App.Contexts
{
Expand Down Expand Up @@ -58,13 +49,22 @@ internal class ContentPageContext : ObservableObject, IContentPageContext

public bool ShowSearchUnindexedItemsMessage => ShellPage is not null && ShellPage.InstanceViewModel.ShowSearchUnindexedItemsMessage;

public bool CanExecuteGitAction => ShellPage is not null && ShellPage.InstanceViewModel.IsGitRepository && !GitHelpers.IsExecutingGitAction;

public ContentPageContext()
{
context.Changing += Context_Changing;
context.Changed += Context_Changed;
GitHelpers.IsExecutingGitActionChanged += GitHelpers_IsExecutingGitActionChanged;

Update();
}

private void GitHelpers_IsExecutingGitActionChanged(object? sender, PropertyChangedEventArgs e)
{
OnPropertyChanged(nameof(CanExecuteGitAction));
}

private void Context_Changing(object? sender, EventArgs e)
{
if (ShellPage is IShellPage page)
Expand Down Expand Up @@ -149,6 +149,9 @@ private void InstanceViewModel_PropertyChanged(object? sender, PropertyChangedEv
case nameof(CurrentInstanceViewModel.ShowSearchUnindexedItemsMessage):
OnPropertyChanged(nameof(ShowSearchUnindexedItemsMessage));
break;
case nameof(CurrentInstanceViewModel.IsGitRepository):
OnPropertyChanged(nameof(CanExecuteGitAction));
break;
}
}

Expand Down Expand Up @@ -191,6 +194,7 @@ private void Update()
OnPropertyChanged(nameof(IsMultiPaneEnabled));
OnPropertyChanged(nameof(IsMultiPaneActive));
OnPropertyChanged(nameof(ShowSearchUnindexedItemsMessage));
OnPropertyChanged(nameof(CanExecuteGitAction));
}

private void UpdatePageType()
Expand Down
7 changes: 2 additions & 5 deletions src/Files.App/Contexts/ContentPage/IContentPageContext.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.App.Filesystem;
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Files.App.Contexts
{
public interface IContentPageContext : INotifyPropertyChanged
Expand Down Expand Up @@ -36,5 +31,7 @@ public interface IContentPageContext : INotifyPropertyChanged
bool IsMultiPaneActive { get; }

bool ShowSearchUnindexedItemsMessage { get; }

bool CanExecuteGitAction { get; }
}
}
2 changes: 1 addition & 1 deletion src/Files.App/Data/Items/BranchItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace Files.App.Data.Items
{
public record BranchItem(string Name, bool IsRemote);
public record BranchItem(string Name, bool IsRemote, int? AheadBy, int? BehindBy);
}
11 changes: 11 additions & 0 deletions src/Files.App/Data/Models/DirectoryPropertiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public bool ShowLocals
}
}

private string _PullInfo = "0";
public string PullInfo
{
get => _PullInfo;
set => SetProperty(ref _PullInfo, value);
}

public ObservableCollection<string> BranchesNames => _ShowLocals
? _localBranches
: _remoteBranches;
Expand All @@ -84,6 +91,10 @@ public void UpdateGitInfo(bool isGitRepository, string? repositoryPath, BranchIt
_gitRepositoryPath = repositoryPath;
ShowLocals = true;

PullInfo = branches.Any()
? branches[0].BehindBy.ToString() ?? "0"
: "0";

if (isGitRepository)
{
_localBranches.Clear();
Expand Down
Loading