MobileUtils.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Autofac;
  2. using InABox.Core;
  3. using Microsoft.Maui.ApplicationModel;
  4. namespace InABox.Avalonia
  5. {
  6. public static class MobileUtils
  7. {
  8. public static void Init()
  9. {
  10. Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(CoreUtils.SyncfusionLicense(SyncfusionVersion.v29_x));
  11. }
  12. private static async Task<bool> Retry(Func<Task<bool>> action, int interval, int retryCount = 3)
  13. {
  14. bool flag = false;
  15. List<Exception> source = new List<Exception>();
  16. TimeSpan delay = TimeSpan.FromMilliseconds((double)interval);
  17. for (int index = 0; index < retryCount; ++index)
  18. {
  19. try
  20. {
  21. flag = await action();
  22. if (flag)
  23. return true;
  24. Task.Delay(delay).Wait();
  25. }
  26. catch (Exception ex)
  27. {
  28. if (source.All<Exception>((Func<Exception, bool>)(x => x.Message != ex.Message)))
  29. source.Add(ex);
  30. Task.Delay(delay).Wait();
  31. }
  32. }
  33. if (!flag && !source.Any<Exception>())
  34. return false;
  35. if (source.Count<Exception>() == 1)
  36. throw source.First();
  37. if (source.Count<Exception>() > 1)
  38. throw new AggregateException((IEnumerable<Exception>)source);
  39. return flag;
  40. }
  41. public static async Task<bool> IsPermitted<TPermission>() where TPermission : Permissions.BasePermission, new()
  42. {
  43. try
  44. {
  45. PermissionStatus status = await Permissions.CheckStatusAsync<TPermission>();
  46. if (status == PermissionStatus.Granted)
  47. return true;
  48. var request = await Permissions.RequestAsync<TPermission>();
  49. return request == PermissionStatus.Granted;
  50. }
  51. catch (TaskCanceledException ex)
  52. {
  53. return false;
  54. }
  55. }
  56. // public static async Task<bool> CheckPermissions<TPermission>() where TPermission : Permissions.BasePermission, new()
  57. // {
  58. // var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
  59. // bool request = false;
  60. // if (permissionStatus == PermissionStatus.Denied)
  61. // {
  62. // if (String.Equals(Device.RuntimePlatform, Device.iOS))
  63. // {
  64. //
  65. // var title = $"{permission} Permission";
  66. // var question = $"To use this plugin the {permission} permission is required. Please go into Settings and turn on {permission} for the app.";
  67. // var positive = "Settings";
  68. // var negative = "Maybe Later";
  69. // var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
  70. // if (task == null)
  71. // return false;
  72. //
  73. // var result = await task;
  74. // if (result)
  75. // {
  76. // CrossPermissions.Current.OpenAppSettings();
  77. // }
  78. //
  79. // return false;
  80. // }
  81. //
  82. // request = true;
  83. //
  84. // }
  85. //
  86. // if (request || permissionStatus != PermissionStatus.Granted)
  87. // {
  88. // var newStatus = await CrossPermissions.Current.RequestPermissionsAsync(permission);
  89. // if (newStatus.ContainsKey(permission) && newStatus[permission] != PermissionStatus.Granted)
  90. // {
  91. // var title = $"{permission} Permission";
  92. // var question = $"To use the plugin the {permission} permission is required.";
  93. // var positive = "Settings";
  94. // var negative = "Maybe Later";
  95. // var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
  96. // if (task == null)
  97. // return false;
  98. //
  99. // var result = await task;
  100. // if (result)
  101. // {
  102. // CrossPermissions.Current.OpenAppSettings();
  103. // }
  104. // return false;
  105. // }
  106. // }
  107. //
  108. // return true;
  109. // }
  110. // public static byte[] ImageSourceToByteArray(ImageSource source)
  111. // {
  112. // StreamImageSource streamImageSource = (StreamImageSource)source;
  113. // System.Threading.CancellationToken cancellationToken = System.Threading.CancellationToken.None;
  114. // Task<Stream> task = streamImageSource.Stream(cancellationToken);
  115. // Stream stream = task.Result;
  116. // byte[] bytes = new byte[stream.Length];
  117. // stream.Read(bytes, 0, bytes.Length);
  118. // return bytes;
  119. // }
  120. //
  121. // public static string ImageSourceToBase64(ImageSource source)
  122. // {
  123. // var bytes = ImageSourceToByteArray(source);
  124. // string s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);
  125. // return s;
  126. // }
  127. }
  128. }