204 lines
6.6 KiB
C#
204 lines
6.6 KiB
C#
using ModVersionChecker.enums;
|
|
using ModVersionChecker.managers.interfaces;
|
|
using ModVersionChecker.model;
|
|
using ModVersionChecker.service.interfaces;
|
|
using ModVersionChecker.utils;
|
|
|
|
namespace ModVersionChecker.service
|
|
{
|
|
public class AppService: IAppService
|
|
{
|
|
private readonly IApiService _apiService;
|
|
private readonly IAppRepository _appRepository;
|
|
private readonly IVersionService _versionService;
|
|
private readonly IStateService _stateService;
|
|
private readonly INotificationService _notificationService;
|
|
private readonly Config _config;
|
|
|
|
public AppService(
|
|
IApiService apiService,
|
|
IVersionService versionService,
|
|
IAppRepository appManager,
|
|
IStateService stateService,
|
|
INotificationService notificationService
|
|
|
|
) {
|
|
_apiService = apiService;
|
|
_appRepository = appManager;
|
|
_versionService = versionService;
|
|
_stateService = stateService;
|
|
_notificationService = notificationService;
|
|
_config = _stateService.GetConfig();
|
|
}
|
|
|
|
public bool CreateApp(App? app)
|
|
{
|
|
if (app != null)
|
|
{
|
|
_appRepository.Insert(app);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool DeleteApp(App? app)
|
|
{
|
|
if (app !=null)
|
|
{
|
|
_appRepository.Delete(app.Id);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool UpdateApp(App? app)
|
|
{
|
|
if (app != null)
|
|
{
|
|
_appRepository.Update(app);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public async Task<List<App>> GetAndUpdateCurrentApps()
|
|
{
|
|
|
|
try
|
|
{
|
|
List<App> apps = _appRepository.Load();
|
|
|
|
if (apps == null) throw new InvalidOperationException("Failed to load apps: repository returned null.");
|
|
if (apps.Count == 0) return new List<App>();
|
|
|
|
List<App> updatedApps = await _apiService.GetAppsByIds(apps);
|
|
|
|
if (updatedApps == null) throw new InvalidOperationException("Failed to load apps: repository returned null.");
|
|
if (updatedApps.Count == 0) return apps;
|
|
|
|
foreach (App app in updatedApps)
|
|
{
|
|
app.CurrentVersion = _versionService.GetCurrentVersion(app);
|
|
CheckAppStatus(app);
|
|
_appRepository.Update(app);
|
|
}
|
|
_stateService.SetApps(updatedApps);
|
|
return updatedApps;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex);
|
|
return new List<App>();
|
|
}
|
|
}
|
|
|
|
public App CheckAppStatus(App app)
|
|
{
|
|
var status = AppStatus.NONE;
|
|
if (app.LatestVersion == null)
|
|
{
|
|
app.Status = AppStatus.ERROR;
|
|
_appRepository.Update(app);
|
|
return app;
|
|
}
|
|
var currentVersion = app.CurrentVersion;
|
|
var updateMessage = "";
|
|
if (_versionService.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);
|
|
return app;
|
|
}
|
|
|
|
public void CheckAllApps()
|
|
{
|
|
|
|
var apps = _appRepository.Load();
|
|
var sources = _stateService.GetSources();
|
|
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)) ||
|
|
string.IsNullOrWhiteSpace(sourceId)
|
|
)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
try
|
|
{
|
|
var latesstVersion = app.LatestVersion;
|
|
var source = sources.FirstOrDefault(s => s.Id == sourceId);
|
|
if (source == null)
|
|
{
|
|
errorMessages.Add($"{app.Name} has an invalid source: {sourceId}");
|
|
continue;
|
|
}
|
|
var typeConfig = _config.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 = CheckAppStatus(app);
|
|
_appRepository.Update(updatedApp);
|
|
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)
|
|
{
|
|
_notificationService.ShowBalloonTip(
|
|
10000,
|
|
"Updates Available",
|
|
string.Join("\n", updateMessages),
|
|
ToolTipIcon.Info
|
|
);
|
|
}
|
|
if (errorMessages.Count > 0)
|
|
{
|
|
_notificationService.ShowBalloonTip(
|
|
10000,
|
|
"Errors",
|
|
string.Join("\n", errorMessages),
|
|
ToolTipIcon.Error
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
public List<App> PurgeExisitingApps(List<App> apps)
|
|
{
|
|
var _existingApps = _stateService.GetApps();
|
|
if (_existingApps == null || _existingApps.Count == 0)
|
|
{
|
|
return apps;
|
|
}
|
|
return apps.Where(a => !_existingApps.Any(e => e.Id == a.Id)).ToList();
|
|
}
|
|
}
|
|
}
|