summary refs log tree commit diff
path: root/LibBeatmapDownload/DomainStats.cs
blob: a026b7d801419f3798e42d396e54812573624085 (plain) (blame)
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
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace LibBeatmapDownload;

public class DomainStats(string url) : INotifyPropertyChanged {
	public string Domain { get; } = new Uri(url).Host;

	private int _total = 0;

	public int Total => Success + Failed;

	private int _success = 0;

	public int Success {
		get => _success;
		set => SetField(ref _success, value);
	}

	private int _failed = 0;

	public int Failed {
		get => _failed;
		set => SetField(ref _failed, value);
	}

	public double Progress => Total == 0 ? 100 : (double)Success / Total * 100;
	public string ProgressString => $"{Domain} {Progress/100:P0} ({Failed}/{Total} failed)";

	public event PropertyChangedEventHandler? PropertyChanged;

	private readonly Stopwatch sw = Stopwatch.StartNew();
	protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) {
		PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
		PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Progress)));
		PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ProgressString)));
	}

	protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null) {
		if (EqualityComparer<T>.Default.Equals(field, value)) return false;
		field = value;
		OnPropertyChanged(propertyName);
		return true;
	}
}