Make manual shorebird update actually work

Restarting just the activity doesn't seem to work.
To work around this, I finished the process and started the activity via alarm manager.
This is inspired from ProcessPhoenix
This commit is contained in:
Gergely Hegedus 2025-01-17 21:54:22 +02:00
parent 2519513638
commit 20c693004a
5 changed files with 123 additions and 28 deletions

View file

@ -1,15 +1,16 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shorebird_code_push/shorebird_code_push.dart';
import 'package:restart_app/restart_app.dart';
void main() {
runApp(const MyApp());
}
class ShorebirdPatch extends StatefulWidget {
const ShorebirdPatch({super.key});
@override
State<StatefulWidget> createState() => _ShorebirdPatchState();
}
@ -20,39 +21,65 @@ enum PatchState {
updateReady;
}
enum MainChannelMethods {
restart(methodName: "restart");
final String methodName;
const MainChannelMethods({required this.methodName});
}
class _ShorebirdPatchState extends State<ShorebirdPatch> {
final shorebird = ShorebirdCodePush();
late Timer timer;
PatchState patchState = PatchState.noUpdate;
final methodChannel = const MethodChannel("MainChannel");
@override
void initState() {
timer = Timer.periodic(const Duration(minutes: 5), (timer) async {
if (!shorebird.isShorebirdAvailable()) {
timer.cancel();
return;
}
if (!await shorebird.isNewPatchAvailableForDownload()) {
setState(() {
patchState = PatchState.noUpdate;
});
return;
}
final download = shorebird.downloadUpdateIfAvailable();
setState(() {
patchState = PatchState.loading;
});
await download;
if (await shorebird.isNewPatchReadyToInstall()) {
setState(() {
patchState = PatchState.updateReady;
});
return;
}
timer = Timer.periodic(const Duration(minutes: 1), (timer) async {
checkForPatch(timer);
});
checkForPatch(timer);
super.initState();
}
Future<void> checkForPatch(Timer timer) async {
print('timer activated');
if (await shorebird.isNewPatchReadyToInstall()) {
print('timer await shorebird.isNewPatchReadyToInstall()');
setState(() {
patchState = PatchState.updateReady;
});
return;
}
if (!shorebird.isShorebirdAvailable()) {
print('timer !shorebird.isShorebirdAvailable()');
timer.cancel();
return;
}
if (!await shorebird.isNewPatchAvailableForDownload()) {
print('timer !await shorebird.isNewPatchAvailableForDownload()');
setState(() {
patchState = PatchState.noUpdate;
});
return;
}
final download = shorebird.downloadUpdateIfAvailable();
setState(() {
patchState = PatchState.loading;
});
await download;
if (await shorebird.isNewPatchReadyToInstall()) {
print('timer await shorebird.isNewPatchReadyToInstall()');
setState(() {
patchState = PatchState.updateReady;
});
return;
}
print('timer end');
}
@override
void dispose() {
timer.cancel();
@ -70,7 +97,7 @@ class _ShorebirdPatchState extends State<ShorebirdPatch> {
return InkWell(
child: const SizedBox.square(dimension: 48, child: Icon(Icons.update, size: 24)),
onTap: () {
Restart.restartApp();
methodChannel.invokeMethod(MainChannelMethods.restart.methodName);
},
);
}
@ -117,12 +144,16 @@ class _MyHomePageState extends State<MyHomePage> {
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
actions: [ShorebirdPatch()],
actions: const [ShorebirdPatch()],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'This is a release 1.9.1',
// 'You have times:',
),
const Text(
'You have pushed the button this many times:',
// 'You have times:',