Files
umbrix/lib/features/app_update/widget/new_version_dialog.dart
Umbrix Developer 597d9f59ae
Some checks failed
CI / run (push) Has been cancelled
feat: Add portable ZIP update for Windows
Two update methods now available:
1. EXE installer (requires UAC, shows SmartScreen without signature)

Changes:
- Added .zip asset detection with priority (portable/windows/win)
- ZIP auto-install: extract → replace files → restart
- Priority: .zip → .exe (portable first for better UX)
- Batch script handles file replacement after app closes
- No administrator rights needed for ZIP updates

Benefits of ZIP:
 No UAC prompts
 No SmartScreen warnings
 Fast updates (just file replacement)
 Perfect for testing without code signing
 Fallback to .exe if .zip not available
2026-01-20 10:55:14 +03:00

311 lines
12 KiB
Dart
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.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:go_router/go_router.dart';
import 'package:umbrix/core/localization/translations.dart';
import 'package:umbrix/features/app_update/model/remote_version_entity.dart';
import 'package:umbrix/features/app_update/notifier/app_update_notifier.dart';
import 'package:umbrix/utils/utils.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:dio/dio.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:open_file/open_file.dart';
class NewVersionDialog extends HookConsumerWidget with PresLogger {
NewVersionDialog(
this.currentVersion,
this.newVersion, {
this.canIgnore = true,
}) : super(key: _dialogKey);
final String currentVersion;
final RemoteVersionEntity newVersion;
final bool canIgnore;
static final _dialogKey = GlobalKey(debugLabel: 'new version dialog');
/// Перезапуск приложения (кроссплатформенный метод)
void _restartApplication() {
final executable = Platform.resolvedExecutable;
loggy.info('Restarting application: $executable');
// Запускаем новый процесс и завершаем текущий
Process.start(executable, []).then((_) {
exit(0);
}).catchError((e) {
loggy.error('Failed to restart application', e);
});
}
Future<void> show(BuildContext context) async {
if (_dialogKey.currentContext == null) {
return showDialog(
context: context,
builder: (context) => this,
);
} else {
loggy.warning("new version dialog is already open");
}
}
@override
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);
final theme = Theme.of(context);
final isDownloading = useState(false);
final downloadProgress = useState(0.0);
Future<void> downloadAndInstallUpdate() async {
// Для Android - просто открываем браузер (в production - ссылка на Google Play)
if (Platform.isAndroid) {
await UriUtils.tryLaunch(Uri.parse(newVersion.url));
if (context.mounted) context.pop();
return;
}
// Для Desktop (Windows/macOS/Linux) - скачиваем с прогресс-баром
try {
isDownloading.value = true;
downloadProgress.value = 0.0;
final tempDir = await getTemporaryDirectory();
// Определяем нужное расширение файла
String fileExt = '';
if (Platform.isWindows) {
// Для Windows приоритет: .zip (portable) → .exe (installer)
// ZIP не требует UAC и подписи кода!
fileExt = '.zip';
final zipUrl = newVersion.findAssetByExtension('.zip');
if (zipUrl == null) {
fileExt = '.exe'; // Fallback на .exe если нет .zip
}
} else if (Platform.isMacOS)
fileExt = '.dmg';
else if (Platform.isLinux) fileExt = '.deb';
// Ищем asset с нужным расширением
final downloadUrl = newVersion.findAssetByExtension(fileExt);
if (downloadUrl == null) {
if (context.mounted) {
CustomToast.error('Файл установки $fileExt не найден в релизе').show(context);
}
return;
}
final savePath = '${tempDir.path}/umbrix-${newVersion.version}$fileExt';
final file = File(savePath);
if (await file.exists()) await file.delete();
final dio = Dio();
await dio.download(downloadUrl, savePath, onReceiveProgress: (received, total) {
if (total != -1) downloadProgress.value = received / total;
});
loggy.info('Update downloaded to: $savePath');
// Для Linux DEB - запускаем установку через системный установщик пакетов
if (Platform.isLinux && fileExt == '.deb') {
try {
// Пытаемся установить через pkexec apt install
final result = await Process.run('pkexec', ['apt', 'install', '-y', savePath]);
if (result.exitCode == 0) {
if (context.mounted) {
CustomToast.success('Обновление установлено! Приложение перезагрузится...').show(context);
context.pop();
}
// Автоматический перезапуск после небольшой задержки
Future.delayed(const Duration(seconds: 2), () {
_restartApplication();
});
return;
}
} catch (e) {
loggy.warning('Failed to install via pkexec apt: $e');
}
}
// Для Windows EXE - запускаем тихую установку
if (Platform.isWindows && fileExt == '.exe') {
try {
if (context.mounted) {
CustomToast('Установка обновления...', type: AlertType.info).show(context);
}
// Запускаем установщик в тихом режиме с правами администратора
// /VERYSILENT - без UI, /SUPPRESSMSGBOXES - без диалогов
// /NORESTART - не перезагружать систему
final result = await Process.run(
'powershell',
[
'-Command',
'Start-Process',
'-FilePath',
'"$savePath"',
'-ArgumentList',
'"/VERYSILENT", "/SUPPRESSMSGBOXES", "/NORESTART"',
'-Verb',
'RunAs',
'-Wait'
],
);
if (result.exitCode == 0) {
if (context.mounted) {
CustomToast.success('Обновление установлено! Приложение перезагрузится...').show(context);
context.pop();
}
// Автоматический перезапуск после небольшой задержки
Future.delayed(const Duration(seconds: 2), () {
_restartApplication();
});
return;
}
} catch (e) {
loggy.warning('Failed to install via PowerShell: $e');
}
}
// Windows portable ZIP update
if (Platform.isWindows && fileExt == '.zip') {
try {
if (context.mounted) {
CustomToast('Установка обновления из ZIP...', type: AlertType.info).show(context);
}
// Получить путь к исполняемому файлу приложения
final exePath = Platform.resolvedExecutable;
final appDir = Directory(exePath).parent.path;
// Распаковать во временную папку
final tempDir = Directory('${Directory.systemTemp.path}\\umbrix_update_${DateTime.now().millisecondsSinceEpoch}');
await tempDir.create(recursive: true);
loggy.info('Extracting ZIP to: ${tempDir.path}');
// Распаковка через PowerShell
final extractResult = await Process.run(
'powershell',
[
'-Command',
'Expand-Archive',
'-Path',
'"$savePath"',
'-DestinationPath',
'"${tempDir.path}"',
'-Force'
],
);
if (extractResult.exitCode != 0) {
throw Exception('Failed to extract ZIP: ${extractResult.stderr}');
}
loggy.info('ZIP extracted successfully');
// Скрипт для замены файлов после закрытия приложения
final updateScript = '''
@echo off
echo Waiting for application to close...
timeout /t 3 /nobreak > nul
echo Updating files...
xcopy /E /Y "${tempDir.path}\\*" "$appDir\\"
echo Cleanup...
rmdir /S /Q "${tempDir.path}"
echo Starting application...
start "" "$exePath"
echo Update complete!
del "%~f0"
''';
final scriptPath = '${Directory.systemTemp.path}\\umbrix_update.bat';
await File(scriptPath).writeAsString(updateScript);
if (context.mounted) {
CustomToast.success('Обновление установлено! Приложение перезагрузится...').show(context);
context.pop();
}
// Запустить скрипт и закрыть приложение
await Process.start('cmd', ['/c', scriptPath], mode: ProcessStartMode.detached);
// Задержка перед выходом
Future.delayed(const Duration(seconds: 1), () {
exit(0);
});
return;
} catch (e) {
loggy.warning('Failed to install from ZIP: $e');
}
}
// Для других платформ или если автоустановка не сработала - просто открываем файл
final result = await OpenFile.open(savePath);
if (result.type != ResultType.done && context.mounted) {
CustomToast.error('Не удалось открыть: ${result.message}').show(context);
} else if (context.mounted) {
context.pop();
}
} catch (e, st) {
loggy.error('Download failed', e, st);
if (context.mounted) CustomToast.error('Ошибка: $e').show(context);
} finally {
isDownloading.value = false;
}
}
return AlertDialog(
title: Text(t.appUpdate.dialogTitle),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(t.appUpdate.updateMsg),
const Gap(8),
Text.rich(TextSpan(children: [
TextSpan(text: "${t.appUpdate.currentVersionLbl}: ", style: theme.textTheme.bodySmall),
TextSpan(text: currentVersion, style: theme.textTheme.labelMedium),
])),
Text.rich(TextSpan(children: [
TextSpan(text: "${t.appUpdate.newVersionLbl}: ", style: theme.textTheme.bodySmall),
TextSpan(text: newVersion.presentVersion, style: theme.textTheme.labelMedium),
])),
if (isDownloading.value) ...[
const Gap(16),
LinearProgressIndicator(value: downloadProgress.value),
const Gap(8),
Text('Скачивание: ${(downloadProgress.value * 100).toStringAsFixed(0)}%', style: theme.textTheme.bodySmall),
],
],
),
actions: [
if (canIgnore && !isDownloading.value)
TextButton(
onPressed: () async {
await ref.read(appUpdateNotifierProvider.notifier).ignoreRelease(newVersion);
if (context.mounted) context.pop();
},
child: Text(t.appUpdate.ignoreBtnTxt),
),
if (!isDownloading.value) TextButton(onPressed: context.pop, child: Text(t.appUpdate.laterBtnTxt)),
TextButton(
onPressed: isDownloading.value ? null : downloadAndInstallUpdate,
child: Text(isDownloading.value ? 'Скачивание...' : t.appUpdate.updateNowBtnTxt),
),
],
);
}
}