parse static method

DVFileStorageConfig parse(
  1. Object? raw
)

Reads dartvel.fileStorage. A missing section is the default: the application's own directory and nothing else.

Implementation

static DVFileStorageConfig parse(Object? raw) {
  if (raw == null) return const DVFileStorageConfig();
  if (raw is! Map) {
    return const DVFileStorageConfig(problems: <String>[
      'dartvel.fileStorage must be a map with access, reason and shareAppFiles.',
    ]);
  }
  final List<String> problems = <String>[];
  for (final Object? key in raw.keys) {
    if (!const <String>{'access', 'reason', 'shareAppFiles'}.contains('$key')) {
      problems.add('dartvel.fileStorage.$key is not a setting; the settings are access, reason and shareAppFiles.');
    }
  }
  final List<DVDeviceFileAccess> access = <DVDeviceFileAccess>[];
  final Object? listed = raw['access'];
  if (listed != null && listed is! List) {
    problems.add('dartvel.fileStorage.access must be a list, for example [photos, documents].');
  }
  for (final Object? entry in listed is List ? listed : const <Object?>[]) {
    final DVDeviceFileAccess? kind = DVDeviceFileAccess.fromKey('$entry'.trim());
    if (kind == null) {
      problems.add('dartvel.fileStorage.access has "$entry"; the kinds are '
          '${DVDeviceFileAccess.values.map((DVDeviceFileAccess value) => value.key).join(', ')}.');
    } else if (!access.contains(kind)) {
      access.add(kind);
    }
  }
  final Object? reason = raw['reason'];
  if (reason != null && (reason is! String || reason.trim().isEmpty)) {
    problems.add('dartvel.fileStorage.reason must be a sentence saying why the app needs these files.');
  }
  final bool needsReason = access.any((DVDeviceFileAccess kind) =>
      kind == DVDeviceFileAccess.photos || kind == DVDeviceFileAccess.media);
  if (needsReason && (reason is! String || reason.trim().isEmpty)) {
    problems.add('dartvel.fileStorage.reason is required with photos or media: iOS and macOS show it when they ask, and refuse the request without it.');
  }
  final Object? share = raw['shareAppFiles'];
  if (share != null && share is! bool) {
    problems.add('dartvel.fileStorage.shareAppFiles must be true or false.');
  }
  return DVFileStorageConfig(
    access: List<DVDeviceFileAccess>.unmodifiable(access),
    reason: reason is String && reason.trim().isNotEmpty ? reason.trim() : null,
    shareAppFiles: share == true,
    problems: List<String>.unmodifiable(problems),
  );
}