Keeps track of event history in event-agent :event-history and :back-count. This is in preparation for the forward and back buttons. Also set up the s/defs for the event-agent. And I'm seriously considering replacing the agent with an atom because I keep having to add 'await event-agent' stuff. Ugh.

This commit is contained in:
Robert C. Martin 2022-05-29 09:07:17 -05:00
parent 3a060484e0
commit 3b063f4401
3 changed files with 81 additions and 19 deletions

View File

@ -3,7 +3,8 @@
[more-speech.ui.swing.article-tree :refer :all]
[more-speech.ui.swing.article-tree-util :refer :all]
[more-speech.nostr.util :as util]
[more-speech.ui.swing.ui-context :refer :all])
[more-speech.ui.swing.ui-context :refer :all]
[more-speech.ui.swing.article-panel :as article-panel])
(:import (javax.swing.tree DefaultMutableTreeNode)))
(describe "header tree"
@ -293,18 +294,42 @@
(should-not (node-contains? node 2))))
(it "finds children from beginning to end"
(let [node (DefaultMutableTreeNode. nil)
child1 (DefaultMutableTreeNode. 1)
child2 (DefaultMutableTreeNode. 2)
child3 (DefaultMutableTreeNode. 3)
_ (.add node child1)
_ (.add node child2)
_ (.add node child3)]
(should (node-contains? node 1))
(should (node-contains? node 2))
(should (node-contains? node 3))
(should-not (node-contains? node 4))))
)
(let [node (DefaultMutableTreeNode. nil)
child1 (DefaultMutableTreeNode. 1)
child2 (DefaultMutableTreeNode. 2)
child3 (DefaultMutableTreeNode. 3)
_ (.add node child1)
_ (.add node child2)
_ (.add node child3)]
(should (node-contains? node 1))
(should (node-contains? node 2))
(should (node-contains? node 3))
(should-not (node-contains? node 4)))))
(context "selecting nodes"
(with-stubs)
(it "remembers which articles have been read and loads article"
(with-redefs
[article-panel/load-article-info
(stub :load-article-info {:return nil})]
(let [event-agent (agent {:read-event-ids #{}
:selected-event nil
:event-history []
:back-count 1})
_ (reset! ui-context {:event-agent event-agent})
selected-event-id 1
selected-node (DefaultMutableTreeNode. selected-event-id)
tab-name :bob-tab
_ (select-article tab-name selected-node)
_ (await event-agent) ;ugh.
event-state @event-agent
read-event-ids (:read-event-ids event-state)
selected-event (:selected-event event-state)
event-history (:event-history event-state)
back-count (:back-count event-state)]
(should-have-invoked :load-article-info {:with [selected-event-id]})
(should= read-event-ids #{selected-event-id})
(should= selected-event selected-event-id)
(should= [[tab-name selected-event-id]] event-history)
(should= 0 back-count)))))
)

View File

@ -15,13 +15,16 @@
(s/def ::tag (s/tuple keyword? number?))
(s/def ::tags (s/coll-of ::tag))
(s/def ::references (s/coll-of number?))
(s/def ::relay-url string?)
(s/def ::relays (s/coll-of ::relay-url))
(s/def ::event (s/keys :req-un [::id
::pubkey
::created-at
::content
::sig
::tags
::references]))
::references]
:opt-un [::relays]))
(defn hexify [n]
(util/num32->hex-string n))
@ -33,6 +36,32 @@
(declare process-text-event
process-name-event)
(s/def ::chronological-text-events (s/coll-of ::id))
(s/def ::text-event-map (s/map-of :id :event))
(s/def ::nicknames (s/map-of ::id string?))
(s/def ::name string?)
(s/def ::about string?)
(s/def ::picture string?)
(s/def ::public-key string?)
(s/def ::private-key string?)
(s/def ::keys (s/keys :req-un [::name ::about ::picture ::public-key ::private-key]))
(s/def ::read-event-ids (s/coll-of ::id :kind set?))
(s/def ::selected (s/coll-of ::id))
(s/def ::blocked (s/coll-of ::id))
(s/def ::tab (s/keys :req-un [::selected ::blocked]))
(s/def ::tabs (s/map-of keyword? ::tab))
(s/def ::event-history (s/coll-of (s/tuple keyword? ::id)))
(s/def ::back-count number?)
(s/def ::event-agent (s/keys :req-un [::chronological-text-events
::text-event-map
::nicknames
::keys
::read-event-ids
::tabs
::event-history
::back-count]))
(defn make-event-agent [event-agent-map]
(agent (merge {:chronological-text-events []
:text-event-map {}
@ -40,13 +69,18 @@
:keys {}
:read-event-ids #{}
:tabs {}
:event-history []
:back-count 0
}
event-agent-map)))
(defn select-event [event-state id]
(defn select-event
"called when an event has been selected, but not by back/forward traversal."
[event-state tab-name id]
(-> event-state
(update :read-event-ids conj id)
(assoc :selected-event id)))
(update :event-history conj [tab-name id])
(assoc :selected-event id :back-count 0)))
(defn to-json [o]
(json/write-str o :escape-slash false :escape-unicode false))

View File

@ -41,11 +41,14 @@
(with-out-str
(clojure.pprint/pprint event))))
(defn select-article [tab-name selected-node]
(defn select-article
"This should not be called for back/forward traversal because
event/select-event adds to the event history."
[tab-name selected-node]
(let [selected-id (.getUserObject selected-node)
event-agent (:event-agent @ui-context)]
(swap! ui-context assoc :selected-tab tab-name)
(send event-agent events/select-event selected-id)
(send event-agent events/select-event tab-name selected-id)
(article-panel/load-article-info selected-id)))
(defn node-selected [tab-name e]