demo/Demo/MainWindow.axaml.cs

156 lines
4.8 KiB
C#
Raw Permalink Normal View History

2024-09-05 13:22:57 +00:00
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Platform.Storage;
using Demo.EntityModels;
using Demo.Utils;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Tmds.DBus.Protocol;
namespace Demo
{
public partial class MainWindow : Window
{
//flags
private bool AdminMode = false;
private bool IsFiltred = false;
//null - nothing, 0 - asc, 1 - desc
private bool? SortedMode = null;
//changedList
private string SearchWord = "";
//constants for comboboxes
private string[] sortValues = new string[3] { "<22><><EFBFBD><EFBFBD><EFBFBD>", "<22><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.", "<22><> <20><><EFBFBD><EFBFBD>." };
private List<Service> services = new List<Service>()
{
new Service(){ ServiceName = "Abc", ServiceDiscount="Abc", ServicePriceMinutes = "Abc" },
new Service(){ ServiceName = "cba", ServiceDiscount="cba", ServicePriceMinutes = "cba" },
new Service(){ ServiceName = "bca", ServiceDiscount="bca", ServicePriceMinutes = "bca" },
new Service(){ ServiceName = "ccb", ServiceDiscount="ccb", ServicePriceMinutes = "ccb" }
};
public MainWindow()
{
InitializeComponent();
MyImage.Source = new Bitmap(AssetLoader.Open(new System.Uri("avares://Assets/service_layout.png")));
services = Context
.DbContext
.Services.Select(service => new Service
{
ServiceDiscount = service.Discount.ToString(),
ServiceName = service.Title,
ServicePriceMinutes = $"{service.Cost}/{service.Durationinminutes}"
}).ToList();
SortComboBox.ItemsSource = sortValues;
SortComboBox.SelectedIndex = 0;
DisplayService();
}
private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
AdminPanel.IsVisible = !AdminPanel.IsVisible;
}
private void SearchTextBox_TextChanging(object? sender, Avalonia.Controls.TextChangingEventArgs e)
{
string inputValue = (sender as TextBox).Text;
SearchWord = inputValue.ToLower();
DisplayService();
}
public async void ChangeModeButton_ClickAsync(object sender, RoutedEventArgs e)
{
var changeModeDialog = new ChangeModeDialog();
AdminMode = await changeModeDialog.ShowDialog<bool>(this);
AdminPanel.IsVisible = AdminMode;
DisplayService();
}
private void DisplayService()
{
List<Service> displayList = services;
if (!string.IsNullOrEmpty(SearchWord)) {
displayList =
displayList
.Where(x =>
x.ServiceName
.ToLower()
.Contains(SearchWord)
).ToList();
}
switch (SortedMode)
{
case true: displayList =
displayList
.OrderBy(
service =>
service.ServiceName)
.ToList(); break;
case false: displayList =
displayList
.OrderByDescending(
service =>
service.ServiceName)
.ToList(); break;
case null:
break;
}
ServiceList.ItemsSource = displayList
.Select(service => service.SetAdminMode(AdminMode))
.ToList();
CountValues.Text = $"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:{displayList.Count}/<2F><><EFBFBD><EFBFBD><EFBFBD>:{services.Count}";
}
class Service()
{
public string ServiceName { get; set; }
public string ServicePriceMinutes { get; set; }
public string ServiceDiscount { get; set; }
public bool AdminMode { get; set; } = false;
public Service SetAdminMode(bool mode)
{
this.AdminMode = mode;
return this;
}
}
private void SortComboBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e)
{
int selectedIndex = (sender as ComboBox).SelectedIndex;
switch (selectedIndex) {
case 1: SortedMode = false; break;
case 2: SortedMode = true; break;
default:
SortedMode = null; break;
}
DisplayService();
}
}
}