Skip to content

Commit

Permalink
DateSelector for date input options (#227)
Browse files Browse the repository at this point in the history
* DateSelector

* DateSelector.MinDate

* Use Date <-> DateTimeOffset conversion from commonItems

* Update README

* Minor optimization
  • Loading branch information
IhateTrains authored May 3, 2023
1 parent c078a77 commit c930025
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Fronter.NET/Fronter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.0-preview6" />
<PackageReference Include="DynamicData" Version="7.13.5" />
<PackageReference Include="MessageBox.Avalonia" Version="2.3.1-prev6" />
<PackageReference Include="PGCG.commonItems" Version="8.0.1" />
<PackageReference Include="PGCG.commonItems" Version="8.1.0" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.117" />
<PackageReference Include="system.io.filesystem" Version="4.3.0" />
<PackageReference Include="System.Net.Primitives" Version="4.3.1" />
Expand Down
48 changes: 48 additions & 0 deletions Fronter.NET/Models/Configuration/Options/DateSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using commonItems;
using System;

namespace Fronter.Models.Configuration.Options;

public class DateSelector : Selector {
public DateSelector(BufferedReader reader) {
var parser = new Parser();
RegisterKeys(parser);
parser.ParseStream(reader);
}
private void RegisterKeys(Parser parser) {
parser.RegisterKeyword("editable", reader => Editable = reader.GetString() == "true");
parser.RegisterKeyword("value", reader => {
var valueStr = reader.GetString();
Value = string.IsNullOrWhiteSpace(valueStr) ? null : new Date(valueStr);
});
parser.RegisterKeyword("minDate", reader => MinDate = new Date(reader.GetString()).ToDateTimeOffset());
parser.RegisterKeyword("maxDate", reader => MaxDate = new Date(reader.GetString()).ToDateTimeOffset());
parser.RegisterKeyword("tooltip", reader => Tooltip = reader.GetString());
parser.RegisterRegex(CommonRegexes.Catchall, ParserHelpers.IgnoreAndLogItem);
}

public bool Editable { get; private set; } = true; // editable unless disabled
public DateTimeOffset MinDate { get; set; } = DateTimeOffset.MinValue;
public DateTimeOffset MaxDate { get; set; } = DateTimeOffset.MaxValue;
public DateTimeOffset? DateTimeOffsetValue { get; set; }

public Date? Value {
get {
if (DateTimeOffsetValue is null) {
return null;
}

var offsetValue = DateTimeOffsetValue.Value;
return new Date(offsetValue);
}
set {
if (value is null) {
DateTimeOffsetValue = null;
} else {
DateTimeOffsetValue = value.ToDateTimeOffset();
}
}
}

public string? Tooltip { get; private set; }
}
21 changes: 11 additions & 10 deletions Fronter.NET/Models/Configuration/Options/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ private void RegisterKeys(Parser parser) {
parser.RegisterKeyword("textSelector", reader => {
TextSelector = new TextSelector(reader);
});
parser.RegisterKeyword("dateSelector", reader => {
DateSelector = new DateSelector(reader);
});
parser.RegisterKeyword("checkBoxSelector", reader => {
CheckBoxSelector = new CheckBoxSelector(reader);
});
Expand Down Expand Up @@ -59,20 +62,17 @@ public void SetCheckBoxSelectorIds(ISet<int> selection) {
CheckBoxSelector.SetSelectedIds(selection);
}

public void SetTextSelectorValue(string selection) {
if (TextSelector is null) {
logger.Warn("Attempted setting a text control which does not exist!");
return;
}
TextSelector.Value = selection;
}

public string GetValue() {
if (RadioSelector is not null) {
return RadioSelector.GetSelectedValue();
}

return TextSelector is not null ? TextSelector.Value : string.Empty;
if (TextSelector is not null) {
return TextSelector.Value;
}
if (DateSelector?.Value is Date dateValue) {
return dateValue.ToString();
}
return string.Empty;
}

public HashSet<string> GetValues() {
Expand Down Expand Up @@ -110,4 +110,5 @@ public void SetCheckBoxSelectorPreloaded() {
public RadioSelector? RadioSelector { get; private set; }
public TextSelector? TextSelector { get; private set; }
public CheckBoxSelector? CheckBoxSelector { get; private set; }
public DateSelector? DateSelector { get; private set; }
}
19 changes: 17 additions & 2 deletions Fronter.NET/Views/OptionsView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,24 @@
</ComboBox.ItemTemplate>
</ComboBox>

<Border ToolTip.Tip="{ns:DynamicLoc TextSelector.Tooltip, FallbackValue={}}" IsVisible="{Binding TextSelector, Converter={x:Static ObjectConverters.IsNotNull}}">
<TextBox Text="{Binding TextSelector.Value, FallbackValue={}}" IsEnabled="{Binding TextSelector.Editable, FallbackValue=false}"></TextBox>
<Border
ToolTip.Tip="{ns:DynamicLoc TextSelector.Tooltip, FallbackValue={}}"
IsVisible="{Binding TextSelector, Converter={x:Static ObjectConverters.IsNotNull}}"
>
<TextBox
Text="{Binding TextSelector.Value, FallbackValue={}}"
IsEnabled="{Binding TextSelector.Editable, FallbackValue=false}"
/>
</Border>

<DatePicker
ToolTip.Tip="{ns:DynamicLoc DateSelector.Tooltip, FallbackValue={}}"
SelectedDate="{Binding DateSelector.DateTimeOffsetValue, FallbackValue={}}"
IsVisible="{Binding DateSelector, Converter={x:Static ObjectConverters.IsNotNull}}"
IsEnabled="{Binding DateSelector.Editable, FallbackValue=false}"
MinYear="{Binding DateSelector.MinDate, FallbackValue={}}"
MaxYear="{Binding DateSelector.MaxDate, FallbackValue={}}"
/>
</StackPanel>
</Border>
</DataTemplate>
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ option = {
}
}
option = {
name = bookmark_date
displayName = BOOKMARK_DATE
tooltip = BOOKMARK_DATE_TIP
dateSelector = {
value = ""
minDate = 1.1.1
maxDate = 1400.1.1
editable = true
tooltip = BOOKMARK_DATE_TIP2
}
}
option = {
name = some_checkbox_control
displayName = THENAME
Expand Down

0 comments on commit c930025

Please sign in to comment.