getStoragePermission static method

Future<bool> getStoragePermission({
  1. required BuildContext context,
  2. String? permissionContent,
  3. String? deniedContent,
})

Implementation

static Future<bool> getStoragePermission(
    {required BuildContext context,
    String? permissionContent,
    String? deniedContent}) async {
  // var info = await PackageInfo.fromPlatform();
  var sdkVersion = 0;
  if (Platform.isAndroid) {
    var sdk = await DeviceInfoPlugin().androidInfo;
    sdkVersion = sdk.version.sdkInt;
  } else {
    sdkVersion = 0;
  }

  if (sdkVersion < 33 && Platform.isAndroid) {
    final permission = await Permission.storage.status;
    if (permission != PermissionStatus.granted &&
        permission != PermissionStatus.permanentlyDenied) {
      const newPermission = Permission.storage;
      if (context.mounted) {
        var deniedPopupValue = await mirrorFlyPermissionDialog(
            context: context,
            icon: filePermission,
            content: AppConstants.filePermission,
            appName: AppConstants.appName);
        if (deniedPopupValue) {
          // return await newPermission.request().isGranted;
          var newp = await newPermission.request();
          if (newp.isGranted) {
            return true;
          } else {
            if (!context.mounted) return false;
            var popupValue = await customPermissionDialog(
                icon: filePermission,
                content:
                    deniedContent ?? getPermissionAlertMessage("storage"),
                context: context,
                appName: AppConstants.appName);
            if (popupValue) {
              openAppSettings();
              return false;
            } else {
              return false;
            }
          }
        } else {
          return newPermission.status.isGranted;
        }
      }
      return false;
    } else {
      return permission.isGranted;
    }
  } else if (Platform.isIOS) {
    final photos = await Permission.photos.status;
    final storage = await Permission.storage.status;

    const newPermission = [
      Permission.photos,
      Permission.storage,
      // Permission.audio
    ];
    if ((photos != PermissionStatus.granted &&
            photos != PermissionStatus.permanentlyDenied) ||
        (storage != PermissionStatus.granted &&
            storage != PermissionStatus.permanentlyDenied)) {
      mirrorFlyLog("showing mirrorfly popup", "");
      if (!context.mounted) return false;
      var deniedPopupValue = await mirrorFlyPermissionDialog(
          icon: filePermission,
          content: permissionContent ?? Constants.filePermission,
          context: context,
          appName: AppConstants.appName);
      if (deniedPopupValue) {
        var newp = await newPermission.request();
        PermissionStatus? photo = newp[Permission.photos];
        PermissionStatus? storage = newp[Permission.storage];
        // var audio = await newPermission[2].isGranted;
        if (photo!.isGranted && storage!.isGranted) {
          return true;
        } else if (photo.isPermanentlyDenied ||
            storage!.isPermanentlyDenied) {
          if (!context.mounted) return false;
          var popupValue = await customPermissionDialog(
              icon: filePermission,
              content: deniedContent ?? getPermissionAlertMessage("storage"),
              context: context,
              appName: AppConstants.appName);
          if (popupValue) {
            openAppSettings();
            return false;
          } else {
            return false;
          }
        } else {
          return false;
        }
      } else {
        return false; //PermissionStatus.denied;
      }
    } else {
      mirrorFlyLog("showing mirrorfly popup",
          "${photos.isGranted} ${storage.isGranted}");
      return (photos.isGranted && storage.isGranted);
      // ? photos
      // : photos;
    }
  } else {
    if (context.mounted) {
      return getAndroid13Permission(context);
    } else {
      return false;
    }
  }
}