2026-01-17 13:09:20 +03:00
|
|
|
|
import 'dart:io';
|
2023-07-27 18:03:41 +03:30
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
import 'package:gap/gap.dart';
|
|
|
|
|
|
import 'package:go_router/go_router.dart';
|
2026-01-17 13:09:20 +03:00
|
|
|
|
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';
|
2023-07-27 18:03:41 +03:30
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2026-01-17 13:09:20 +03:00
|
|
|
|
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';
|
2023-07-27 18:03:41 +03:30
|
|
|
|
|
2023-10-04 18:06:48 +03:30
|
|
|
|
class NewVersionDialog extends HookConsumerWidget with PresLogger {
|
|
|
|
|
|
NewVersionDialog(
|
2023-07-27 18:03:41 +03:30
|
|
|
|
this.currentVersion,
|
|
|
|
|
|
this.newVersion, {
|
|
|
|
|
|
this.canIgnore = true,
|
2023-10-04 18:06:48 +03:30
|
|
|
|
}) : super(key: _dialogKey);
|
2023-07-27 18:03:41 +03:30
|
|
|
|
|
2023-09-12 15:22:58 +03:30
|
|
|
|
final String currentVersion;
|
2023-12-01 12:56:24 +03:30
|
|
|
|
final RemoteVersionEntity newVersion;
|
2023-07-27 18:03:41 +03:30
|
|
|
|
final bool canIgnore;
|
|
|
|
|
|
|
2023-10-04 18:06:48 +03:30
|
|
|
|
static final _dialogKey = GlobalKey(debugLabel: 'new version dialog');
|
|
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
}
|
2023-07-27 18:03:41 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
|
|
|
|
final t = ref.watch(translationsProvider);
|
|
|
|
|
|
final theme = Theme.of(context);
|
2026-01-17 13:09:20 +03:00
|
|
|
|
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)
|
|
|
|
|
|
fileExt = '.exe';
|
|
|
|
|
|
else if (Platform.isMacOS)
|
|
|
|
|
|
fileExt = '.dmg';
|
|
|
|
|
|
else if (Platform.isLinux) fileExt = '.AppImage';
|
|
|
|
|
|
|
|
|
|
|
|
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(newVersion.url, savePath, onReceiveProgress: (received, total) {
|
|
|
|
|
|
if (total != -1) downloadProgress.value = received / total;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
loggy.info('Update downloaded to: $savePath');
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-07-27 18:03:41 +03:30
|
|
|
|
|
|
|
|
|
|
return AlertDialog(
|
2023-09-07 01:56:59 +03:30
|
|
|
|
title: Text(t.appUpdate.dialogTitle),
|
2023-07-27 18:03:41 +03:30
|
|
|
|
content: Column(
|
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
|
children: [
|
|
|
|
|
|
Text(t.appUpdate.updateMsg),
|
|
|
|
|
|
const Gap(8),
|
2026-01-17 13:09:20 +03:00
|
|
|
|
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),
|
|
|
|
|
|
],
|
2023-07-27 18:03:41 +03:30
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
actions: [
|
2026-01-17 13:09:20 +03:00
|
|
|
|
if (canIgnore && !isDownloading.value)
|
2023-07-27 18:03:41 +03:30
|
|
|
|
TextButton(
|
2023-10-05 21:49:36 +03:30
|
|
|
|
onPressed: () async {
|
2026-01-17 13:09:20 +03:00
|
|
|
|
await ref.read(appUpdateNotifierProvider.notifier).ignoreRelease(newVersion);
|
2023-10-05 21:49:36 +03:30
|
|
|
|
if (context.mounted) context.pop();
|
2023-07-27 18:03:41 +03:30
|
|
|
|
},
|
2023-09-07 01:56:59 +03:30
|
|
|
|
child: Text(t.appUpdate.ignoreBtnTxt),
|
2023-07-27 18:03:41 +03:30
|
|
|
|
),
|
2026-01-17 13:09:20 +03:00
|
|
|
|
if (!isDownloading.value) TextButton(onPressed: context.pop, child: Text(t.appUpdate.laterBtnTxt)),
|
2023-07-27 18:03:41 +03:30
|
|
|
|
TextButton(
|
2026-01-17 13:09:20 +03:00
|
|
|
|
onPressed: isDownloading.value ? null : downloadAndInstallUpdate,
|
|
|
|
|
|
child: Text(isDownloading.value ? 'Скачивание...' : t.appUpdate.updateNowBtnTxt),
|
2023-07-27 18:03:41 +03:30
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|