refactor: date time format

- generate example dynamically
- update commit/author time in histories immediately after data time format changed
This commit is contained in:
leo 2024-12-31 10:43:00 +08:00
parent a6420aff9b
commit 87e7b792bb
No known key found for this signature in database
4 changed files with 40 additions and 20 deletions

View file

@ -1,16 +1,20 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
namespace SourceGit.Models
{
public class DateTimeFormat
{
public string DisplayText { get; set; }
public string DateOnly { get; set; }
public string DateTime { get; set; }
public DateTimeFormat(string displayText, string dateOnly, string dateTime)
public string Example
{
get => _example.ToString(DateTime);
}
public DateTimeFormat(string dateOnly, string dateTime)
{
DisplayText = displayText;
DateOnly = dateOnly;
DateTime = dateTime;
}
@ -28,18 +32,19 @@ namespace SourceGit.Models
public static readonly List<DateTimeFormat> Supported = new List<DateTimeFormat>
{
new DateTimeFormat("2025/01/31 08:00:00", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss"),
new DateTimeFormat("2025.01.31 08:00:00", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss"),
new DateTimeFormat("2025-01-31 08:00:00", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"),
new DateTimeFormat("01/31/2025 08:00:00", "MM/dd/yyyy", "MM/dd/yyyy HH:mm:ss"),
new DateTimeFormat("01.31.2025 08:00:00", "MM.dd.yyyy", "MM.dd.yyyy HH:mm:ss"),
new DateTimeFormat("01-31-2025 08:00:00", "MM-dd-yyyy", "MM-dd-yyyy HH:mm:ss"),
new DateTimeFormat("31/01/2025 08:00:00", "dd/MM/yyyy", "dd/MM/yyyy HH:mm:ss"),
new DateTimeFormat("31.01.2025 08:00:00", "dd.MM.yyyy", "dd.MM.yyyy HH:mm:ss"),
new DateTimeFormat("31-01-2025 08:00:00", "dd-MM-yyyy", "dd-MM-yyyy HH:mm:ss"),
new DateTimeFormat("Jan 31 2025 08:00:00", "MMM d yyyy", "MMM d yyyy HH:mm:ss"),
new DateTimeFormat("31 Jan 2025 08:00:00", "d MMM yyyy", "d MMM yyyy HH:mm:ss"),
new DateTimeFormat("yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss"),
new DateTimeFormat("yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss"),
new DateTimeFormat("yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"),
new DateTimeFormat("MM/dd/yyyy", "MM/dd/yyyy HH:mm:ss"),
new DateTimeFormat("MM.dd.yyyy", "MM.dd.yyyy HH:mm:ss"),
new DateTimeFormat("MM-dd-yyyy", "MM-dd-yyyy HH:mm:ss"),
new DateTimeFormat("dd/MM/yyyy", "dd/MM/yyyy HH:mm:ss"),
new DateTimeFormat("dd.MM.yyyy", "dd.MM.yyyy HH:mm:ss"),
new DateTimeFormat("dd-MM-yyyy", "dd-MM-yyyy HH:mm:ss"),
new DateTimeFormat("MMM d yyyy", "MMM d yyyy HH:mm:ss"),
new DateTimeFormat("d MMM yyyy", "d MMM yyyy HH:mm:ss"),
};
private static readonly DateTime _example = new DateTime(2025, 1, 31, 8, 0, 0, DateTimeKind.Local);
}
}

View file

@ -191,7 +191,8 @@
FontWeight="{Binding FontWeight}"
Opacity="{Binding Opacity}"
UseAuthorTime="{Binding Source={x:Static vm:Preference.Instance}, Path=ShowAuthorTimeInGraph, Mode=OneWay}"
ShowAsDateTime="{Binding Source={x:Static vm:Preference.Instance}, Path=!DisplayTimeAsPeriodInHistories}"/>
ShowAsDateTime="{Binding Source={x:Static vm:Preference.Instance}, Path=!DisplayTimeAsPeriodInHistories}"
DateTimeFormat="{Binding Source={x:Static vm:Preference.Instance}, Path=DateTimeFormat}"/>
</Border>
</Grid>
</DataTemplate>

View file

@ -343,6 +343,15 @@ namespace SourceGit.Views
set => SetValue(ShowAsDateTimeProperty, value);
}
public static readonly StyledProperty<int> DateTimeFormatProperty =
AvaloniaProperty.Register<CommitTimeTextBlock, int>(nameof(DateTimeFormat), 0);
public int DateTimeFormat
{
get => GetValue(DateTimeFormatProperty);
set => SetValue(DateTimeFormatProperty, value);
}
public static readonly StyledProperty<bool> UseAuthorTimeProperty =
AvaloniaProperty.Register<CommitTimeTextBlock, bool>(nameof(UseAuthorTime), true);
@ -371,6 +380,11 @@ namespace SourceGit.Views
else
StartTimer();
}
else if (change.Property == DateTimeFormatProperty)
{
if (ShowAsDateTime)
SetCurrentValue(TextProperty, GetDisplayText());
}
}
protected override void OnLoaded(RoutedEventArgs e)
@ -426,10 +440,10 @@ namespace SourceGit.Views
if (commit == null)
return string.Empty;
var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime;
if (ShowAsDateTime)
return DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime().ToString(Models.DateTimeFormat.Actived.DateTime);
return UseAuthorTime ? commit.AuthorTimeStr : commit.CommitterTimeStr;
var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime;
var now = DateTime.Now;
var localTime = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
var span = now - localTime;

View file

@ -72,7 +72,7 @@
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="{x:Type m:DateTimeFormat}">
<Grid ColumnDefinitions="*,8,*">
<TextBlock Grid.Column="0" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding DisplayText}"/>
<TextBlock Grid.Column="0" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding Example}"/>
<TextBlock Grid.Column="2" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding DateTime}" Foreground="{DynamicResource Brush.FG2}"/>
</Grid>
</DataTemplate>