Add RenewContext
This commit is contained in:
@ -40,6 +40,9 @@ namespace Elwig.Helpers {
|
||||
public DbSet<DeliveryPartModifier> DeliveryPartModifiers { get; private set; }
|
||||
|
||||
private readonly StreamWriter? LogFile = null;
|
||||
public static DateTime LastWriteTime => File.GetLastWriteTime(App.Config.DatabaseFile);
|
||||
public DateTime SavedLastWriteTime { get; private set; }
|
||||
public bool HasBackendChanged => SavedLastWriteTime != LastWriteTime;
|
||||
|
||||
public AppDbContext() {
|
||||
if (App.Config.DatabaseLog != null) {
|
||||
@ -52,6 +55,9 @@ namespace Elwig.Helpers {
|
||||
MessageBox.Show($"Unable to open database log file:\n\n{e.Message}", "Database Log", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
SavedLastWriteTime = LastWriteTime;
|
||||
SavedChanges += OnSavedChanges;
|
||||
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
|
||||
@ -67,6 +73,10 @@ namespace Elwig.Helpers {
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private void OnSavedChanges(object? sender, SavedChangesEventArgs evt) {
|
||||
SavedLastWriteTime = LastWriteTime;
|
||||
}
|
||||
|
||||
protected void Log(string msg) {
|
||||
LogFile?.WriteLine(msg);
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ using System.Windows.Controls.Primitives;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.IO.Ports;
|
||||
using System.Net.Sockets;
|
||||
using System.Collections;
|
||||
|
||||
namespace Elwig.Helpers {
|
||||
public static partial class Utils {
|
||||
@ -129,6 +130,40 @@ namespace Elwig.Helpers {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void RenewItemsSource(Selector selector, IEnumerable? source, Func<object?, object?> getId) {
|
||||
var selectedId = getId(selector.SelectedItem);
|
||||
selector.ItemsSource = source;
|
||||
if (selectedId != null && source != null)
|
||||
selector.SelectedItem = source.Cast<object>().FirstOrDefault(i => selectedId.Equals(getId(i)));
|
||||
}
|
||||
|
||||
public static void RenewItemsSource(Xceed.Wpf.Toolkit.Primitives.Selector selector, IEnumerable? source, Func<object?, object?> getId) {
|
||||
var selectedIds = selector.SelectedItems.Cast<object>().Select(i => getId(i)).ToList();
|
||||
selector.ItemsSource = source;
|
||||
if (source != null) {
|
||||
foreach (var i in source.Cast<object>().Where(i => selectedIds.Contains(getId(i))))
|
||||
selector.SelectedItems.Add(i);
|
||||
}
|
||||
}
|
||||
|
||||
public static void RenewItemsSource(DataGrid dataGrid, IEnumerable? source, Func<object?, object?> getId, bool keepSort = true) {
|
||||
var column = dataGrid.CurrentCell.Column;
|
||||
var sortColumns = dataGrid.Columns.Select(c => c.SortDirection).ToList();
|
||||
var sort = dataGrid.Items.SortDescriptions.ToList();
|
||||
var selectedId = getId(dataGrid.SelectedItem);
|
||||
dataGrid.ItemsSource = source;
|
||||
if (keepSort) {
|
||||
for (int i = 0; i < dataGrid.Columns.Count; i++)
|
||||
dataGrid.Columns[i].SortDirection = sortColumns[i];
|
||||
foreach (var s in sort)
|
||||
dataGrid.Items.SortDescriptions.Add(s);
|
||||
}
|
||||
if (selectedId != null && source != null)
|
||||
dataGrid.SelectedItem = source.Cast<object>().FirstOrDefault(i => selectedId.Equals(getId(i)));
|
||||
if (dataGrid.SelectedItem != null)
|
||||
dataGrid.CurrentCell = new(dataGrid.SelectedItem, column);
|
||||
}
|
||||
|
||||
public static int Modulo(string a, int b) {
|
||||
if (a.Length == 0 || !a.All(char.IsAsciiDigit)) {
|
||||
throw new ArgumentException("First argument has to be a decimal string");
|
||||
|
Reference in New Issue
Block a user