6
6
using MsBox . Avalonia . Enums ;
7
7
using ReactiveUI ;
8
8
using System ;
9
- using System . IO ;
10
9
using System . Linq ;
11
10
using System . Text . Json ;
12
11
using System . Threading . Tasks ;
13
12
using System . Web ;
14
13
using System . Windows . Input ;
15
- using System . Xml . Linq ;
16
14
17
15
namespace Downloader . ViewModels ;
18
16
@@ -31,32 +29,40 @@ public ImportAlbumsViewModel(MainViewModel mainViewModel)
31
29
IsImporting = true ;
32
30
_ = Task . Run ( async ( ) =>
33
31
{
32
+ string reqUrl ;
33
+ LastFmApi json ;
34
34
try
35
35
{
36
36
var songs = _mainViewModel . Data . Musics . Where ( x => x . Album == null ) ;
37
37
if ( ImportLocalOnly )
38
38
{
39
39
songs = songs . Where ( x => x . Source == "localfile" ) ;
40
40
}
41
+ int i = 0 ;
42
+ var songCount = songs . Count ( ) ;
41
43
foreach ( var s in songs )
42
44
{
43
- var reqUrl = $ "https://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key={ LastFmApiKey } &artist={ HttpUtility . UrlEncode ( s . Artist ) } &track={ HttpUtility . UrlEncode ( s . Name ) } &format=json";
44
- var json = JsonSerializer . Deserialize < LastFmApi > ( await _mainViewModel . Client . GetStringAsync ( reqUrl ) ) ;
45
+ reqUrl = $ "https://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key={ LastFmApiKey } &artist={ HttpUtility . UrlEncode ( s . Artist ) } &track={ HttpUtility . UrlEncode ( s . Name ) } &format=json";
46
+ json = JsonSerializer . Deserialize < LastFmApi > ( await _mainViewModel . Client . GetStringAsync ( reqUrl ) , _mainViewModel . JsonOptions ) ;
45
47
46
- if ( json . Message ! = null && json . Track . Album != null && json . Track . Album . Image . Any ( ) )
48
+ if ( json . Message = = null && json . Track . Album != null && json . Track . Album . Image . Any ( ) )
47
49
{
48
50
string album = json . Track . Album . Title ;
49
51
var url = json . Track . Album . Image . Last ( ) . Text ;
50
52
if ( url != string . Empty )
51
53
{
52
54
var path = _mainViewModel . GetAlbumName ( s . Artist , album ) ;
53
- if ( _mainViewModel . Data . Albums . Any ( x => x . Key == path ) )
55
+ if ( ! _mainViewModel . Data . Albums . Any ( x => x . Key == path ) )
54
56
{
55
57
await foreach ( var prog in ProcessManager . DownloadImageAsync ( _mainViewModel . Client , url , $ "{ _mainViewModel . DataFolderPath } /icon/{ path } .png") ) { }
58
+ s . Album = path ;
56
59
_mainViewModel . SaveImage ( path , album , url ) ;
57
60
}
58
61
}
59
62
}
63
+
64
+ i ++ ;
65
+ ImportAlbums = i / ( float ) songCount ;
60
66
}
61
67
}
62
68
catch ( Exception ex )
@@ -69,6 +75,9 @@ public ImportAlbumsViewModel(MainViewModel mainViewModel)
69
75
}
70
76
finally
71
77
{
78
+ _mainViewModel . SaveData ( ) ;
79
+ UpdateAffectedFiles ( ) ;
80
+ ImportAlbums = 0f ;
72
81
IsImporting = false ;
73
82
}
74
83
} ) ;
@@ -86,6 +95,11 @@ public void AfterInit()
86
95
UpdateAffectedFiles ( ) ;
87
96
}
88
97
98
+ public void OnDataRefresh ( )
99
+ {
100
+ UpdateAffectedFiles ( ) ;
101
+ }
102
+
89
103
private MainViewModel _mainViewModel ;
90
104
91
105
public ICommand ImportAll { get ; }
0 commit comments