Skip to content

Commit

Permalink
Delay branch load
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrariofilippo committed Nov 23, 2023
1 parent 575efb8 commit 66f37c4
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 47 deletions.
41 changes: 23 additions & 18 deletions src/Files.App/Data/Models/DirectoryPropertiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ public DirectoryPropertiesViewModel()
=> GitHelpers.CreateNewBranchAsync(_gitRepositoryPath!, _localBranches[ACTIVE_BRANCH_INDEX]));
}

public void UpdateGitInfo(bool isGitRepository, string? repositoryPath, BranchItem[] branches)
public void UpdateGitInfo(bool isGitRepository, string? repositoryPath, BranchItem? head)
{
GitBranchDisplayName = isGitRepository &&
branches.Any() &&
!(ContentPageContext.ShellPage!.InstanceViewModel.IsPageTypeSearchResults)
? branches[ACTIVE_BRANCH_INDEX].Name
head is not null &&
!ContentPageContext.ShellPage!.InstanceViewModel.IsPageTypeSearchResults
? head.Name
: null;

_gitRepositoryPath = repositoryPath;
Expand All @@ -108,27 +108,32 @@ public void UpdateGitInfo(bool isGitRepository, string? repositoryPath, BranchIt
if (!IsBranchesFlyoutExpaned)
ShowLocals = true;

var behind = branches.Any() ? branches[0].BehindBy ?? 0 : 0;
var ahead = branches.Any() ? branches[0].AheadBy ?? 0 : 0;
var behind = head is not null ? head.BehindBy ?? 0 : 0;
var ahead = head is not null ? head.AheadBy ?? 0 : 0;

ExtendedStatusInfo = string.Format("GitSyncStatusExtendedInfo".GetLocalizedResource(), ahead, behind);
StatusInfo = $"{ahead} / {behind}";
}

if (isGitRepository)
{
_localBranches.Clear();
_remoteBranches.Clear();
public async Task LoadBranches()
{
if (string.IsNullOrEmpty(_gitRepositoryPath))
return;

foreach (var branch in branches)
{
if (branch.IsRemote)
_remoteBranches.Add(branch.Name);
else
_localBranches.Add(branch.Name);
}
var branches = await GitHelpers.GetBranchesNames(_gitRepositoryPath);

_localBranches.Clear();
_remoteBranches.Clear();

SelectedBranchIndex = ShowLocals ? ACTIVE_BRANCH_INDEX : -1;
foreach (var branch in branches)
{
if (branch.IsRemote)
_remoteBranches.Add(branch.Name);
else
_localBranches.Add(branch.Name);
}

SelectedBranchIndex = ShowLocals ? ACTIVE_BRANCH_INDEX : -1;
}
}
}
2 changes: 1 addition & 1 deletion src/Files.App/Data/Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public async Task SetWorkingDirectoryAsync(string? value)
}

GitDirectory = GitHelpers.GetGitRepositoryPath(WorkingDirectory, pathRoot);
IsValidGitDirectory = !string.IsNullOrEmpty(await GitHelpers.GetRepositoryHeadName(GitDirectory));
IsValidGitDirectory = !string.IsNullOrEmpty((await GitHelpers.GetRepositoryHead(GitDirectory))?.Name);

OnPropertyChanged(nameof(WorkingDirectory));
}
Expand Down
3 changes: 2 additions & 1 deletion src/Files.App/UserControls/StatusBarControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ public StatusBarControl()
InitializeComponent();
}

private void BranchesFlyout_Opening(object _, object e)
private async void BranchesFlyout_Opening(object _, object e)
{
if (DirectoryPropertiesViewModel is null)
return;

DirectoryPropertiesViewModel.IsBranchesFlyoutExpaned = true;
DirectoryPropertiesViewModel.ShowLocals = true;
await DirectoryPropertiesViewModel.LoadBranches();
DirectoryPropertiesViewModel.SelectedBranchIndex = DirectoryPropertiesViewModel.ACTIVE_BRANCH_INDEX;
}

Expand Down
21 changes: 10 additions & 11 deletions src/Files.App/Utils/Git/GitHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,31 +152,30 @@ public static async Task<BranchItem[]> GetBranchesNames(string? path)
return returnValue;
}

public static async Task<string> GetRepositoryHeadName(string? path)
public static async Task<BranchItem?> GetRepositoryHead(string? path)
{
if (string.IsNullOrWhiteSpace(path) || !Repository.IsValid(path))
return string.Empty;
return null;

var (result, returnValue) = await PostMethodToThreadWithMessageQueueAsync<(GitOperationResult, string)>(() =>
var (_, returnValue) = await PostMethodToThreadWithMessageQueueAsync<(GitOperationResult, BranchItem?)>(() =>
{
string branchName = string.Empty;
BranchItem? head = null;
try
{
using var repository = new Repository(path);
branchName = GetValidBranches(repository.Branches)
.FirstOrDefault(b => b.IsCurrentRepositoryHead)?.FriendlyName ?? string.Empty;
var branch = GetValidBranches(repository.Branches).FirstOrDefault(b => b.IsCurrentRepositoryHead);
if (branch is not null)
head = new BranchItem(branch.FriendlyName, branch.IsRemote, TryGetTrackingDetails(branch)?.AheadBy ?? 0, TryGetTrackingDetails(branch)?.BehindBy ?? 0);
}
catch
{
return (GitOperationResult.GenericError, string.Empty);
return (GitOperationResult.GenericError, head);
}

return (GitOperationResult.Success, branchName);
return (GitOperationResult.Success, head);
});

return result is GitOperationResult.Success
? returnValue!
: string.Empty;
return returnValue;
}

public static async Task<bool> Checkout(string? repositoryPath, string? branch)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static async Task<List<ListedItem>> ListEntries(
IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
bool CalculateFolderSizes = userSettingsService.FoldersSettingsService.CalculateFolderSizes;

var isGitRepo = GitHelpers.IsRepositoryEx(path, out var repoPath) && !string.IsNullOrEmpty(await GitHelpers.GetRepositoryHeadName(repoPath));
var isGitRepo = GitHelpers.IsRepositoryEx(path, out var repoPath) && !string.IsNullOrEmpty((await GitHelpers.GetRepositoryHead(repoPath))?.Name);

do
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private async Task LoadPreviewAndDetailsAsync()
!string.IsNullOrEmpty(repoPath))
{
var gitDirectory = GitHelpers.GetGitRepositoryPath(Folder.Path, Path.GetPathRoot(Folder.Path));
var headName = await GitHelpers.GetRepositoryHeadName(gitDirectory);
var headName = (await GitHelpers.GetRepositoryHead(gitDirectory))?.Name ?? string.Empty;
var repositoryName = GitHelpers.GetOriginRepositoryName(gitDirectory);

if(!string.IsNullOrEmpty(gitDirectory))
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Views/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private async Task OnNavigatedToAsync(NavigationEventArgs eventArgs)
// Set path of working directory empty
await AppInstance.FilesystemViewModel.SetWorkingDirectoryAsync("Home");

AppInstance.SlimContentPage?.DirectoryPropertiesViewModel.UpdateGitInfo(false, string.Empty, Array.Empty<BranchItem>());
AppInstance.SlimContentPage?.DirectoryPropertiesViewModel.UpdateGitInfo(false, string.Empty, null);

// Clear the path UI and replace with Favorites
AppInstance.ToolbarViewModel.PathComponents.Clear();
Expand Down
35 changes: 22 additions & 13 deletions src/Files.App/Views/Shells/BaseShellPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,13 @@ protected async void FilesystemViewModel_DirectoryInfoUpdated(object sender, Eve
if (InstanceViewModel.GitRepositoryPath != FilesystemViewModel.GitDirectory)
{
InstanceViewModel.GitRepositoryPath = FilesystemViewModel.GitDirectory;
InstanceViewModel.GitBranchName = InstanceViewModel.IsGitRepository
? await GitHelpers.GetRepositoryHeadName(InstanceViewModel.GitRepositoryPath)

BranchItem? headBranch = headBranch = InstanceViewModel.IsGitRepository
? await GitHelpers.GetRepositoryHead(InstanceViewModel.GitRepositoryPath)
: null;

InstanceViewModel.GitBranchName = headBranch is not null
? headBranch.Name
: string.Empty;

if (!_gitFetch.IsCompleted)
Expand All @@ -261,14 +266,14 @@ protected async void FilesystemViewModel_DirectoryInfoUpdated(object sender, Eve
() => GitHelpers.FetchOrigin(InstanceViewModel.GitRepositoryPath),
_gitFetchToken.Token);
}
}

if (!GitHelpers.IsExecutingGitAction)
{
ContentPage.DirectoryPropertiesViewModel.UpdateGitInfo(
InstanceViewModel.IsGitRepository,
InstanceViewModel.GitRepositoryPath,
await GitHelpers.GetBranchesNames(InstanceViewModel.GitRepositoryPath));
if (!GitHelpers.IsExecutingGitAction)
{
ContentPage.DirectoryPropertiesViewModel.UpdateGitInfo(
InstanceViewModel.IsGitRepository,
InstanceViewModel.GitRepositoryPath,
headBranch);
}
}

ContentPage.DirectoryPropertiesViewModel.DirectoryItemCount = $"{FilesystemViewModel.FilesAndFolders.Count} {directoryItemCountLocalization}";
Expand All @@ -280,14 +285,18 @@ protected async void FilesystemViewModel_GitDirectoryUpdated(object sender, Even
if (GitHelpers.IsExecutingGitAction)
return;

InstanceViewModel.GitBranchName = InstanceViewModel.IsGitRepository
? await GitHelpers.GetRepositoryHeadName(InstanceViewModel.GitRepositoryPath)
var head = InstanceViewModel.IsGitRepository
? await GitHelpers.GetRepositoryHead(InstanceViewModel.GitRepositoryPath)
: null;

InstanceViewModel.GitBranchName = head is not null
? head.Name
: string.Empty;

ContentPage?.DirectoryPropertiesViewModel.UpdateGitInfo(
InstanceViewModel.IsGitRepository,
InstanceViewModel.GitRepositoryPath,
await GitHelpers.GetBranchesNames(InstanceViewModel.GitRepositoryPath));
head);
}

protected async void GitCheckout_Required(object? sender, string branchName)
Expand All @@ -302,7 +311,7 @@ protected async void GitCheckout_Required(object? sender, string branchName)
ContentPage.DirectoryPropertiesViewModel.UpdateGitInfo(
InstanceViewModel.IsGitRepository,
InstanceViewModel.GitRepositoryPath,
await GitHelpers.GetBranchesNames(InstanceViewModel.GitRepositoryPath));
await GitHelpers.GetRepositoryHead(InstanceViewModel.GitRepositoryPath));
}
}

Expand Down

0 comments on commit 66f37c4

Please sign in to comment.