Skip to content

Commit 5dc2835

Browse files
authored
Make optional/move repetitive checks of property values outside item enumeration loop (#2069)
1 parent 134edec commit 5dc2835

File tree

5 files changed

+168
-56
lines changed

5 files changed

+168
-56
lines changed

Files/Filesystem/ListedItem.cs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,73 @@ public bool IsImage()
160160
return false;
161161
}
162162

163-
public ListedItem(string folderRelativeId)
163+
/// <summary>
164+
/// Create an item object, optionally with an explicitly-specified dateReturnFormat.
165+
/// </summary>
166+
/// <param name="folderRelativeId"></param>
167+
/// <param name="dateReturnFormat">Specify a date return format to reduce redundant checks of this setting.</param>
168+
public ListedItem(string folderRelativeId, string dateReturnFormat = null)
164169
{
165170
FolderRelativeId = folderRelativeId;
171+
if (dateReturnFormat != null)
172+
{
173+
DateReturnFormat = dateReturnFormat;
174+
}
175+
else
176+
{
177+
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
178+
string returnformat = Enum.Parse<TimeStyle>(localSettings.Values[LocalSettings.DateTimeFormat].ToString()) == TimeStyle.Application ? "D" : "g";
179+
DateReturnFormat = returnformat;
180+
}
166181
}
167182

168-
public static string GetFriendlyDate(DateTimeOffset d)
183+
private string DateReturnFormat { get; }
184+
185+
private string GetFriendlyDate(DateTimeOffset d)
169186
{
170-
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
171187
var elapsed = DateTimeOffset.Now - d;
172188

173-
string returnformat = Enum.Parse<TimeStyle>(localSettings.Values[LocalSettings.DateTimeFormat].ToString()) == TimeStyle.Application ? "D" : "g";
189+
if (elapsed.TotalDays > 7)
190+
{
191+
return d.ToString(DateReturnFormat);
192+
}
193+
else if (elapsed.TotalDays > 2)
194+
{
195+
return string.Format(ResourceController.GetTranslation("DaysAgo"), elapsed.Days);
196+
}
197+
else if (elapsed.TotalDays > 1)
198+
{
199+
return string.Format(ResourceController.GetTranslation("DayAgo"), elapsed.Days);
200+
}
201+
else if (elapsed.TotalHours > 2)
202+
{
203+
return string.Format(ResourceController.GetTranslation("HoursAgo"), elapsed.Hours);
204+
}
205+
else if (elapsed.TotalHours > 1)
206+
{
207+
return string.Format(ResourceController.GetTranslation("HourAgo"), elapsed.Hours);
208+
}
209+
else if (elapsed.TotalMinutes > 2)
210+
{
211+
return string.Format(ResourceController.GetTranslation("MinutesAgo"), elapsed.Minutes);
212+
}
213+
else if (elapsed.TotalMinutes > 1)
214+
{
215+
return string.Format(ResourceController.GetTranslation("MinuteAgo"), elapsed.Minutes);
216+
}
217+
else
218+
{
219+
return string.Format(ResourceController.GetTranslation("SecondsAgo"), elapsed.Seconds);
220+
}
221+
}
222+
223+
public static string GetFriendlyDateFromFormat(DateTimeOffset d, string returnFormat)
224+
{
225+
var elapsed = DateTimeOffset.Now - d;
174226

175227
if (elapsed.TotalDays > 7)
176228
{
177-
return d.ToString(returnformat);
229+
return d.ToString(returnFormat);
178230
}
179231
else if (elapsed.TotalDays > 2)
180232
{
@@ -213,7 +265,7 @@ public static string GetFriendlyDate(DateTimeOffset d)
213265

214266
public class RecycleBinItem : ListedItem
215267
{
216-
public RecycleBinItem(string folderRelativeId) : base(folderRelativeId)
268+
public RecycleBinItem(string folderRelativeId, string returnFormat) : base(folderRelativeId, returnFormat)
217269
{
218270
}
219271

@@ -223,7 +275,7 @@ public RecycleBinItem(string folderRelativeId) : base(folderRelativeId)
223275

224276
public class ShortcutItem : ListedItem
225277
{
226-
public ShortcutItem(string folderRelativeId) : base(folderRelativeId)
278+
public ShortcutItem(string folderRelativeId, string returnFormat) : base(folderRelativeId, returnFormat)
227279
{
228280
}
229281

0 commit comments

Comments
 (0)