Skip to content

Commit 4959416

Browse files
committed
Simplify keyboard tracking logic.
1 parent 0a142af commit 4959416

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

samples/Unity.Mvvm.ToDoList/Assets/Scripts/UIElements/MobileInputAdaptivePage.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ namespace UIElements
1212
{
1313
public class MobileInputAdaptivePage : VisualElement, IDisposable
1414
{
15+
private bool _isActivated;
1516
private float _parentHeight;
1617
private float _initialPaddingBottom;
1718
private float _defaultPaddingBottom;
1819
private float? _paddingBottomWithKeyboard;
1920
private MobileInputDialogController _inputDialog;
2021

21-
private AsyncLazy _trackKeyboardActivityTask;
2222
private CancellationTokenSource _cancellationTokenSource;
2323

2424
public MobileInputAdaptivePage()
@@ -29,14 +29,21 @@ public MobileInputAdaptivePage()
2929
}
3030
}
3131

32+
public bool IsActivated => _isActivated;
3233
public float OffsetFromKeyboardPx { get; set; }
33-
public bool IsActivated => _trackKeyboardActivityTask is { Task: { Status: UniTaskStatus.Pending } };
34-
34+
3535
public async UniTask ActivateAsync()
3636
{
37+
if (_isActivated)
38+
{
39+
return;
40+
}
41+
42+
_isActivated = true;
43+
3744
if (IsScreenKeyboardSupported())
3845
{
39-
ActivateKeyboardTrackingAsync().Forget();
46+
TrackKeyboardActivityAsync().Forget();
4047
}
4148

4249
SetVisible(true);
@@ -48,6 +55,13 @@ public async UniTask ActivateAsync()
4855

4956
public async UniTask DeactivateAsync()
5057
{
58+
if (_isActivated == false)
59+
{
60+
return;
61+
}
62+
63+
_isActivated = false;
64+
5165
if (IsScreenKeyboardSupported())
5266
{
5367
_inputDialog.HideScreenKeyboard();
@@ -89,25 +103,15 @@ private void SetVisible(bool value) // TODO: Move to another place?
89103
parent.visible = value;
90104
}
91105

92-
private async UniTaskVoid ActivateKeyboardTrackingAsync()
93-
{
94-
if (_trackKeyboardActivityTask?.Task.Status.IsCompleted() ?? true)
95-
{
96-
_trackKeyboardActivityTask = TrackKeyboardActivityAsync().ToAsyncLazy();
97-
}
98-
99-
await _trackKeyboardActivityTask;
100-
}
101-
102-
private async UniTask TrackKeyboardActivityAsync()
106+
private async UniTaskVoid TrackKeyboardActivityAsync()
103107
{
104108
_cancellationTokenSource = new CancellationTokenSource();
105109

106110
try
107111
{
108112
var cancellationToken = _cancellationTokenSource.Token;
109113

110-
while (cancellationToken.IsCancellationRequested == false)
114+
while (_isActivated)
111115
{
112116
if (TouchScreenKeyboard.visible == false)
113117
{

0 commit comments

Comments
 (0)