Gestiti allegati da galleria e fotocamera e aggiunto sentry
This commit is contained in:
101
SteUp.Maui/Core/Services/AttachedService.cs
Normal file
101
SteUp.Maui/Core/Services/AttachedService.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using SteUp.Shared.Core.Dto;
|
||||
using SteUp.Shared.Core.Interface.System;
|
||||
|
||||
namespace SteUp.Maui.Core.Services;
|
||||
|
||||
public class AttachedService : IAttachedService
|
||||
{
|
||||
public async Task<AttachedDto?> SelectImageFromCamera()
|
||||
{
|
||||
var cameraPerm = await Permissions.RequestAsync<Permissions.Camera>();
|
||||
var storagePerm = await Permissions.RequestAsync<Permissions.StorageWrite>();
|
||||
|
||||
if (cameraPerm != PermissionStatus.Granted || storagePerm != PermissionStatus.Granted)
|
||||
return null;
|
||||
|
||||
FileResult? result;
|
||||
|
||||
try
|
||||
{
|
||||
result = await MediaPicker.Default.CapturePhotoAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Errore cattura foto: {ex.Message}");
|
||||
SentrySdk.CaptureException(ex);
|
||||
return null;
|
||||
}
|
||||
|
||||
return result is null ? null : await ConvertToDto(result, AttachedDto.TypeAttached.Image);
|
||||
}
|
||||
|
||||
public async Task<AttachedDto?> SelectImageFromGallery()
|
||||
{
|
||||
var storagePerm = await Permissions.RequestAsync<Permissions.StorageRead>();
|
||||
if (storagePerm != PermissionStatus.Granted)
|
||||
return null;
|
||||
|
||||
FileResult? result;
|
||||
|
||||
try
|
||||
{
|
||||
result = await MediaPicker.Default.PickPhotoAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Errore selezione galleria: {ex.Message}");
|
||||
SentrySdk.CaptureException(ex);
|
||||
return null;
|
||||
}
|
||||
|
||||
return result is null ? null : await ConvertToDto(result, AttachedDto.TypeAttached.Image);
|
||||
}
|
||||
|
||||
private static async Task<AttachedDto> ConvertToDto(FileResult file, AttachedDto.TypeAttached type)
|
||||
{
|
||||
var stream = await file.OpenReadAsync();
|
||||
using var ms = new MemoryStream();
|
||||
await stream.CopyToAsync(ms);
|
||||
|
||||
return new AttachedDto
|
||||
{
|
||||
Name = file.FileName,
|
||||
Path = file.FullPath,
|
||||
MimeType = file.ContentType,
|
||||
DimensionBytes = ms.Length,
|
||||
FileBytes = ms.ToArray(),
|
||||
Type = type
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<string> SaveToTempStorage(Stream file, string fileName, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(file);
|
||||
|
||||
if (file.CanSeek)
|
||||
file.Position = 0;
|
||||
|
||||
fileName = Path.GetFileName(fileName);
|
||||
|
||||
var dir = FileSystem.CacheDirectory;
|
||||
var filePath = Path.Combine(dir, fileName);
|
||||
|
||||
await using var fileStream = File.Create(filePath);
|
||||
await file.CopyToAsync(fileStream, ct);
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public Task OpenFile(string fileName, string filePath)
|
||||
{
|
||||
#if IOS
|
||||
throw new NotImplementedException();
|
||||
#else
|
||||
return Launcher.OpenAsync(new OpenFileRequest
|
||||
{
|
||||
Title = "Apri file",
|
||||
File = new ReadOnlyFile(filePath)
|
||||
});
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,12 @@ namespace SteUp.Maui.Core.System;
|
||||
|
||||
public class GenericSystemService : IGenericSystemService
|
||||
{
|
||||
public string GetCurrentAppVersion() => AppInfo.VersionString;
|
||||
public string GetCurrentAppVersion()
|
||||
{
|
||||
#if DEBUG
|
||||
return $"v{AppInfo.VersionString} [DEBUG]";
|
||||
#else
|
||||
return $"v{AppInfo.VersionString}";
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,14 @@ namespace SteUp.Maui
|
||||
.UseMauiApp<App>()
|
||||
.UseIntegry(appToken: AppToken, useLoginAzienda: true)
|
||||
.UseMauiCommunityToolkit()
|
||||
.UseSentry(options =>
|
||||
{
|
||||
options.Dsn = "https://c1ff03e3d1d2dd973a1ce73c58f92a8d@o4508499810254848.ingest.de.sentry.io/4510900455866448";
|
||||
#if DEBUG
|
||||
options.Debug = true;
|
||||
#endif
|
||||
options.TracesSampleRate = 1.0;
|
||||
})
|
||||
.ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); });
|
||||
|
||||
builder.Services.AddMauiBlazorWebView();
|
||||
|
||||
@@ -120,6 +120,7 @@
|
||||
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="9.0.120"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="9.0.120"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.12" />
|
||||
<PackageReference Include="Sentry.Maui" Version="5.16.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user