Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -65,22 +66,22 @@ public PreviewPanel(Settings settings, string filePath)

if (Settings.ShowCreatedDateInPreviewPanel)
{
CreatedAt = File
.GetCreationTime(filePath)
.ToString(
$"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}",
CultureInfo.CurrentCulture
);
DateTime createdDate = File.GetCreationTime(filePath);
string formattedDate = createdDate.ToString(
$"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}",
CultureInfo.CurrentCulture
);
CreatedAt = $"{GetDiffTimeString(createdDate)} - {formattedDate}";
}

if (Settings.ShowModifiedDateInPreviewPanel)
{
LastModifiedAt = File
.GetLastWriteTime(filePath)
.ToString(
$"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}",
CultureInfo.CurrentCulture
);
DateTime lastModifiedDate = File.GetLastWriteTime(filePath);
string formattedDate = lastModifiedDate.ToString(
$"{Settings.PreviewPanelDateFormat} {Settings.PreviewPanelTimeFormat}",
CultureInfo.CurrentCulture
);
LastModifiedAt = $"{GetDiffTimeString(lastModifiedDate)} - {formattedDate}";
}

_ = LoadImageAsync();
Expand All @@ -90,7 +91,30 @@ private async Task LoadImageAsync()
{
PreviewImage = await Main.Context.API.LoadImageAsync(FilePath, true).ConfigureAwait(false);
}


private string GetDiffTimeString(DateTime fileDateTime)
{
DateTime now = DateTime.Now;
TimeSpan difference = now - fileDateTime;

if (fileDateTime > now)
return "In the future";

if (difference.TotalDays < 1)
return "Today";
if (difference.TotalDays < 30)
return $"{(int)difference.TotalDays} days ago";

int monthsDiff = (now.Year - fileDateTime.Year) * 12 + now.Month - fileDateTime.Month;
if (monthsDiff < 12)
return monthsDiff == 1 ? "1 month ago" : $"{monthsDiff} months ago";

int yearsDiff = now.Year - fileDateTime.Year;
if (now.Month < fileDateTime.Month || (now.Month == fileDateTime.Month && now.Day < fileDateTime.Day))
yearsDiff--;

return yearsDiff == 1 ? "1 year ago" : $"{yearsDiff} years ago";
}
Comment on lines +95 to +117
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix pluralization issue and consider localization improvements.

The method has a grammatical error and could benefit from some improvements:

  1. Critical Issue: Line 106 will display "1 days ago" instead of "1 day ago" when the file is exactly 1 day old.

  2. Optional Improvements: Consider localization for international users and more precise day boundary handling.

Fix the pluralization bug:

-        if (difference.TotalDays < 30)
-            return $"{(int)difference.TotalDays} days ago";
+        if (difference.TotalDays < 30)
+        {
+            int days = (int)difference.TotalDays;
+            return days == 1 ? "1 day ago" : $"{days} days ago";
+        }

Optional enhancement for better localization:

Consider extracting the hardcoded English strings to resource files to support multiple languages, as this feature enhances user experience and would benefit international users.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private string GetDiffTimeString(DateTime fileDateTime)
{
DateTime now = DateTime.Now;
TimeSpan difference = now - fileDateTime;
if (fileDateTime > now)
return "In the future";
if (difference.TotalDays < 1)
return "Today";
if (difference.TotalDays < 30)
return $"{(int)difference.TotalDays} days ago";
int monthsDiff = (now.Year - fileDateTime.Year) * 12 + now.Month - fileDateTime.Month;
if (monthsDiff < 12)
return monthsDiff == 1 ? "1 month ago" : $"{monthsDiff} months ago";
int yearsDiff = now.Year - fileDateTime.Year;
if (now.Month < fileDateTime.Month || (now.Month == fileDateTime.Month && now.Day < fileDateTime.Day))
yearsDiff--;
return yearsDiff == 1 ? "1 year ago" : $"{yearsDiff} years ago";
}
private string GetDiffTimeString(DateTime fileDateTime)
{
DateTime now = DateTime.Now;
TimeSpan difference = now - fileDateTime;
if (fileDateTime > now)
return "In the future";
if (difference.TotalDays < 1)
return "Today";
if (difference.TotalDays < 30)
{
int days = (int)difference.TotalDays;
return days == 1 ? "1 day ago" : $"{days} days ago";
}
int monthsDiff = (now.Year - fileDateTime.Year) * 12 + now.Month - fileDateTime.Month;
if (monthsDiff < 12)
return monthsDiff == 1 ? "1 month ago" : $"{monthsDiff} months ago";
int yearsDiff = now.Year - fileDateTime.Year;
if (now.Month < fileDateTime.Month
|| (now.Month == fileDateTime.Month && now.Day < fileDateTime.Day))
{
yearsDiff--;
}
return yearsDiff == 1 ? "1 year ago" : $"{yearsDiff} years ago";
}
🤖 Prompt for AI Agents
In Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs between
lines 95 and 117, fix the pluralization bug by ensuring that when the difference
in days is exactly 1, the method returns "1 day ago" instead of "1 days ago".
Additionally, for better localization support, extract all hardcoded English
strings into resource files to enable easy translation and adaptation for
international users.

public event PropertyChangedEventHandler? PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
Expand Down
Loading