Skip to content

Commit

Permalink
Button to load more messages
Browse files Browse the repository at this point in the history
  • Loading branch information
danigm committed Aug 27, 2017
1 parent 0118466 commit 6129510
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
4 changes: 1 addition & 3 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ Fixs:
* Sort rooms by last message or fav?

Functionality:
* Infinite scroll is done, but we need a button at the top, because the
event is send only if the scroll exists
* Register
* Room search
* Room creation
* Leave room
* Set topic
* Set room avatar
* Change user display name
* Change user avatar
* Register
* Show media messages (images / videos)
* Send media messages (images / videos)
* Store last read message to show differently
Expand Down
41 changes: 35 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct AppOp {
backend: Sender<backend::BKCommand>,
active_room: String,
members: HashMap<String, Member>,
load_more_btn: gtk::Button,
}

#[derive(Debug)]
Expand Down Expand Up @@ -269,8 +270,8 @@ impl AppOp {
let messages = self.gtk_builder
.get_object::<gtk::ListBox>("message_list")
.expect("Can't find message_list in ui file.");
for ch in messages.get_children() {
messages.remove(&ch);
for ch in messages.get_children().iter().skip(1) {
messages.remove(ch);
}

self.members.clear();
Expand Down Expand Up @@ -490,7 +491,7 @@ impl AppOp {

match msgpos {
MsgPos::Bottom => messages.add(&msg),
MsgPos::Top => messages.prepend(&msg),
MsgPos::Top => messages.insert(&msg, 1),
};

} else {
Expand Down Expand Up @@ -536,8 +537,13 @@ impl AppOp {

pub fn load_more_messages(&self) {
let room = self.active_room.clone();
self.load_more_btn.set_label("loading...");
self.backend.send(BKCommand::GetRoomMessagesTo(room)).unwrap();
}

pub fn load_more_normal(&self) {
self.load_more_btn.set_label("load more messages");
}
}

/// State for the main thread.
Expand Down Expand Up @@ -569,6 +575,7 @@ impl App {
let op = Arc::new(Mutex::new(
AppOp{
gtk_builder: gtk_builder.clone(),
load_more_btn: gtk::Button::new_with_label("Load more messages"),
backend: apptx,
active_room: String::from(""),
members: HashMap::new(),
Expand Down Expand Up @@ -604,16 +611,21 @@ impl App {
theop.lock().unwrap().set_room_avatar(avatar);
},
Ok(BKResponse::RoomMessages(msgs)) => {
for msg in msgs {
theop.lock().unwrap().add_room_message(&msg, MsgPos::Bottom);
for msg in msgs.iter() {
theop.lock().unwrap().add_room_message(msg, MsgPos::Bottom);
}

if !msgs.is_empty() {
theop.lock().unwrap().scroll_down();
}
theop.lock().unwrap().scroll_down();

theop.lock().unwrap().set_loading(false);
},
Ok(BKResponse::RoomMessagesTo(msgs)) => {
for msg in msgs.iter().rev() {
theop.lock().unwrap().add_room_message(msg, MsgPos::Top);
}
theop.lock().unwrap().load_more_normal();
},
Ok(BKResponse::RoomMembers(members)) => {
let mut ms = members;
Expand Down Expand Up @@ -664,6 +676,8 @@ impl App {
window.set_application(app);
});

self.create_load_more_btn();

self.connect_user_button();
self.connect_login_button();

Expand All @@ -675,6 +689,21 @@ impl App {
self.connect_send();
}

fn create_load_more_btn(&self) {
let messages = self.gtk_builder
.get_object::<gtk::ListBox>("message_list")
.expect("Can't find message_list in ui file.");

let btn = self.op.lock().unwrap().load_more_btn.clone();
btn.show();
messages.add(&btn);

let op = self.op.clone();
btn.connect_clicked(move |_| {
op.lock().unwrap().load_more_messages();
});
}

fn connect_msg_scroll(&self) {
let s = self.gtk_builder
.get_object::<gtk::ScrolledWindow>("messages_scroll")
Expand Down

0 comments on commit 6129510

Please sign in to comment.