checkAndroidUpdate method

Future<bool> checkAndroidUpdate(
  1. Map<String, dynamic> androidData,
  2. PackageInfo packageInfo,
  3. bool allowSkip,
  4. BuildContext context,
  5. DownloadState downloadState,
  6. UpdateCenterConfig config,
  7. String downloadUrl,
)

Checks for an available update for Android platform and shows update dialog if necessary.

Implementation

Future<bool> checkAndroidUpdate(
  Map<String, dynamic> androidData,
  PackageInfo packageInfo,
  bool allowSkip,
  BuildContext context,
  DownloadState downloadState,
  UpdateCenterConfig config,
  String downloadUrl,
) async {
  AndroidModel model = AndroidModel(
    androidData['versionName'],
    androidData['downloadUrl'],
    androidData['changeLog'],
    androidData['sourceUrl'],
    androidData['versionCode'],
    androidData['minSupport'],
  );

  int buildNumber = int.parse(packageInfo.buildNumber);

  if (model.minSupport > buildNumber) {
    allowSkip = false; // Update is mandatory
  }

  // Check if the current build number is less than the update version code
  if (buildNumber < model.versionCode) {
    // Get the stored version info
    String? storedVersion = await MemoryProvider.getVersionInfoAndroid();

    // Compare with the new version
    if (storedVersion != null && storedVersion != model.versionName) {
      // Versions differ, delete the old file and stored version info
      MemoryProvider.deleteFileDirectory();
    }

    // Save the new version info
    await MemoryProvider.saveVersionInfoAndroid(model.versionName);

    if (context.mounted && config.globalConfig.androidDialogBuilder != null) {
      await config.globalConfig.androidDialogBuilder!(
        context,
        model, // Assume this is your AndroidModel instance with update info
        config,
        downloadState, // Your DownloadState instance
        allowSkip,
      );
    }

    downloadUrl = model.downloadUrl; // Set the download URL

    return true; // Return true to indicate that an update is available
  }

  // Returns your custom widget "No updates found"
  if (buildNumber >= model.versionCode) {
    if (config.globalConfig.isNoUpdateAvailableToast && config.globalConfig.androidNoUpdateAvailableBuilder != null) {
      config.globalConfig.androidNoUpdateAvailableBuilder?.call(context);
    }

    MemoryProvider.deleteFileDirectory(); // Deletes the old file if there is no update
    return false;
  }

  return false; // No update available
}