diff --git a/src/DaylioData/DaylioData.csproj b/src/DaylioData/DaylioData.csproj index 392d634..7f5ca21 100644 --- a/src/DaylioData/DaylioData.csproj +++ b/src/DaylioData/DaylioData.csproj @@ -14,8 +14,8 @@ LICENSE ..\..\BuildResults true - 0.1.4.0 - 0.1.4 + 0.1.5.0 + 0.1.5 diff --git a/src/DaylioData/Methods.cs b/src/DaylioData/Methods.cs index 9c3bca6..e660cf1 100644 --- a/src/DaylioData/Methods.cs +++ b/src/DaylioData/Methods.cs @@ -1,5 +1,4 @@ using DaylioData.Models; -using System.Runtime.InteropServices; namespace DaylioData { @@ -127,12 +126,12 @@ public static void InitData(DaylioData daylioData) public static IEnumerable? GetEntriesWithMood(string mood) { if (string.IsNullOrWhiteSpace(mood) || - _daylioData?.DataRepo?.Moods.Where(entry => entry.Equals(mood)).Any() == false) + _daylioData?.DataRepo?.Moods.Where(entryMoods => entryMoods.Equals(mood)).Any() == false) { return null; } - return _daylioData?.DataRepo?.CSVData?.Where(entry => entry.Mood?.Equals(mood, StringComparison.InvariantCultureIgnoreCase) == true); + return _daylioData?.DataRepo?.CSVData?.Where(entry => entry.Mood.Equals(mood, StringComparison.InvariantCultureIgnoreCase) == true); } /// @@ -162,6 +161,7 @@ public static void InitData(DaylioData daylioData) /// /// Gets the number of entries that include a specified activity. /// + /// The instance to use. /// An activity string /// The number of activities that include a specified activity. public static int? GetActivityCount(DaylioData daylioData, string activity) @@ -185,7 +185,6 @@ public static void InitData(DaylioData daylioData) /// /// Gets entries that contain a specified string in the note. - /// Assumes that has been initialized, otherwise returns null. /// /// The instance to use. /// The to search for within entries. @@ -196,5 +195,50 @@ public static void InitData(DaylioData daylioData) InitData(daylioData); return GetEntriesWithString(searchString, comparisonMethod); } + + /// + /// Gets an avererage mood rating for a specified activity. Requires levels to be set for each mood. + /// Assumes that has been initialized, otherwise returns null. + /// + /// The activity name to get an avergae mood rating for. + /// An average mood rating for the specified activity. + public static decimal? GetAverageActivityMood(string activity) + { + if (_daylioData == null || _daylioData.DataRepo == null || string.IsNullOrWhiteSpace(activity) + || !_daylioData.DataRepo.Activities.Contains(activity) || _daylioData.DataRepo.Moods.Where(x => x.Key == null).Any()) + { + return null; + } + + uint moodSum = 0; + uint count = 0; + + foreach (DaylioCSVDataModel entry in _daylioData.DataRepo.CSVData?.Where(entry => entry.ActivitiesCollection + .Any(entryActivity => entryActivity.Equals(activity, StringComparison.InvariantCultureIgnoreCase))) + ?? Enumerable.Empty()) + { + if (entry == Enumerable.Empty()) + { + continue; + } + moodSum += Convert.ToUInt32(_daylioData.DataRepo.Moods[entry.Mood]); + count++; + } + + return count == 0 ? null + : (decimal)moodSum / count; + } + + /// + /// Gets an avererage mood rating for a specified activity. Requires levels to be set for each mood. + /// + /// The instance to use. + /// The activity name to get an avergae mood rating for. + /// An average mood rating for the specified activity. + public static decimal? GetAverageActivityMood(DaylioData daylioData, string activity) + { + InitData(daylioData); + return GetAverageActivityMood(activity); + } } -} +} \ No newline at end of file diff --git a/src/DaylioData/Models/DaylioCSVDataModel.cs b/src/DaylioData/Models/DaylioCSVDataModel.cs index f7b2e98..33b86d4 100644 --- a/src/DaylioData/Models/DaylioCSVDataModel.cs +++ b/src/DaylioData/Models/DaylioCSVDataModel.cs @@ -8,19 +8,19 @@ namespace DaylioData.Models public class DaylioCSVDataModel { [Index(0)] - public DateOnly FullDate { get; set; } + public required DateOnly FullDate { get; set; } [Index(1)] - public DateOnly Date { get; set; } + public required DateOnly Date { get; set; } [Index(2)] - public string? Weekday { get; set; } + public required string? Weekday { get; set; } [Index(3)] - public TimeOnly Time { get; set; } + public required TimeOnly Time { get; set; } [Index(4)] - public string? Mood { get; set; } + public required string Mood { get; set; } [Index(5)] public string? Activities { get; set; } diff --git a/src/DaylioData/Repo/DaylioDataRepo.cs b/src/DaylioData/Repo/DaylioDataRepo.cs index dd13198..7df24f7 100644 --- a/src/DaylioData/Repo/DaylioDataRepo.cs +++ b/src/DaylioData/Repo/DaylioDataRepo.cs @@ -10,10 +10,18 @@ public class DaylioDataRepo private IEnumerable? _CSVData; private DaylioFileAccess? _fileAccess; + private readonly Dictionary _defaultMoods = new Dictionary() + { + { "rad", 5 }, + { "good", 4 }, + { "meh", 3 }, + { "bad", 2 }, + { "awful", 1 } + }; public IEnumerable? CSVData => _CSVData; public HashSet Activities = new HashSet(); - public HashSet Moods = new HashSet(); + public Dictionary Moods = new Dictionary(); internal DaylioDataRepo(DaylioFileAccess fileAccess) { @@ -33,10 +41,40 @@ public void UpdateFile(string filePath) InitializeMoods(); } + /// + /// Used to set custom mood levels. + /// + /// The name of the mood to set a level for. + /// The level to set for the mood. + public void SetMoodLevel(string moodName, short? moodLevel) + { + if (Moods.ContainsKey(moodName)) + { + Moods[moodName] = moodLevel; + } + } + + /// + /// Sets mood levels based on Daylio's default moods. This cannot be used with custom moods.

+ /// Rad - 5, Good - 4, Meh - 3, Bad - 2, Awful - 1 + ///
+ /// + public void SetDefaultMoodLevels() + { + foreach (KeyValuePair mood in _defaultMoods) + { + if (!Moods.ContainsKey(mood.Key)) + { + throw new InvalidOperationException("Attempting to assign default mood levels to non-default moods."); + } + } + + Moods = _defaultMoods.ToDictionary(x => x.Key, x => (short?)x.Value); + } + /// /// Moods can be customized and can be any string. This will keep track of all unique moods. /// - /// Unfortunately there is no way to assign a scale to the moods from the CSV data. This could potentially eventually be done through a manual assignment extension. private void InitializeMoods() { if (_CSVData == null) @@ -44,12 +82,9 @@ private void InitializeMoods() return; } - foreach (string? mood in _CSVData.Select(x => x.Mood).Distinct()) + foreach (string mood in _CSVData.Select(x => x.Mood).Distinct()) { - if (mood != null) - { - Moods.Add(mood); - } + Moods.Add(mood, null); } }