Ejemplo n.º 1
0
        public async Task AddSongAsync(SongMetadata newMetadata)
        {
            if (!IsInitialized)
            {
                return;
            }

            if (newMetadata.IsUnknownMetadata)
            {
                return;
            }

            var item = new SongHistoryItem()
            {
                Track = newMetadata.Track, Artist = newMetadata.Artist, StationPlayedOn = newMetadata.StationPlayedOn, PlayedDate = DateTime.Now
            };

            SongAdded?.Invoke(this, new SongHistorianSongUpdatedEventArgs(item));

            await historyFileLock.WaitAsync();

            try
            {
                await FileIO.AppendTextAsync(historyFile, FormatSongHistoryItemToTSV(item));
            }
            catch (Exception ex)
            {
            }
            finally
            {
                historyFileLock.Release();
            }
        }
Ejemplo n.º 2
0
        public IObservable <SongHistoryItem> ObserveHistoryOfSongsAsync()
        {
            if (!IsInitialized)
            {
                Observable.Empty <SongHistoryItem>();
            }
            return(Observable.Create <SongHistoryItem>(async o =>
            {
                try
                {
                    var lines = await FileIO.ReadLinesAsync(historyFile);

                    foreach (var line in lines.Reverse())
                    {
                        SongHistoryItem item = ParseSongHistoryItemFromTSVLine(line);

                        o.OnNext(item);
                    }
                }
                catch (Exception ex)
                {
                    o.OnError(ex);
                }

                o.OnCompleted();
            }));
        }
Ejemplo n.º 3
0
        public async Task <IEnumerable <SongHistoryItem> > GetHistoryOfSongsAsync()
        {
            if (!IsInitialized)
            {
                return(null);
            }

            List <SongHistoryItem> results = new List <SongHistoryItem>();

            try
            {
                var lines = await FileIO.ReadLinesAsync(historyFile).AsTask().ConfigureAwait(false);

                foreach (var line in lines.Reverse())
                {
                    try
                    {
                        SongHistoryItem item = ParseSongHistoryItemFromTSVLine(line);

                        results.Add(item);
                    }
                    catch (IndexOutOfRangeException)
                    {
                        //A line didn't have enough tabs.
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(results.AsEnumerable());
        }
Ejemplo n.º 4
0
        private SongHistoryItem ParseSongHistoryItemFromTSVLine(string line)
        {
            string strippedLine = line.Trim();

            string[] splice = strippedLine.Split('\t');

            var result = new SongHistoryItem();

            result.Track           = splice[0].Trim();
            result.Artist          = splice[1].Trim();
            result.StationPlayedOn = splice[2].Trim();
            result.PlayedDate      = DateTime.Parse(splice[3].Trim());

            return(result);
        }
Ejemplo n.º 5
0
        internal async Task InitializeAsync()
        {
            if (IsInitialized)
            {
                return;
            }

            dataFolder  = NepApp.CacheManager.RoamingDataFilesFolder;
            historyFile = await dataFolder.CreateFileAsync("History.tsv", CreationCollisionOption.OpenIfExists);

            StorageFile oldHistoryFile = await dataFolder.TryGetItemAsync("History.json") as StorageFile;

            if (oldHistoryFile != null)
            {
                var historyOfSongs = new List <SongHistoryItem>();

                //upgrade to the new history format

                var accessStream = await oldHistoryFile.OpenReadAsync();

                byte[] data = null;
                using (Stream stream = accessStream.AsStreamForRead())
                {
                    data = new byte[(int)stream.Length];
                    await stream.ReadAsync(data, 0, (int)stream.Length);
                }
                using (StringReader sr = new StringReader(System.Text.UTF8Encoding.UTF8.GetString(data, 0, data.Length)))
                {
                    using (JsonTextReader jtr = new JsonTextReader(sr))
                    {
                        var coll = serializer.Deserialize <List <OldSongHistoryItem> >(jtr);

                        if (coll != null)
                        {
                            foreach (OldSongHistoryItem oldSongHistoryItem in coll)
                            {
                                var newHistoryItem = new SongHistoryItem();
                                newHistoryItem.Artist          = oldSongHistoryItem.Metadata.Artist;
                                newHistoryItem.Track           = oldSongHistoryItem.Metadata.Track;
                                newHistoryItem.StationPlayedOn = oldSongHistoryItem.Metadata.StationPlayedOn;
                                newHistoryItem.PlayedDate      = oldSongHistoryItem.PlayedDate;
                                historyOfSongs.Add(newHistoryItem);
                            }
                        }
                    }
                }

                accessStream.Dispose();

                await oldHistoryFile.DeleteAsync();

                string tsvText = string.Empty;
                foreach (var item in historyOfSongs)
                {
                    tsvText += FormatSongHistoryItemToTSV(item);
                }

                await historyFileLock.WaitAsync();

                try
                {
                    await FileIO.AppendTextAsync(historyFile, tsvText);
                }
                finally
                {
                    historyFileLock.Release();
                }
            }

            IsInitialized = true;
        }
Ejemplo n.º 6
0
 private string FormatSongHistoryItemToTSV(SongHistoryItem item)
 {
     return(string.Format("{0}\t{1}\t{2}\t{3}\n", item.Track, item.Artist, item.StationPlayedOn, item.PlayedDate));
 }