feat: auto-updater
This commit is contained in:
@@ -11,11 +11,26 @@ import 'package:flutter_slidable/flutter_slidable.dart';
|
||||
import '../models/bar_tab.dart';
|
||||
import '../models/product.dart';
|
||||
import '../models/tab_item.dart';
|
||||
import '../utils/app_updater.dart';
|
||||
import '../viewmodels/bar_screen_view_model.dart';
|
||||
|
||||
class BarScreenView extends StatelessWidget {
|
||||
class BarScreenView extends StatefulWidget {
|
||||
const BarScreenView({super.key});
|
||||
|
||||
@override
|
||||
State<BarScreenView> createState() => _BarScreenViewState();
|
||||
}
|
||||
|
||||
class _BarScreenViewState extends State<BarScreenView> {
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
UpdateChecker.check(context);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final viewModel = context.watch<BarScreenViewModel>();
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'package:go_router/go_router.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../models/settings.dart';
|
||||
import '../utils/app_update_util.dart';
|
||||
import '../viewmodels/pin_lock_view_model.dart';
|
||||
import '../viewmodels/settings_view_model.dart';
|
||||
|
||||
@@ -213,12 +214,102 @@ class _SettingsScreenViewState extends State<SettingsScreenView> {
|
||||
),
|
||||
],
|
||||
),
|
||||
_SettingsSection(
|
||||
title: 'Updates',
|
||||
children: [
|
||||
_SettingsTile(
|
||||
icon: Icons.system_update_alt_rounded,
|
||||
title: settingsViewModel.checkingForUpdates
|
||||
? 'Checking for updates...'
|
||||
: 'Check for updates',
|
||||
onTap: settingsViewModel.checkingForUpdates
|
||||
? null
|
||||
: _checkForUpdates,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _checkForUpdates() async {
|
||||
final vm = context.read<SettingsViewModel>();
|
||||
|
||||
try {
|
||||
final update = await vm.checkForUpdates();
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
if (update == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('You are already on the latest version.'),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
_showUpdateDialog(update);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Update check failed: $e')),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _showUpdateDialog(UpdateInfo update) {
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: !update.mandatory,
|
||||
builder: (context) => 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);
|
||||
|
||||
try {
|
||||
await context.read<SettingsViewModel>().installUpdate(
|
||||
update,
|
||||
onProgress: (progress) {
|
||||
debugPrint(
|
||||
"Download ${(progress * 100).toStringAsFixed(0)}%",
|
||||
);
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("Update failed: $e")),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: const Text("Update"),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SettingsSection extends StatelessWidget {
|
||||
|
||||
Reference in New Issue
Block a user