-
Notifications
You must be signed in to change notification settings - Fork 1
/
UpdateTracker.cs
82 lines (76 loc) · 2.53 KB
/
UpdateTracker.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace BSU.Sync
{
class UpdateTracker
{
private readonly int _updates;
private readonly long _updatesBytes;
private readonly int _downloads;
private readonly long _downloadsBytes;
private readonly Action<DownloadProgressEventArgs> _downloadHandler;
private readonly Action<DownloadProgressEventArgs> _updateHandler;
private readonly List<TaskState> _states = new List<TaskState>();
private readonly object _stateslock = new object();
public UpdateTracker(int updates, long updatesBytes, int downloads, long downloadsBytes, Action<DownloadProgressEventArgs> downloadHandler, Action<DownloadProgressEventArgs> updateHandler)
{
_updates = updates;
_updatesBytes = updatesBytes;
_downloads = downloads;
_downloadsBytes = downloadsBytes;
_downloadHandler = downloadHandler;
_updateHandler = updateHandler;
}
public TaskState NewTask(ChangeReason type)
{
var state = new TaskState
{
BytesDownloaded = 0,
Complete = false,
Type = type
};
lock (_stateslock)
{
_states.Add(state);
}
return state;
}
public void Update(TaskState state)
{
long bytes;
int items;
lock (_stateslock)
{
bytes = _states.Where(s => s.Type == state.Type).Sum(s => s.BytesDownloaded);
items = _states.Count(s => s.Type == state.Type && s.Complete);
}
if (state.Type == ChangeReason.New)
{
_downloadHandler(new DownloadProgressEventArgs
{
BytesDonwloaded = bytes,
BytesTotal = _downloadsBytes,
Files = items,
FilesTotal = _downloads
});
}
else
{
_updateHandler(new DownloadProgressEventArgs
{
BytesDonwloaded = bytes,
BytesTotal = _updatesBytes,
Files = items,
FilesTotal = _updates
});
}
}
public class TaskState
{
public long BytesDownloaded;
public bool Complete;
public ChangeReason Type;
}
}
}