12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
+ use std:: { fmt:: Debug , sync:: Arc } ;
16
+
17
+ use futures_util:: { pin_mut, StreamExt } ;
18
+ use matrix_sdk_common:: { SendOutsideWasm , SyncOutsideWasm } ;
15
19
use matrix_sdk_ui:: spaces:: {
20
+ room_list:: SpaceServiceRoomListPaginationState as UISpaceServiceRoomListPaginationState ,
16
21
SpaceService as UISpaceService , SpaceServiceRoom as UISpaceServiceRoom ,
22
+ SpaceServiceRoomList as UISpaceServiceRoomList ,
17
23
} ;
18
24
use ruma:: RoomId ;
19
25
@@ -22,6 +28,8 @@ use crate::{
22
28
error:: ClientError ,
23
29
room:: { Membership , RoomHero } ,
24
30
room_preview:: RoomType ,
31
+ runtime:: get_runtime_handle,
32
+ TaskHandle ,
25
33
} ;
26
34
27
35
#[ derive( uniffi:: Object ) ]
@@ -30,28 +38,104 @@ pub struct SpaceService {
30
38
}
31
39
32
40
impl SpaceService {
33
- /// Create a new instance of `SpaceService`.
34
41
pub fn new ( inner : UISpaceService ) -> Self {
35
42
Self { inner }
36
43
}
37
44
}
38
45
39
46
#[ matrix_sdk_ffi_macros:: export]
40
47
impl SpaceService {
41
- /// Get the list of joined spaces.
42
48
pub fn joined_spaces ( & self ) -> Vec < SpaceServiceRoom > {
43
49
self . inner . joined_spaces ( ) . into_iter ( ) . map ( Into :: into) . collect ( )
44
50
}
45
51
46
- /// Get the top-level children for a given space.
47
- pub async fn top_level_children_for (
52
+ pub fn top_level_children_for (
48
53
& self ,
49
54
space_id : String ,
50
- ) -> Result < Vec < SpaceServiceRoom > , ClientError > {
55
+ ) -> Result < SpaceServiceRoomList , ClientError > {
51
56
let space_id = RoomId :: parse ( space_id) ?;
52
- let children = self . inner . top_level_children_for ( space_id) . await ?;
53
- Ok ( children. into_iter ( ) . map ( Into :: into) . collect ( ) )
57
+ Ok ( SpaceServiceRoomList :: new ( self . inner . space_room_list ( space_id) ) )
58
+ }
59
+ }
60
+
61
+ #[ derive( uniffi:: Object ) ]
62
+ pub struct SpaceServiceRoomList {
63
+ inner : UISpaceServiceRoomList ,
64
+ }
65
+
66
+ impl SpaceServiceRoomList {
67
+ pub fn new ( inner : UISpaceServiceRoomList ) -> Self {
68
+ Self { inner }
69
+ }
70
+ }
71
+
72
+ #[ matrix_sdk_ffi_macros:: export]
73
+ impl SpaceServiceRoomList {
74
+ pub fn pagination_state ( & self ) -> SpaceServiceRoomListPaginationState {
75
+ self . inner . pagination_state ( ) . into ( )
76
+ }
77
+
78
+ pub fn subscribe_to_pagiation_state_updates (
79
+ & self ,
80
+ listener : Box < dyn SpaceServiceRoomListPaginationStateListener > ,
81
+ ) {
82
+ let pagination_state = self . inner . subscribe_to_pagination_state_updates ( ) ;
83
+
84
+ Arc :: new ( TaskHandle :: new ( get_runtime_handle ( ) . spawn ( async move {
85
+ pin_mut ! ( pagination_state) ;
86
+
87
+ while let Some ( state) = pagination_state. next ( ) . await {
88
+ listener. on_update ( state. into ( ) ) ;
89
+ }
90
+ } ) ) ) ;
54
91
}
92
+
93
+ pub fn rooms ( & self ) -> Vec < SpaceServiceRoom > {
94
+ self . inner . rooms ( ) . into_iter ( ) . map ( Into :: into) . collect ( )
95
+ }
96
+
97
+ pub fn subscribe_to_room_update ( & self , listener : Box < dyn SpaceServiceRoomListEntriesListener > ) {
98
+ let entries_stream = self . inner . subscribe_to_room_updates ( ) ;
99
+
100
+ Arc :: new ( TaskHandle :: new ( get_runtime_handle ( ) . spawn ( async move {
101
+ pin_mut ! ( entries_stream) ;
102
+
103
+ while let Some ( rooms) = entries_stream. next ( ) . await {
104
+ listener. on_update ( rooms. into_iter ( ) . map ( Into :: into) . collect ( ) ) ;
105
+ }
106
+ } ) ) ) ;
107
+ }
108
+ }
109
+
110
+ #[ derive( uniffi:: Enum ) ]
111
+ pub enum SpaceServiceRoomListPaginationState {
112
+ Idle { end_reached : bool } ,
113
+ Loading ,
114
+ }
115
+
116
+ impl From < UISpaceServiceRoomListPaginationState > for SpaceServiceRoomListPaginationState {
117
+ fn from ( state : UISpaceServiceRoomListPaginationState ) -> Self {
118
+ match state {
119
+ UISpaceServiceRoomListPaginationState :: Idle { end_reached } => {
120
+ SpaceServiceRoomListPaginationState :: Idle { end_reached }
121
+ }
122
+ UISpaceServiceRoomListPaginationState :: Loading => {
123
+ SpaceServiceRoomListPaginationState :: Loading
124
+ }
125
+ }
126
+ }
127
+ }
128
+
129
+ #[ matrix_sdk_ffi_macros:: export( callback_interface) ]
130
+ pub trait SpaceServiceRoomListPaginationStateListener :
131
+ SendOutsideWasm + SyncOutsideWasm + Debug
132
+ {
133
+ fn on_update ( & self , pagination_state : SpaceServiceRoomListPaginationState ) ;
134
+ }
135
+
136
+ #[ matrix_sdk_ffi_macros:: export( callback_interface) ]
137
+ pub trait SpaceServiceRoomListEntriesListener : SendOutsideWasm + SyncOutsideWasm + Debug {
138
+ fn on_update ( & self , rooms : Vec < SpaceServiceRoom > ) ;
55
139
}
56
140
57
141
#[ derive( uniffi:: Record ) ]
0 commit comments