Created
July 7, 2025 17:00
-
-
Save EsinShadrach/76c56ddab95edaaa384fb16e32a7aa84 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:shorebird_code_push/shorebird_code_push.dart'; | |
| import 'package:restart_app/restart_app.dart'; | |
| // uses the drops package [https://pub.dev/packages/drops] | |
| class Updater { | |
| final BuildContext context; | |
| final _updater = ShorebirdUpdater(); | |
| Updater({required this.context}) { | |
| _updater.readCurrentPatch().then((patch) { | |
| final currentPatch = patch?.number.toString() ?? "Not available"; | |
| debugPrint("CURRENT PATCH IS: $currentPatch"); | |
| }); | |
| } | |
| // check for updates | |
| Future<UpdateStatus> checkForUpdates() async { | |
| final status = await _updater.checkForUpdate(); | |
| return status; | |
| } | |
| Future<void> decideAction() async { | |
| final status = await checkForUpdates(); | |
| if (status == UpdateStatus.outdated) { | |
| onNeedUpdate(); | |
| } else if (status == UpdateStatus.restartRequired) { | |
| onNeedRestart(); | |
| } | |
| } | |
| /// update action | |
| Future<void> onNeedUpdate() async { | |
| Drops.show( | |
| context, | |
| title: "Update Available for download", | |
| icon: CupertinoIcons.download_circle_fill, | |
| iconColor: context.colorScheme.primary, | |
| position: DropPosition.bottom, | |
| trailingButtonText: "Download", | |
| trailingButtonFilled: true, | |
| trailingButtonAction: _update, | |
| ); | |
| } | |
| /// onNeedRestart | |
| Future<void> onNeedRestart() async { | |
| Drops.show( | |
| context, | |
| title: "Update Available for download", | |
| icon: CupertinoIcons.download_circle_fill, | |
| iconColor: context.colorScheme.primary, | |
| trailingButtonText: "Download", | |
| trailingButtonFilled: true, | |
| position: DropPosition.bottom, | |
| trailingButtonAction: () { | |
| _update(restart: true); | |
| }, | |
| ); | |
| } | |
| Future<void> _internalRestartViaPlatform() async { | |
| await Restart.restartApp( | |
| notificationTitle: 'Restarting App', | |
| notificationBody: 'Please tap here to open the app again.', | |
| ); | |
| } | |
| // update should start update, then show an undismiss drop till update is done | |
| Future<void> _update({bool restart = false}) async { | |
| Drops.hide(); | |
| Drops.show( | |
| context, | |
| title: "Downloading update...", | |
| iconWidget: _updatingIcon(), | |
| iconColor: context.colorScheme.primary, | |
| autoDismiss: false, | |
| ); | |
| try { | |
| // await _updater.update(); | |
| await Future.delayed(const Duration(seconds: 10)); | |
| Drops.hide(); | |
| if (restart && context.mounted) { | |
| Drops.show( | |
| context, | |
| title: "Update downloaded, restart now.", | |
| icon: CupertinoIcons.checkmark_circle_fill, | |
| iconColor: context.colorScheme.primary, | |
| trailingButtonText: "Restart", | |
| trailingButtonFilled: true, | |
| position: DropPosition.bottom, | |
| trailingButtonAction: _internalRestartViaPlatform, | |
| ); | |
| } | |
| } catch (error, stackTrace) { | |
| Drops.hide(); | |
| handleError( | |
| error: error, | |
| eventName: "code-push-update", | |
| stackTrace: stackTrace, | |
| ); | |
| } | |
| } | |
| Widget _updatingIcon() { | |
| return Stack( | |
| children: [ | |
| CircularProgressIndicator( | |
| strokeWidth: 2, | |
| ).withSizedBox(dimension: 24).centered, | |
| Positioned( | |
| top: 0, | |
| left: 0, | |
| child: Icon( | |
| size: 24, | |
| CupertinoIcons.download_circle_fill, | |
| color: context.colorScheme.primary, | |
| ), | |
| ), | |
| ], | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment