Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 4bc9602

Browse files
committed
Merge enhancements/loading
2 parents 883ed16 + abeb39e commit 4bc9602

26 files changed

+467
-166
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ abstract class ApplicationManagerBase : IApplicationManager
1212
protected static ILogging Logger { get; } = LogHelper.GetLogger<IApplicationManager>();
1313

1414
private RepositoryManager repositoryManager;
15+
protected bool isBusy;
16+
public event Action<IProgress> OnProgress;
1517

1618
public ApplicationManagerBase(SynchronizationContext synchronizationContext)
1719
{
@@ -57,12 +59,11 @@ public void Run(bool firstRun)
5759
{
5860
Logger.Trace("No git path found in settings");
5961

60-
var initEnvironmentTask = new ActionTask<NPath>(CancellationToken, (b, path) =>
61-
{
62-
InitializeEnvironment(path);
63-
}) { Affinity = TaskAffinity.UI };
62+
isBusy = true;
63+
var initEnvironmentTask = new ActionTask<NPath>(CancellationToken, (b, path) => InitializeEnvironment(path)) { Affinity = TaskAffinity.UI };
6464
var findExecTask = new FindExecTask("git", CancellationToken)
65-
.FinallyInUI((b, ex, path) => {
65+
.FinallyInUI((b, ex, path) =>
66+
{
6667
if (b && path != null)
6768
{
6869
Logger.Trace("FindExecTask Success: {0}", path);
@@ -73,6 +74,7 @@ public void Run(bool firstRun)
7374
Logger.Warning("FindExecTask Failure");
7475
Logger.Error("Git not found");
7576
}
77+
isBusy = false;
7678
});
7779

7880
var gitInstaller = new GitInstaller(Environment, CancellationToken);
@@ -230,6 +232,7 @@ public void Dispose()
230232
public ISettings SystemSettings { get; protected set; }
231233
public ISettings UserSettings { get; protected set; }
232234
public IUsageTracker UsageTracker { get; protected set; }
235+
public bool IsBusy { get { return isBusy || RepositoryManager.IsBusy; } }
233236
protected TaskScheduler UIScheduler { get; private set; }
234237
protected SynchronizationContext SynchronizationContext { get; private set; }
235238
protected IRepositoryManager RepositoryManager { get { return repositoryManager; } }

src/GitHub.Api/Application/IApplicationManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ public interface IApplicationManager : IDisposable
1717
ITaskManager TaskManager { get; }
1818
IGitClient GitClient { get; }
1919
IUsageTracker UsageTracker { get; }
20-
20+
bool IsBusy { get; }
2121
void Run(bool firstRun);
2222
void RestartRepository();
2323
ITask InitializeRepository();
24+
event Action<IProgress> OnProgress;
2425
}
2526
}

src/GitHub.Api/Installer/IZipHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ namespace GitHub.Unity
66
interface IZipHelper
77
{
88
void Extract(string archive, string outFolder, CancellationToken cancellationToken,
9-
IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null);
9+
Func<long, long, bool> onProgress = null);
1010
}
1111
}

src/GitHub.Api/Installer/UnzipTask.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,21 @@ class UnzipTask: TaskBase
1111
private readonly IZipHelper zipHelper;
1212
private readonly IFileSystem fileSystem;
1313
private readonly string expectedMD5;
14-
private readonly IProgress<float> zipFileProgress;
15-
private readonly IProgress<long> estimatedDurationProgress;
1614

17-
public UnzipTask(CancellationToken token, string archiveFilePath, NPath extractedPath, IFileSystem fileSystem, string expectedMD5 = null, IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null) :
18-
this(token, archiveFilePath, extractedPath, ZipHelper.Instance, fileSystem, expectedMD5, zipFileProgress, estimatedDurationProgress)
15+
public UnzipTask(CancellationToken token, string archiveFilePath, NPath extractedPath, IFileSystem fileSystem, string expectedMD5 = null) :
16+
this(token, archiveFilePath, extractedPath, ZipHelper.Instance, fileSystem, expectedMD5)
1917
{
2018

2119
}
2220

23-
public UnzipTask(CancellationToken token, string archiveFilePath, NPath extractedPath, IZipHelper zipHelper, IFileSystem fileSystem, string expectedMD5 = null, IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null)
21+
public UnzipTask(CancellationToken token, string archiveFilePath, NPath extractedPath, IZipHelper zipHelper, IFileSystem fileSystem, string expectedMD5 = null)
2422
: base(token)
2523
{
2624
this.archiveFilePath = archiveFilePath;
2725
this.extractedPath = extractedPath;
2826
this.zipHelper = zipHelper;
2927
this.fileSystem = fileSystem;
3028
this.expectedMD5 = expectedMD5;
31-
this.zipFileProgress = zipFileProgress;
32-
this.estimatedDurationProgress = estimatedDurationProgress;
3329
}
3430

3531
protected void BaseRun(bool success)
@@ -74,6 +70,11 @@ protected virtual void RunUnzip(bool success)
7470
try
7571
{
7672
zipHelper.Extract(archiveFilePath, extractedPath, Token, zipFileProgress, estimatedDurationProgress);
73+
(value, total) =>
74+
{
75+
UpdateProgress(value, total);
76+
return !Token.IsCancellationRequested;
77+
});
7778

7879
if (expectedMD5 != null)
7980
{

src/GitHub.Api/Installer/ZipHelper.cs

Lines changed: 12 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -23,87 +23,17 @@ public static IZipHelper Instance
2323
}
2424
}
2525

26-
public static bool Copy(Stream source, Stream destination, int chunkSize, long totalSize,
27-
Func<long, long, bool> progress, int progressUpdateRate)
28-
{
29-
var buffer = new byte[chunkSize];
30-
var bytesRead = 0;
31-
long totalRead = 0;
32-
var averageSpeed = -1f;
33-
var lastSpeed = 0f;
34-
var smoothing = 0.005f;
35-
long readLastSecond = 0;
36-
long timeToFinish = 0;
37-
Stopwatch watch = null;
38-
var success = true;
39-
40-
var trackProgress = totalSize > 0 && progress != null;
41-
if (trackProgress)
42-
{
43-
watch = new Stopwatch();
44-
}
45-
46-
do
47-
{
48-
if (trackProgress)
49-
{
50-
watch.Start();
51-
}
52-
53-
bytesRead = source.Read(buffer, 0, chunkSize);
54-
55-
if (trackProgress)
56-
{
57-
watch.Stop();
58-
}
59-
60-
totalRead += bytesRead;
61-
62-
if (bytesRead > 0)
63-
{
64-
destination.Write(buffer, 0, bytesRead);
65-
if (trackProgress)
66-
{
67-
readLastSecond += bytesRead;
68-
if (watch.ElapsedMilliseconds >= progressUpdateRate || totalRead == totalSize)
69-
{
70-
watch.Reset();
71-
lastSpeed = readLastSecond;
72-
readLastSecond = 0;
73-
averageSpeed = averageSpeed < 0f
74-
? lastSpeed
75-
: smoothing * lastSpeed + (1f - smoothing) * averageSpeed;
76-
timeToFinish = Math.Max(1L,
77-
(long)((totalSize - totalRead) / (averageSpeed / progressUpdateRate)));
78-
79-
if (!progress(totalRead, timeToFinish))
80-
{
81-
break;
82-
}
83-
}
84-
}
85-
}
86-
} while (bytesRead > 0);
87-
88-
if (totalRead > 0)
89-
{
90-
destination.Flush();
91-
}
92-
93-
return success;
94-
}
95-
9626
public void Extract(string archive, string outFolder, CancellationToken cancellationToken,
97-
IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null)
27+
Func<long, long, bool> onProgress = null)
9828
{
99-
ExtractZipFile(archive, outFolder, cancellationToken, zipFileProgress, estimatedDurationProgress);
29+
ExtractZipFile(archive, outFolder, cancellationToken, onProgress);
10030
}
10131

10232
public static void ExtractZipFile(string archive, string outFolder, CancellationToken cancellationToken,
103-
IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null)
33+
Func<long, long, bool> onProgress)
10434
{
35+
const int chunkSize = 4096; // 4K is optimum
10536
ZipFile zf = null;
106-
var estimatedDuration = 1L;
10737
var startTime = DateTime.Now;
10838
var processed = 0;
10939
var totalBytes = 0L;
@@ -112,9 +42,11 @@ public static void ExtractZipFile(string archive, string outFolder, Cancellation
11242
{
11343
var fs = File.OpenRead(archive);
11444
zf = new ZipFile(fs);
45+
var totalSize = fs.Length;
11546

11647
foreach (ZipEntry zipEntry in zf)
11748
{
49+
cancellationToken.ThrowIfCancellationRequested();
11850
if (zipEntry.IsDirectory)
11951
{
12052
continue; // Ignore directories
@@ -152,28 +84,16 @@ public static void ExtractZipFile(string archive, string outFolder, Cancellation
15284
var targetFile = new FileInfo(fullZipToPath);
15385
using (var streamWriter = targetFile.OpenWrite())
15486
{
155-
const int chunkSize = 4096; // 4K is optimum
156-
Copy(zipStream, streamWriter, chunkSize, zipEntry.Size, (totalRead, timeToFinish) =>
157-
{
158-
estimatedDuration = timeToFinish;
159-
160-
estimatedDurationProgress?.Report(estimatedDuration);
161-
zipFileProgress?.Report((float)(totalBytes + totalRead) / zipEntry.Size);
162-
return true;
163-
}, 100);
164-
cancellationToken.ThrowIfCancellationRequested();
87+
if (!Utils.Copy(zipStream, streamWriter, zipEntry.Size, chunkSize,
88+
progress: (totalRead, timeToFinish) => {
89+
totalBytes += totalRead;
90+
return onProgress(totalBytes, totalSize);
91+
}))
92+
return;
16593
}
16694

16795
targetFile.LastWriteTime = zipEntry.DateTime;
16896
processed++;
169-
totalBytes += zipEntry.Size;
170-
171-
var elapsedMillisecondsPerFile = (DateTime.Now - startTime).TotalMilliseconds / processed;
172-
estimatedDuration = Math.Max(1L, (long)((fs.Length - totalBytes) * elapsedMillisecondsPerFile));
173-
174-
estimatedDurationProgress?.Report(estimatedDuration);
175-
zipFileProgress?.Report((float)processed / zf.Count);
176-
cancellationToken.ThrowIfCancellationRequested();
17797
}
17898
}
17999
finally

src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ protected override void InitializeUI()
3131
Logger.Trace("Restarted {0}", Environment.Repository);
3232
EnvironmentCache.Instance.Flush();
3333

34+
isBusy = false;
3435
ProjectWindowInterface.Initialize(Environment.Repository);
3536
var window = Window.GetWindow();
3637
if (window != null)

src/UnityExtension/Assets/Editor/GitHub.Unity/EntryPoint.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ private static void Initialize()
6262
Debug.LogFormat("Initialized GitHub for Unity version {0}{1}Log file: {2}", ApplicationInfo.Version, Environment.NewLine, logPath);
6363
}
6464

65-
LogHelper.LogAdapter = new FileLogAdapter(logPath);
65+
LogHelper.LogAdapter = new MultipleLogAdapter(new FileLogAdapter(logPath)
66+
, new UnityLogAdapter()
67+
);
6668
LogHelper.Info("Initializing GitHub for Unity version " + ApplicationInfo.Version);
6769

6870
ApplicationManager.Run(ApplicationCache.Instance.FirstRun);

src/UnityExtension/Assets/Editor/GitHub.Unity/GitHub.Unity.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
<Compile Include="UI\LoadingView.cs" />
109109
<Compile Include="UI\PublishView.cs" />
110110
<Compile Include="UI\InitProjectView.cs" />
111+
<Compile Include="UI\Spinner.cs" />
111112
<Compile Include="UI\TreeControl.cs" />
112113
<Compile Include="UI\UserSettingsView.cs" />
113114
<Compile Include="UI\GitPathView.cs" />
@@ -217,6 +218,20 @@
217218
<EmbeddedResource Include="IconsAndLogos\globe%402x.png" />
218219
<EmbeddedResource Include="IconsAndLogos\globe.png" />
219220
</ItemGroup>
221+
<ItemGroup>
222+
<EmbeddedResource Include="IconsAndLogos\spinner-inside%402x.png" />
223+
<EmbeddedResource Include="IconsAndLogos\spinner-inside.png" />
224+
<EmbeddedResource Include="IconsAndLogos\spinner-outside%402x.png" />
225+
<EmbeddedResource Include="IconsAndLogos\spinner-outside.png" />
226+
</ItemGroup>
227+
<ItemGroup>
228+
<EmbeddedResource Include="IconsAndLogos\code%402x.png" />
229+
<EmbeddedResource Include="IconsAndLogos\code.png" />
230+
<EmbeddedResource Include="IconsAndLogos\merge%402x.png" />
231+
<EmbeddedResource Include="IconsAndLogos\merge.png" />
232+
<EmbeddedResource Include="IconsAndLogos\rocket%402x.png" />
233+
<EmbeddedResource Include="IconsAndLogos\rocket.png" />
234+
</ItemGroup>
220235
<Import Project="..\..\..\..\..\common\nativelibraries.props" />
221236
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
222237
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)