import 'dart:collection'; import 'dart:convert'; import 'package:aves/model/entry.dart'; import 'package:aves/theme/colors.dart'; import 'package:aves/utils/xmp_utils.dart'; import 'package:aves/widgets/common/identity/aves_expansion_tile.dart'; import 'package:aves/widgets/viewer/info/metadata/xmp_namespaces.dart'; import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class XmpDirTile extends StatefulWidget { final AvesEntry entry; final String title; final SplayTreeMap allTags, tags; final ValueNotifier? expandedNotifier; final bool initiallyExpanded; const XmpDirTile({ super.key, required this.entry, required this.title, required this.allTags, required this.tags, required this.expandedNotifier, required this.initiallyExpanded, }); @override State createState() => _XmpDirTileState(); } class _XmpDirTileState extends State { late final Map _schemaRegistryPrefixes, _tags; AvesEntry get entry => widget.entry; static const schemaRegistryPrefixesKey = 'schemaRegistryPrefixes'; @override void initState() { super.initState(); _tags = Map.from(widget.tags)..remove(schemaRegistryPrefixesKey); final prefixesJson = widget.allTags[schemaRegistryPrefixesKey]; final Map prefixesDecoded = prefixesJson != null ? jsonDecode(prefixesJson) : {}; _schemaRegistryPrefixes = Map.fromEntries(prefixesDecoded.entries.map((kv) => MapEntry(kv.key, kv.value as String))); } @override Widget build(BuildContext context) { final sections = groupBy, String>(_tags.entries, (kv) { final fullKey = kv.key; final i = fullKey.indexOf(XMP.propNamespaceSeparator); final nsPrefix = i == -1 ? '' : fullKey.substring(0, i + 1); return nsPrefix; }).entries.map((kv) { final nsPrefix = kv.key; final nsUri = _schemaRegistryPrefixes[nsPrefix] ?? ''; final rawProps = Map.fromEntries(kv.value); return XmpNamespace.create(nsUri, nsPrefix, rawProps); }).toList() ..sort((a, b) => compareAsciiUpperCase(a.displayTitle, b.displayTitle)); return AvesExpansionTile( // title may contain parent to distinguish multiple XMP directories title: widget.title, highlightColor: context.select((v) => v.xmp), expandedNotifier: widget.expandedNotifier, initiallyExpanded: widget.initiallyExpanded, children: [ Padding( padding: const EdgeInsets.only(left: 8, right: 8, bottom: 8), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: sections.expand((section) => section.buildNamespaceSection(context)).toList(), ), ), ], ); } }