diff options
| author | SamusAranX <hallo@emmalyx.site> | 2023-05-12 01:56:37 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-12 01:56:37 +0200 |
| commit | 531da8a1c0760c8ebf121ac83ba4c840ead9e443 (patch) | |
| tree | 1cc849b054d33f0981444e9ba7323c0a15235f92 /src/Ryujinx.Ava/UI | |
| parent | 5cbdfbc7a4b7413a4f633c77190a79bfc6520e98 (diff) | |
Changed LastPlayed field from string to nullable DateTime (#4861)
* Changed LastPlayed field from string to nullable DateTime
Added ApplicationData.LastPlayedString property
Added NullableDateTimeConverter for the DateTime->string conversion in Avalonia
* Added migration from string-based last_played to DateTime-based last_played_utc
* Updated comment style
* Added MarkupExtension to NullableDateTimeConverter and changed its usage
Cleaned up leftover usings
* Missed one comment
Diffstat (limited to 'src/Ryujinx.Ava/UI')
4 files changed, 48 insertions, 12 deletions
diff --git a/src/Ryujinx.Ava/UI/Controls/ApplicationListView.axaml b/src/Ryujinx.Ava/UI/Controls/ApplicationListView.axaml index fa8ebf62..227b4723 100644 --- a/src/Ryujinx.Ava/UI/Controls/ApplicationListView.axaml +++ b/src/Ryujinx.Ava/UI/Controls/ApplicationListView.axaml @@ -129,7 +129,7 @@ TextWrapping="Wrap" /> <TextBlock HorizontalAlignment="Stretch" - Text="{Binding LastPlayed}" + Text="{Binding LastPlayed, Converter={helpers:NullableDateTimeConverter}}" TextAlignment="Right" TextWrapping="Wrap" /> <TextBlock diff --git a/src/Ryujinx.Ava/UI/Helpers/NullableDateTimeConverter.cs b/src/Ryujinx.Ava/UI/Helpers/NullableDateTimeConverter.cs new file mode 100644 index 00000000..1d862de0 --- /dev/null +++ b/src/Ryujinx.Ava/UI/Helpers/NullableDateTimeConverter.cs @@ -0,0 +1,38 @@ +using Avalonia.Data.Converters; +using Avalonia.Markup.Xaml; +using Ryujinx.Ava.Common.Locale; +using System; +using System.Globalization; + +namespace Ryujinx.Ava.UI.Helpers +{ + internal class NullableDateTimeConverter : MarkupExtension, IValueConverter + { + private static readonly NullableDateTimeConverter _instance = new(); + + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value == null) + { + return LocaleManager.Instance[LocaleKeys.Never]; + } + + if (value is DateTime dateTime) + { + return dateTime.ToLocalTime().ToString(culture); + } + + throw new NotSupportedException(); + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotSupportedException(); + } + + public override object ProvideValue(IServiceProvider serviceProvider) + { + return _instance; + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.Ava/UI/Models/Generic/LastPlayedSortComparer.cs b/src/Ryujinx.Ava/UI/Models/Generic/LastPlayedSortComparer.cs index 98caceb5..3627ada9 100644 --- a/src/Ryujinx.Ava/UI/Models/Generic/LastPlayedSortComparer.cs +++ b/src/Ryujinx.Ava/UI/Models/Generic/LastPlayedSortComparer.cs @@ -1,4 +1,3 @@ -using Ryujinx.Ava.Common.Locale; using Ryujinx.Ui.App.Common; using System; using System.Collections.Generic; @@ -14,20 +13,20 @@ namespace Ryujinx.Ava.UI.Models.Generic public int Compare(ApplicationData x, ApplicationData y) { - string aValue = x.LastPlayed; - string bValue = y.LastPlayed; + var aValue = x.LastPlayed; + var bValue = y.LastPlayed; - if (aValue == LocaleManager.Instance[LocaleKeys.Never]) + if (!aValue.HasValue) { - aValue = DateTime.UnixEpoch.ToString(); + aValue = DateTime.UnixEpoch; } - if (bValue == LocaleManager.Instance[LocaleKeys.Never]) + if (!bValue.HasValue) { - bValue = DateTime.UnixEpoch.ToString(); + bValue = DateTime.UnixEpoch; } - return (IsAscending ? 1 : -1) * DateTime.Compare(DateTime.Parse(bValue), DateTime.Parse(aValue)); + return (IsAscending ? 1 : -1) * DateTime.Compare(bValue.Value, aValue.Value); } } }
\ No newline at end of file diff --git a/src/Ryujinx.Ava/UI/ViewModels/MainWindowViewModel.cs b/src/Ryujinx.Ava/UI/ViewModels/MainWindowViewModel.cs index 4db78afe..f8dd4143 100644 --- a/src/Ryujinx.Ava/UI/ViewModels/MainWindowViewModel.cs +++ b/src/Ryujinx.Ava/UI/ViewModels/MainWindowViewModel.cs @@ -1524,10 +1524,9 @@ namespace Ryujinx.Ava.UI.ViewModels { ApplicationLibrary.LoadAndSaveMetaData(titleId, appMetadata => { - if (DateTime.TryParse(appMetadata.LastPlayed, out DateTime lastPlayedDateTime)) + if (appMetadata.LastPlayed.HasValue) { - double sessionTimePlayed = DateTime.UtcNow.Subtract(lastPlayedDateTime).TotalSeconds; - + double sessionTimePlayed = DateTime.UtcNow.Subtract(appMetadata.LastPlayed.Value).TotalSeconds; appMetadata.TimePlayed += Math.Round(sessionTimePlayed, MidpointRounding.AwayFromZero); } }); |
