improved tree creation. move events now get printed

This commit is contained in:
vishalxl
2022-08-06 23:59:59 +05:30
parent 78462f257d
commit 666606831c
2 changed files with 40 additions and 22 deletions

View File

@@ -1,6 +1,7 @@
import 'dart:io'; import 'dart:io';
import 'dart:convert'; import 'dart:convert';
//import 'dart:svg';
const bool enableVerticalLines = false; const bool enableVerticalLines = false;
const int spacesPerDepth = 8; const int spacesPerDepth = 8;
@@ -39,6 +40,17 @@ class EventData {
List<Contact> contactList = []; // used for kind:3 events, which is contact list event List<Contact> contactList = []; // used for kind:3 events, which is contact list event
String getParent() {
if( eTagParent != "") {
return eTagParent;
}
if( eTagsRest.length > 0) {
return eTagsRest[eTagsRest.length - 1];
}
return "";
}
EventData(this.id, this.pubkey, this.createdAt, this.kind, this.content, this.eTagParent, this.eTagsRest, this.pTags, this.contactList); EventData(this.id, this.pubkey, this.createdAt, this.kind, this.content, this.eTagParent, this.eTagsRest, this.pTags, this.contactList);
factory EventData.fromJson(dynamic json) { factory EventData.fromJson(dynamic json) {
@@ -157,7 +169,7 @@ class Event {
void printEvent(int depth) { void printEvent(int depth) {
eventData.printEventData(depth); eventData.printEventData(depth);
print("\n$originalJson \n"); //stdout.write("\n$originalJson \n");
} }
@override @override
@@ -175,31 +187,36 @@ class Tree {
stdout.write("in factory fromEvents list. number of events: ${events.length}\n"); stdout.write("in factory fromEvents list. number of events: ${events.length}\n");
List<Tree> childTrees = []; List<Tree> childTrees = [];
List<Event> nonTopEvents = []; Map<String, Tree> m = {};
events.forEach((element) { m[element.eventData.id] = Tree(element, []); });
for( int i = 0; i < events.length; i++) { stdout.write(m);
Event e = events[i]; List<String> processed = [];
//stdout.write("processing event number $i : $e \n");
if( e.eventData.eTagsRest.isNotEmpty) { m.forEach((key, value) {
// in case the event has a parent, add it to the list of non Top Events if( !processed.contains(key)) {
//stdout.write("Event has e tags: ${e.eventData.eTagsRest}\n"); if( !value.e.eventData.eTagsRest.isNotEmpty ) {
nonTopEvents.add(e); // in case this node is a parent, then move it to processed()
} processed.add(key);
else { } else {
//stdout.write("Adding top child: $e\n"); // is not a parent, find its parent and then add this element to that parent Tree
Tree node; stdout.write("added to parent a child\n");
node = Tree(e, []); String id = key;
childTrees.add(node); String parentId = value.e.eventData.getParent();
m[parentId]?.addChildNode(value);
}
} else { // entry already exists
// do nothing
} }
});
for( var value in m.values) {
if( !value.e.eventData.eTagsRest.isNotEmpty) { // if its a parent
childTrees.add(value);
}
} }
// add each of nonTopEvents to their parent tree ( if parent is found in tree)
for(int i = 0; i < nonTopEvents.length; i++) {
Event e = nonTopEvents[i];
insertIntoTrees( childTrees, e);
// String parentId = e.eventData.tags
}
stdout.write("Ending: factory fromEvents list. number of events: ${events.length}\n"); stdout.write("Ending: factory fromEvents list. number of events: ${events.length}\n");
return Tree( events[0], childTrees); // TODO remove events[0] return Tree( events[0], childTrees); // TODO remove events[0]
} }

View File

@@ -41,6 +41,7 @@ void main() {
Tree node = Tree.fromEvents(listEvents); Tree node = Tree.fromEvents(listEvents);
node.printTree(0, true); node.printTree(0, true);
print("=========================");
}); });
test('createNodeTree_unordered1', () { test('createNodeTree_unordered1', () {