-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathArtistViewModel.cs
More file actions
188 lines (154 loc) · 5.62 KB
/
ArtistViewModel.cs
File metadata and controls
188 lines (154 loc) · 5.62 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using Espera.Core;
using ReactiveUI;
using Splat;
namespace Espera.View.ViewModels
{
public sealed class ArtistViewModel : ReactiveObject, IEquatable<ArtistViewModel>, IDisposable
{
private readonly ObservableAsPropertyHelper<BitmapSource> cover;
private readonly int orderHint;
/// <summary>
/// The constructor.
/// </summary>
/// <param name="artistName"></param>
/// <param name="artworkKeys"></param>
/// <param name="orderHint">
/// A hint that tells this instance which position it has in the artist list. This helps for
/// priorizing the album cover loading. The higher the number, the earlier it is in the list
/// (Think of a reversed sorted list).
/// </param>
public ArtistViewModel(string artistName, IObservable<string> artworkKeys, int orderHint = 1)
{
this.orderHint = orderHint;
this.cover = artworkKeys
.Where(x => x != null)
.Distinct() // Ignore duplicate artworks
.Select(key => Observable.FromAsync(() => this.LoadArtworkAsync(key)))
.Concat()
.FirstOrDefaultAsync(pic => pic != null)
.ToProperty(this, x => x.Cover);
var connect = this.Cover; // Connect the property to the source observable immediately
this.Name = artistName;
this.IsAllArtists = false;
}
public ArtistViewModel(string allArtistsName)
{
this.Name = allArtistsName;
this.IsAllArtists = true;
}
public BitmapSource Cover
{
get { return this.cover == null ? null : this.cover.Value; }
}
public bool IsAllArtists { get; private set; }
public string Name { get; private set; }
public void Dispose()
{
this.cover?.Dispose();
}
public bool Equals(ArtistViewModel other)
{
if (Object.ReferenceEquals(other, null))
{
return false;
}
if (this.IsAllArtists && other.IsAllArtists)
{
return true;
}
if (this.IsAllArtists || other.IsAllArtists)
{
return false;
}
return this.Name.Equals(other.Name, StringComparison.InvariantCultureIgnoreCase);
}
public override bool Equals(object obj)
{
return base.Equals(obj as ArtistViewModel);
}
public override int GetHashCode()
{
return new { A = this.IsAllArtists, B = this.Name }.GetHashCode();
}
private async Task<BitmapSource> LoadArtworkAsync(string key)
{
try
{
IBitmap img = await ArtworkCache.Instance.Retrieve(key, 50, orderHint);
return img.ToNative();
}
catch (KeyNotFoundException ex)
{
this.Log().WarnException(String.Format("Could not find key {0} of album cover. This reeks like a threading problem", key), ex);
return null;
}
catch (ArtworkCacheException ex)
{
this.Log().ErrorException(String.Format("Unable to load artwork with key {0} from cache", key), ex);
return null;
}
catch (Exception ex)
{
this.Log().InfoException(String.Format("Akavache threw an error on artist cover loading for key {0}", key), ex);
return null;
}
}
/// <summary>
/// A custom equality class for the artist grouping, until
/// https://github.com/RolandPheasant/DynamicData/issues/31 is resolved
/// </summary>
public class ArtistString : IEquatable<ArtistString>
{
private readonly string artistName;
public ArtistString(string artistName)
{
this.artistName = artistName;
}
public static implicit operator ArtistString(string source)
{
return new ArtistString(source);
}
public static implicit operator string(ArtistString source)
{
return source.artistName;
}
public bool Equals(ArtistString other)
{
return StringComparer.InvariantCultureIgnoreCase.Equals(this.artistName, other.artistName);
}
public override bool Equals(object obj)
{
return this.Equals(obj as ArtistString);
}
public override int GetHashCode()
{
return StringComparer.InvariantCultureIgnoreCase.GetHashCode(this.artistName);
}
}
public class Comparer : IComparer<ArtistViewModel>
{
public int Compare(ArtistViewModel x, ArtistViewModel y)
{
if (x.IsAllArtists && y.IsAllArtists)
{
return 0;
}
if (x.IsAllArtists)
{
return -1;
}
if (y.IsAllArtists)
{
return 1;
}
return String.Compare(SortHelpers.RemoveArtistPrefixes(x.Name), SortHelpers.RemoveArtistPrefixes(y.Name), StringComparison.InvariantCultureIgnoreCase);
}
}
}
}