2025-04-30 15:58:47 -07:00
|
|
|
using GLib;
|
|
|
|
|
using Gee;
|
|
|
|
|
|
|
|
|
|
public class MessageListModel : Object, ListModel
|
|
|
|
|
{
|
2025-04-30 19:50:36 -07:00
|
|
|
public signal void messages_changed();
|
|
|
|
|
|
2025-04-30 15:58:47 -07:00
|
|
|
public SortedSet<Message> messages {
|
|
|
|
|
owned get { return _messages.read_only_view; }
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 15:09:12 -07:00
|
|
|
public string conversation_guid { get; private set; }
|
2025-04-30 15:58:47 -07:00
|
|
|
private SortedSet<Message> _messages;
|
|
|
|
|
|
|
|
|
|
public MessageListModel(string conversation_guid) {
|
|
|
|
|
_messages = new TreeSet<Message>((a, b) => {
|
|
|
|
|
// Sort by date in descending order (newest first)
|
|
|
|
|
return (int)(b.date - a.date);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Repository.get_instance().messages_updated.connect(got_messages_updated);
|
2025-05-02 15:09:12 -07:00
|
|
|
this.conversation_guid = conversation_guid;
|
2025-04-30 15:58:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void load_messages() {
|
|
|
|
|
try {
|
2025-05-02 15:09:12 -07:00
|
|
|
Message[] messages = Repository.get_instance().get_messages(conversation_guid);
|
2025-04-30 15:58:47 -07:00
|
|
|
|
|
|
|
|
// Clear existing set
|
|
|
|
|
uint old_count = _messages.size;
|
|
|
|
|
_messages.clear();
|
|
|
|
|
|
|
|
|
|
// Notify of removal
|
|
|
|
|
if (old_count > 0) {
|
|
|
|
|
items_changed(0, old_count, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process each conversation
|
|
|
|
|
uint position = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < messages.length; i++) {
|
|
|
|
|
var message = messages[i];
|
|
|
|
|
_messages.add(message);
|
|
|
|
|
position++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Notify of additions
|
|
|
|
|
if (position > 0) {
|
|
|
|
|
items_changed(0, 0, position);
|
|
|
|
|
}
|
|
|
|
|
} catch (Error e) {
|
|
|
|
|
warning("Failed to load messages: %s", e.message);
|
|
|
|
|
}
|
2025-04-30 19:50:36 -07:00
|
|
|
|
|
|
|
|
messages_changed();
|
2025-04-30 15:58:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void got_messages_updated(string conversation_guid) {
|
2025-05-02 15:09:12 -07:00
|
|
|
if (conversation_guid == this.conversation_guid) {
|
2025-04-30 15:58:47 -07:00
|
|
|
load_messages();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListModel implementation
|
|
|
|
|
public Type get_item_type() {
|
|
|
|
|
return typeof(Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint get_n_items() {
|
|
|
|
|
return _messages.size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Object? get_item(uint position) {
|
|
|
|
|
return _messages.to_array()[position];
|
|
|
|
|
}
|
|
|
|
|
}
|