2025-04-28 18:21:02 -07:00
|
|
|
using GLib;
|
|
|
|
|
using Gee;
|
|
|
|
|
|
|
|
|
|
public class ConversationListModel : Object, ListModel
|
|
|
|
|
{
|
|
|
|
|
public SortedSet<Conversation> conversations {
|
|
|
|
|
owned get { return _conversations.read_only_view; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SortedSet<Conversation> _conversations;
|
|
|
|
|
|
|
|
|
|
public ConversationListModel() {
|
|
|
|
|
_conversations = new TreeSet<Conversation>((a, b) => {
|
|
|
|
|
// Sort by date in descending order (newest first)
|
|
|
|
|
return (int)(b.date - a.date);
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-30 15:19:44 -07:00
|
|
|
Repository.get_instance().conversations_updated.connect(load_conversations);
|
2025-04-28 18:21:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void load_conversations() {
|
|
|
|
|
try {
|
2025-04-30 15:19:44 -07:00
|
|
|
Conversation[] conversations = Repository.get_instance().get_conversations();
|
2025-04-28 18:21:02 -07:00
|
|
|
|
|
|
|
|
// Clear existing set
|
|
|
|
|
uint old_count = _conversations.size;
|
|
|
|
|
_conversations.clear();
|
|
|
|
|
|
|
|
|
|
// Notify of removal
|
|
|
|
|
if (old_count > 0) {
|
|
|
|
|
items_changed(0, old_count, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process each conversation
|
|
|
|
|
uint position = 0;
|
|
|
|
|
|
2025-04-30 15:19:44 -07:00
|
|
|
for (int i = 0; i < conversations.length; i++) {
|
|
|
|
|
var conversation = conversations[i];
|
2025-04-28 18:21:02 -07:00
|
|
|
_conversations.add(conversation);
|
|
|
|
|
position++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Notify of additions
|
|
|
|
|
if (position > 0) {
|
|
|
|
|
items_changed(0, 0, position);
|
|
|
|
|
}
|
|
|
|
|
} catch (Error e) {
|
|
|
|
|
warning("Failed to load conversations: %s", e.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListModel implementation
|
|
|
|
|
public Type get_item_type() {
|
|
|
|
|
return typeof(Conversation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public uint get_n_items() {
|
|
|
|
|
return _conversations.size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Object? get_item(uint position) {
|
|
|
|
|
return _conversations.to_array()[position];
|
|
|
|
|
}
|
|
|
|
|
}
|