Skip to content

Commit 6c8397c

Browse files
committed
[v0.1.9] Destroy's replaced with TearDown
1 parent 4f5c8fb commit 6c8397c

8 files changed

+70
-63
lines changed

Runtime/LoadingCurtainCloseButton.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,13 @@ internal sealed class LoadingContinueButton : LoadingCurtainView
1717
private Expectant _buttonClicked;
1818
private LoadingCurtainViewModel _viewModel;
1919

20-
private void OnDestroy()
21-
{
22-
_buttonClicked?.Dispose();
23-
24-
if (_button != null)
25-
{
26-
_button.clicked -= OnClicked;
27-
}
28-
29-
if (_viewModel != null)
30-
{
31-
_viewModel.Progress.Changed -= OnProgressChanged;
32-
}
33-
}
34-
3520
public override void Initialize(LoadingCurtainViewModel viewModel, IGroupExpectant expectant)
3621
{
3722
_viewModel = viewModel;
3823
_viewModel.Progress.Changed += OnProgressChanged;
39-
expectant.With(_buttonClicked = new Expectant());
4024

41-
var document = GetComponent<UIDocument>();
42-
var rootElement = document.rootVisualElement;
43-
_button = rootElement.Q<Button>(_bindingPath);
25+
expectant.With(_buttonClicked = new Expectant());
26+
_button = GetComponent<UIDocument>().rootVisualElement.Q<Button>(_bindingPath);
4427
if (_button == null)
4528
{
4629
return;
@@ -50,15 +33,32 @@ public override void Initialize(LoadingCurtainViewModel viewModel, IGroupExpecta
5033
_button.clicked += OnClicked;
5134
}
5235

36+
public override void TearDown()
37+
{
38+
_buttonClicked?.Dispose();
39+
40+
if (_button != null)
41+
{
42+
_button.clicked -= OnClicked;
43+
}
44+
45+
if (_viewModel != null)
46+
{
47+
_viewModel.Progress.Changed -= OnProgressChanged;
48+
}
49+
}
50+
5351
private void OnClicked()
5452
{
53+
_button.clicked -= OnClicked;
5554
_button.SetEnabled(false);
5655
_buttonClicked.SetReady();
5756
}
5857

5958
private void OnProgressChanged(float progress)
6059
{
61-
if (Mathf.Approximately(progress, 1))
60+
var completed = Mathf.Approximately(progress, 1) || progress >= 1;
61+
if (_button.enabledSelf == false && completed)
6262
{
6363
_button.SetEnabled(true);
6464
}

Runtime/LoadingCurtainDescription.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ public sealed class LoadingCurtainDescription : LoadingCurtainView
1717
private UIDocument _document;
1818
private LoadingCurtainViewModel _viewModel;
1919

20-
private void OnDestroy()
21-
{
22-
if (_viewModel != null)
23-
{
24-
_viewModel.Description.Changed -= OnDescriptionChanged;
25-
}
26-
}
27-
2820
public override void Initialize(LoadingCurtainViewModel viewModel, IGroupExpectant expectant)
2921
{
3022
_viewModel = viewModel;
3123
_viewModel.Description.Changed += OnDescriptionChanged;
3224
_label = GetComponent<UIDocument>().rootVisualElement.Q<Label>(_bindingPath);
3325
}
3426

27+
public override void TearDown()
28+
{
29+
if (_viewModel != null)
30+
{
31+
_viewModel.Description.Changed -= OnDescriptionChanged;
32+
}
33+
}
34+
3535
private void OnDescriptionChanged(OperationDescription description) => _label.text = description.Text;
3636
}
3737
}

Runtime/LoadingCurtainProgressBar.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Depra.Loading
99
{
10-
[DisallowMultipleComponent]
1110
[RequireComponent(typeof(UIDocument))]
1211
public sealed class LoadingCurtainProgressBar : LoadingCurtainView
1312
{
@@ -33,15 +32,6 @@ private void Update()
3332
_bar.value = delta > _maxDelta ? _bar.value + delta : _target;
3433
}
3534

36-
private void OnDestroy()
37-
{
38-
_filled?.Dispose();
39-
if (_viewModel != null)
40-
{
41-
_viewModel.Progress.Changed -= OnProgressChanged;
42-
}
43-
}
44-
4535
public override void Initialize(LoadingCurtainViewModel viewModel, IGroupExpectant expectant)
4636
{
4737
_viewModel = viewModel;
@@ -52,6 +42,15 @@ public override void Initialize(LoadingCurtainViewModel viewModel, IGroupExpecta
5242
enabled = true;
5343
}
5444

45+
public override void TearDown()
46+
{
47+
_filled?.Dispose();
48+
if (_viewModel != null)
49+
{
50+
_viewModel.Progress.Changed -= OnProgressChanged;
51+
}
52+
}
53+
5554
private void OnProgressChanged(float value) => _target = value;
5655
}
5756
}

Runtime/LoadingCurtainProgressLabel.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ public sealed class LoadingCurtainProgressLabel : LoadingCurtainView
1616
private Label _label;
1717
private LoadingCurtainViewModel _viewModel;
1818

19-
private void OnDestroy()
20-
{
21-
if (_viewModel != null)
22-
{
23-
_viewModel.Progress.Changed -= OnProgressChanged;
24-
}
25-
}
26-
2719
public override void Initialize(LoadingCurtainViewModel viewModel, IGroupExpectant expectant)
2820
{
2921
_viewModel = viewModel;
3022
_viewModel.Progress.Changed += OnProgressChanged;
3123
_label = GetComponent<UIDocument>().rootVisualElement.Q<Label>(_bindingPath);
3224
}
3325

26+
public override void TearDown()
27+
{
28+
if (_viewModel != null)
29+
{
30+
_viewModel.Progress.Changed -= OnProgressChanged;
31+
}
32+
}
33+
3434
private void OnProgressChanged(float value) =>
3535
_label.text = string.Format(_format, Mathf.RoundToInt(value * 100));
3636
}

Runtime/LoadingCurtainView.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ namespace Depra.Loading
88
{
99
public abstract class LoadingCurtainView : MonoBehaviour
1010
{
11+
protected private void OnDestroy() { }
12+
1113
public abstract void Initialize(LoadingCurtainViewModel viewModel, IGroupExpectant expectant);
14+
15+
public virtual void TearDown() { }
1216
}
1317
}

Runtime/LoadingCurtainViewRoot.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ public void Initialize(LoadingCurtainViewModel viewModel, IGroupExpectant expect
1818
view.Initialize(viewModel, expectant);
1919
}
2020
}
21+
22+
public void TearDown()
23+
{
24+
foreach (var view in _views)
25+
{
26+
view.TearDown();
27+
}
28+
}
2129
#if UNITY_EDITOR
2230
[ContextMenu(nameof(Refill))]
2331
private void Refill()

Runtime/OverlayLoadingCurtain.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public sealed class OverlayLoadingCurtain : ILoadingCurtain
2727

2828
public OverlayLoadingCurtain(IAssetFile<LoadingCurtainViewRoot> assetFile) => _assetFile = assetFile;
2929

30-
public async Task Load(Queue<ILoadingOperation> operations, CancellationToken cancellationToken)
30+
public async Task Load(Queue<ILoadingOperation> operations, CancellationToken token)
3131
{
3232
_operationIndex = 0;
3333
_operationsCount = operations.Count;
3434

35-
_original = await FetchViewOriginal(cancellationToken);
35+
_original ??= await _assetFile.LoadAsync(cancellationToken: token);
3636
_viewModel = new LoadingCurtainViewModel();
3737
_view = Object.Instantiate(_original);
3838

@@ -46,23 +46,24 @@ public async Task Load(Queue<ILoadingOperation> operations, CancellationToken ca
4646
foreach (var operation in operations)
4747
{
4848
_viewModel.Description.Value = operation.Description;
49-
await operation.Load(new Progress<float>(OnProgress), cancellationToken);
49+
await operation.Load(new Progress<float>(OnProgress), token);
5050
_operationIndex++;
5151
}
5252

5353
operationsReady.SetReady();
54-
await WaitForViewClosed(cancellationToken);
54+
await WaitForViewClosed(token);
5555
}
5656

57-
public Task Unload(CancellationToken cancellationToken)
57+
public Task Unload(CancellationToken token)
5858
{
5959
_operationIndex = 0;
6060
_operationsCount = 0;
6161
_viewModel?.Dispose();
6262
_viewReady?.Dispose();
6363

64-
if (_view != null)
64+
if (_view)
6565
{
66+
_view.TearDown();
6667
Object.Destroy(_view.gameObject);
6768
}
6869

@@ -78,22 +79,17 @@ public Task Unload(CancellationToken cancellationToken)
7879
return Task.CompletedTask;
7980
}
8081

81-
private void OnProgress(float progress) => _viewModel.Progress.Value = NormalizeProgress(progress);
82-
83-
private float NormalizeProgress(float progress)
82+
private void OnProgress(float progress)
8483
{
85-
var normalizedProgress = (_operationIndex + progress) / _operationsCount;
86-
if (Mathf.Abs(normalizedProgress - 1) < 0.01f)
84+
var normalized = (_operationIndex + progress) / _operationsCount;
85+
if (Mathf.Abs(normalized - 1) < 0.01f)
8786
{
88-
normalizedProgress = 1;
87+
normalized = 1;
8988
}
9089

91-
return normalizedProgress;
90+
_viewModel.Progress.Value = normalized;
9291
}
9392

94-
private async Task<LoadingCurtainViewRoot> FetchViewOriginal(CancellationToken token) =>
95-
_original == null ? await _assetFile.LoadAsync(cancellationToken: token) : _original;
96-
9793
private async Task WaitForViewClosed(CancellationToken token)
9894
{
9995
while (_viewReady.IsReady() == false)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.depra.loading.unity",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"displayName": "Depra.Loading",
55
"description": "",
66
"unity": "2022.3",

0 commit comments

Comments
 (0)