relatica/lib/models/timeline.dart

55 lines
1.3 KiB
Dart
Raw Normal View History

2022-11-17 16:04:14 +00:00
import 'TimelineIdentifiers.dart';
import 'entry_tree_item.dart';
class Timeline {
final TimelineIdentifiers id;
final List<EntryTreeItem> _posts = [];
final Set<EntryTreeItem> _postsSet = {};
int _lowestStatusId = 0;
int _highestStatusId = 0;
int get highestStatusId => _highestStatusId;
2022-11-17 16:04:14 +00:00
Timeline(this.id, {List<EntryTreeItem>? initialPosts}) {
if (initialPosts != null) {
addPosts(initialPosts);
}
}
List<EntryTreeItem> get posts => List.unmodifiable(_posts);
void addPosts(List<EntryTreeItem> newPosts) {
for (final p in newPosts) {
final id = int.parse(p.id);
if (_lowestStatusId > id) {
_lowestStatusId = id;
}
if (_highestStatusId < id) {
_highestStatusId = id;
}
_postsSet.add(p);
}
_posts.clear();
_posts.addAll(_postsSet);
_posts.sort((p1, p2) {
return p2.entry.backdatedTimestamp.compareTo(p1.entry.backdatedTimestamp);
});
}
void clear() {
_posts.clear();
_postsSet.clear();
_lowestStatusId = 0;
_highestStatusId = 0;
}
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Timeline && runtimeType == other.runtimeType && id == other.id;
@override
int get hashCode => id.hashCode;
}