MobileDocumentSource.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Avalonia.Controls;
  2. using InABox.Avalonia.Platform;
  3. namespace InABox.Avalonia
  4. {
  5. public abstract class MobileDocumentSource
  6. {
  7. public abstract Task<MobileDocument> From(TopLevel window);
  8. }
  9. public abstract class MobileDocumentSource<TOptions> : MobileDocumentSource
  10. where TOptions : class, IMobileDocumentOptions, new()
  11. {
  12. public TOptions Options { get; set; }
  13. protected MobileDocumentSource(TOptions options)
  14. {
  15. Options = options;
  16. }
  17. protected abstract Task<bool> IsEnabled();
  18. // protected async Task<bool> IsEnabled<TPermission>()
  19. // where TPermission : Permissions.BasePermission, new()
  20. // {
  21. // var status = await Permissions.CheckStatusAsync<TPermission>();
  22. // if (status != PermissionStatus.Granted && status != PermissionStatus.Restricted)
  23. // status = await Permissions.RequestAsync<TPermission>();
  24. // return status == PermissionStatus.Granted;
  25. // }
  26. protected abstract Task<ImageFile?> Capture(TopLevel window);
  27. public override async Task<MobileDocument> From(TopLevel window)
  28. {
  29. var result = new MobileDocument();
  30. if (await IsEnabled())
  31. {
  32. try
  33. {
  34. var file = await Capture(window);
  35. if (file?.Data != null)
  36. {
  37. result.FileName = Path.GetFileName(file.FileName);
  38. result.Data = file.Data;
  39. //file.OriginalFilename ?? file.Path);
  40. //await using (var stream = await file.OpenReadAsync())
  41. //{
  42. // BinaryReader br = new BinaryReader(file.Stream);
  43. // result.Data = br.ReadBytes((int)file.Stream.Length);
  44. //}
  45. }
  46. ApplyOptions(result);
  47. }
  48. // catch (FeatureNotSupportedException fnsEx)
  49. // {
  50. // MobileLogging.Log(fnsEx, "Capture(FNS)");
  51. // MobileDialog.ShowMessage("This operation is not Supported on this Device!");
  52. // }
  53. // catch (PermissionException pEx)
  54. // {
  55. // MobileLogging.Log(pEx, "Capture(PERM)");
  56. // MobileDialog.ShowMessage("This operation is not Permitted on this Device!");
  57. // }
  58. catch (Exception ex)
  59. {
  60. MobileLogging.Log(ex, "Capture(ERR)");
  61. MobileDialog.ShowMessage($"Oops! Something went wrong!\n{ex.Message}");
  62. }
  63. }
  64. return result;
  65. }
  66. protected abstract void ApplyOptions(MobileDocument document);
  67. }
  68. }