Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WinUI project added to DateCalculator MVVM solution #86

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<vm:DateViewModel />
</ContentPage.BindingContext>
<ContentPage.Resources>
<xct:DateTimeOffsetConverter x:Key="DateTimeOffsetConverter" />
<xct:InvertedBoolConverter x:Key="InverseBoolConverter" />
<xct:IntToBoolConverter x:Key="IntToBoolConverter" />
</ContentPage.Resources>
Expand All @@ -37,7 +38,7 @@
<Label
Style="{StaticResource Caption}"
Text="From" />
<DatePicker Date="{Binding StartDate}">
<DatePicker Date="{Binding StartDate, Converter={StaticResource DateTimeOffsetConverter}}">
<DatePicker.Behaviors>
<xct:EventToCommandBehavior
Command="{Binding DateDiffCommand}"
Expand All @@ -51,7 +52,7 @@
<Label
Style="{StaticResource Caption}"
Text="To" />
<DatePicker Date="{Binding EndDate}">
<DatePicker Date="{Binding EndDate, Converter={StaticResource DateTimeOffsetConverter}}">
<DatePicker.Behaviors>
<xct:EventToCommandBehavior
Command="{Binding DateDiffCommand}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<vm:DateViewModel />
</ContentPage.BindingContext>
<pages:MauiPage.Resources>
<mct:DateTimeOffsetConverter x:Key="DateTimeOffsetConverter" />
<mct:InvertedBoolConverter x:Key="InverseBoolConverter" />
<!--<mct:IntToBoolConverter x:Key="IntToBoolConverter" />-->
<!--<converters:CheckedChangedEventArgsConverter x:Key="CheckedChangedEventArgsConverter" />-->
Expand All @@ -39,7 +40,7 @@
<Label
Style="{StaticResource Caption}"
Text="From" />
<DatePicker Date="{Binding StartDate}">
<DatePicker Date="{Binding StartDate, Converter={StaticResource DateTimeOffsetConverter}}">
<DatePicker.Behaviors>
<mct:EventToCommandBehavior
Command="{Binding DateDiffCommand}"
Expand All @@ -53,7 +54,7 @@
<Label
Style="{StaticResource Caption}"
Text="To" />
<DatePicker Date="{Binding EndDate}">
<DatePicker Date="{Binding EndDate, Converter={StaticResource DateTimeOffsetConverter}}">
<DatePicker.Behaviors>
<mct:EventToCommandBehavior
Command="{Binding DateDiffCommand}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
using DateCalculator.Extensions;
using DateCalculator.Helpers;
using DateCalculator.Models;
using System.ComponentModel;
using VijayAnand.Toolkit.ObjectModel;

namespace DateCalculator.ViewModels
{
public partial class DateViewModel : BaseViewModel
{
[ObservableProperty]
private DateTime startDate = DateTime.Today;
[NotifyPropertyChangedFor(nameof(StartDate1))]
//[TypeConverter(typeof(DateTimeOffsetConverter))]
private DateTimeOffset startDate = DateTime.Today;

[ObservableProperty]
private DateTime endDate = DateTime.Today;
[NotifyPropertyChangedFor(nameof(EndDate1))]
//[TypeConverter(typeof(DateTimeOffsetConverter))]
private DateTimeOffset endDate = DateTime.Today;

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(DiffModeInverse))]
Expand Down Expand Up @@ -55,31 +60,43 @@ public DateViewModel()
FindTheDate();
}

public IReadOnlyList<string> Options { get; } = ["Difference between dates", "Add or subtract days"];
public IReadOnlyList<string> Options { get; } = ["Difference between dates", "Add or subtract days"];

public IReadOnlyList<int> Range { get; } = Enumerable.Range(0, 1000).ToList();

#region For WinForms
#region For WinForms
public DateTime StartDate1
{
get => StartDate.Date;
set => StartDate = value;
}

public DateTime EndDate1
{
get => EndDate.Date;
set => EndDate = value;
}

public IReadOnlyList<int> YearRange { get; } = Enumerable.Range(0, 1000).ToList();

public IReadOnlyList<int> MonthRange { get; } = Enumerable.Range(0, 1000).ToList();

public IReadOnlyList<int> WeekRange { get; } = Enumerable.Range(0, 1000).ToList();

public IReadOnlyList<int> DayRange { get; } = Enumerable.Range(0, 1000).ToList();
#endregion
#endregion

public bool DiffModeInverse => !DiffMode;

// While using classic MVVM
/*
public DateTime StartDate
public DateTimeOffset StartDate
{
get => startDate;
set => SetProperty(ref startDate, value);
}

public DateTime EndDate
public DateTimeOffset EndDate
{
get => endDate;
set => SetProperty(ref endDate, value);
Expand Down Expand Up @@ -152,7 +169,6 @@ public int SelectedDay
get => selectedDay;
set => SetProperty(ref selectedDay, value, onChanged: FindTheDate);
}

public string ResultCaption
{
get => resultCaption;
Expand Down Expand Up @@ -196,9 +212,9 @@ partial void OnDiffModeChanged(bool value)

partial void OnSelectedOptionChanged(int value) => DiffMode = value == 0;

partial void OnStartDateChanged(DateTime value) => FindTheDate();
partial void OnStartDateChanged(DateTimeOffset value) => FindTheDate();

partial void OnEndDateChanged(DateTime value) => FindTheDate();
partial void OnEndDateChanged(DateTimeOffset value) => FindTheDate();

partial void OnSelectedModeChanged(string? value)
{
Expand Down Expand Up @@ -233,17 +249,18 @@ private void FindTheDate()
dtEnd = StartDate;
}

DiffResult = Utility.DateDiff(dtStart, dtEnd);
DiffInDays = ((int)(dtEnd - dtStart).TotalDays).InWords(TimeUnit.Day);
DiffResult = Utility.DateDiff(dtStart.Date, dtEnd.Date);
DiffInDays = ((int)(dtEnd.Date - dtStart.Date).TotalDays).InWords(TimeUnit.Day);
}
else
{
int factor = AddMode ? 1 : -1;
DiffResult = StartDate.AddYears(SelectedYear * factor)
.AddMonths(SelectedMonth * factor)
.AddWeeks(SelectedWeek * factor)
.AddDays(SelectedDay * factor)
.ToLongDateString();
DiffResult = StartDate.Date
.AddYears(SelectedYear * factor)
.AddMonths(SelectedMonth * factor)
.AddWeeks(SelectedWeek * factor)
.AddDays(SelectedDay * factor)
.ToLongDateString();
DiffInDays = string.Empty;
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ private void ConfigureDataBindings()
cboOptions.DataBindings.Add(new Binding(nameof(ComboBox.DataSource), dateViewModelBindingSource, nameof(DateViewModel.Options), true));
cboOptions.DataBindings.Add(new Binding(nameof(ComboBox.SelectedIndex), dateViewModelBindingSource, nameof(DateViewModel.SelectedOption), true, DataSourceUpdateMode.OnPropertyChanged));
// dtpStartDate
dtpStartDate.DataBindings.Add(new Binding(nameof(DateTimePicker.Value), dateViewModelBindingSource, nameof(DateViewModel.StartDate), true, DataSourceUpdateMode.OnPropertyChanged));
dtpStartDate.DataBindings.Add(new Binding(nameof(DateTimePicker.Value), dateViewModelBindingSource, nameof(DateViewModel.StartDate1), true, DataSourceUpdateMode.OnPropertyChanged));
// dtpEndDate
dtpEndDate.DataBindings.Add(new Binding(nameof(DateTimePicker.Value), dateViewModelBindingSource, nameof(DateViewModel.EndDate), true, DataSourceUpdateMode.OnPropertyChanged));
dtpEndDate.DataBindings.Add(new Binding(nameof(DateTimePicker.Value), dateViewModelBindingSource, nameof(DateViewModel.EndDate1), true, DataSourceUpdateMode.OnPropertyChanged));
dtpEndDate.DataBindings.Add(new Binding(nameof(Visible), dateViewModelBindingSource, nameof(DateViewModel.DiffMode), true, DataSourceUpdateMode.OnPropertyChanged));
// addSubPanel
addSubPanel.DataBindings.Add(new Binding(nameof(Visible), dateViewModelBindingSource, nameof(DateViewModel.DiffModeInverse), true, DataSourceUpdateMode.OnPropertyChanged));
Expand Down
70 changes: 70 additions & 0 deletions src/NET_8/DateCalculator/DateCalculator.WinUI/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Application
x:Class="DateCalculator.WinUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:DateCalculator.WinUI"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
</ResourceDictionary.MergedDictionaries>
<!-- Other app resources here -->
<Color x:Key="Primary">#512BD4</Color>

<Style TargetType="StackPanel">
<Setter Property="Spacing" Value="5 " />
</Style>

<SolidColorBrush
x:Key="PrimaryBrush"
Color="{StaticResource Primary}" />
<SolidColorBrush
x:Key="WhiteBrush"
Color="White" />
<SolidColorBrush
x:Key="BlackBrush"
Color="Black" />

<x:Double x:Key="AppFontSize">14</x:Double>

<Style
x:Key="MyLabel"
TargetType="TextBlock">
<Setter Property="Foreground" Value="{StaticResource PrimaryBrush}" />
</Style>

<Style
x:Key="Action"
TargetType="Button">
<Setter Property="FontSize" Value="{StaticResource AppFontSize}" />
<Setter Property="Padding" Value="14,10" />
</Style>

<Style
x:Key="PrimaryAction"
BasedOn="{StaticResource Action}"
TargetType="Button">
<Setter Property="Background" Value="{StaticResource PrimaryBrush}" />
<Setter Property="CornerRadius" Value="8" />
<Setter Property="Foreground" Value="{StaticResource WhiteBrush}" />
</Style>

<Style
x:Key="Caption"
TargetType="TextBlock">

</Style>

<Style
x:Key="BoldText"
TargetType="TextBlock">
<Setter Property="FontWeight" Value="SemiBold" />
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
30 changes: 30 additions & 0 deletions src/NET_8/DateCalculator/DateCalculator.WinUI/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace DateCalculator.WinUI
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
}

/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
m_window?.Activate();
}

private Window? m_window;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace DateCalculator.WinUI.Converters
{
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
=> (bool)value ? Visibility.Visible : Visibility.Collapsed;

public object ConvertBack(object value, Type targetType, object parameter, string language)
=> value is Visibility visibility && visibility == Visibility.Visible;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace DateCalculator.WinUI.Converters
{
public class InvertedBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
=> !(bool)value;
public object ConvertBack(object value, Type targetType, object parameter, string language)
=> !(bool)value;
}
}
Loading