Files
kooltab/lib/utils/app_updater.dart
T
2026-07-30 04:46:03 +02:00

74 lines
1.9 KiB
Dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:kooltab2/utils/app_update_util.dart';
import 'app_config.dart';
class UpdateChecker {
static bool _hasChecked = false;
static Future<void> check(BuildContext context) async {
if (!Platform.isAndroid) return;
if (_hasChecked) {
debugPrint("Update already checked this boot");
return;
}
_hasChecked = true;
final updater = AppUpdateUtil(serverUrl: kUpdateServerUrl);
try {
final update = await updater.checkForUpdate();
if (update == null) {
debugPrint("No update available");
return;
}
debugPrint("Update available: ${update.version}");
if (!context.mounted) return;
_showUpdateDialog(context, update);
} catch (e) {
debugPrint("Update check failed: $e");
}
}
static void _showUpdateDialog(BuildContext context, UpdateInfo update) {
showDialog(
context: context,
barrierDismissible: !update.mandatory,
builder: (dialogContext) => AlertDialog(
title: const Text("Update available"),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Version ${update.version} is available."),
const SizedBox(height: 12),
Text(update.notes),
],
),
actions: [
if (!update.mandatory)
TextButton(
onPressed: () => Navigator.pop(dialogContext),
child: const Text("Later"),
),
FilledButton(
onPressed: () {
Navigator.pop(dialogContext);
context.push('/update-progress', extra: update);
},
child: const Text("Update"),
),
],
),
);
}
}