UI javítás + beküldés, képméretezés
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
using AutoMapper;
|
||||
using Android.Health.Connect.DataTypes.Units;
|
||||
using AutoMapper;
|
||||
using Java.Net;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using WorkFlowCheck.Common.DTO;
|
||||
using WorkFlowCheck.MAUI.DataLayer;
|
||||
using WorkFlowCheck.MAUI.DataLayer.Entities;
|
||||
using WorkFlowCheck.MAUI.Handlers;
|
||||
using WorkFlowCheck.MAUI.Services.Interfaces;
|
||||
using Location = WorkFlowCheck.MAUI.DataLayer.Entities.Location;
|
||||
|
||||
@@ -18,30 +22,27 @@ namespace WorkFlowCheck.MAUI.Services
|
||||
{
|
||||
_userService = userService;
|
||||
}
|
||||
public async Task SyncDatas()
|
||||
public async Task SyncDatas(Action<double, string> reportProgress)
|
||||
{
|
||||
await SyncCheckPointsDown();
|
||||
await SyncLocations();
|
||||
await SyncEquipments();
|
||||
await SyncCheckListTemplates();
|
||||
await SyncCheckList();
|
||||
await SyncCheckPoints_Down(reportProgress);
|
||||
await SyncLocations_Down(reportProgress);
|
||||
await SyncEquipments_Down(reportProgress);
|
||||
await SyncCheckListTemplates_Down(reportProgress);
|
||||
await SyncCheckListHeader_Down(reportProgress);
|
||||
}
|
||||
public async Task SyncCheckPointsDown()
|
||||
public async Task SyncCheckPoints_Down(Action<double, string> reportProgress)
|
||||
{
|
||||
try
|
||||
{
|
||||
await PrepareAuthenticatedRequestAsync();
|
||||
// Az API végpont meghatározása
|
||||
string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckPoints";
|
||||
var responseList = DownloadDataAsync<CheckPointDTO>(endpoint, reportProgress).Result;
|
||||
|
||||
// HTTP GET kérés küldése
|
||||
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<CheckPointDTO>>>(endpoint);
|
||||
if (response != null)
|
||||
if (responseList != null)
|
||||
{
|
||||
if (response.IsSuccess)
|
||||
if (responseList.IsSuccess)
|
||||
{
|
||||
var entityList = await _dbContext.CheckPoints.ToListAsync();
|
||||
var missingItems = response.Data.Where(f => !entityList.Any(s => s.Id == f.Id)).ToList();
|
||||
var missingItems = responseList.Data.Where(f => !entityList.Any(s => s.Id == f.Id)).ToList();
|
||||
|
||||
var mappedMissingItems = _mapper.Map<List<CheckPoint>>(missingItems);
|
||||
|
||||
@@ -52,49 +53,14 @@ namespace WorkFlowCheck.MAUI.Services
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Hiba visszaadása
|
||||
|
||||
Console.WriteLine($"Hiba történt: {ex.Message}");
|
||||
}
|
||||
}
|
||||
public async Task SyncCheckPointsUp()
|
||||
public async Task SyncCheckPoints_Up(Action<double, string> reportProgress)
|
||||
{
|
||||
try
|
||||
{
|
||||
await PrepareAuthenticatedRequestAsync();
|
||||
// Az API végpont meghatározása
|
||||
string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckPoints";
|
||||
// HTTP GET kérés küldése
|
||||
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<CheckPointDTO>>>(endpoint);
|
||||
if (response != null)
|
||||
{
|
||||
if (response.IsSuccess)
|
||||
{
|
||||
var entityList = await _dbContext.CheckPoints.ToListAsync();
|
||||
var missingItems = response.Data.Where(f => !entityList.Any(s => s.Id == f.Id & s.Code == f.Code)).ToList();
|
||||
|
||||
|
||||
|
||||
var checkPointListDTO = _mapper.Map<List<CheckPointDTO>>(missingItems);
|
||||
|
||||
foreach (var item in checkPointListDTO)
|
||||
{
|
||||
var entity = await _dbContext.CheckPoints.Where(w => w.Id == item.Id).FirstOrDefaultAsync();
|
||||
if (entity != null)
|
||||
{
|
||||
item.ShortName = entity.ShortName;
|
||||
item.Code = entity.Code;
|
||||
}
|
||||
}
|
||||
|
||||
endpoint = $"{_httpClient.BaseAddress}api/Sync/UpdateAllCheckPoint";
|
||||
using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, checkPointListDTO))
|
||||
{
|
||||
httpResponseMessage.EnsureSuccessStatusCode();
|
||||
var jsonString = await httpResponseMessage.Content.ReadAsStringAsync();
|
||||
response = JsonConvert.DeserializeObject<ApiResponseDTO<List<CheckPointDTO>>>(jsonString);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -102,22 +68,19 @@ namespace WorkFlowCheck.MAUI.Services
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SyncLocations()
|
||||
public async Task SyncLocations_Down(Action<double, string> reportProgress)
|
||||
{
|
||||
try
|
||||
{
|
||||
await PrepareAuthenticatedRequestAsync();
|
||||
// Az API végpont meghatározása
|
||||
string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllLocations";
|
||||
var responseList = DownloadDataAsync<LocationDTO>(endpoint, reportProgress).Result;
|
||||
|
||||
// HTTP GET kérés küldése
|
||||
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<LocationDTO>>>(endpoint);
|
||||
if (response != null)
|
||||
if (responseList != null)
|
||||
{
|
||||
if (response.IsSuccess)
|
||||
if (responseList.IsSuccess)
|
||||
{
|
||||
var entityList = await _dbContext.Locations.ToListAsync();
|
||||
var missingItems = response.Data.Where(f => !entityList.Any(s => s.Id == f.Id)).ToList();
|
||||
var missingItems = responseList.Data.Where(f => !entityList.Any(s => s.Id == f.Id)).ToList();
|
||||
|
||||
var mappedMissingItems = _mapper.Map<List<Location>>(missingItems);
|
||||
|
||||
@@ -132,22 +95,19 @@ namespace WorkFlowCheck.MAUI.Services
|
||||
|
||||
}
|
||||
}
|
||||
public async Task SyncEquipments()
|
||||
public async Task SyncEquipments_Down(Action<double, string> reportProgress)
|
||||
{
|
||||
try
|
||||
{
|
||||
await PrepareAuthenticatedRequestAsync();
|
||||
// Az API végpont meghatározása
|
||||
string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllEquipments";
|
||||
var responseList = DownloadDataAsync<LocationDTO>(endpoint, reportProgress).Result;
|
||||
|
||||
// HTTP GET kérés küldése
|
||||
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<EquipmentDTO>>>(endpoint);
|
||||
if (response != null)
|
||||
if (responseList != null)
|
||||
{
|
||||
if (response.IsSuccess)
|
||||
if (responseList.IsSuccess)
|
||||
{
|
||||
var entityList = await _dbContext.Locations.ToListAsync();
|
||||
var missingItems = response.Data.Where(f => !entityList.Any(s => s.Id == f.Id)).ToList();
|
||||
var missingItems = responseList.Data.Where(f => !entityList.Any(s => s.Id == f.Id)).ToList();
|
||||
|
||||
var mappedMissingItems = _mapper.Map<List<Equipment>>(missingItems);
|
||||
|
||||
@@ -162,23 +122,20 @@ namespace WorkFlowCheck.MAUI.Services
|
||||
|
||||
}
|
||||
}
|
||||
public async Task SyncCheckListTemplates()
|
||||
public async Task SyncCheckListTemplates_Down(Action<double, string> reportProgress)
|
||||
{
|
||||
try
|
||||
{
|
||||
await PrepareAuthenticatedRequestAsync();
|
||||
// Az API végpont meghatározása
|
||||
var userId = _userService.GetCurrentUser()?.Id;
|
||||
string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckListTemplateHeader/{userId}";
|
||||
var responseList = DownloadDataAsync<CheckListTemplateHeaderDTO>(endpoint, reportProgress).Result;
|
||||
|
||||
// HTTP GET kérés küldése
|
||||
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<CheckListTemplateHeaderDTO>>>(endpoint);
|
||||
if (response != null)
|
||||
if (responseList != null)
|
||||
{
|
||||
if (response.IsSuccess)
|
||||
if (responseList.IsSuccess)
|
||||
{
|
||||
var checkListTemplateHeaderList = await _dbContext.CheckListTemplateHeaders.ToListAsync();
|
||||
var missingItems = response.Data.Where(f => !checkListTemplateHeaderList.Any(s => s.Id == f.Id)).ToList();
|
||||
var missingItems = responseList.Data.Where(f => !checkListTemplateHeaderList.Any(s => s.Id == f.Id)).ToList();
|
||||
|
||||
var missingCheckListTemplateHeaders = _mapper.Map<List<CheckListTemplateHeader>>(missingItems);
|
||||
|
||||
@@ -193,96 +150,123 @@ namespace WorkFlowCheck.MAUI.Services
|
||||
|
||||
}
|
||||
}
|
||||
public async Task SyncCheckList()
|
||||
|
||||
|
||||
public async Task SyncCheckListHeader_Down(Action<double, string> reportProgress)
|
||||
{
|
||||
try
|
||||
{
|
||||
await PrepareAuthenticatedRequestAsync();
|
||||
if (_userService == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Az API végpont meghatározása
|
||||
var userId = _userService.GetCurrentUser()?.Id;
|
||||
string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckListHeader/{userId}";
|
||||
var responseList = DownloadDataAsync<CheckListHeaderDTO>(endpoint, reportProgress).Result;
|
||||
|
||||
// HTTP GET kérés küldése
|
||||
|
||||
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<CheckListHeaderDTO>>>(endpoint);
|
||||
if (response != null)
|
||||
if (responseList != null && responseList.IsSuccess)
|
||||
{
|
||||
if (response.IsSuccess)
|
||||
var checkListHeaderList = await _dbContext.CheckListHeaders.ToListAsync();
|
||||
var missingItems = responseList.Data
|
||||
.Where(f => !checkListHeaderList.Any(s => s.GuidNumber == f.GuidNumber))
|
||||
.ToList();
|
||||
|
||||
var missingCheckListHeaders = _mapper.Map<List<CheckListHeader>>(missingItems);
|
||||
_dbContext.CheckListHeaders.AddRange(missingCheckListHeaders);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
int i = 1;
|
||||
foreach (var item in missingItems)
|
||||
{
|
||||
var checkListHeaderList = await _dbContext.CheckListHeaders.ToListAsync();
|
||||
var missingItems = response.Data.Where(f => !checkListHeaderList.Any(s => s.GuidNumber == f.GuidNumber)).ToList();
|
||||
|
||||
var missingCheckListHeaders = _mapper.Map<List<CheckListHeader>>(missingItems);
|
||||
|
||||
_dbContext.CheckListHeaders.AddRange(missingCheckListHeaders);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
foreach (var item in missingItems)
|
||||
{
|
||||
var checkListHeaderDTO = item as CheckListHeaderDTO;
|
||||
|
||||
foreach (var checkListRowDTO in checkListHeaderDTO.CheckListRowDTO)
|
||||
{
|
||||
var checkListRow = _dbContext.CheckListRows.Where(w => w.GuidNumber == checkListRowDTO.GuidNumber).FirstOrDefault();
|
||||
var isAdd = false;
|
||||
if (checkListRow == null)
|
||||
{
|
||||
isAdd = true;
|
||||
checkListRow = new CheckListRow();
|
||||
checkListRow.CheckListHeaderId = checkListHeaderDTO.Id;
|
||||
}
|
||||
checkListRow.Answer = checkListRowDTO.Answer;
|
||||
checkListRow.CheckListTemplateRowId = checkListRowDTO.CheckListTemplateRowId;
|
||||
checkListRow.GuidNumber = checkListRowDTO.GuidNumber;
|
||||
checkListRow.Photo = checkListRowDTO.Photo;
|
||||
|
||||
if (isAdd) _dbContext.CheckListRows.Add(checkListRow);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
double itemPercentage = (double)i / missingItems.Count * 100;
|
||||
reportProgress(itemPercentage, "Adatbázis szinkronizáció..."); // Közvetlen progress jelentés
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Hiba visszaadása
|
||||
|
||||
Console.WriteLine($"Hiba történt: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SyncCheckListHeaderUp(int Id)
|
||||
public async Task<ApiResponseDTO<string>> SyncCheckListHeader_Up(int Id, Action<double, string> reportProgress)
|
||||
{
|
||||
var retVal = new ApiResponseDTO<string>();
|
||||
try
|
||||
{
|
||||
await PrepareAuthenticatedRequestAsync();
|
||||
|
||||
var checkListHeader = await _dbContext.CheckListHeaders
|
||||
.Where(w => w.Id == Id)
|
||||
.Where(w => w.Id == Id)
|
||||
.Include(i => i.CheckListRows)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
|
||||
var checkListHeaderDTO = _mapper.Map<CheckListHeaderDTO>(checkListHeader);
|
||||
|
||||
|
||||
var endpoint = $"{_httpClient.BaseAddress}api/Sync/UpdateCheckListHeader";
|
||||
using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, checkListHeaderDTO))
|
||||
|
||||
var json = JsonConvert.SerializeObject(checkListHeaderDTO);
|
||||
var jsonBytes = Encoding.UTF8.GetBytes(json);
|
||||
var jsonStream = new MemoryStream(jsonBytes);
|
||||
|
||||
var progressContent = new ProgressableStreamContent(jsonStream, 8192, (uploaded, total) =>
|
||||
{
|
||||
double percentage = (double)uploaded / total * 100;
|
||||
reportProgress(percentage, "Beküldés...");
|
||||
});
|
||||
|
||||
progressContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
||||
|
||||
using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsync(endpoint, progressContent))
|
||||
{
|
||||
httpResponseMessage.EnsureSuccessStatusCode();
|
||||
var jsonString = await httpResponseMessage.Content.ReadAsStringAsync();
|
||||
var response = JsonConvert.DeserializeObject<ApiResponseDTO<CheckListHeaderDTO>>(jsonString);
|
||||
retVal = JsonConvert.DeserializeObject<ApiResponseDTO<string>>(jsonString);
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Hiba visszaadása
|
||||
retVal.IsSuccess = false;
|
||||
retVal.Errors.Add(ex.Message);
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
public async Task<ApiResponseDTO<List<T>>> DownloadDataAsync<T>(string endpoint, Action<double, string> reportProgress)
|
||||
{
|
||||
await PrepareAuthenticatedRequestAsync();
|
||||
|
||||
using var response = await _httpClient.GetAsync(endpoint, HttpCompletionOption.ResponseHeadersRead);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
using var stream = await response.Content.ReadAsStreamAsync();
|
||||
using var memoryStream = new MemoryStream();
|
||||
|
||||
var buffer = new byte[8192];
|
||||
long totalRead = 0;
|
||||
int bytesRead;
|
||||
int chunkCounter = 0;
|
||||
|
||||
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
|
||||
{
|
||||
await memoryStream.WriteAsync(buffer, 0, bytesRead);
|
||||
totalRead += bytesRead;
|
||||
chunkCounter++;
|
||||
|
||||
// Gyakori progress frissítés
|
||||
if (chunkCounter % 5 == 0) // 5 blokk után
|
||||
{
|
||||
double estimatedProgress = Math.Min((double)totalRead / (8192 * 100) * 100, 99);
|
||||
reportProgress(estimatedProgress, "Letöltés ...");
|
||||
}
|
||||
}
|
||||
|
||||
// Végső progress 100%-ra állítása
|
||||
reportProgress(100, "Letöltés ... kész!");
|
||||
|
||||
memoryStream.Position = 0;
|
||||
var jsonString = Encoding.UTF8.GetString(memoryStream.ToArray());
|
||||
|
||||
// Generikus deszerializálás
|
||||
var responseList = JsonConvert.DeserializeObject<ApiResponseDTO<List<T>>>(jsonString);
|
||||
return responseList;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user