import 'package:flutter/widgets.dart'; import 'package:flutter_app_update/flutter_app_update.dart'; import 'package:flutter_app_update/result_model.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; final updateProgressProvider = StateProvider((_) => null); final updateUrlProvider = StateProvider((_) => null); final updateDialogVisibleProvider = StateProvider((_) => false); bool _listenerRegistered = false; /// The native download callback can outlive the update dialog, so it uses the /// app's provider container instead of retaining a dialog WidgetRef. void ensureUpdateProgressListener(BuildContext context) { if (_listenerRegistered) return; _listenerRegistered = true; final container = ProviderScope.containerOf(context, listen: false); AzhonAppUpdate.listener((result) => applyUpdateProgress(container, result)); } @visibleForTesting void applyUpdateProgress(ProviderContainer container, ResultModel result) { switch (result.type) { case ResultType.downloading: if (result.progress == null || result.max == null || result.max == 0) { container.read(updateProgressProvider.notifier).state = null; break; } container.read(updateProgressProvider.notifier).state = result.progress!.toDouble() / result.max!.toDouble(); break; case ResultType.done: container.read(updateProgressProvider.notifier).state = 1; container.read(updateUrlProvider.notifier).state = result.apk; break; case ResultType.error: case ResultType.cancel: container.read(updateProgressProvider.notifier).state = null; break; default: break; } }