elfisa-pharmacy/src/ElectronicPharmacy/HelpForms/HF_UpdateAvailable.cs
Exest 3039c324be Add in-app auto-updater that preserves local database.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-31 13:44:44 +03:00

101 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using Электроннаяармация.Classes;
namespace Электроннаяармация.HelpForms
{
public partial class HF_UpdateAvailable : Form
{
private readonly UpdateCheckResult _result;
private bool _updating;
public HF_UpdateAvailable(UpdateCheckResult result)
{
_result = result ?? throw new ArgumentNullException(nameof(result));
InitializeComponent();
UiThemeHelper.ApplyToControlTree(this);
var latest = string.IsNullOrWhiteSpace(_result.LatestVersion) ? "?" : _result.LatestVersion;
var local = string.IsNullOrWhiteSpace(_result.LocalVersion) ? "?" : _result.LocalVersion;
lblMessage.Text =
$"Доступно новое обновление {latest} (у вас {local}).{Environment.NewLine}{Environment.NewLine}" +
"Нажмите «Обновить» — программа скачает и установит новую версию сама." + Environment.NewLine +
"Локальная база и данные сохранятся.";
btnDownload.Text = "Обновить";
}
private async void btnDownload_Click(object sender, EventArgs e)
{
if (_updating)
{
return;
}
if (string.IsNullOrWhiteSpace(_result.DownloadUrl) &&
string.IsNullOrWhiteSpace(_result.ReleaseUrl))
{
UiDialogs.ShowError("Ссылка на обновление не задана.", "Обновление", this);
return;
}
_updating = true;
btnDownload.Enabled = false;
btnExit.Enabled = false;
ControlBox = false;
var progress = new Progress<string>(msg =>
{
lblMessage.Text = msg;
});
try
{
await AutoUpdater.ApplyUpdateAsync(_result, progress);
lblMessage.Text = "Обновление скачано. Перезапуск...";
DialogResult = DialogResult.OK;
Application.Exit();
}
catch (Exception ex)
{
AppDebugLog.Error("Update", "Автообновление не удалось", ex);
_updating = false;
btnDownload.Enabled = true;
btnExit.Enabled = true;
ControlBox = true;
UiDialogs.ShowError(
"Не удалось установить обновление автоматически." + Environment.NewLine +
ex.Message,
"Обновление",
this);
}
}
private void btnExit_Click(object sender, EventArgs e)
{
if (_updating)
{
return;
}
DialogResult = DialogResult.Abort;
Close();
}
private void HF_UpdateAvailable_FormClosing(object sender, FormClosingEventArgs e)
{
if (_updating)
{
e.Cancel = true;
return;
}
// Крестик / Alt+F4 — выход из приложения (не пускать в работу).
if (DialogResult != DialogResult.Abort && DialogResult != DialogResult.OK)
{
DialogResult = DialogResult.Abort;
}
}
}
}