public async Task <PlaybackCounter> GetPlaybackCountersAsync(string path) { PlaybackCounter counters = null; await Task.Run(() => { try { using (var conn = this.factory.GetConnection()) { try { counters = conn.Query <PlaybackCounter>("SELECT Path, SafePath, PlayCount, SkipCount, DateLastPlayed FROM Track WHERE SafePath=?", path.ToSafePath()).FirstOrDefault(); } catch (Exception ex) { LogClient.Error("Could not get PlaybackCounters for path='{0}'. Exception: {1}", path, ex.Message); } } } catch (Exception ex) { LogClient.Error("Could not connect to the database. Exception: {0}", ex.Message); } }); return(counters); }
private async void PlaybackService_PlaybackCountersChanged(IList <PlaybackCounter> counters) { if (this.Tracks == null) { return; } if (counters == null) { return; } if (counters.Count == 0) { return; } await Task.Run(() => { foreach (TrackViewModel vm in this.Tracks) { if (counters.Select(c => c.SafePath).Contains(vm.Track.SafePath)) { // The UI is only updated if PropertyChanged is fired on the UI thread PlaybackCounter counter = counters.Where(c => c.SafePath.Equals(vm.Track.SafePath)).FirstOrDefault(); Application.Current.Dispatcher.Invoke(() => vm.UpdateVisibleCounters(counter)); } } }); }
public void UpdateVisibleCounters(PlaybackCounter counters) { this.Track.PlayCount = counters.PlayCount; this.Track.SkipCount = counters.SkipCount; this.Track.DateLastPlayed = counters.DateLastPlayed; this.RaisePropertyChanged(nameof(this.PlayCount)); this.RaisePropertyChanged(nameof(this.SkipCount)); this.RaisePropertyChanged(nameof(this.DateLastPlayed)); this.RaisePropertyChanged(nameof(this.SortDateLastPlayed)); }
private async Task UpdatePlaybackCountersAsync(string path, bool incrementPlayCount, bool incrementSkipCount) { if (!this.playbackCounters.ContainsKey(path)) { // Try to find an existing counter PlaybackCounter counters = await this.trackRepository.GetPlaybackCountersAsync(path); // If no existing counter was found, create a new one. if (counters == null) { counters = new PlaybackCounter(); } // Add statistic to the dictionary lock (this.playbackCountersLock) { this.playbackCounters.Add(path, counters); } } await Task.Run(() => { lock (this.playbackCountersLock) { try { if (incrementPlayCount) { this.playbackCounters[path].PlayCount += 1; this.playbackCounters[path].DateLastPlayed = DateTime.Now.Ticks; } if (incrementSkipCount) { this.playbackCounters[path].SkipCount += 1; } } catch (Exception ex) { LogClient.Error("Could not update track statistics for track with path='{0}'. Exception: {1}", path, ex.Message); } } }); this.ResetSavePlaybackCountersTimer(); }
public async Task <TrackViewModel> CreateTrackAsync(string path) { TrackViewModel returnTrack = null; try { PlaybackCounter playbackCounters = await this.trackRepository.GetPlaybackCountersAsync(path); Track track = await MetadataUtils.Path2TrackAsync(path); returnTrack = container.ResolveTrackViewModel(track); } catch (Exception ex) { // Make sure the file can be opened by creating a Track with some default values returnTrack = container.ResolveTrackViewModel(Track.CreateDefault(path)); LogClient.Error("Error while creating Track from file '{0}'. Creating default track. Exception: {1}", path, ex.Message); } return(returnTrack); }
public async Task UpdatePlaybackCountersAsync(PlaybackCounter counters) { await Task.Run(() => { try { using (var conn = this.factory.GetConnection()) { try { conn.Execute("UPDATE Track SET PlayCount=?, SkipCount=?, DateLastPlayed=? WHERE SafePath=?", counters.PlayCount, counters.SkipCount, counters.DateLastPlayed, counters.Path.ToSafePath()); } catch (Exception ex) { LogClient.Error("Could not update statistics for path='{0}'. Exception: {1}", counters.Path, ex.Message); } } } catch (Exception ex) { LogClient.Error("Could not connect to the database. Exception: {0}", ex.Message); } }); }