Skip to content

Commit 44175b4

Browse files
committed
Cleanup
1 parent 4e58369 commit 44175b4

File tree

3 files changed

+55
-54
lines changed

3 files changed

+55
-54
lines changed

Diff for: src/Files.App/Utils/Git/GitHelpers.cs

+49-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Files.App.Dialogs;
55
using LibGit2Sharp;
66
using Microsoft.Extensions.Logging;
7-
using Sentry;
87
using System.Net.Http;
98
using System.Net.Http.Json;
109
using System.Text;
@@ -13,6 +12,8 @@ namespace Files.App.Utils.Git
1312
{
1413
internal static class GitHelpers
1514
{
15+
private static readonly StatusCenterViewModel StatusCenterViewModel = Ioc.Default.GetRequiredService<StatusCenterViewModel>();
16+
1617
private const string GIT_RESOURCE_NAME = "Files:https://github.com";
1718

1819
private const string GIT_RESOURCE_USERNAME = "Personal Access Token";
@@ -878,5 +879,52 @@ public static bool IsValidRepoUrl(string url)
878879
{
879880
return !string.IsNullOrWhiteSpace(url) && url.Contains("github.com");
880881
}
882+
883+
public static async Task CloneRepoAsync(string repoUrl, string repoName, string targetDirectory)
884+
{
885+
var banner = StatusCenterHelper.AddCard_GitClone(repoName.CreateEnumerable(), targetDirectory.CreateEnumerable(), ReturnResult.InProgress);
886+
var fsProgress = new StatusCenterItemProgressModel(banner.ProgressEventSource, enumerationCompleted: true, FileSystemStatusCode.InProgress);
887+
888+
bool isSuccess = await Task.Run(() =>
889+
{
890+
try
891+
{
892+
var cloneOptions = new CloneOptions
893+
{
894+
FetchOptions =
895+
{
896+
OnTransferProgress = progress =>
897+
{
898+
banner.CancellationToken.ThrowIfCancellationRequested();
899+
fsProgress.ItemsCount = progress.TotalObjects;
900+
fsProgress.SetProcessedSize(progress.ReceivedBytes);
901+
fsProgress.AddProcessedItemsCount(1);
902+
fsProgress.Report((int)((progress.ReceivedObjects / (double)progress.TotalObjects) * 100));
903+
return true;
904+
},
905+
OnProgress = _ => !banner.CancellationToken.IsCancellationRequested
906+
},
907+
OnCheckoutProgress = (path, completed, total) =>
908+
banner.CancellationToken.ThrowIfCancellationRequested()
909+
};
910+
911+
Repository.Clone(repoUrl, targetDirectory, cloneOptions);
912+
return true;
913+
}
914+
catch
915+
{
916+
return false;
917+
}
918+
}, banner.CancellationToken);
919+
920+
StatusCenterViewModel.RemoveItem(banner);
921+
922+
StatusCenterHelper.AddCard_GitClone(
923+
repoName.CreateEnumerable(),
924+
targetDirectory.CreateEnumerable(),
925+
isSuccess ? ReturnResult.Success :
926+
banner.CancellationToken.IsCancellationRequested ? ReturnResult.Cancelled :
927+
ReturnResult.Failed);
928+
}
881929
}
882930
}

Diff for: src/Files.App/ViewModels/Dialogs/CloneRepoDialogViewModel.cs

+6-51
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace Files.App.ViewModels.Dialogs
99
{
1010
public sealed partial class CloneRepoDialogViewModel : ObservableObject
1111
{
12-
private readonly StatusCenterViewModel StatusCenterViewModel = Ioc.Default.GetRequiredService<StatusCenterViewModel>();
13-
1412
public bool CanCloneRepo
1513
{
1614
get
@@ -45,11 +43,11 @@ public string RepoName
4543

4644
}
4745

48-
private string workingDirectory;
49-
public string WorkingDirectory
46+
private string targetDirectory;
47+
public string TargetDirectory
5048
{
51-
get => workingDirectory;
52-
set => SetProperty(ref workingDirectory, value);
49+
get => targetDirectory;
50+
set => SetProperty(ref targetDirectory, value);
5351

5452
}
5553

@@ -70,57 +68,14 @@ public CloneRepoDialogViewModel(string repoUrl, string workingDirectory)
7068
RepoName = string.Empty;
7169
}
7270

73-
WorkingDirectory = workingDirectory;
71+
TargetDirectory = Path.Combine(workingDirectory, RepoName);
7472

7573
CloneRepoCommand = new AsyncRelayCommand(DoCloneRepo);
7674
}
7775

7876
private async Task DoCloneRepo()
7977
{
80-
string targetDirectory = Path.Combine(WorkingDirectory, RepoName);
81-
var banner = StatusCenterHelper.AddCard_GitClone(RepoName.CreateEnumerable(), targetDirectory.CreateEnumerable(), ReturnResult.InProgress);
82-
var fsProgress = new StatusCenterItemProgressModel(banner.ProgressEventSource, enumerationCompleted: true, FileSystemStatusCode.InProgress);
83-
84-
bool isSuccess = await Task.Run(() =>
85-
{
86-
try
87-
{
88-
var cloneOptions = new CloneOptions
89-
{
90-
FetchOptions =
91-
{
92-
OnTransferProgress = progress =>
93-
{
94-
banner.CancellationToken.ThrowIfCancellationRequested();
95-
fsProgress.ItemsCount = progress.TotalObjects;
96-
fsProgress.SetProcessedSize(progress.ReceivedBytes);
97-
fsProgress.AddProcessedItemsCount(1);
98-
fsProgress.Report((int)((progress.ReceivedObjects / (double)progress.TotalObjects) * 100));
99-
return true;
100-
},
101-
OnProgress = _ => !banner.CancellationToken.IsCancellationRequested
102-
},
103-
OnCheckoutProgress = (path, completed, total) =>
104-
banner.CancellationToken.ThrowIfCancellationRequested()
105-
};
106-
107-
Repository.Clone(RepoUrl, targetDirectory, cloneOptions);
108-
return true;
109-
}
110-
catch
111-
{
112-
return false;
113-
}
114-
}, banner.CancellationToken);
115-
116-
StatusCenterViewModel.RemoveItem(banner);
117-
118-
StatusCenterHelper.AddCard_GitClone(
119-
RepoName.CreateEnumerable(),
120-
targetDirectory.CreateEnumerable(),
121-
isSuccess ? ReturnResult.Success :
122-
banner.CancellationToken.IsCancellationRequested ? ReturnResult.Cancelled :
123-
ReturnResult.Failed);
78+
await GitHelpers.CloneRepoAsync(RepoUrl, repoName, targetDirectory);
12479
}
12580
}
12681
}

Diff for: src/Files.App/ViewModels/Layouts/BaseLayoutViewModel.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4-
using Files.App.Actions;
5-
using LibGit2Sharp;
64
using Microsoft.Extensions.Logging;
75
using Microsoft.UI.Xaml;
86
using Microsoft.UI.Xaml.Input;

0 commit comments

Comments
 (0)