Skip to content

Commit 5659c48

Browse files
authored
Merge pull request #58 from patchkit-net/feature/v3.10.0/progress-bar
Feature/v3.10.0/progress bar
2 parents bc5635c + f442010 commit 5659c48

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

Assets/PatchKit Patcher/Scripts/Patcher.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static Patcher Instance
7171
private readonly ManualResetEvent _userDecisionSetEvent = new ManualResetEvent(false);
7272

7373
private readonly IRequestTimeoutCalculator _requestTimeoutCalculator = new SimpleRequestTimeoutCalculator();
74-
74+
7575
private bool _hasAutomaticallyInstalledApp;
7676

7777
private bool _hasAutomaticallyCheckedForAppUpdate;
@@ -138,7 +138,7 @@ public IReadOnlyReactiveProperty<PatcherData> Data
138138
{
139139
get { return _data; }
140140
}
141-
141+
142142
private readonly ReactiveProperty<string> _warning = new ReactiveProperty<string>();
143143

144144
public IReadOnlyReactiveProperty<string> Warning
@@ -206,11 +206,11 @@ public void Quit()
206206
private void CloseLockFile()
207207
{
208208
try
209-
{
209+
{
210210
if (_lockFileStream != null)
211211
{
212212
_lockFileStream.Close();
213-
213+
214214
DebugLogger.Log("Deleting the lock file.");
215215
File.Delete(_data.Value.LockFilePath);
216216
}
@@ -278,7 +278,7 @@ private void OnApplicationQuit()
278278
DebugLogger.Log("Cancelling application quit because patcher thread is alive.");
279279

280280
Application.CancelQuit();
281-
281+
282282
StartCoroutine(KillThread());
283283
}
284284
}
@@ -433,11 +433,11 @@ private void ThreadExecution(CancellationToken cancellationToken)
433433
catch (ThreadAbortException)
434434
{
435435
DebugLogger.Log("Patcher thread finished: thread has been aborted.");
436-
}
436+
}
437437
catch (MultipleInstancesException exception)
438438
{
439439
DebugLogger.LogException(exception);
440-
Quit();
440+
Quit();
441441
}
442442
catch (Exception exception)
443443
{
@@ -630,11 +630,11 @@ private void ThreadWaitForUserDecision(CancellationToken cancellationToken)
630630
private void ThreadExecuteUserDecision(CancellationToken cancellationToken)
631631
{
632632
bool displayWarningInsteadOfError = false;
633-
633+
634634
try
635635
{
636636
_warning.Value = string.Empty;
637-
637+
638638
DebugLogger.Log(string.Format("Executing user decision {0}...", _userDecision));
639639

640640
switch (_userDecision)
@@ -687,7 +687,7 @@ private void ThreadExecuteUserDecision(CancellationToken cancellationToken)
687687
catch (ApiConnectionException e)
688688
{
689689
DebugLogger.LogException(e);
690-
690+
691691
if (displayWarningInsteadOfError)
692692
{
693693
_warning.Value = "Unable to check for updates. Please check your internet connection.";
@@ -737,6 +737,8 @@ private void ThreadDisplayError(PatcherError error, CancellationToken cancellati
737737
{
738738
try
739739
{
740+
_state.Value = PatcherState.DisplayingError;
741+
740742
DebugLogger.Log(string.Format("Displaying patcher error {0}...", error));
741743

742744
ErrorDialog.Display(error, cancellationToken);
@@ -777,7 +779,7 @@ private void ThreadStartApp()
777779

778780
private void ThreadUpdateApp(bool automatically, CancellationToken cancellationToken)
779781
{
780-
_state.Value = PatcherState.UpdatingApp;
782+
_state.Value = PatcherState.Connecting;
781783

782784
_appInfo.Value = _app.RemoteMetaData.GetAppInfo(!automatically);
783785
_remoteVersionId.Value = _app.GetLatestVersionId(!automatically);
@@ -795,10 +797,16 @@ private void ThreadUpdateApp(bool automatically, CancellationToken cancellationT
795797
try
796798
{
797799
_updaterStatus.Value = appUpdater.Status;
798-
appUpdater.Update(_updateAppCancellationTokenSource.Token);
800+
801+
using (_updaterStatus.Take(1).Subscribe((status) => _state.Value = PatcherState.UpdatingApp))
802+
{
803+
appUpdater.Update(_updateAppCancellationTokenSource.Token);
804+
}
799805
}
800806
finally
801807
{
808+
_state.Value = PatcherState.None;
809+
802810
_updaterStatus.Value = null;
803811
_updateAppCancellationTokenSource = null;
804812
}

Assets/PatchKit Patcher/Scripts/PatcherState.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
public enum PatcherState
44
{
55
None,
6+
Connecting,
67
LoadingPatcherData,
78
LoadingPatcherConfiguration,
89
WaitingForUserDecision,
910
UpdatingApp,
10-
StartingApp
11+
StartingApp,
12+
DisplayingError,
1113
}
1214
}

Assets/PatchKit Patcher/Scripts/UI/ProgressBar.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ public class ProgressBar : MonoBehaviour
1111

1212
public Image Image;
1313

14+
private struct UpdateData
15+
{
16+
public double Progress;
17+
public PatcherState State;
18+
}
19+
1420
private void SetBar(float start, float end)
1521
{
1622
var anchorMax = Image.rectTransform.anchorMax;
@@ -23,15 +29,31 @@ private void SetBar(float start, float end)
2329
Image.rectTransform.anchorMin = anchorMin;
2430
}
2531

26-
private void SetProgress(double progress)
32+
private void SetProgress(UpdateData data)
2733
{
28-
if (progress <= 0 && _isIdle)
34+
_isIdle = data.State == PatcherState.Connecting;
35+
36+
if (data.State == PatcherState.None)
37+
{
38+
Text.text = "";
39+
SetBar(0, 0);
40+
return;
41+
}
42+
43+
if (data.State == PatcherState.DisplayingError)
44+
{
45+
Text.text = "Error!";
46+
SetBar(0, 0);
47+
return;
48+
}
49+
50+
if (data.State == PatcherState.Connecting)
2951
{
3052
Text.text = "Connecting...";
3153
return;
3254
}
3355

34-
_isIdle = false;
56+
double progress = data.Progress;
3557

3658
Text.text = progress >= 0.0 ? progress.ToString("0.0%") : "";
3759
float visualProgress = progress >= 0.0 ? (float) progress : 0.0f;
@@ -41,15 +63,18 @@ private void SetProgress(double progress)
4163

4264
private void Start()
4365
{
44-
Patcher.Instance.UpdaterStatus
45-
.SelectSwitchOrDefault(s => s.Progress, -1.0)
66+
var progress = Patcher.Instance.UpdaterStatus
67+
.SelectSwitchOrDefault(s => s.Progress, -1.0);
68+
69+
Patcher.Instance.State
70+
.CombineLatest(progress, (state, d) => new UpdateData { Progress = d, State = state })
4671
.ObserveOnMainThread()
4772
.Subscribe(SetProgress)
4873
.AddTo(this);
4974
}
5075

5176

52-
private bool _isIdle = true;
77+
private bool _isIdle = false;
5378
private const float IdleBarWidth = 0.2f;
5479
private const float IdleBarSpeed = 1.2f;
5580
private float _idleProgress = -IdleBarWidth;

0 commit comments

Comments
 (0)