Ejemplo n.º 1
0
        public MusicLoader(string syncDir, string token, long userId)
        {
            DependencyUtility.BuildUp(this);

            syncDirectory = syncDir;
            this.userId   = userId;

            api = new VkApi();
            api.Authorize(token, userId);
            api.Invoke("stats.trackVisitor", new Dictionary <string, string>(), true); // статистика посещаемости приложения

            remoteAudioSizeHelper = new RemoteAudioSizeHelper(Path.Combine(AppPaths.SettingsPath, "sizes.dat"));

            tracks = new ConcurrentSortedList <int, Track>();

            int key = 0;

            foreach (var track in TrackRepository.GetTracks())
            {
                tracks.Add(key++, track);
            }
        }
Ejemplo n.º 2
0
        private void Load()
        {
            if (!Monitor.TryEnter(musicLoadLock))
            {
                return;
            }

            OnSynchronizationStarted();

            var audios = api.Audio.Get(userId);

            OnProgressChanged(new ProgressStatus {
                TotalCount = audios.Count
            });

            var addedList = new List <Track>();

            var trackIDs    = tracks.Values.Select(t => t.Id).ToList();
            var tracksCount = tracks.Count;

            tracks = new ConcurrentSortedList <int, Track>();

            int loadedTracksCount = 0;

            var tracksDictionary = audios.ToDictionary(audio => audio, audio => new Track().Update(audio));

            var trackIndexes = tracksDictionary.Values.Select((s, i) => new { s, i }).ToDictionary(x => x.s, x => x.i);

            Parallel.ForEach(audios, new ParallelOptions {
                MaxDegreeOfParallelism = 4
            }, (audio) =>
            {
                var track = tracksDictionary[audio];

                var fileName = track.GetFilePath();

                var fi = new FileInfo(fileName);
                if (remoteAudioSizeHelper.GetSize(audio) > 0)
                {
                    if (!trackIDs.Contains(audio.Id))
                    {
                        addedList.Add(track);
                    }
                    if (!fi.Exists || fi.Length < remoteAudioSizeHelper.GetSize(audio))
                    {
                        TrySaveFileFromUrl(audio.Url.ToString(), fileName);
                    }

                    tracks.Add(trackIndexes[track], track);

                    OnAddedOneTrack(track, tracks.Values.ToList());

                    Interlocked.Increment(ref loadedTracksCount);

                    OnProgressChanged(new ProgressStatus {
                        TotalCount = audios.Count, DoneCount = loadedTracksCount
                    });
                }
            });

            remoteAudioSizeHelper.Save();

            TrackRepository.SaveTracks(tracks.Values.ToList());
            // TODO do we really need deleted list?
            var deletedList = GetDeletedTrackIDs();

            OnCompleted();

            lastLoadTime = DateTime.UtcNow;

            Monitor.Exit(musicLoadLock);
        }