CustomModuleUtils.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.WPF;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace PRSDesktop
  12. {
  13. public static class CustomModuleUtils
  14. {
  15. public static List<CustomModule> LoadCustomModules(string section, DataModel model)
  16. {
  17. return new Client<CustomModule>()
  18. .Query(
  19. new Filter<CustomModule>(x => x.Section).IsEqualTo(section)
  20. .And(x => x.DataModel).IsEqualTo(model.Name)
  21. .And(x => x.Visible).IsEqualTo(true),
  22. null,
  23. new SortOrder<CustomModule>(x => x.Name))
  24. .ToObjects<CustomModule>().ToList();
  25. }
  26. public static IEnumerable<Tuple<CustomModule, Bitmap>> LoadCustomModuleThumbnails(IList<CustomModule> modules)
  27. {
  28. var thumbids = modules.Where(x => x.Thumbnail.IsValid()).Select(x => x.Thumbnail.ID);
  29. var thumbs = thumbids.Any()
  30. ? new Client<Document>().Load(Filter<Document>.List(x => x.ID, ListOperator.Includes, thumbids))
  31. : Array.Empty<Document>();
  32. var list = new List<Tuple<CustomModule, Bitmap>>();
  33. foreach (var module in modules)
  34. {
  35. Bitmap image;
  36. try
  37. {
  38. var doc = thumbs.FirstOrDefault(x => x.ID.Equals(module.Thumbnail.ID));
  39. if (doc != null)
  40. {
  41. var bmp = (new ImageConverter().ConvertFrom(doc.Data) as Bitmap)!;
  42. image = bmp;
  43. }
  44. else
  45. {
  46. image = PRSDesktop.Resources.edit;
  47. }
  48. }
  49. catch
  50. {
  51. image = PRSDesktop.Resources.edit;
  52. }
  53. yield return new(module, image);
  54. }
  55. }
  56. public static IEnumerable<Tuple<CustomModule, Bitmap>> LoadCustomModuleThumbnails(string section, DataModel model)
  57. {
  58. return LoadCustomModuleThumbnails(LoadCustomModules(section, model));
  59. }
  60. }
  61. }