update banner view page binding

This commit is contained in:
kwan.nguyen 2025-01-09 13:10:21 +07:00
parent 8a620196b5
commit 2bdc220a19
4 changed files with 142 additions and 14 deletions

View File

@ -29,6 +29,7 @@
<NavigationViewItem Icon="Document" Content="Categories" x:Name="Categories" Tag="Categories" Background="{ThemeResource SystemAccentColor}"/> <NavigationViewItem Icon="Document" Content="Categories" x:Name="Categories" Tag="Categories" Background="{ThemeResource SystemAccentColor}"/>
<NavigationViewItem Icon="Folder" Content="Files" x:Name="Files" Tag="Files" Background="{ThemeResource SystemAccentColor}"/> <NavigationViewItem Icon="Folder" Content="Files" x:Name="Files" Tag="Files" Background="{ThemeResource SystemAccentColor}"/>
<NavigationViewItem Icon="People" Content="Users" x:Name="Users" Tag="Users" Background="{ThemeResource SystemAccentColor}"/> <NavigationViewItem Icon="People" Content="Users" x:Name="Users" Tag="Users" Background="{ThemeResource SystemAccentColor}"/>
<NavigationViewItem Icon="OutlineStar" Content="Banner" x:Name="Banners" Tag="Banners" Background="{ThemeResource SystemAccentColor}"/>
<NavigationViewItem Icon="Clock" Content="Logs" x:Name="Logs" Tag="Logs" Background="{ThemeResource SystemAccentColor}"/> <NavigationViewItem Icon="Clock" Content="Logs" x:Name="Logs" Tag="Logs" Background="{ThemeResource SystemAccentColor}"/>
</NavigationView.MenuItems> </NavigationView.MenuItems>

View File

@ -1,4 +1,5 @@
using System; using PCUT.Pages.BannerManagement;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -53,6 +54,9 @@ namespace PCUT.Pages
case "Users": case "Users":
ContentFrame.Navigate(typeof(UserListPage)); ContentFrame.Navigate(typeof(UserListPage));
break; break;
case "Banners":
ContentFrame.Navigate(typeof(BannerPage));
break;
case "Logs": case "Logs":
ContentFrame.Navigate(typeof(LogInformationPage)); ContentFrame.Navigate(typeof(LogInformationPage));
break; break;

View File

@ -45,11 +45,12 @@
Grid.Row="1" Grid.Row="1"
x:Name="Search" x:Name="Search"
PlaceholderText="Search" PlaceholderText="Search"
Text="{Binding Pagination.SearchText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
MinWidth="300" MaxHeight="32" CornerRadius="20" MinWidth="300" MaxHeight="32" CornerRadius="20"
HorizontalAlignment="Right" VerticalAlignment="Center"/> HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid> </Grid>
<ListView Grid.Row="1" x:Name="BannerListView" IsItemClickEnabled="False"> <ListView Grid.Row="1" x:Name="BannerListView" ItemsSource="{Binding FilteredBanners.Items}" IsItemClickEnabled="False">
<ListView.ItemContainerStyle> <ListView.ItemContainerStyle>
<Style TargetType="ListViewItem"> <Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" /> <Setter Property="HorizontalContentAlignment" Value="Stretch" />
@ -93,18 +94,18 @@
<Grid> <Grid>
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="0.05*"/> <ColumnDefinition Width="0.05*"/>
<ColumnDefinition Width="0.25*"/> <ColumnDefinition Width="0.2*"/>
<ColumnDefinition Width="0.5*"/> <ColumnDefinition Width="0.55*"/>
<ColumnDefinition Width="0.2*"/> <ColumnDefinition Width="0.2*"/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderBrush="Black" BorderThickness="1,0,0,1"> <Border Grid.Column="0" BorderBrush="Black" BorderThickness="1,0,0,1">
<TextBlock Text="{Binding DisplayId, Mode=OneWay}" Margin="10" Foreground="Black" TextWrapping="Wrap" HorizontalAlignment="Center"/> <TextBlock Text="{Binding DisplayId, Mode=OneWay}" Margin="10" Foreground="Black" TextWrapping="Wrap" HorizontalAlignment="Center"/>
</Border> </Border>
<Border Grid.Column="1" BorderBrush="Black" BorderThickness="1,0,0,1"> <Border Grid.Column="1" BorderBrush="Black" BorderThickness="1,0,0,1">
<TextBlock Text="{Binding Name, Mode=OneWay}" Foreground="Black" TextWrapping="Wrap"/> <TextBlock Text="{Binding Name, Mode=OneWay}" Foreground="Black" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border> </Border>
<Border Grid.Column="2" BorderBrush="Black" BorderThickness="1,0,0,1"> <Border Grid.Column="2" BorderBrush="Black" BorderThickness="1,0,0,1">
<TextBlock Text="{Binding Content , Mode=OneWay}" Foreground="Black" TextWrapping="Wrap"/> <TextBlock Text="{Binding Content , Mode=OneWay}" Foreground="Black" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border> </Border>
<Border Grid.Column="3" BorderBrush="Black" BorderThickness="1,0,1,1"> <Border Grid.Column="3" BorderBrush="Black" BorderThickness="1,0,1,1">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" > <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" >

View File

@ -1,9 +1,14 @@
using PCUT.ViewModels; using Http.Core.Exceptions;
using Http.Core.Extensions;
using Http.Core;
using PCUT.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime; using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation; using Windows.Foundation;
using Windows.Foundation.Collections; using Windows.Foundation.Collections;
using Windows.UI.Xaml; using Windows.UI.Xaml;
@ -13,6 +18,7 @@ using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation; using Windows.UI.Xaml.Navigation;
using static Http.Core.Constants.HttpConstants;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238 // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
@ -28,6 +34,7 @@ namespace PCUT.Pages.BannerManagement
public BannerPage() public BannerPage()
{ {
this.InitializeComponent(); this.InitializeComponent();
ViewModels = DataContext as BannerViewModel;
} }
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
@ -37,27 +44,142 @@ namespace PCUT.Pages.BannerManagement
private void Page_Loaded(object sender, RoutedEventArgs e) private void Page_Loaded(object sender, RoutedEventArgs e)
{ {
ViewModels.Pagination.Page = 1;
} }
protected override void OnNavigatedFrom(NavigationEventArgs e) protected override void OnNavigatedFrom(NavigationEventArgs e)
{ {
base.OnNavigatedFrom(e); ViewModels.Pagination.Dispose();
} }
private void AddBanner_Click(object sender, RoutedEventArgs e) private async void AddBanner_Click(object sender, RoutedEventArgs e)
{ {
var dialog = new BannerUpsertDialog();
var result = await dialog.ShowAsync();
if (result == ContentDialogResult.Primary)
await ViewModels.Reload();
} }
private void EditBanner_Click(object sender, RoutedEventArgs e) private async void EditBanner_Click(object sender, RoutedEventArgs e)
{ {
if (sender is Button button && button.CommandParameter is string bannerId)
{
if (CanEdit())
{
var dialog = new BannerUpsertDialog(bannerId);
var result = await dialog.ShowAsync();
if (result == ContentDialogResult.Primary)
await ViewModels.Reload();
_editFlag = false;
}
}
} }
private void DeleteBanner_Click(object sender, RoutedEventArgs e) private static readonly object _editLock = new object();
private static bool _editFlag = false;
private bool CanEdit()
{ {
if (!Monitor.TryEnter(_editLock))
return false;
try
{
if (_editFlag)
return false;
_editFlag = true;
return true;
}
finally
{
Monitor.Exit(_editLock);
}
}
private async void DeleteBanner_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.CommandParameter is string bannerId)
{
if (CanDelete())
{
//Confirm diaglog before delete
ContentDialog deleteDialog = new ContentDialog
{
Title = "Confirm Delete?",
Content = "Are you sure want delete this banner?",
PrimaryButtonText = "Delete",
CloseButtonText = "Cancel"
};
deleteDialog.PrimaryButtonStyle = Application.Current.Resources["PrimaryDialogButton"] as Style;
deleteDialog.CloseButtonStyle = Application.Current.Resources["CloseDialogButton"] as Style;
var result = await deleteDialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
await DeleteBannerAsync(bannerId);
}
_deleteFlag = false;
}
}
}
private static readonly object _deleteLock = new object();
private static bool _deleteFlag = false;
private bool CanDelete()
{
if (!Monitor.TryEnter(_deleteLock))
return false;
try
{
if (_deleteFlag)
return false;
_deleteFlag = true;
return true;
}
finally
{
Monitor.Exit(_deleteLock);
}
}
private async Task DeleteBannerAsync(string bannerId)
{
using (var httpClient = HttpClientFactory.CreateClient(ClientNames.ApiClient))
{
try
{
var response = await httpClient.DeleteAsync(Api.BannerById.FormatRoute(bannerId));
if (response.IsSuccessStatusCode)
{
await ShowDialog("Delete Success", "Banner Deleted!");
await ViewModels.Reload();
}
else
{
await ShowDialog("Error!", "Can't delete banner");
}
}
catch (Exception ex) when (!(ex is AppOutdatedException))
{
await ShowDialog("Error!", $"Got exception: {ex.Message}");
}
}
}
private async Task ShowDialog(string title, string content)
{
ContentDialog dialog = new ContentDialog
{
Title = title,
Content = content,
CloseButtonText = "OK"
};
dialog.CloseButtonStyle = Application.Current.Resources["CloseDialogButton"] as Style;
await dialog.ShowAsync();
} }
private void btnFirst_Click(object sender, RoutedEventArgs e) private void btnFirst_Click(object sender, RoutedEventArgs e)