Skip to content

Commit 87e7b79

Browse files
committed
refactor: date time format
- generate example dynamically - update commit/author time in histories immediately after data time format changed
1 parent a6420af commit 87e7b79

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

src/Models/DateTimeFormat.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace SourceGit.Models
45
{
56
public class DateTimeFormat
67
{
7-
public string DisplayText { get; set; }
88
public string DateOnly { get; set; }
99
public string DateTime { get; set; }
1010

11-
public DateTimeFormat(string displayText, string dateOnly, string dateTime)
11+
public string Example
12+
{
13+
get => _example.ToString(DateTime);
14+
}
15+
16+
public DateTimeFormat(string dateOnly, string dateTime)
1217
{
13-
DisplayText = displayText;
1418
DateOnly = dateOnly;
1519
DateTime = dateTime;
1620
}
@@ -28,18 +32,19 @@ public static DateTimeFormat Actived
2832

2933
public static readonly List<DateTimeFormat> Supported = new List<DateTimeFormat>
3034
{
31-
new DateTimeFormat("2025/01/31 08:00:00", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss"),
32-
new DateTimeFormat("2025.01.31 08:00:00", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss"),
33-
new DateTimeFormat("2025-01-31 08:00:00", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"),
34-
new DateTimeFormat("01/31/2025 08:00:00", "MM/dd/yyyy", "MM/dd/yyyy HH:mm:ss"),
35-
new DateTimeFormat("01.31.2025 08:00:00", "MM.dd.yyyy", "MM.dd.yyyy HH:mm:ss"),
36-
new DateTimeFormat("01-31-2025 08:00:00", "MM-dd-yyyy", "MM-dd-yyyy HH:mm:ss"),
37-
new DateTimeFormat("31/01/2025 08:00:00", "dd/MM/yyyy", "dd/MM/yyyy HH:mm:ss"),
38-
new DateTimeFormat("31.01.2025 08:00:00", "dd.MM.yyyy", "dd.MM.yyyy HH:mm:ss"),
39-
new DateTimeFormat("31-01-2025 08:00:00", "dd-MM-yyyy", "dd-MM-yyyy HH:mm:ss"),
40-
41-
new DateTimeFormat("Jan 31 2025 08:00:00", "MMM d yyyy", "MMM d yyyy HH:mm:ss"),
42-
new DateTimeFormat("31 Jan 2025 08:00:00", "d MMM yyyy", "d MMM yyyy HH:mm:ss"),
35+
new DateTimeFormat("yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss"),
36+
new DateTimeFormat("yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss"),
37+
new DateTimeFormat("yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"),
38+
new DateTimeFormat("MM/dd/yyyy", "MM/dd/yyyy HH:mm:ss"),
39+
new DateTimeFormat("MM.dd.yyyy", "MM.dd.yyyy HH:mm:ss"),
40+
new DateTimeFormat("MM-dd-yyyy", "MM-dd-yyyy HH:mm:ss"),
41+
new DateTimeFormat("dd/MM/yyyy", "dd/MM/yyyy HH:mm:ss"),
42+
new DateTimeFormat("dd.MM.yyyy", "dd.MM.yyyy HH:mm:ss"),
43+
new DateTimeFormat("dd-MM-yyyy", "dd-MM-yyyy HH:mm:ss"),
44+
new DateTimeFormat("MMM d yyyy", "MMM d yyyy HH:mm:ss"),
45+
new DateTimeFormat("d MMM yyyy", "d MMM yyyy HH:mm:ss"),
4346
};
47+
48+
private static readonly DateTime _example = new DateTime(2025, 1, 31, 8, 0, 0, DateTimeKind.Local);
4449
}
4550
}

src/Views/Histories.axaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@
191191
FontWeight="{Binding FontWeight}"
192192
Opacity="{Binding Opacity}"
193193
UseAuthorTime="{Binding Source={x:Static vm:Preference.Instance}, Path=ShowAuthorTimeInGraph, Mode=OneWay}"
194-
ShowAsDateTime="{Binding Source={x:Static vm:Preference.Instance}, Path=!DisplayTimeAsPeriodInHistories}"/>
194+
ShowAsDateTime="{Binding Source={x:Static vm:Preference.Instance}, Path=!DisplayTimeAsPeriodInHistories}"
195+
DateTimeFormat="{Binding Source={x:Static vm:Preference.Instance}, Path=DateTimeFormat}"/>
195196
</Border>
196197
</Grid>
197198
</DataTemplate>

src/Views/Histories.axaml.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,15 @@ public bool ShowAsDateTime
343343
set => SetValue(ShowAsDateTimeProperty, value);
344344
}
345345

346+
public static readonly StyledProperty<int> DateTimeFormatProperty =
347+
AvaloniaProperty.Register<CommitTimeTextBlock, int>(nameof(DateTimeFormat), 0);
348+
349+
public int DateTimeFormat
350+
{
351+
get => GetValue(DateTimeFormatProperty);
352+
set => SetValue(DateTimeFormatProperty, value);
353+
}
354+
346355
public static readonly StyledProperty<bool> UseAuthorTimeProperty =
347356
AvaloniaProperty.Register<CommitTimeTextBlock, bool>(nameof(UseAuthorTime), true);
348357

@@ -371,6 +380,11 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
371380
else
372381
StartTimer();
373382
}
383+
else if (change.Property == DateTimeFormatProperty)
384+
{
385+
if (ShowAsDateTime)
386+
SetCurrentValue(TextProperty, GetDisplayText());
387+
}
374388
}
375389

376390
protected override void OnLoaded(RoutedEventArgs e)
@@ -426,10 +440,10 @@ private string GetDisplayText()
426440
if (commit == null)
427441
return string.Empty;
428442

429-
var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime;
430443
if (ShowAsDateTime)
431-
return DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime().ToString(Models.DateTimeFormat.Actived.DateTime);
444+
return UseAuthorTime ? commit.AuthorTimeStr : commit.CommitterTimeStr;
432445

446+
var timestamp = UseAuthorTime ? commit.AuthorTime : commit.CommitterTime;
433447
var now = DateTime.Now;
434448
var localTime = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
435449
var span = now - localTime;

src/Views/Preference.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<ComboBox.ItemTemplate>
7373
<DataTemplate x:DataType="{x:Type m:DateTimeFormat}">
7474
<Grid ColumnDefinitions="*,8,*">
75-
<TextBlock Grid.Column="0" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding DisplayText}"/>
75+
<TextBlock Grid.Column="0" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding Example}"/>
7676
<TextBlock Grid.Column="2" FontFamily="{DynamicResource Fonts.Monospace}" Text="{Binding DateTime}" Foreground="{DynamicResource Brush.FG2}"/>
7777
</Grid>
7878
</DataTemplate>

0 commit comments

Comments
 (0)