diff --git a/src/application/main-window.vala b/src/application/main-window.vala index a03e3a1..3420fd5 100644 --- a/src/application/main-window.vala +++ b/src/application/main-window.vala @@ -3,6 +3,9 @@ using Gtk; public class MainWindow : Adw.ApplicationWindow { + private ConversationListView conversation_list_view; + private MessageListView message_list_view; + public MainWindow () { Object (title: "Kordophone"); @@ -10,10 +13,22 @@ public class MainWindow : Adw.ApplicationWindow split_view.set_min_sidebar_width (400); set_content (split_view); - var conversation_list_page = new NavigationPage (new ConversationListView (), "Conversations"); + conversation_list_view = new ConversationListView (); + conversation_list_view.conversation_selected.connect (conversation_selected); + + var conversation_list_page = new NavigationPage (conversation_list_view, "Conversations"); split_view.sidebar = conversation_list_page; - var message_list_page = new NavigationPage (new MessageListView (new MessageListModel ("123")), "Messages"); + message_list_view = new MessageListView (new MessageListModel ("123")); + var message_list_page = new NavigationPage (message_list_view, "Messages"); split_view.content = message_list_page; } + + private void conversation_selected(string? conversation_guid) { + if (conversation_guid == null) { + message_list_view.model = null; + } else { + message_list_view.model = new MessageListModel (conversation_guid); + } + } } \ No newline at end of file diff --git a/src/conversation-list/conversation-list-view.vala b/src/conversation-list/conversation-list-view.vala index 9342272..d667870 100644 --- a/src/conversation-list/conversation-list-view.vala +++ b/src/conversation-list/conversation-list-view.vala @@ -3,6 +3,8 @@ using Gtk; public class ConversationListView : Adw.Bin { + public signal void conversation_selected(string? conversation_guid); + private Adw.ToolbarView container; private ListBox list_box; private ScrolledWindow scrolled_window; @@ -21,6 +23,11 @@ public class ConversationListView : Adw.Bin list_box.set_selection_mode (SelectionMode.SINGLE); scrolled_window.set_child (list_box); + list_box.row_selected.connect ((row) => { + var conversation_row = (ConversationRow?) row; + conversation_selected(conversation_row != null ? conversation_row.conversation.guid : null); + }); + header_bar = new Adw.HeaderBar (); header_bar.set_title_widget (new Label ("Kordophone")); container.add_top_bar (header_bar); diff --git a/src/conversation-list/conversation-row.vala b/src/conversation-list/conversation-row.vala index 7238235..5ddb121 100644 --- a/src/conversation-list/conversation-row.vala +++ b/src/conversation-list/conversation-row.vala @@ -2,11 +2,11 @@ using Adw; using Gtk; public class ConversationRow : Adw.ActionRow { + public Conversation conversation; private Label unread_badge; public ConversationRow(Conversation conversation) { - Object(); - + this.conversation = conversation; title = conversation.display_name; subtitle = conversation.last_message_preview; subtitle_lines = 1; diff --git a/src/message-list/message-drawing-area.vala b/src/message-list/message-drawing-area.vala index 0fa74f2..8791059 100644 --- a/src/message-list/message-drawing-area.vala +++ b/src/message-list/message-drawing-area.vala @@ -92,5 +92,6 @@ private class MessageDrawingArea : Widget }); queue_draw(); + queue_resize(); } } \ No newline at end of file diff --git a/src/message-list/message-layout.vala b/src/message-list/message-layout.vala index 8135a96..86c66ec 100644 --- a/src/message-list/message-layout.vala +++ b/src/message-list/message-layout.vala @@ -23,7 +23,7 @@ private class MessageLayout : Object this.max_width = max_width; this.parent = parent; - layout = parent.create_pango_layout(message.content); + layout = parent.create_pango_layout(message.text); // Set text attributes var font_desc = Pango.FontDescription.from_string(font_description); @@ -35,7 +35,7 @@ private class MessageLayout : Object public bool from_me { get { - return message.sender == null; + return message.from_me; } } diff --git a/src/message-list/message-list-model.vala b/src/message-list/message-list-model.vala index 029ef39..3bfd75e 100644 --- a/src/message-list/message-list-model.vala +++ b/src/message-list/message-list-model.vala @@ -3,6 +3,8 @@ using Gee; public class MessageListModel : Object, ListModel { + public signal void messages_changed(); + public SortedSet messages { owned get { return _messages.read_only_view; } } @@ -49,6 +51,8 @@ public class MessageListModel : Object, ListModel } catch (Error e) { warning("Failed to load messages: %s", e.message); } + + messages_changed(); } private void got_messages_updated(string conversation_guid) { diff --git a/src/message-list/message-list-view.vala b/src/message-list/message-list-view.vala index 4ea9b46..84f6e40 100644 --- a/src/message-list/message-list-view.vala +++ b/src/message-list/message-list-view.vala @@ -4,12 +4,31 @@ using Gee; public class MessageListView : Adw.Bin { + public MessageListModel? model { + get { + return _model; + } + set { + _model = value; + + if (model != null) { + model.messages_changed.connect(reload_messages); + model.load_messages(); + } else { + message_drawing_area.set_messages(new TreeSet()); + } + } + } + + private MessageListModel? _model = null; private Adw.ToolbarView container; private MessageDrawingArea message_drawing_area = new MessageDrawingArea(); private ScrolledWindow scrolled_window = new ScrolledWindow(); - public MessageListView(MessageListModel model) { + public MessageListView(MessageListModel? model = null) { + this.model = model; + container = new Adw.ToolbarView(); set_child(container); @@ -20,14 +39,9 @@ public class MessageListView : Adw.Bin var header_bar = new Adw.HeaderBar(); header_bar.set_title_widget(new Label("Messages")); container.add_top_bar(header_bar); + } - // Create test message set - var messages = new TreeSet(); - messages.add(new Message("Hello, world!", 1, "user")); - messages.add(new Message("How, are you?", 2, null)); - messages.add(new Message("I'm fine, thank you!", 3, "user")); - messages.add(new Message("GTK also supports color expressions, which allow colors to be transformed to new ones and can be nested, providing a rich language to define colors. Color expressions resemble functions, taking 1 or more colors and in some cases a number as arguments.", 4, "user")); - - message_drawing_area.set_messages(messages); + private void reload_messages() { + message_drawing_area.set_messages(_model.messages); } } diff --git a/src/models/message.vala b/src/models/message.vala index b7fc7c9..8a1ff4b 100644 --- a/src/models/message.vala +++ b/src/models/message.vala @@ -3,19 +3,26 @@ using GLib; public class Message : Object { public string guid { get; set; default = ""; } - public string content { get; set; default = ""; } + public string text { get; set; default = ""; } public int64 date { get; set; default = 0; } - public string? sender { get; set; default = null; } + public string sender { get; set; default = null; } - public Message(string content, int64 date, string? sender) { - this.content = content; + public bool from_me { + get { + // Hm, this may have been accidental. + return sender == "(Me)"; + } + } + + public Message(string text, int64 date, string? sender) { + this.text = text; this.date = date; this.sender = sender; } public Message.from_hash_table(HashTable message_data) { - guid = message_data["guid"].get_string(); - content = message_data["content"].get_string(); + guid = message_data["id"].get_string(); + text = message_data["text"].get_string(); date = message_data["date"].get_int64(); sender = message_data["sender"].get_string(); }