Files
kooltab/lib/app/app.dart
T

128 lines
2.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:kooltab2/theme.dart';
import 'package:kooltab2/utils/app_update_util.dart';
class KoolTabApp extends StatefulWidget {
const KoolTabApp({super.key, required this.router});
final GoRouter router;
@override
State<KoolTabApp> createState() => _KoolTabAppState();
}
class _KoolTabAppState extends State<KoolTabApp> {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
_checkForUpdates();
});
}
Future<void> _checkForUpdates() async {
final updater = AppUpdateUtil(serverUrl: "http://localhost:3000");
try {
final update = await updater.checkForUpdate();
if (update == null) {
debugPrint("No update available");
return;
}
debugPrint("Update available: ${update.version}");
if (!mounted) return;
_showUpdateDialog(update);
} catch (e) {
debugPrint("Update check failed: $e");
}
}
void _showUpdateDialog(UpdateInfo update) {
showDialog(
context: context,
barrierDismissible: !update.mandatory,
builder: (context) {
return 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(context);
},
child: const Text("Later"),
),
FilledButton(
onPressed: () async {
Navigator.pop(context);
await _downloadAndInstall(update);
},
child: const Text("Update"),
),
],
);
},
);
}
Future<void> _downloadAndInstall(UpdateInfo update) async {
final updater = AppUpdateUtil(
serverUrl: "http://localhost:3000",
);
try {
final apk = await updater.downloadApk(
update,
onProgress: (progress) {
debugPrint(
"Download ${(progress * 100).toStringAsFixed(0)}%",
);
},
);
final valid = await updater.verifySha256(
apk,
update.sha256,
);
if (!valid) {
throw Exception("Invalid update file");
}
await updater.installApk(apk);
} catch (e) {
debugPrint("Update failed: $e");
}
}
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'KoolTab',
debugShowCheckedModeBanner: false,
routerConfig: widget.router,
theme: darkTheme,
);
}
}