-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuzzyMediaInfo.cs
115 lines (93 loc) · 5.1 KB
/
FuzzyMediaInfo.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
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
114
115
#region Copyright (C) 2017-2021 Starflash Studios
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License (Version 3.0)
// as published by the Free Software Foundation.
//
// More information can be found here: https://www.gnu.org/licenses/gpl-3.0.en.html
#endregion
#nullable enable
#region Using Directives
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Threading.Tasks;
using Windows.Media.Control;
using GSMTCMediaProperties = Windows.Media.Control.GlobalSystemMediaTransportControlsSessionMediaProperties;
using GSMTCSession = Windows.Media.Control.GlobalSystemMediaTransportControlsSession;
using GSMTCSessionManager = Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager;
#endregion
namespace QMediaVSIX {
public readonly struct FuzzyMediaInfo : IEquatable<FuzzyMediaInfo> {
public readonly string? Title, Artist;
public readonly GlobalSystemMediaTransportControlsSessionPlaybackStatus? Status;
public readonly TimeSpan? CurrentTime, EndTime;
readonly bool _SuccessfullyGenerated;
FuzzyMediaInfo(string? Title, string? Artist, GlobalSystemMediaTransportControlsSessionPlaybackStatus? Status, TimeSpan? CurrentTime, TimeSpan? EndTime, bool SuccessfullyGenerated) {
this.Title = Title;
this.Artist = Artist;
this.Status = Status;
this.CurrentTime = CurrentTime;
this.EndTime = EndTime;
_SuccessfullyGenerated = SuccessfullyGenerated;
}
public FuzzyMediaInfo(string? Title, string? Artist, GlobalSystemMediaTransportControlsSessionPlaybackStatus? Status, TimeSpan? CurrentTime, TimeSpan? EndTime) : this(Title, Artist, Status, CurrentTime, EndTime, true) { }
public static async Task<FuzzyMediaInfo> GetAsync(GSMTCSessionManager? Manager ) => (await GetAsyncEx(Manager)).Item1;
[SuppressMessage("Style", "VSTHRD200:Use \"Async\" suffix for async methods", Justification = "<Pending>")]
public static async Task<(FuzzyMediaInfo, GSMTCSession?, GSMTCMediaProperties?)> GetAsyncEx(GSMTCSessionManager? Manager) {
GSMTCSession? Session = Manager?.GetCurrentSession();
if (Manager == null || Session == null) { return (new FuzzyMediaInfo(null, null, null, null, null, false), null, null); }
GSMTCMediaProperties? Props = await Session.TryGetMediaPropertiesAsync();
GlobalSystemMediaTransportControlsSessionTimelineProperties? TimeProps = Session.GetTimelineProperties();
return (new FuzzyMediaInfo(Props?.Title, Props?.Artist, Session.GetPlaybackInfo()?.PlaybackStatus, TimeProps?.Position, TimeProps?.EndTime, Session != null), Session, Props);
}
public override bool Equals(object? Obj) => Obj is FuzzyMediaInfo Info && Equals(Info);
/// <inheritdoc />
public override int GetHashCode() {
unchecked {
int HashCode = Title != null ? StringComparer.InvariantCultureIgnoreCase.GetHashCode(Title) : 0;
HashCode = (HashCode * 397) ^ (Artist != null ? StringComparer.InvariantCultureIgnoreCase.GetHashCode(Artist) : 0);
HashCode = (HashCode * 397) ^ Status.GetHashCode();
//HashCode = (HashCode * 397) ^ CurrentTime.GetHashCode();
HashCode = (HashCode * 397) ^ EndTime.GetHashCode();
HashCode = (HashCode * 397) ^ _SuccessfullyGenerated.GetHashCode();
return HashCode;
}
}
public bool Equals(FuzzyMediaInfo Other) =>
Title == Other.Title &&
Artist == Other.Artist &&
Status == Other.Status &&
//CurrentTime == Other.CurrentTime &&
EndTime == Other.EndTime &&
_SuccessfullyGenerated == Other._SuccessfullyGenerated;
public static bool operator ==(FuzzyMediaInfo Left, FuzzyMediaInfo Right) => Left.Equals(Right);
public static bool operator !=(FuzzyMediaInfo Left, FuzzyMediaInfo Right) => !(Left == Right);
public string GetHeading() {
if (!_SuccessfullyGenerated) { return "#GEN/NULL!"; }
StringBuilder SB = new StringBuilder();
if (Artist != null) {
SB.Append(Artist);
SB.Append(" - ");
}
SB.Append(Title ?? "#TITLE/NULL!");
return SB.ToString();
}
public string GetSubheading() {
if (!_SuccessfullyGenerated) { return "#GEN/NULL!"; }
bool Any = false;
StringBuilder SB = new StringBuilder();
if (CurrentTime.HasValue) {
SB.Append(CurrentTime.Value.ToString("g"));
Any = true;
}
if (EndTime.HasValue) {
SB.Append(" / ");
SB.Append(EndTime.Value.ToString("g"));
Any = true;
}
if (!Any) { SB.Append("#TIME/ALL/NULL!"); }
return SB.ToString();
}
public override string ToString() => GetHeading();
}
}