Commit fb79b77f authored by Daniel García Moreno's avatar Daniel García Moreno

directory: Spinner for loading

I've added an spinner meantime we're loading rooms in the directory.
I've also changed the backend to return each third party once it's
finished so we don't need to wait for all third party, we'll view
results as soon as possible.

I've to move the ordering of rooms to the frontend.

I've also added a default image in the image widget.
parent b6aba9d6
Pipeline #13198 failed with stages
in 11 minutes and 7 seconds
......@@ -36,15 +36,16 @@ pub fn protocols(bk: &Backend) -> Result<(), Error> {
desc: String::from(s.split('/').last().unwrap_or("")),
});
let prs = r.as_object().unwrap();
for k in prs.keys() {
let ins = prs[k]["instances"].as_array().unwrap();
for i in ins {
let p = Protocol{
id: String::from(i["instance_id"].as_str().unwrap()),
desc: String::from(i["desc"].as_str().unwrap()),
};
protocols.push(p);
if let Some(prs) = r.as_object() {
for k in prs.keys() {
let ins = prs[k]["instances"].as_array();
for i in ins.unwrap_or(&vec![]) {
let p = Protocol{
id: String::from(i["instance_id"].as_str().unwrap_or_default()),
desc: String::from(i["desc"].as_str().unwrap_or_default()),
};
protocols.push(p);
}
}
}
......@@ -78,29 +79,28 @@ pub fn room_search(bk: &Backend,
let (rooms_sender, rooms_receiver) = channel::<Vec<Room>>();
fetch_rooms(bk, url.clone(), query.clone(), None, more, rooms_sender.clone())?;
let mut requests_count = 0;
let mut requests_count = 1;
if let Ok(_) = fetch_rooms(bk, url.clone(), query.clone(), None, more, rooms_sender.clone()) {
requests_count += 1;
}
if let Some(tps) = third_parties {
requests_count += tps.len();
for tp in tps {
fetch_rooms(bk, url.clone(), query.clone(), Some(tp.to_string()), more,
rooms_sender.clone())?;
if let Ok(_) = fetch_rooms(bk, url.clone(), query.clone(),
Some(tp.to_string()), more, rooms_sender.clone()) {
requests_count += 1;
}
}
}
let mut rooms: Vec<Room> = vec![];
for _ in 0..requests_count {
rooms.append(&mut rooms_receiver.recv().unwrap());
for i in 0..requests_count {
if let Ok(rooms) = rooms_receiver.recv() {
bk.tx.send(BKResponse::DirectorySearch(rooms)).unwrap();
}
}
rooms.sort_by(|a, b| a.n_members.cmp(&b.n_members));
rooms.dedup_by(|a, b| a.id == b.id);
rooms.reverse();
bk.tx.send(BKResponse::DirectorySearch(rooms)).unwrap();
bk.tx.send(BKResponse::FinishDirectorySearch).unwrap();
Ok(())
}
......
......@@ -84,6 +84,7 @@ pub enum BKResponse {
SendMsg,
DirectoryProtocols(Vec<Protocol>),
DirectorySearch(Vec<Room>),
FinishDirectorySearch,
JoinRoom,
LeaveRoom,
MarkedAsRead(String, String),
......
......@@ -93,15 +93,12 @@ pub fn backend_loop(rx: Receiver<BKResponse>) {
APPOP!(set_protocols, (protocols));
}
Ok(BKResponse::DirectorySearch(rooms)) => {
if rooms.len() == 0 {
let error = gettext("No rooms found");
APPOP!(show_error, (error));
}
for room in rooms {
APPOP!(set_directory_room, (room));
}
APPOP!(set_directory_rooms, (rooms));
}
Ok(BKResponse::FinishDirectorySearch) => {
APPOP!(finish_directory_search);
}
Ok(BKResponse::JoinRoom) => {
APPOP!(reload_rooms);
}
......
......@@ -19,7 +19,7 @@ impl AppOp {
self.protocols = protocols;
}
pub fn search_rooms(&self, more: bool) {
pub fn search_rooms(&mut self, more: bool) {
let protocols: Vec<String> = self.protocols.clone().into_iter()
.map(|p| p.id).collect();
......@@ -54,24 +54,61 @@ impl AppOp {
for ch in directory.get_children() {
directory.remove(&ch);
}
let spinner = gtk::Spinner::new();
spinner.start();
spinner.show();
directory.add(&spinner);
self.directory.clear();
}
self.backend
.send(BKCommand::DirectorySearch(homeserver, q.get_text().unwrap(), requested_protocols, more))
.send(BKCommand::DirectorySearch(homeserver, q.get_text().unwrap_or_default(), requested_protocols, more))
.unwrap();
}
pub fn load_more_rooms(&self) {
pub fn load_more_rooms(&mut self) {
self.search_rooms(true);
}
pub fn set_directory_room(&self, room: Room) {
pub fn finish_directory_search(&self) {
let directory = self.ui.builder
.get_object::<gtk::ListBox>("directory_room_list")
.expect("Can't find directory_room_list in ui file.");
for ch in directory.get_children() {
if ch.is::<gtk::Spinner>() {
directory.remove(&ch);
}
}
}
pub fn set_directory_rooms(&mut self, rooms: Vec<Room>) {
for r in rooms.iter() {
if self.directory.contains(r) {
continue;
}
self.directory.push(r.clone());
}
self.directory.sort_by_key(|a| -a.n_members);
self.redraw_directory_rooms();
}
let rb = widgets::RoomBox::new(&room, &self);
let room_widget = rb.widget();
directory.add(&room_widget);
pub fn redraw_directory_rooms(&self) {
let directory = self.ui.builder
.get_object::<gtk::ListBox>("directory_room_list")
.expect("Can't find directory_room_list in ui file.");
for ch in directory.get_children() {
if !ch.is::<gtk::Spinner>() {
directory.remove(&ch);
}
}
for r in self.directory.iter() {
let rb = widgets::RoomBox::new(&r, &self);
let room_widget = rb.widget();
directory.add(&room_widget);
}
}
}
......@@ -96,6 +96,7 @@ pub struct AppOp {
pub stickers: Vec<StickerGroup>,
pub protocols: Vec<Protocol>,
pub directory: Vec<Room>,
}
impl PasswordStorage for AppOp {}
......@@ -146,6 +147,7 @@ impl AppOp {
stickers: vec![],
protocols: vec![],
directory: vec![],
}
}
......
......@@ -181,7 +181,14 @@ pub fn load_pixbuf(pix: Arc<Mutex<Option<Pixbuf>>>, scaled: Arc<Mutex<Option<Pix
*scaled.lock().unwrap() = None;
}
_ => {
*pix.lock().unwrap() = None;
let pixbuf = match gtk::IconTheme::get_default() {
None => None,
Some(i1) => match i1.load_icon("image-x-generic-symbolic", 80, gtk::IconLookupFlags::empty()) {
Err(_) => None,
Ok(i2) => i2,
}
};
*pix.lock().unwrap() = pixbuf;
*scaled.lock().unwrap() = None;
}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment