@@ -82,13 +82,52 @@ let handle = try await roomListService.allRooms().entries(listener: listener)
82
82
await syncService.start ()
83
83
```
84
84
85
- Finally we can send messages into a room (with built in support for markdown).
85
+ When we're ready to display the events from a room we use the timeline API.
86
+
87
+ ``` swift
88
+ class TimelineItemListener : TimelineListener {
89
+ /// The loaded items for this room's timeline
90
+ var timelineItems: [TimelineItem] = []
91
+
92
+ func onUpdate (diff : [TimelineDiff]) {
93
+ // Update the timeline items on each update.
94
+ for update in diff {
95
+ switch update.change () {
96
+ case .reset :
97
+ timelineItems = update.reset ()!
98
+ default :
99
+ break // Handle all the other cases accordingly.
100
+ }
101
+ }
102
+ }
103
+ }
104
+
105
+ // Fetch the room from the listener and initialise it's timeline.
106
+ let room = listener.rooms .first !
107
+ if ! room.isTimelineInitialized () {
108
+ try await room.initTimeline (eventTypeFilter : nil , internalIdPrefix : nil )
109
+ }
110
+ let timeline = try await room.fullRoom ().timeline ()
111
+
112
+ // Listen to timeline item updates.
113
+ let timelineItemsListener = TimelineItemListener ()
114
+ let timelineHandle = await timeline.addListener (listener : timelineItemsListener)
115
+
116
+ // Wait for the items array to be updated…
117
+
118
+ // Get the event contents from an item.
119
+ let timelineItem = timelineItemsListener.timelineItems .last !
120
+ if let messageEvent = timelineItem.asEvent ()? .content ().asMessage () {
121
+ print (messageEvent)
122
+ }
123
+ ```
124
+
125
+ Finally we can send messages into the room (with built in support for markdown).
86
126
87
127
``` swift
88
128
// Create the message content from a markdown string.
89
129
let message = messageEventContentFromMarkdown (md : " Hello, World!" )
90
130
91
- // Send the message content to the first room in the list .
92
- _ = try await listener. rooms . first ? . fullRoom (). timeline () .send (msg : message)
131
+ // Send the message content via the room's timeline (so that we show a local echo) .
132
+ _ = try await timeline.send (msg : message)
93
133
```
94
-
0 commit comments