-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindowViewModel.cs
More file actions
71 lines (62 loc) · 2.21 KB
/
Copy pathMainWindowViewModel.cs
File metadata and controls
71 lines (62 loc) · 2.21 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
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using Avalonia.Threading;
namespace TorrentFlow
{
public class MainWindowViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private ObservableCollection<TorrentView> _torrents = new();
public ObservableCollection<TorrentView> Torrents
{
get => _torrents;
private set
{
_torrents = value;
OnPropertyChanged();
NotifyTorrentCountChanged();
}
}
public int TorrentsCount => Torrents?.Count ?? 0;
public bool HasTorrents => TorrentsCount > 0;
public bool NoTorrents => TorrentsCount == 0;
public MainWindowViewModel()
{
Torrents.CollectionChanged += (s, e) => NotifyTorrentCountChanged();
}
private void NotifyTorrentCountChanged()
{
OnPropertyChanged(nameof(TorrentsCount));
OnPropertyChanged(nameof(HasTorrents));
OnPropertyChanged(nameof(NoTorrents));
}
public void UpdateAllTorrentVisuals()
{
foreach (var torrentView in Torrents)
{
torrentView.UpdateProgress(); // For Progress, Status, Completed
torrentView.UpdateSpeeds(); // For DownloadSpeed, UploadSpeed
}
}
public void AddTorrent(TorrentView torrentView)
{
if (!Torrents.Any(t => t.Name == torrentView.Name))
{
Dispatcher.UIThread.Post(() => Torrents.Add(torrentView));
}
}
public void RemoveTorrent(TorrentView torrentView)
{
if (Torrents.Contains(torrentView))
{
Dispatcher.UIThread.Post(() => Torrents.Remove(torrentView));
}
}
}
}