import 'package:json_annotation/json_annotation.dart'; import '../../json_lenient.dart'; import '../files_sharing/queries/share/share.dart'; part 'autocomplete_response.g.dart'; /// Maps an autocomplete result's `source` to the matching Nextcloud share type. /// Groups become [kShareTypeGroup]; everything else (users, and any unknown /// source) defaults to [kShareTypeUser]. int shareTypeFromSource(String? source) { if (source != null && source.startsWith('groups')) return kShareTypeGroup; return kShareTypeUser; } @JsonSerializable(explicitToJson: true) class AutocompleteResponse { @JsonKey(fromJson: _entriesFromJson) List data; AutocompleteResponse(this.data); factory AutocompleteResponse.fromJson(Map json) => _$AutocompleteResponseFromJson(json); Map toJson() => _$AutocompleteResponseToJson(this); } List _entriesFromJson(Object? raw) => decodeListLenient( raw, AutocompleteResponseObject.fromJson, what: 'autocomplete entry', ); /// The server sends a user-status object for users that have one and an empty /// string otherwise; only the status string itself is kept. String? _statusFromJson(Object? raw) => switch (raw) { Map status => status['status']?.toString(), String status when status.isNotEmpty => status, _ => null, }; @JsonSerializable() class AutocompleteResponseObject { String id; String label; String? icon; String? source; @JsonKey(fromJson: _statusFromJson) String? status; String? subline; String? shareWithDisplayNameUnique; AutocompleteResponseObject( this.id, this.label, this.icon, this.source, this.status, this.subline, this.shareWithDisplayNameUnique, ); factory AutocompleteResponseObject.fromJson(Map json) => _$AutocompleteResponseObjectFromJson(json); Map toJson() => _$AutocompleteResponseObjectToJson(this); }