updateVersionInfo method

Future<bool> updateVersionInfo()

Implementation

Future<bool> updateVersionInfo() async {
  // If there is an appcast for this platform
  if (isAppcastThisPlatform()) {
    if (debugLogging) {
      if (kDebugMode) {
        print('hcUpgrade: appcast is available for this platform');
      }
    }

    final appcast = this.appcast ?? HcAppCast(client: client);
    await appcast.parseAppCastItemsFromUri(appCastConfig!.url!);
    if (debugLogging) {
      var count = appcast.items == null ? 0 : appcast.items!.length;
      if (kDebugMode) {
        print('hcUpgrade: appcast item count: $count');
      }
    }
    final criticalUpdateItem = appcast.bestCriticalItem();
    final criticalVersion = criticalUpdateItem?.versionString ?? '';

    final bestItem = appcast.bestItem();
    if (bestItem != null &&
        bestItem.versionString != null &&
        bestItem.versionString!.isNotEmpty) {
      if (debugLogging) {
        if (kDebugMode) {
          print(
            'hcUpgrade: appcast best item version: ${bestItem.versionString}');
          print(
              'hcUpgrade: appcast critical update item version: ${criticalUpdateItem?.versionString}');
        }
      }

      try {
        if (criticalVersion.isNotEmpty &&
            Version.parse(_installedVersion!) <
                Version.parse(criticalVersion)) {
          _isCriticalUpdate = true;
        }
      } catch (e) {
        if (kDebugMode) {
          print('hcUpgrade: updateVersionInfo could not parse version info $e');
        }
        _isCriticalUpdate = false;
      }

      _appStoreVersion = bestItem.versionString;
      _appStoreListingURL = bestItem.fileURL;
      _releaseNotes = bestItem.itemDescription;
    }
  } else {
    if (_packageInfo == null || _packageInfo!.packageName.isEmpty) {
      return false;
    }

    // The  country code of the locale, defaulting to `US`.
    final country = countryCode ?? findCountryCode();
    if (debugLogging) {
      if (kDebugMode) {
        print('hcUpgrade: countryCode: $country');
      }
    }

    // The  language code of the locale, defaulting to `en`.
    final language = languageCode ?? findLanguageCode();
    if (debugLogging) {
      if (kDebugMode) {
        print('hcUpgrade: languageCode: $language');
      }
    }

    // Get Android version from Google Play Store, or
    // get iOS version from iTunes Store.
    if (upgradeOS.isAndroid) {
      await getAndroidStoreVersion(country: country, language: language);
    } else if (upgradeOS.isIOS) {
      final iTunes = ITunesSearchAPI();
      iTunes.debugLogging = debugLogging;
      iTunes.client = client;
      final response = await (iTunes
          .lookupByBundleId(_packageInfo!.packageName, country: country));

      if (response != null) {
        _appStoreVersion = iTunes.version(response);
        _appStoreListingURL = iTunes.trackViewUrl(response);
        _releaseNotes ??= iTunes.releaseNotes(response);
        final mav = iTunes.minAppVersion(response);
        if (mav != null) {
          minAppVersion = mav.toString();
          if (debugLogging) {
            if (kDebugMode) {
              print('hcUpgrade: ITunesResults.minAppVersion: $minAppVersion');
            }
          }
        }
      }
    }
  }

  return true;
}