showPortalUpdateDialog function

Future<bool> showPortalUpdateDialog(
  1. BuildContext context,
  2. PortalUpdateService service,
  3. PortalRelease release, {
  4. String? title,
  5. bool rememberRefusal = true,
})

Offers release, then downloads and installs it if the user accepts.

Returns true when the installer was launched. Android then shows its own confirmation — nothing here installs anything silently.

Implementation

Future<bool> showPortalUpdateDialog(
  BuildContext context,
  PortalUpdateService service,
  PortalRelease release, {
  String? title,
  bool rememberRefusal = true,
}) async {
  final accepted = await showDialog<bool>(
    context: context,
    builder: (context) => AlertDialog(
      title: Text(title ?? 'Update to ${release.versionName}'),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (release.notes != null) ...[
            Text(release.notes!),
            const SizedBox(height: 12),
          ],
          Text(
            release.readableSize.isEmpty
                ? 'Downloads the new version, then opens the installer.'
                : 'Downloads ${release.readableSize}, then opens the '
                    'installer.',
            style: Theme.of(context).textTheme.bodySmall,
          ),
        ],
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.of(context).pop(false),
          child: const Text('Not now'),
        ),
        FilledButton(
          onPressed: () => Navigator.of(context).pop(true),
          child: const Text('Download'),
        ),
      ],
    ),
  );

  if (accepted != true) {
    // Remember the refusal so the launch check does not re-ask every start.
    // The manual tile ignores this, and a newer publish clears it by having a
    // higher versionCode. Off when the release is a *different* app: the key
    // is per-package, so recording someone else's versionCode there would
    // silence this app's own update prompt.
    if (rememberRefusal) await service.markDismissed(release.versionCode);
    return false;
  }
  if (!context.mounted) return false;

  String? error;
  try {
    await service.install(release);
  } on PortalUpdateException catch (e) {
    error = e.message;
  } catch (e) {
    error = 'Update failed: $e';
  }

  if (!context.mounted) return error == null;
  // The browser has the download now; the install sheet appears when the
  // user opens it, so the one thing left to say is where to look.
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(
      content: Text(
        error ?? 'Downloading in your browser. Open it when it finishes to install.',
      ),
    ),
  );
  return error == null;
}