@@ -16,16 +16,18 @@ First we need to authenticate the user.
16
16
``` swift
17
17
import MatrixRustSDK
18
18
19
- // Create an authentication service to streamline the login process.
20
- let service = AuthenticationService (basePath : URL.applicationSupportDirectory .path (), passphrase : nil )
21
-
22
- // Configure the service for a particular homeserver.
19
+ // Create a client for a particular homeserver.
23
20
// Note that we can pass a server name (the second part of a Matrix user ID) instead of the direct URL.
24
- // This allows the SDK to discover the homeserver's well-known configuration for OIDC and Sliding Sync support.
25
- try service.configureHomeserver (serverName : " matrix.org" )
21
+ // This allows the SDK to discover the homeserver's well-known configuration for Sliding Sync support.
22
+ let client = try await ClientBuilder ()
23
+ .serverNameOrHomeserverUrl (serverNameOrUrl : " matrix.org" )
24
+ .sessionPaths (dataPath : URL.applicationSupportDirectory .path (percentEncoded : false ),
25
+ cachePath : URL.cachesDirectory .path (percentEncoded : false ))
26
+ .slidingSyncVersionBuilder (versionBuilder : .discoverProxy )
27
+ .build ()
26
28
27
- // Login through the service which creates a client .
28
- let client = try service .login (username : " alice" , password : " secret" , initialDeviceName : nil , deviceId : nil )
29
+ // Login using password authentication .
30
+ try await client .login (username : " alice" , password : " secret" , initialDeviceName : nil , deviceId : nil )
29
31
30
32
let session = try client.session ()
31
33
// Store the session in the keychain.
@@ -38,43 +40,46 @@ Or, if the user has previously authenticated we can restore their session instea
38
40
let session = …
39
41
40
42
// Build a client for the homeserver.
41
- let client = try ClientBuilder ()
42
- .basePath (path : URL.applicationSupportDirectory .path ())
43
+ let client = try await ClientBuilder ()
44
+ .sessionPaths (dataPath : URL.applicationSupportDirectory .path (percentEncoded : false ),
45
+ cachePath : URL.cachesDirectory .path (percentEncoded : false ))
43
46
.homeserverUrl (url : session.homeserverUrl )
44
47
.build ()
45
48
46
49
// Restore the client using the session.
47
- try client.restoreSession (session : session)
50
+ try await client.restoreSession (session : session)
48
51
```
49
52
50
- Next we need to start the client and listen for updates. The following code does so using syncv2 .
53
+ Next we need to start the sync loop and listen for room updates .
51
54
52
55
``` swift
53
- class ClientListener : ClientDelegate {
56
+ class AllRoomsListener : RoomListEntriesListener {
54
57
/// The user's list of rooms.
55
- var rooms = [Room]()
56
-
57
- func didReceiveSyncUpdate () {
58
- // Update the user's room list on each sync response.
59
- self .rooms = client.rooms ()
60
- }
58
+ var rooms: [RoomListItem] = []
61
59
62
- func didReceiveAuthError (isSoftLogout : Bool ) {
63
- // Ask the user to reauthenticate.
64
- }
65
-
66
- func didUpdateRestoreToken () {
67
- let session = try ? client.session ()
68
- // Update the session in the keychain.
60
+ func onUpdate (roomEntriesUpdate : [MatrixRustSDK.RoomListEntriesUpdate]) {
61
+ // Update the user's room list on each update.
62
+ for update in roomEntriesUpdate {
63
+ switch update {
64
+ case .reset (values : let values):
65
+ rooms = values
66
+ default :
67
+ break // Handle all the other cases accordingly.
68
+ }
69
+ }
69
70
}
70
71
}
71
72
72
- // Listen to updates from the client.
73
- let listener = ClientListener ()
74
- client.setDelegate (delegate : listener)
73
+ // Create a sync service which controls the sync loop.
74
+ let syncService = try await client.syncService ().finish ()
75
+
76
+ // Listen to room list updates.
77
+ let listener = AllRoomsListener ()
78
+ let roomListService = syncService.roomListService ()
79
+ let handle = try await roomListService.allRooms ().entries (listener : listener)
75
80
76
- // Start the client using syncv2 .
77
- client. startSync ( timelineLimit : 20 )
81
+ // Start the sync loop .
82
+ await syncService. start ( )
78
83
```
79
84
80
85
Finally we can send messages into a room (with built in support for markdown).
@@ -84,6 +89,6 @@ Finally we can send messages into a room (with built in support for markdown).
84
89
let message = messageEventContentFromMarkdown (md : " Hello, World!" )
85
90
86
91
// Send the message content to the first room in the list.
87
- try listener.rooms .first ? .send (msg : message, txnId : UUID (). uuidString )
92
+ _ = try await listener.rooms .first ? .fullRoom (). timeline (). send (msg : message)
88
93
```
89
94
0 commit comments