MobileUtils.cs 4.7 KB

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