Megy a program letöltése is...

This commit is contained in:
2025-04-06 19:33:47 +02:00
parent dc3e6e2f7b
commit a64304a05f
11 changed files with 242 additions and 32 deletions
+80 -1
View File
@@ -1,4 +1,5 @@
using AutoMapper;
using Android.Content;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
@@ -27,6 +28,84 @@ namespace WorkFlowCheck.MAUI.Services
await SyncCheckListTemplates_Down(reportProgress);
await SyncCheckListHeader_Down(reportProgress);
}
public async Task DownloadAPK(Action<double, string> reportProgress)
{
try
{
await PrepareAuthenticatedRequestAsync();
string endpoint = $"{_httpClient.BaseAddress}api/Sync/Download";
string fileName = "com.nuvolar.wfcapp-Signed.apk";
// Az Androidon lévő privát letöltési mappa
string downloadFolder = Path.Combine(Android.App.Application.Context.GetExternalFilesDir(null)?.AbsolutePath, "APK");
// Ellenőrizd, hogy a mappa létezik-e, ha nem, hozd létre
if (!Directory.Exists(downloadFolder))
{
Directory.CreateDirectory(downloadFolder);
}
string filePath = Path.Combine(downloadFolder, fileName);
using (_httpClient)
{
var response = await _httpClient.GetAsync(endpoint, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var totalBytes = response.Content.Headers.ContentLength ?? -1L;
long receivedBytes = 0;
using (var stream = await response.Content.ReadAsStreamAsync())
using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
byte[] buffer = new byte[1024 * 1024]
;
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await fileStream.WriteAsync(buffer, 0, bytesRead);
receivedBytes += bytesRead;
if (totalBytes != -1)
{
double progress = (double)receivedBytes / totalBytes * 100;
reportProgress(progress, $"APK letöltés...");
}
else
{
reportProgress(-1, $"APK letöltés...");
}
}
}
// Telepítés indítása a FileProvider segítségével
var apkUri = FileProvider.GetUriForFile(Android.App.Application.Context,
$"{Android.App.Application.Context.PackageName}.fileprovider",
new Java.IO.File(filePath));
var installIntent = new Intent(Intent.ActionView);
installIntent.SetDataAndType(apkUri, "application/vnd.android.package-archive");
installIntent.AddFlags(ActivityFlags.NewTask);
installIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
Android.App.Application.Context.StartActivity(installIntent);
}
else
{
reportProgress(0, $"Letöltési hiba: {response.StatusCode}");
Console.WriteLine($"Letöltési hiba: {response.StatusCode}");
}
}
}
catch (Exception ex)
{
reportProgress(0, $"Hiba történt: {ex.Message}");
Console.WriteLine($"Hiba történt: {ex.Message}");
}
}
public async Task SyncCheckPoints_Down(Action<double, string> reportProgress)
{
try