Skip to content
This repository was archived by the owner on Dec 29, 2021. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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/';

Expand Down
61 changes: 38 additions & 23 deletions lib/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@ 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)
String title;
@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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -235,18 +255,15 @@ 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,
source: source ?? this.source,
title: title,
order: order,
parentId: parentId ?? this.parentId,
parentSectionId: parentSectionId ?? this.parentSectionId,
sectionId: sectionId ?? this.sectionId,
id: id);
}

Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions lib/models.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 35 additions & 11 deletions lib/site-service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,47 @@ class SiteBoxes {
return section;
}

/// Set the section references of passed in list.
Future<List<SectionReference>> resolveIterable(
List<SectionReference> references) async {
final sectionFutures =
references.map((e) => sections.get(e.sectionId)).toList();
/// Returns the parent of the given data item.
Future<void> resolveParent<T extends SectionReference>(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<List<T>> resolveIterable<T extends SectionReference>(
List<T> references) async {
final parentFutures = references.map((e) => resolveParent(e)).toList();

if (parentFutures.isEmpty) {
return references;
}

await Future.wait(parentFutures);

return references;
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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