public void GetWorkSession(Action <WorkSession, Exception> callback) { string trackDirectory = GetTrackDirectory(); string[] sessionFiles = Directory.GetFiles(trackDirectory); var mostRecentTrackDate = new DateTime(1970, 1, 1); string mostRecentTrackFile = ""; WorkSession session = null; foreach (string fileName in sessionFiles) { var dateTime = GetTrackDate(fileName); if (dateTime > mostRecentTrackDate) { mostRecentTrackDate = dateTime; mostRecentTrackFile = fileName; } } if (mostRecentTrackFile != "") { var deserialized = WorkSession.Deserialize(File.ReadAllText(mostRecentTrackFile)); if (deserialized != null) { if (deserialized.Today.Date != DateTime.Today.Date) { foreach (var task in deserialized.Tasks) { task.Duration = 0; } deserialized.Today = DateTime.Today; } else { foreach (var task in deserialized.Tasks) { deserialized.TotalTasksDuration += task.Duration; } } } session = deserialized; } if (session == null) { session = new WorkSession(); } callback(session, null); }
public IEnumerable <WorkSession> GetWorkSessionHistory() { var trackDirectory = GetTrackDirectory(); var sessionFiles = Directory.GetFiles(trackDirectory); foreach (var s in sessionFiles) { var deserialized = WorkSession.Deserialize(File.ReadAllText(s)); if (deserialized != null) { yield return(deserialized); } } }
public void SaveWorkSession(WorkSession workSession) { var sessionString = workSession.Serialize(); var trackDirectory = GetTrackDirectory(); try { File.WriteAllText( Path.Combine(trackDirectory, PREFIX + workSession.Today.ToString(DATE_FORMAT) + ".txt"), sessionString); } catch { } }