CustomerPanel.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Threading;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Configuration;
  11. using InABox.Core;
  12. using InABox.DynamicGrid;
  13. namespace PRSDesktop
  14. {
  15. public class CustomerPanelSettings : IUserConfigurationSettings
  16. {
  17. public CustomerPanelSettings()
  18. {
  19. AnchorWidth = 500F;
  20. View = DynamicSplitPanelView.Combined;
  21. }
  22. public DynamicSplitPanelView View { get; set; }
  23. public double AnchorWidth { get; set; }
  24. }
  25. public partial class CustomerPanel : UserControl, IPanel<Customer>
  26. {
  27. private CustomerPanelSettings settings;
  28. private Customer _customer;
  29. private enum PageIndex
  30. {
  31. Contacts,
  32. Spreadsheets
  33. }
  34. public CustomerPanel()
  35. {
  36. InitializeComponent();
  37. }
  38. public bool IsReady { get; set; }
  39. public event DataModelUpdateEvent? OnUpdateDataModel;
  40. public Dictionary<string, object[]> Selected()
  41. {
  42. return new Dictionary<string, object[]> { { typeof(Customer).EntityName(), Customers.SelectedRows } };
  43. }
  44. public void Setup()
  45. {
  46. settings = new UserConfiguration<CustomerPanelSettings>().Load();
  47. SplitPanel.View = settings.View;
  48. SplitPanel.AnchorWidth = settings.AnchorWidth;
  49. Customers.ColumnsTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : "";
  50. Customers.Refresh(true, false);
  51. CustomerContacts.Refresh(true, false);
  52. CustomerSpreadsheets.Refresh(true,false);
  53. CustomerActivities.Refresh(true,false);
  54. CustomerProducts.Refresh(true,false);
  55. }
  56. public void Shutdown(CancelEventArgs? cancel)
  57. {
  58. }
  59. public void CreateToolbarButtons(IPanelHost host)
  60. {
  61. //host.CreateToolbarButton(new PanelAction() { Caption = "Select Columns", OnExecute = DoSelectColumns, Image = PRSDesktop.Resources.shared });
  62. }
  63. public void Refresh()
  64. {
  65. Customers.Refresh(true, true);
  66. }
  67. public string SectionName => "Customers";
  68. public DataModel DataModel(Selection selection)
  69. {
  70. var ids = Customers.ExtractValues(x => x.ID, selection).ToArray();
  71. return new BaseDataModel<Customer>(new Filter<Customer>(x => x.ID).InList(ids));
  72. }
  73. public void Heartbeat(TimeSpan time)
  74. {
  75. }
  76. public Type DataType()
  77. {
  78. return typeof(Customer);
  79. }
  80. private void RefreshSubPage(ICustomerGrid grid)
  81. {
  82. if (grid == null)
  83. return;
  84. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("CustomerPanel: RefreshSubPage({0})", grid.GetType().EntityName()));
  85. grid.Customer = _customer;
  86. var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
  87. timer.Tick += (o, e) =>
  88. {
  89. timer.Stop();
  90. grid.Refresh(!grid.IsReady, true);
  91. };
  92. timer.Start();
  93. }
  94. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  95. {
  96. settings.View = SplitPanel.View;
  97. settings.AnchorWidth = SplitPanel.AnchorWidth;
  98. new UserConfiguration<CustomerPanelSettings>().Save(settings);
  99. var newTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : "";
  100. if (Customers.ColumnsTag != newTag)
  101. {
  102. Customers.ColumnsTag = newTag;
  103. Customers.Refresh(true, true);
  104. }
  105. }
  106. private void CustomerDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
  107. {
  108. if (e.AddedItems.Count > 0)
  109. {
  110. var t = e.AddedItems[0] as TabItem;
  111. if (t != null && t.Visibility == Visibility.Visible)
  112. RefreshSubPage(t.Content as ICustomerGrid);
  113. }
  114. }
  115. private void Customers_OnOnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  116. {
  117. var row = e.Rows?.FirstOrDefault();
  118. _customer = row?.ToObject<Customer>() ?? new Customer();
  119. Dispatcher.Invoke(() => { RefreshSubPage(CustomerDetails.SelectedContent as ICustomerGrid); });
  120. }
  121. }
  122. }