This commit is contained in:
Jose Conde
2025-09-29 16:02:00 +02:00
parent dc57da8136
commit 5e16f781b4
74 changed files with 1621 additions and 1856 deletions

View File

@@ -0,0 +1,47 @@
using ModVersionChecker.repository.api;
using ModVersionChecker.repository.api.dto;
using ModVersionChecker.service.interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ModVersionChecker.service
{
public class ApiService : IApiService
{
private readonly IApiRepository _apiRepository;
public ApiService(IApiRepository apiRepository)
{
_apiRepository = apiRepository;
}
public Task<bool> AuthenticateAsync(string username, string password)
=> _apiRepository.AuthenticateAsync(username, password);
public Task<List<AppVersionsResponse>?> GetAppVersionsAsync(List<App> apps)
=> _apiRepository.GetAppVersionsAsync(apps);
public Task<AppVersionsResponse?> GetAppLatestVersionAsync(App app)
=> _apiRepository.GetAppLatestVersionAsync(app);
public Task<List<TypeResponse>> GetTypes()
=> _apiRepository.GetTypes();
public Task<List<SourceResponse>> GetSources()
=> _apiRepository.GetSources();
public async Task<List<App>> GetAppsByIds(App[] apps)
{
var appResponses = await _apiRepository.GetAppsByIds(apps);
return appResponses.Select(AppResponse.toModel).ToList();
}
public Task<List<App>?> SearchApps(string searchText)
{
var appResponses = _apiRepository.SearchApps(searchText);
return appResponses.ContinueWith(t => t.Result?.Select(AppResponse.toModel).ToList());
}
}
}

View File

@@ -0,0 +1,22 @@
using ModVersionChecker.service.interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModVersionChecker.service
{
public class NotifyIconService : INotifyIconService
{
private NotifyIcon? _notifyIcon;
public void SetNotifyIcon(NotifyIcon icon)
{
_notifyIcon = icon;
}
public void ShowBalloonTip(int millis, string title, string message, ToolTipIcon icon)
{
_notifyIcon?.ShowBalloonTip(millis, title, message, icon);
}
}
}

View File

@@ -0,0 +1,141 @@
using ModVersionChecker.enums;
using ModVersionChecker.managers.interfaces;
using ModVersionChecker.model;
using ModVersionChecker.service.interfaces;
using ModVersionChecker.utils;
using NuGet.Versioning;
namespace ModVersionChecker.service
{
public class VersionService: IVersionService
{
private readonly IApiService _apiService;
private readonly IAppsManager _appsManager;
private readonly INotifyIconService _notifyIconService;
private readonly Config _globalConfig;
public VersionService(
IApiService apiVersionService,
IAppsManager appsManager,
IConfigManager configManager,
INotifyIconService notifyIconService)
{
_apiService = apiVersionService;
_appsManager = appsManager ?? throw new ArgumentNullException(nameof(appsManager));
_notifyIconService = notifyIconService ?? throw new ArgumentNullException(nameof(notifyIconService));
_globalConfig = configManager.Load() ?? new Config();
}
public App CheckApp(App app)
{
var status = AppStatus.NONE;
if (app.LatestVersion== null)
{
app.Status = AppStatus.ERROR;
_appsManager.Update(app);
return app;
}
var currentVersion = app.CurrentVersion;
var updateMessage = "";
if (isUpdateAvailable(currentVersion, app.LatestVersion))
{
updateMessage = $"{app.Name}: New version {app.LatestVersion} (current: {currentVersion})";
status = AppStatus.UPDATE_AVAILABLE;
}
app.Status = status;
app.LastCheckedAt = TimeUtils.GetUnixTimeMillis(null);
_appsManager.Update(app);
return app;
}
public async Task CheckAllApps()
{
var apps = _appsManager.Load();
var sources = await _apiService.GetSources();
var appVersionsMap = await _apiService.GetAppVersionsAsync(apps);
List<string> errorMessages = new List<string>();
List<string> updateMessages = new List<string>();
if (apps == null || apps.Count == 0)
{
return;
}
foreach (App app in apps)
{
var sourceId = app.Source;
if (
app.Status != AppStatus.ERROR && app.LastCheckedAt != 0 && app.LastCheckedAt < TimeUtils.GetUnixTimeMillis(DateTime.UtcNow.AddMinutes(-60)) ||
!app.Active || string.IsNullOrWhiteSpace(sourceId)
)
{
continue;
}
try
{
var latesstVersion = appVersionsMap.FirstOrDefault(a => a.Id == app.Id)?.LatestVersion;
var source = sources.FirstOrDefault(s => s.Id == sourceId);
if (source == null)
{
errorMessages.Add($"{app.Name} has an invalid source: {sourceId}");
continue;
}
var typeConfig = _globalConfig.Types.FirstOrDefault(t => t.ShortName == app.Type);
if (typeConfig == null)
{
errorMessages.Add($"{app.Name} has no valid type config.");
continue;
}
app.CurrentVersion = VersionUtils.GetCurrentVersion(app, typeConfig);
var updatedApp = CheckApp(app);
if (updatedApp.Status == AppStatus.UPDATE_AVAILABLE)
{
updateMessages.Add($"{app.Name}: New version {app.LatestVersion} (current: {app.CurrentVersion})");
}
}
catch (Exception ex)
{
errorMessages.Add($"Failed to check {app.Name}: {ex.Message}");
}
}
if (updateMessages.Count > 0)
{
_notifyIconService.ShowBalloonTip(
10000,
"Updates Available",
string.Join("\n", updateMessages),
ToolTipIcon.Info
);
}
if (errorMessages.Count > 0)
{
_notifyIconService.ShowBalloonTip(
10000,
"Errors",
string.Join("\n", errorMessages),
ToolTipIcon.Error
);
}
}
private bool isUpdateAvailable(string currentVersion, string latestVersion)
{
try
{
var current = NuGetVersion.Parse(currentVersion);
var latest = NuGetVersion.Parse(latestVersion);
return latest.CompareTo(current) == 1;
}
catch (Exception ex)
{
Console.WriteLine($"Failed to compare versions: {ex.Message}");
return false;
}
}
}
}

View File

@@ -0,0 +1,19 @@
using ModVersionChecker.repository.api.dto;
using NuGet.Versioning;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ModVersionChecker.service.interfaces
{
public interface IApiService
{
Task<List<AppVersionsResponse>> GetAppVersionsAsync(List<App> apps);
Task<List<TypeResponse>> GetTypes();
Task<List<SourceResponse>> GetSources();
Task<AppVersionsResponse?> GetAppLatestVersionAsync(App app);
Task<bool> AuthenticateAsync(string username, string password);
Task<List<App>> GetAppsByIds(App[] apps);
Task<List<App>?> SearchApps(string searchText);
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModVersionChecker.service.interfaces
{
public interface INotifyIconService
{
void SetNotifyIcon(NotifyIcon icon);
void ShowBalloonTip(int millis, string title, string message, ToolTipIcon icon);
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModVersionChecker.service.interfaces
{
public interface IVersionService
{
App CheckApp(App app);
Task CheckAllApps();
}
}