diff --git a/bin/run.dart b/bin/run.dart index 92ba9fd..7c6c29c 100644 --- a/bin/run.dart +++ b/bin/run.dart @@ -10,7 +10,7 @@ final encoder = JsonEncoder.withIndent('\t'); final currentRawSiteFile = File('rawsite.current.json'); const dropBoxFile = '/site.v$dataVersion.json.gz'; -const isDebug = false; +const isDebug = true; const sourceUrl = isDebug ? 'http://localhost/' : 'https://insidechassidus.org/'; diff --git a/lib/models.dart b/lib/models.dart index 59b99e1..c00d031 100644 --- a/lib/models.dart +++ b/lib/models.dart @@ -9,9 +9,11 @@ export 'site.dart'; part 'models.g.dart'; /// Basic site data which is common to all particular site data items. -class SiteDataItem { +class SiteDataItem implements SectionReference { @HiveField(0) int id; + + @override @HiveField(1) int parentId; @HiveField(2) @@ -19,9 +21,14 @@ class SiteDataItem { @HiveField(3) String description; - /// This is silly... Returns the ID of closest parent. Which is always [parentId], - /// except for with [Media]... - int get closestSectionId => parentId; + @override + SiteDataItem parent; + + @override + Section section; + + @override + int get sectionId => parentId; @override bool operator ==(Object other) { @@ -42,15 +49,31 @@ abstract class CountableSiteDataItem implements SiteDataItem { } // A class which contains a reference to a section. -abstract class SectionReference { +abstract class SectionReference implements ParentReference { int get sectionId; Section section; + + @override + int get parentId => sectionId; + @override + SiteDataItem get parent => section; + @override + set parent(SiteDataItem value) => section = value; } -@HiveType(typeId: 1) -@JsonSerializable() +extension SectionReferenceExtensions on SectionReference { + bool get hasSection => this != null && (sectionId ?? 0) > 0; + bool get hasParent => this != null && (parentId ?? 0) > 0; +} + +abstract class ParentReference { + int get parentId; + SiteDataItem parent; +} /// One section. A section contains any amount of media or child sections. +@HiveType(typeId: 1) +@JsonSerializable() class Section extends SiteDataItem implements CountableSiteDataItem { @override @HiveField(4) @@ -148,7 +171,7 @@ class Section extends SiteDataItem implements CountableSiteDataItem { /// It is an error to provide two data points to one [SectionContent]. @JsonSerializable() @HiveType(typeId: 2) -class SectionContent implements SectionReference { +class SectionContent extends SectionReference { @HiveField(0) @override final int sectionId; @@ -193,14 +216,11 @@ class Media extends SiteDataItem { @HiveField(6) final int order; - /// In a [MediaSection], [parentId] is the ID of that [MediaSection] and [parentSectionId] - /// is the ID of the [Section] in which the [MediaSection] resides. This property simplifies - /// navigation from [Media] to [Section] + /// In a [MediaSection], [parentId] is the ID of that [MediaSection] and [sectionId] + /// is the ID of the [Section] in which the [MediaSection] resides. @HiveField(7) - final int parentSectionId; - @override - int get closestSectionId => parentSectionId ?? parentId; + final int sectionId; Media({ int id, @@ -209,7 +229,7 @@ class Media extends SiteDataItem { String description, this.source, this.order, - this.parentSectionId, + this.sectionId, Duration length, }) : _length = length?.inMilliseconds ?? 0, super( @@ -235,10 +255,7 @@ class Media extends SiteDataItem { } Media copyWith( - {Duration length, - int parentId, - String source, - int parentSectionId}) => + {Duration length, int parentId, String source, int sectionId}) => Media( description: description, length: length ?? this.length, @@ -246,7 +263,7 @@ class Media extends SiteDataItem { title: title, order: order, parentId: parentId ?? this.parentId, - parentSectionId: parentSectionId ?? this.parentSectionId, + sectionId: sectionId ?? this.sectionId, id: id); } @@ -279,9 +296,7 @@ class MediaSection extends SiteDataItem implements CountableSiteDataItem { media: media ?? (parentId == null ? this.media - : this - .media - .map((e) => e.copyWith(parentSectionId: parentId))) + : this.media.map((e) => e.copyWith(sectionId: parentId))) .toList(), title: title, order: order, diff --git a/lib/models.g.dart b/lib/models.g.dart index cb07c79..8e23221 100644 --- a/lib/models.g.dart +++ b/lib/models.g.dart @@ -112,7 +112,7 @@ class MediaAdapter extends TypeAdapter { description: fields[3] as String, source: fields[4] as String, order: fields[6] as int, - parentSectionId: fields[7] as int, + sectionId: fields[7] as int, ).._length = fields[5] as int; } @@ -127,7 +127,7 @@ class MediaAdapter extends TypeAdapter { ..writeByte(6) ..write(obj.order) ..writeByte(7) - ..write(obj.parentSectionId) + ..write(obj.sectionId) ..writeByte(0) ..write(obj.id) ..writeByte(1) @@ -293,7 +293,7 @@ Media _$MediaFromJson(Map json) { description: json['description'] as String, source: json['source'] as String, order: json['order'] as int, - parentSectionId: json['parentSectionId'] as int, + sectionId: json['parentSectionId'] as int, length: json['length'] == null ? null : Duration(microseconds: json['length'] as int), @@ -307,7 +307,7 @@ Map _$MediaToJson(Media instance) => { 'description': instance.description, 'source': instance.source, 'order': instance.order, - 'parentSectionId': instance.parentSectionId, + 'parentSectionId': instance.sectionId, 'length': instance.length?.inMicroseconds, }; diff --git a/lib/site-service.dart b/lib/site-service.dart index 122bb6b..058f103 100644 --- a/lib/site-service.dart +++ b/lib/site-service.dart @@ -65,23 +65,47 @@ class SiteBoxes { return section; } - /// Set the section references of passed in list. - Future> resolveIterable( - List references) async { - final sectionFutures = - references.map((e) => sections.get(e.sectionId)).toList(); + /// Returns the parent of the given data item. + Future resolveParent(T child) async { + if (!child.hasParent) { + return; + } - if (sectionFutures.isEmpty) { - return references; + if (child.section != null) { + return; + } + + final parent = await sections.get(child.sectionId); + + if (parent == null) { + return; } - final sectionMap = Map.fromEntries( - (await Future.wait(sectionFutures)).map((e) => MapEntry(e.id, e))); + child.section = parent; - for (final s in references) { - s.section = sectionMap[s.sectionId]; + if (parent.id != child.parentId) { + child.parent = parent.content + .firstWhere((element) => element.mediaSection?.id == child.parentId) + .mediaSection + .media + .firstWhere((element) => element.id == child.parentId); + } else { + child.parent = parent; } + return; + } + + Future> resolveIterable( + List references) async { + final parentFutures = references.map((e) => resolveParent(e)).toList(); + + if (parentFutures.isEmpty) { + return references; + } + + await Future.wait(parentFutures); + return references; } diff --git a/pubspec.lock b/pubspec.lock index efb5d23..4e4e72a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -63,7 +63,7 @@ packages: name: build_runner url: "https://pub.dartlang.org" source: hosted - version: "1.10.11" + version: "1.11.1" build_runner_core: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 069880a..1fd8a6a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -24,5 +24,5 @@ dependencies: dev_dependencies: pedantic: ^1.9.0 json_serializable: ^3.3.0 - build_runner: ^1.10.0 + build_runner: ^1.10.4 hive_generator: ^0.8.2 \ No newline at end of file