using System; using System.IO; using System.Threading.Tasks; using AvaloniaDialogs.Views; using JetBrains.Annotations; using Microsoft.Maui.ApplicationModel; using Microsoft.Maui.Storage; using InABox.Avalonia; namespace InABox.Avalonia { public abstract class MobileDocumentSource { public abstract Task From(); } public abstract class MobileDocumentSource : MobileDocumentSource where TOptions : class, IMobileDocumentOptions, new() { public TOptions Options { get; set; } protected MobileDocumentSource(TOptions options) { Options = options; } protected abstract Task IsEnabled(); protected async Task IsEnabled() where TPermission : Permissions.BasePermission, new() { var status = await Permissions.CheckStatusAsync(); if (status != PermissionStatus.Granted && status != PermissionStatus.Restricted) status = await Permissions.RequestAsync(); return status == PermissionStatus.Granted; } protected abstract Task Capture(); public override async Task From() { var result = new MobileDocument(); if (await IsEnabled()) { try { var file = await Capture(); if (file != null) { result.FileName = Path.GetFileName(file.FileName); //file.OriginalFilename ?? file.Path); await using (var stream = await file.OpenReadAsync()) { BinaryReader br = new BinaryReader(stream); result.Data = br.ReadBytes((int)stream.Length); } } ApplyOptions(result); } catch (FeatureNotSupportedException fnsEx) { MobileLogging.Log(fnsEx, "Capture(FNS)"); MobileDialog.ShowMessage("This operation is not Supported on this Device!"); } catch (PermissionException pEx) { MobileLogging.Log(pEx, "Capture(PERM)"); MobileDialog.ShowMessage("This operation is not Permitted on this Device!"); } catch (Exception ex) { MobileLogging.Log(ex, "Capture(ERR)"); MobileDialog.ShowMessage("Oops! Something went wrong! Please try again.."); } } return result; } protected abstract void ApplyOptions(MobileDocument document); } }