check method

Future<bool> check({
  1. dynamic withDialog = true,
})

Function to check for update

Implementation

Future<bool> check({withDialog = true}) async {
  if (!_isAndroid) return false;

  if (delay != null) await Future.delayed(delay!);

  controller?.setValue(UpdateStatus.Checking);

  Response response = await APITask().get(url);
  dynamic data = response.data;

  UpdateModel model = UpdateModel(
    data['url'],
    data['versionName'],
    data['versionCode'],
    data['minSupport'],
    data['contentText'],
  );

  ///Throw exception if provided download url is not valid
  if (!model.downloadUrl.contains('http')) {
    throw Exception(
        'Invalid download URL.\nThe Download URL should contain http / https.');
  }

  ///Update value in callback function
  if (callBack != null) callBack!(model);

  ///Set server value to `contentText` if `contentText` value is empty
  if (contentText.isEmpty) contentText = model.contentText;

  PackageInfo packageInfo = await PackageInfo.fromPlatform();
  int buildNumber = int.parse(packageInfo.buildNumber);

  ///Return `false` if current build number is greater
  ///than the update version.
  if (buildNumber >= model.versionCode) {
    _updateAvailable = false;
    return false;
  }

  ///Override the `allowSkip` to `false`
  /// if minimum supported version is greater than the current build number
  if (model.minSupport > buildNumber) {
    allowSkip = false;
  }

  _updateAvailable = true;

  controller?.setValue(UpdateStatus.Available);

  _downloadUrl = model.downloadUrl;

  if (withDialog) {
    Future.delayed(Duration.zero).then((value) {
      showDialog(
          context: context,
          barrierDismissible: allowSkip,
          builder: (_) {
            return _buildDialog;
          }).then((value) {
        if (value == null) {
          controller?.setValue(UpdateStatus.DialogDismissed);
        }
      });
    });
  }

  return true; // update is available
}