public static async Task StartSession() { await MiscTools.CheckAndCreateAppFolders(); string loggingFolder = Path.Combine(ApplicationPath, LogsLocation); List <DateTime> previousSessions = new List <DateTime>(); DateTime timestamp = DateTime.UtcNow; foreach (string s in Directory.EnumerateFiles(loggingFolder)) { string timestampString = Path.GetFileNameWithoutExtension(s); if (DateTime.TryParseExact(timestampString, LogDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime parsed)) { previousSessions.Add(parsed); } } List <DateTime> orderByDescending = previousSessions.OrderByDescending(x => x).ToList(); if (orderByDescending.Count != 0) { DateTime latest = previousSessions.First(); string latestPath = Path.Combine(ApplicationPath, LogsLocation, latest.ToString(LogDateTimeFormat, CultureInfo.InvariantCulture) + LogDatabaseExtension); //If the latest log file is newer than 2 hours AND smaller than 5MB, use that file if (latest > DateTime.UtcNow.AddHours(LogFileMaxAge) || new FileInfo(latestPath).Length >= LogFileMaxSize) { timestamp = latest; } } filepath = Path.Combine(ApplicationPath, LogsLocation, timestamp.ToString(LogDateTimeFormat, CultureInfo.InvariantCulture) + LogDatabaseExtension); InitializeDb(); }
private async Task ShowSavedSongs() { //initialize UI variables ExpandableListView savedList = FindViewById <ExpandableListView>(Resource.Id.savedList); ListView savedListNonGrouped = FindViewById <ListView>(Resource.Id.savedListNonGrouped); ProgressBar progressBar = FindViewById <ProgressBar>(Resource.Id.progressBar); //--UI-- progressBar.Visibility = ViewStates.Visible; string path = Path.Combine(ApplicationPath, SavedLyricsLocation); await MiscTools.CheckAndCreateAppFolders(); Log(Type.Info, $"Saved lyrics location is '{path}'"); List <SongBundle> songList = await Database.GetSongList(); if (songList != null) { await GetSavedList(songList); allSongs = new List <SongBundle>(); artistSongs = new Dictionary <Artist, List <SongBundle> >(); foreach (Artist a in artistList) { artistSongs.Add(a, a.Songs); foreach (SongBundle s in a.Songs) { allSongs.Add(s); } } Log(Type.Processing, "Set up adapter data"); if (nonGrouped) { savedListNonGrouped.Adapter = new SavedLyricsAdapter(this, allSongs); Log(Type.Info, "Showing adapter for non grouped view"); progressBar.Visibility = ViewStates.Gone; } else { savedList.SetAdapter(new ExpandableListAdapter(this, artistList, artistSongs)); Log(Type.Info, "Showing adapter for grouped view"); progressBar.Visibility = ViewStates.Gone; } } else { Log(Type.Info, "No files found!"); progressBar.Visibility = ViewStates.Gone; } }
// Writes a song to the saved lyrics database. // Returns true if successful. //TODO: Add ability to return an error, song already saved or saved successfully messages public static async Task <bool> WriteInfoAndLyrics(SongBundle song) { await MiscTools.CheckAndCreateAppFolders(); InitializeTables(); db = await ReadDatabaseFile(DbPath); rdb = await ReadRomanizedDatabaseFile(RomanizedDbPath); try { if (await GetSongFromTable(song.Normal.Id) == null) { // Write lyrics to file string filepath = Path.Combine(LyricsPath, song.Normal.Id + Globals.LyricsExtension); File.WriteAllText(filepath, song.Normal.Lyrics); if (song.Romanized != null) { string romanizedFilepath = Path.Combine(LyricsPath, song.Normal.Id + Globals.RomanizedExtension); File.WriteAllText(romanizedFilepath, song.Romanized.Lyrics); song.Normal.Romanized = true; rdb.Rows.Add( song.Romanized.Id, song.Romanized.Title, song.Romanized.Artist, song.Romanized.Album, song.Romanized.FeaturedArtist); rdb.WriteXml(RomanizedDbPath); } await WriteImages(song.Normal); db.Rows.Add( song.Normal.Id, song.Normal.Title, song.Normal.Artist, song.Normal.Album, song.Normal.FeaturedArtist, song.Normal.Cover, song.Normal.Header, song.Normal.Romanized, song.Normal.ApiPath, song.Normal.Path); db.WriteXml(DbPath); Logging.Log(Logging.Type.Event, $"Wrote song {song.Normal.Id} to file"); Analytics.TrackEvent("Wrote song to file", new Dictionary <string, string> { { "SongID", song.Normal.Id.ToString() }, { "DBSize", db.Rows.Count.ToString() }, { "RomanizedDBSize", rdb.Rows.Count.ToString() } }); return(true); } else { return(false); } } catch (IOException ex) { Crashes.TrackError(ex, new Dictionary <string, string> { { "SongID", song.Normal.Id.ToString() }, { "Exception", ex.ToString() } }); Logging.Log(Logging.Type.Error, "Exception while writing lyrics to disk!\n" + ex); return(false); } catch (NullReferenceException ex) { Crashes.TrackError(ex, new Dictionary <string, string> { { "SongID", song.Normal.Id.ToString() }, { "Exception", ex.ToString() } }); Logging.Log(Logging.Type.Error, "NullReferenceException while writing lyrics to disk! Reload song and try again.\n" + ex); return(false); } catch (Exception ex) { Crashes.TrackError(ex, new Dictionary <string, string> { { "SongID", song.Normal.Id.ToString() }, { "Exception", ex.ToString() } }); Logging.Log(Logging.Type.Error, "Unkown error while writing song to disk!\n" + ex); return(false); } }