checkForUpdate method

Future<UpdateInfo?> checkForUpdate({
  1. BuildContext? context,
  2. String? platform,
  3. String? currentVersion,
  4. bool showDialog = true,
  5. bool checkOnly = false,
})

Check for app updates and automatically show dialog if update is available

Implementation

Future<UpdateInfo?> checkForUpdate({
  BuildContext? context,
  String? platform,
  String? currentVersion,
  bool showDialog = true,
  bool checkOnly = false,
}) async {
  AppLogger.info(
    'Checking for app update (production: $useProduction, checkOnly: $checkOnly)',
    'AppHubUpgrader',
  );

  try {
    final updateInfo = await _versionChecker.checkForUpdate(
      platform: platform,
      currentVersion: currentVersion,
      useProduction: useProduction,
    );

    AppLogger.info(
      'Update check completed - updateAvailable: ${updateInfo.updateAvailable}, '
          'latestVersion: ${updateInfo.latestVersion}, '
          'isForcedUpdate: ${updateInfo.isForced}',
      'AppHubUpgrader',
    );

    if (checkOnly) {
      AppLogger.debug(
        'Check only mode - returning update info without showing dialog',
        'AppHubUpgrader',
      );
      return updateInfo;
    }

    if (updateInfo.updateAvailable && showDialog) {
      final dialogContext = _getValidContext(context);
      if (dialogContext != null && dialogContext.mounted) {
        AppLogger.info('Showing update dialog', 'AppHubUpgrader');
        await _showUpdateDialog(dialogContext, updateInfo);
      } else {
        AppLogger.warning(
          'Cannot show dialog - no valid context available. '
              'Please provide a BuildContext or GlobalKey for NavigatorState',
          'AppHubUpgrader',
        );
      }
    } else if (!updateInfo.updateAvailable) {
      AppLogger.info(
        'No update available - app is up to date',
        'AppHubUpgrader',
      );
    }

    return updateInfo;
  } catch (e, stackTrace) {
    AppLogger.error('Failed to check for update', e, 'AppHubUpgrader');
    AppLogger.debug('Stack trace: $stackTrace', 'AppHubUpgrader');
    return null;
  }
}