-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInactivityTimer.cs
More file actions
113 lines (92 loc) · 2.73 KB
/
InactivityTimer.cs
File metadata and controls
113 lines (92 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
using CCC.Runtime.Utils;
namespace CCC.Runtime
{
/// <summary>
/// Monitors user activity and triggers an action after a specified period of inactivity.
/// Uses UniTask for asynchronous operation and can be paused/resumed as needed.
/// </summary>
public class InactivityTimer : MonoBehaviour
{
#region Serialized Fields
[Tooltip("Time in seconds before the inactivity action is triggered")]
[field: SerializeField]
public uint TimeBeforeAction { get; set; } = 60;
#endregion
#region Private Fields
private float _lastUsedTime = 0;
private CancellationTokenSource _cancellationTokenSource;
#endregion
#region Properties
/// <summary>
/// Gets or sets the action to be executed when the inactivity timer expires.
/// </summary>
/// <value>The action to perform after the specified inactivity period.</value>
public Action InactivityAction { get; private set; }
#endregion
#region Event Functions
/// <summary>
/// Initializes the timer when the component is enabled, starting the inactivity monitoring.
/// </summary>
private void OnEnable()
{
_lastUsedTime = Time.time;
StopTimer();
_cancellationTokenSource = new CancellationTokenSource();
RunTimerAsync(_cancellationTokenSource.Token).Forget();
}
/// <summary>
/// Stops the timer when the component is disabled to prevent memory leaks.
/// </summary>
private void OnDisable()
{
StopTimer();
}
#endregion
#region Public Methods
/// <summary>
/// Resets the inactivity timer, indicating that the user has been active.
/// Call this method whenever user activity is detected.
/// </summary>
public void Used()
{
_lastUsedTime = Time.time;
}
#endregion
#region Private Methods
/// <summary>
/// Stops the current timer and disposes of the cancellation token source.
/// </summary>
private void StopTimer()
{
_cancellationTokenSource?.Cancel();
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = null;
}
/// <summary>
/// Runs the inactivity timer asynchronously, monitoring for user activity.
/// </summary>
/// <param name="cancellationToken">Token used to cancel the timer operation.</param>
private async UniTaskVoid RunTimerAsync(CancellationToken cancellationToken)
{
try
{
float timeLeft;
while ((timeLeft = (_lastUsedTime + TimeBeforeAction) - Time.time) > 0)
{
await UniTaskUtils.Delay(timeLeft, ignoreTimeScale: true, cancellationToken: cancellationToken);
}
if (!cancellationToken.IsCancellationRequested)
{
InactivityAction?.Invoke();
enabled = false;
}
}
catch (OperationCanceledException) { }
}
#endregion
}
}