PlatformTools.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Autofac;
  2. using InABox.Avalonia.Platform;
  3. using InABox.Avalonia.Platform.Barcodes;
  4. using InABox.Core;
  5. using Exception = System.Exception;
  6. namespace InABox.Avalonia.Platform;
  7. public static class PlatformTools
  8. {
  9. private static ContainerBuilder _builder = new ContainerBuilder();
  10. private static IContainer? _container;
  11. private static IDeviceId? _deviceId;
  12. public static IDeviceId DeviceId
  13. {
  14. get
  15. {
  16. _deviceId ??= Resolve<IDeviceId,DefaultDeviceId>();
  17. return _deviceId;
  18. }
  19. }
  20. private static IAppVersion? _appVersion;
  21. public static IAppVersion AppVersion
  22. {
  23. get
  24. {
  25. _appVersion ??= Resolve<IAppVersion, DefaultAppVersion>();
  26. return _appVersion;
  27. }
  28. }
  29. private static IImageTools? _imageTools;
  30. public static IImageTools ImageTools
  31. {
  32. get
  33. {
  34. _imageTools ??= Resolve<IImageTools, DefaultImageTools>();
  35. return _imageTools;
  36. }
  37. }
  38. private static IPdfRenderer? _pdfRenderer;
  39. public static IPdfRenderer PdfRenderer
  40. {
  41. get
  42. {
  43. _pdfRenderer ??= Resolve<IPdfRenderer, DefaultPdfRenderer>();
  44. return _pdfRenderer;
  45. }
  46. }
  47. private static ICameraViewControl? _cameraViewControl;
  48. public static ICameraViewControl CameraViewControl
  49. {
  50. get
  51. {
  52. _cameraViewControl ??= Resolve<ICameraViewControl, DefaultCameraViewControl>();
  53. return _cameraViewControl;
  54. }
  55. }
  56. public static Guid DigitalKeyServiceId = Guid.Parse("ce6c0b18-0000-1000-8000-00805F9B34FB");
  57. public static Guid DigitalKeyConfigId = Guid.Parse("447c1982-77ef-49be-a39a-2920f33c31e5");
  58. public static Guid DigitalKeyControlId = Guid.Parse("5b804487-b73f-406a-8240-649c23ad1590");
  59. public static double ScanTimeoutInSeconds = 15;
  60. private static IBluetooth? _bluetooth;
  61. public static IBluetooth Bluetooth
  62. {
  63. get
  64. {
  65. _bluetooth ??= Resolve<IBluetooth, DefaultBluetooth>();
  66. return _bluetooth;
  67. }
  68. }
  69. private static IGeolocation? _geolocation;
  70. public static IGeolocation Geolocation
  71. {
  72. get
  73. {
  74. _geolocation ??= Resolve<IGeolocation, DefaultGeolocation>();
  75. return _geolocation;
  76. }
  77. }
  78. private static IPermissions? _permissions;
  79. public static IPermissions Permissions
  80. {
  81. get
  82. {
  83. _permissions ??= Resolve<IPermissions, DefaultPermissions>();
  84. return _permissions;
  85. }
  86. }
  87. private static TInterface Resolve<TInterface, TDefault>() where TInterface : notnull, ILoggable where TDefault : TInterface, new()
  88. {
  89. _container ??= _builder.Build();
  90. var result = _container.IsRegistered<TInterface>()
  91. ?_container.Resolve<TInterface>()
  92. : new TDefault();
  93. result.Logger = Logger.Main;
  94. return result;
  95. }
  96. public static void Register<TInterface, TImplementation>()
  97. where TImplementation : TInterface
  98. where TInterface : notnull
  99. {
  100. _builder.RegisterType<TImplementation>().As<TInterface>();
  101. }
  102. }