|
1 | 1 | using System;
|
2 | 2 | using System.Globalization;
|
| 3 | +using System.Windows; |
3 | 4 | using System.Windows.Data;
|
4 | 5 |
|
5 | 6 | namespace UnityLauncherPro.Converters
|
6 | 7 | {
|
7 |
| - [ValueConversion(typeof(DateTime), typeof(String))] |
| 8 | + [ValueConversion(typeof(DateTime), typeof(string))] |
8 | 9 | public class ReleaseDateConverter : IValueConverter
|
9 | 10 | {
|
10 | 11 | public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
11 | 12 | {
|
12 |
| - if (value == null) return null; |
13 |
| - DateTime date = (DateTime)value; |
| 13 | + if (value == null || !(value is DateTime date)) |
| 14 | + { |
| 15 | + return DependencyProperty.UnsetValue; |
| 16 | + } |
14 | 17 |
|
15 |
| - // get first part of string until space character (updates only contain mm/dd/yyyy) |
16 |
| - string dateStrTrimmed = MainWindow.currentDateFormat; |
17 |
| - if (dateStrTrimmed.IndexOf(' ') > -1) dateStrTrimmed = dateStrTrimmed.Split(' ')[0]; |
| 18 | + // Use a default date format if currentDateFormat is null or empty |
| 19 | + string dateStrTrimmed = MainWindow.currentDateFormat ?? "MM/dd/yyyy"; |
18 | 20 |
|
19 |
| - return MainWindow.useHumanFriendlyDateFormat ? Tools.GetElapsedTime(date) : date.ToString(dateStrTrimmed); |
| 21 | + // If the format includes time, use only the date portion |
| 22 | + if (dateStrTrimmed.Contains(" ")) |
| 23 | + { |
| 24 | + dateStrTrimmed = dateStrTrimmed.Split(' ')[0]; |
| 25 | + } |
| 26 | + |
| 27 | + // Return a human-friendly format if enabled; otherwise, format based on dateStrTrimmed |
| 28 | + return MainWindow.useHumanFriendlyDateFormat |
| 29 | + ? Tools.GetElapsedTime(date) |
| 30 | + : date.ToString(dateStrTrimmed, culture); |
20 | 31 | }
|
21 | 32 |
|
22 | 33 | public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
23 | 34 | {
|
24 |
| - // not used ? |
25 |
| - return DateTime.ParseExact((string)value, MainWindow.currentDateFormat, culture); |
26 |
| - } |
| 35 | + if (value == null || string.IsNullOrWhiteSpace(value.ToString())) |
| 36 | + { |
| 37 | + return DependencyProperty.UnsetValue; |
| 38 | + } |
27 | 39 |
|
| 40 | + // Attempt to parse back to DateTime using the specified format |
| 41 | + if (DateTime.TryParseExact((string)value, MainWindow.currentDateFormat ?? "MM/dd/yyyy", culture, DateTimeStyles.None, out DateTime parsedDate)) |
| 42 | + { |
| 43 | + return parsedDate; |
| 44 | + } |
| 45 | + |
| 46 | + return DependencyProperty.UnsetValue; |
| 47 | + } |
28 | 48 | }
|
29 | 49 | }
|
0 commit comments