Invert chronological order so that latest message is at the top of the tree.

This commit is contained in:
Robert C. Martin 2022-05-23 08:20:30 -05:00
parent 09563d1480
commit 8ac8c808ae
3 changed files with 28 additions and 27 deletions

View File

@ -14,18 +14,7 @@
insertion-point (find-chronological-insertion-point root 99 event-map)]
(should= 0 insertion-point)))
(it "returns zero if time is earlier than all events in tree"
(let [root (DefaultMutableTreeNode.)
child-id 1
child (DefaultMutableTreeNode. child-id)
_ (.add ^DefaultMutableTreeNode root child)
event {:id 99 :created-at 1}
event-map {99 event
child-id {:created-at 10}}
insertion-point (find-chronological-insertion-point root 99 event-map)]
(should= 0 insertion-point)))
(it "returns 1 when event is later than only event in tree"
(it "returns zero if time is later than all events in tree"
(let [root (DefaultMutableTreeNode.)
child-id 1
child (DefaultMutableTreeNode. child-id)
@ -34,10 +23,21 @@
event-map {99 event
child-id {:created-at 10}}
insertion-point (find-chronological-insertion-point root 99 event-map)]
(should= 0 insertion-point)))
(it "returns 1 when event is ealier than only event in tree"
(let [root (DefaultMutableTreeNode.)
child-id 1
child (DefaultMutableTreeNode. child-id)
_ (.add ^DefaultMutableTreeNode root child)
event {:id 99 :created-at 5}
event-map {99 event
child-id {:created-at 10}}
insertion-point (find-chronological-insertion-point root 99 event-map)]
(should= 1 insertion-point))
)
(it "returns n when event is later than n events in tree"
(it "returns n when event is earlier than n events in tree"
(let [root (DefaultMutableTreeNode.)
child-id 1
child-1 (DefaultMutableTreeNode. child-id)
@ -46,7 +46,7 @@
_ (.add ^DefaultMutableTreeNode root child-1)
_ (.add ^DefaultMutableTreeNode root child-2)
_ (.add ^DefaultMutableTreeNode root child-3)
event {:id 99 :created-at 20}
event {:id 99 :created-at 5}
event-map {99 event
child-id {:created-at 10}
(+ 1 child-id) {:created-at 10}
@ -54,7 +54,7 @@
insertion-point (find-chronological-insertion-point root 99 event-map)]
(should= 3 insertion-point)))
(it "returns chronological insertion point"
(it "returns chronological insertion point above first earliest"
(let [root (DefaultMutableTreeNode.)
child-id 1
child-1 (DefaultMutableTreeNode. child-id)
@ -63,11 +63,11 @@
_ (.add ^DefaultMutableTreeNode root child-1)
_ (.add ^DefaultMutableTreeNode root child-2)
_ (.add ^DefaultMutableTreeNode root child-3)
event {:id 99 :created-at 25}
event {:id 99 :created-at 15}
event-map {99 event
child-id {:created-at 10}
child-id {:created-at 30}
(+ 1 child-id) {:created-at 20}
(+ 2 child-id) {:created-at 30}}
(+ 2 child-id) {:created-at 10}}
insertion-point (find-chronological-insertion-point root 99 event-map)]
(should= 2 insertion-point))
)

View File

@ -64,14 +64,15 @@
orphans (get-in @ui-context [:orphaned-references parent-id])]
(if (empty? orphans)
nil
(loop [orphans orphans]
(if (empty? orphans)
nil
(let [orphan-id (first orphans)
orphan-node (DefaultMutableTreeNode. orphan-id)]
(.add ^DefaultMutableTreeNode parent-node orphan-node)
(swap! ui-context update-in [:node-map orphan-id] conj orphan-node)
(recur (rest orphans))))
(do
(loop [orphans orphans]
(if (empty? orphans)
nil
(let [orphan-id (first orphans)
orphan-node (DefaultMutableTreeNode. orphan-id)]
(.add ^DefaultMutableTreeNode parent-node orphan-node)
(swap! ui-context update-in [:node-map orphan-id] conj orphan-node)
(recur (rest orphans)))))
(swap! ui-context assoc-in [:orphaned-references parent-id] nil))))
)

View File

@ -10,7 +10,7 @@
(let [comparator (fn [node1 node2]
(let [v1 (->> node1 .getUserObject (get event-map) :created-at)
v2 (->> node2 .getUserObject (get event-map) :created-at)]
(compare v1 v2)))
(compare v2 v1)))
children (enumeration-seq (.children root))
dummy-node (DefaultMutableTreeNode. event-id)
insertion-point (if (nil? children)