Skip to content

Commit

Permalink
fix(sidebar): last message cannot update
Browse files Browse the repository at this point in the history
  • Loading branch information
lomirus committed May 30, 2022
1 parent 5c5b129 commit 9233143
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 51 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ edition = "2021"

[dependencies.relm4]
git = "https://github.com/Relm4/Relm4.git"
# next, 2022/05/24
rev = "74659719607331a591dfabf90dd33b7c590bc2fd"
# next, 2022/05/25
rev = "968d09c9417dce6f84446948523e3befc4954d50"
features = [
"macros",
"libadwaita",
Expand Down
79 changes: 34 additions & 45 deletions src/app/main/sidebar/chat_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use relm4::factory::{DynamicIndex, FactoryComponent};
use relm4::{adw, gtk, Sender};

use adw::{prelude::*, Avatar};
use gtk::{Align, Box, Label, ListBox, ListBoxRow, Orientation};
use gtk::{Align, Box, Label, ListBox, Orientation};

use crate::handler::{FRIEND_LIST, GROUP_LIST};

Expand All @@ -16,15 +16,43 @@ pub struct ChatItem {
pub last_message: String,
}

#[relm4::factory(pub)]
impl FactoryComponent<ListBox, SidebarMsg> for ChatItem {
/// (account, is_group, last_message)
type InitParams = (i64, bool, String);
type Widgets = ();
type Widgets = ChatItemWidgets;
type Input = ();
type Output = ();
type Command = ();
type CommandOutput = ();
type Root = Box;

view! {
root = Box {
set_margin_top: 8,
set_margin_bottom: 8,
Avatar {
set_text: Some(&self.name),
set_show_initials: true,
set_size: 48,
set_margin_end: 8
},
Box {
set_orientation: Orientation::Vertical,
set_halign: Align::Start,
set_spacing: 8,
Label {
set_xalign: 0.0,
set_text: self.name.as_str(),
add_css_class: "heading"
},
#[name = "last_message"]
Label {
set_text: self.last_message.as_str(),
add_css_class: "caption",
set_xalign: 0.0,
},
},
}
}

fn init_model(
init_params: Self::InitParams,
Expand Down Expand Up @@ -60,46 +88,7 @@ impl FactoryComponent<ListBox, SidebarMsg> for ChatItem {
}
}

fn init_root(&self) -> Self::Root {
Box::default()
}

fn init_widgets(
&mut self,
_index: &DynamicIndex,
root: &Self::Root,
_returned_widget: &ListBoxRow,
_input: &Sender<Self::Input>,
_output: &Sender<Self::Output>,
) -> Self::Widgets {
relm4::view! {
item = Box {
set_margin_top: 8,
set_margin_bottom: 8,
append = &Avatar {
set_text: Some(&self.name),
set_show_initials: true,
set_size: 48,
set_margin_end: 8
},
append = &Box {
set_orientation: Orientation::Vertical,
set_halign: Align::Start,
set_spacing: 8,
append = &Label {
set_xalign: 0.0,
set_text: self.name.as_str(),
add_css_class: "heading"
},
append = &Label {
set_text: self.last_message.as_str(),
add_css_class: "caption",
set_xalign: 0.0,
},
},
}
}

root.append(&item);
fn pre_view() {
widgets.last_message.set_label(&self.last_message);
}
}
2 changes: 0 additions & 2 deletions src/app/main/sidebar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ impl SidebarModel {
break;
}
}
chats_list.render_changes();
}

fn insert_chat_item(&self, account: i64, is_group: bool, last_message: String) {
let mut chats_list = self.chats_list.borrow_mut();
chats_list.push_front((account, is_group, last_message));
chats_list.render_changes();
}
}

Expand Down

0 comments on commit 9233143

Please sign in to comment.