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

User Dashboard improvements #684

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
@@ -1,4 +1,5 @@
using WPFGallery.Models;
using System.Windows.Threading;
using WPFGallery.Models;

namespace WPFGallery.ViewModels.Samples
{
Expand All @@ -17,22 +18,19 @@ public partial class UserDashboardPageViewModel : ObservableObject
private User? _editableUser;

[ObservableProperty]
private bool _isRead = true;
private bool _isReadOnly = true;

[ObservableProperty]
private bool _isSaved;

[ObservableProperty]
private bool _isDeleted=false;

[ObservableProperty]
private string _deletedname;
private string _deletedName;
partial void OnSelectedUserChanged(User? oldValue, User? newValue)
{
if (SelectedUser != null && SelectedUser != EditableUser)
{
EditableUser = new User(SelectedUser);
IsRead = true;
IsReadOnly = true;
IsEditing = false;
}
}
Expand All @@ -42,40 +40,56 @@ private void AddUser()
{
Users.Add(new User("New User", ""));
SelectedUser = Users.Last();
EditableUser = new User(SelectedUser);
IsRead = false;

IsReadOnly = false;
IsEditing = true;
}

[RelayCommand]
private void RemoveUser(object selectedUser)
private CancellationTokenSource cancelTokenSource = new();
private Task displayMessageTask = Task.CompletedTask;
partial void OnDeletedNameChanged(string? oldValue, string newValue)
{
if (selectedUser is User user)
if(string.IsNullOrEmpty(newValue))
{
return;
}

cancelTokenSource = new();
displayMessageTask = Task.Delay(2000, cancelTokenSource.Token).ContinueWith(_ =>
{

Deletedname = user.Name;
IsDeleted = true;

Task.Delay(2000).ContinueWith(_ => IsDeleted = false, TaskScheduler.FromCurrentSynchronizationContext());
int index = Users.IndexOf(user);

SelectedUser = Users[index+1];
Users.Remove(user);
IsRead = true;
IsEditing = false;
DeletedName = string.Empty;
}, TaskScheduler.FromCurrentSynchronizationContext());
}


}
[RelayCommand]
private async Task RemoveUser(User selectedUser)
{

cancelTokenSource.Cancel();
await displayMessageTask;

DeletedName = selectedUser.Name;

int index = Users.Last().Equals(selectedUser) ?
Users.IndexOf(selectedUser) - 1 :
Users.IndexOf(selectedUser) + 1;

SelectedUser = index >= 0 ?
Users[index] :
null;

Users.Remove(selectedUser);
IsReadOnly = true;
IsEditing = false;
}

[RelayCommand]
private void EditUserStart()
{

{
if (SelectedUser != null)
{

IsRead = false;
EditableUser = new User(SelectedUser);
IsReadOnly = false;
IsEditing = true;
}
}
Expand All @@ -90,15 +104,11 @@ private void EditUserCommit()
Users.RemoveAt(index);
Users.Insert(index, EditableUser);
SelectedUser = Users[index];
IsRead = true;
IsReadOnly = true;
IsEditing = false;
IsSaved = true;

Task.Delay(2000).ContinueWith(_ => IsSaved = false, TaskScheduler.FromCurrentSynchronizationContext());




}
}

Expand All @@ -107,8 +117,8 @@ private void EditUserCommit()
private void EditUserCancel()
{
EditableUser = null;
EditableUser= new User(SelectedUser);
IsRead = true;
EditableUser = new User(SelectedUser);
IsReadOnly = true;
IsEditing = false;
}

Expand All @@ -117,16 +127,16 @@ public UserDashboardPageViewModel()
_users = GenerateUsers();
}

private ObservableCollection<User> GenerateUsers()
private static ObservableCollection<User> GenerateUsers()
{
var random = new Random();
var users = new ObservableCollection<User>();

DateTime startDate = new DateTime(2020, 1, 1);
DateTime endDate = DateTime.Now.Date;
int range = (endDate - startDate).Days;


var imageids = new[] { "64","65", "91", "103", "177", "334", "338", "342", "349", "366", "367", "373",
"375", "378", "399", "447", "453", "473", "469", "505"};
var names = new[]
Expand Down Expand Up @@ -191,7 +201,7 @@ private ObservableCollection<User> GenerateUsers()
"",
};


for (int i = 0; i < 20; i++)
{
int randomDays = random.Next(range + 1);
Expand All @@ -210,7 +220,7 @@ private ObservableCollection<User> GenerateUsers()
)
);
}


return users;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
Title="UserDashboardPage"
d:DesignHeight="450"
d:DesignWidth="800"
d:DataContext="{d:DesignInstance Type=local:UserDashboardPage}"
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
mc:Ignorable="d"
SizeChanged="Page_SizeChanged">
<Page.Resources>
<helpers:ImageIdToBrushConverter x:Key="ImageIdToBrushConverter" />
<helpers:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
<helpers:EmptyToVisibilityConverter x:Key="EmptyToVisibilityConverter" />

<Style TargetType="Label" x:Key="GenericLabelStyle">
<Setter Property="Opacity" Value="0.67"/>
Expand Down Expand Up @@ -162,20 +164,20 @@
<StackPanel x:Name="FirstNamePanel"
Grid.Column="0" Margin="0,0,10,0" Orientation="Vertical">
<Label Content="First Name" Style="{StaticResource GenericLabelStyle}" FontWeight="SemiBold" />
<TextBox AutomationProperties.Name="First Name" Margin="0,5,0,15" Text="{Binding ViewModel.EditableUser.FirstName}" IsReadOnly="{Binding ViewModel.IsRead}"/>
<TextBox AutomationProperties.Name="First Name" Margin="0,5,0,15" Text="{Binding ViewModel.EditableUser.FirstName}" IsReadOnly="{Binding ViewModel.IsReadOnly}"/>
</StackPanel>
<StackPanel x:Name="LastNamePanel"
Grid.Column="1" Orientation="Vertical">
<Label Content="Last Name" Style="{StaticResource GenericLabelStyle}" FontWeight="SemiBold" />
<TextBox AutomationProperties.Name="Last Name" Margin="0,5,0,15" Text="{Binding ViewModel.EditableUser.LastName}" IsReadOnly="{Binding ViewModel.IsRead}"/>
<TextBox AutomationProperties.Name="Last Name" Margin="0,5,0,15" Text="{Binding ViewModel.EditableUser.LastName}" IsReadOnly="{Binding ViewModel.IsReadOnly}"/>
</StackPanel>
</Grid>

<Label Content="Company" Style="{StaticResource GenericLabelStyle}" FontWeight="SemiBold" />
<TextBox AutomationProperties.Name="Company" Margin="0,5,0,15" Text="{Binding ViewModel.EditableUser.Company}" IsReadOnly="{Binding ViewModel.IsRead}"/>
<TextBox AutomationProperties.Name="Company" Margin="0,5,0,15" Text="{Binding ViewModel.EditableUser.Company}" IsReadOnly="{Binding ViewModel.IsReadOnly}"/>

<Label Content="Address" Style="{StaticResource GenericLabelStyle}" FontWeight="SemiBold" />
<TextBox AutomationProperties.Name="Address" Margin="0,5,0,15" Text="{Binding ViewModel.EditableUser.Address}" IsReadOnly="{Binding ViewModel.IsRead}"/>
<TextBox AutomationProperties.Name="Address" Margin="0,5,0,15" Text="{Binding ViewModel.EditableUser.Address}" IsReadOnly="{Binding ViewModel.IsReadOnly}"/>

<Grid>
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -210,23 +212,23 @@
Saved!
</TextBlock>

<TextBlock FontSize="14" HorizontalAlignment="Left" VerticalAlignment="Center" Visibility="{Binding ViewModel.IsDeleted, Converter={StaticResource BooleanToVisibilityConverter}}" FontStyle="Italic">
<TextBlock FontSize="14" HorizontalAlignment="Left" VerticalAlignment="Center" Visibility="{Binding ViewModel.DeletedName, Converter={StaticResource EmptyToVisibilityConverter }}" FontStyle="Italic">
<Run Text="User" />
<Run Text="{Binding ViewModel.Deletedname, Mode=OneWay}" />
<Run Text="{Binding ViewModel.DeletedName, Mode=OneWay}" />
<Run Text="Deleted!" />
</TextBlock>
<Button
x:Name="edit_button"
Margin="10"
Command="{Binding ViewModel.EditUserStartCommand}"
Visibility="{Binding ViewModel.IsRead, Converter={StaticResource BooleanToVisibilityConverter}}"
Visibility="{Binding ViewModel.IsReadOnly, Converter={StaticResource BooleanToVisibilityConverter}}"
Click="EditButton_Click"
Content="Edit" />
<Button
Margin="10"
Command="{Binding ViewModel.RemoveUserCommand}"
CommandParameter="{Binding ViewModel.SelectedUser}"
Visibility="{Binding ViewModel.IsRead, Converter={StaticResource BooleanToVisibilityConverter}}"
Visibility="{Binding ViewModel.IsReadOnly, Converter={StaticResource BooleanToVisibilityConverter}}"
Content="Delete" />
<Button
x:Name="save_button"
Expand Down
Loading