OpenContacts/lib/models/inventory/resonite_directory.dart

36 lines
1.1 KiB
Dart
Raw Normal View History

2023-09-30 06:22:32 -04:00
import 'package:collection/collection.dart';
import 'package:recon/models/records/record.dart';
2023-09-30 06:22:32 -04:00
class ResoniteDirectory {
static const rootName = "Inventory";
final Record record;
final ResoniteDirectory? parent;
final List<ResoniteDirectory> children;
ResoniteDirectory({required this.record, this.parent, required this.children});
factory ResoniteDirectory.fromRecord({required Record record, ResoniteDirectory? parent}) {
return ResoniteDirectory(record: record, parent: parent, children: []);
}
@override
String toString() {
return record.name;
}
bool get isRoot => record.isRoot;
String get absolutePath => "${parent?.absolutePath ?? ""}/${(record.name)}";
List<String> get absolutePathSegments => (parent?.absolutePathSegments ?? []) + [record.name];
bool containsRecord(Record record) => children.where((element) => element.record.id == record.id).isNotEmpty;
List<Record> get records => children.map((e) => e.record).toList();
bool get isLoaded => children.isNotEmpty;
ResoniteDirectory? findChildByRecord(Record record) => children.firstWhereOrNull((element) => element.record.id == record.id);
}