aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/conversation_summary/view.vala
blob: 33b3a8363db7a606cc18439feeec3302ba28a124 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using Gee;
using Gtk;
using Pango;

using Dino.Entities;
using Xmpp;

namespace Dino.Ui.ConversationSummary {

[GtkTemplate (ui = "/org/dino-im/conversation_summary/view.ui")]
public class View : Box {

    public Conversation? conversation { get; private set; }
    public HashMap<Entities.Message, ConversationItem> conversation_items = new HashMap<Entities.Message, ConversationItem>(Entities.Message.hash_func, Entities.Message.equals_func);

    [GtkChild] private ScrolledWindow scrolled;
    [GtkChild] private Box main;
    [GtkChild] private Stack stack;

    private StreamInteractor stream_interactor;
    private ConversationItem? last_conversation_item;
    private StatusItem typing_status;
    private Entities.Message? earliest_message;
    double? was_value;
    double? was_upper;
    double? was_page_size;
    Object reloading_lock = new Object();
    bool reloading = false;

    public View(StreamInteractor stream_interactor) {
        this.stream_interactor = stream_interactor;
        scrolled.vadjustment.notify["upper"].connect_after(on_upper_notify);
        scrolled.vadjustment.notify["value"].connect(on_value_notify);

        CounterpartInteractionManager.get_instance(stream_interactor).received_state.connect((account, jid, state) => {
            Idle.add(() => { on_received_state(account, jid, state); return false; });
        });
        MessageManager.get_instance(stream_interactor).message_received.connect((message, conversation) => {
            Idle.add(() => { show_message(message, conversation, true); return false; });
        });
        MessageManager.get_instance(stream_interactor).message_sent.connect((message, conversation) => {
            Idle.add(() => { show_message(message, conversation, true); return false; });
        });
        PresenceManager.get_instance(stream_interactor).show_received.connect((show, jid, account) => {
            Idle.add(() => { on_show_received(show, jid, account); return false; });
        });
        Timeout.add_seconds(60, () => {
            foreach (ConversationItem conversation_item in conversation_items.values) {
                MessageItem message_item = conversation_item as MessageItem;
                if (message_item != null) message_item.update();
            }
            return true;
        });

        Util.force_base_background(this);
    }

    public void initialize_for_conversation(Conversation? conversation) {
        this.conversation = conversation;
        stack.set_visible_child_name("void");
        clear();
        conversation_items.clear();
        was_upper = null;
        was_page_size = null;
        last_conversation_item = null;

        ArrayList<Object> objects = new ArrayList<Object>();
        Gee.List<Entities.Message>? messages = MessageManager.get_instance(stream_interactor).get_messages(conversation);
        if (messages != null && messages.size > 0) {
            earliest_message = messages[0];
            objects.add_all(messages);
        }
        HashMap<Jid, ArrayList<Show>>? shows = PresenceManager.get_instance(stream_interactor).get_shows(conversation.counterpart, conversation.account);
        if (shows != null) {
            foreach (Jid jid in shows.keys) objects.add_all(shows[jid]);
        }
        objects.sort((a, b) => {
            DateTime? dt1 = null;
            DateTime? dt2 = null;
            Entities.Message m1 = a as Entities.Message;
            if (m1 != null) dt1 = m1.time;
            Show s1 = a as Show;
            if (s1 != null) dt1 = s1.datetime;
            Entities.Message m2 = b as Entities.Message;
            if (m2 != null) dt2 = m2.time;
            Show s2 = b as Show;
            if (s2 != null) dt2 = s2.datetime;
            return dt1.compare(dt2);
        });
        foreach (Object o in objects) {
            Entities.Message message = o as Entities.Message;
            Show show = o as Show;
            if (message != null) {
                show_message(message, conversation);
            } else if (show != null) {
                on_show_received(show, conversation.counterpart, conversation.account);
            }
        }
        update_chat_state();
        stack.set_visible_child_name("main");
    }

    private void on_received_state(Account account, Jid jid, string state) {
        if (conversation != null && conversation.account.equals(account) && conversation.counterpart.equals_bare(jid)) {
            update_chat_state(state);
        }
    }

    private void update_chat_state(string? state = null) {
        string? state_ = state;
        if (state_ == null) {
            state_ = CounterpartInteractionManager.get_instance(stream_interactor).get_chat_state(conversation.account, conversation.counterpart);
        }
        if (typing_status != null) {
            main.remove(typing_status);
        }
        if (state_ != null) {
            if (state_ == Xep.ChatStateNotifications.STATE_COMPOSING || state_ == Xep.ChatStateNotifications.STATE_PAUSED) {
                if (state_ == Xep.ChatStateNotifications.STATE_COMPOSING) {
                    typing_status = new StatusItem(stream_interactor, conversation, "is typing...");
                } else if (state_ == Xep.ChatStateNotifications.STATE_PAUSED) {
                    typing_status = new StatusItem(stream_interactor, conversation, "has stoped typing");
                }
                main.add(typing_status);
            }
        }
    }

    private void on_show_received(Show show, Jid jid, Account account) {

    }

    private void on_upper_notify() {
        if (was_upper == null || scrolled.vadjustment.value >  was_upper - was_page_size - 1 ||
                scrolled.vadjustment.value >  was_upper - was_page_size - 1) { // scrolled down or content smaller than page size
            scrolled.vadjustment.value = scrolled.vadjustment.upper - scrolled.vadjustment.page_size; // scroll down
        } else if (scrolled.vadjustment.value < scrolled.vadjustment.upper - scrolled.vadjustment.page_size - 1) {
            scrolled.vadjustment.value = scrolled.vadjustment.upper - was_upper + scrolled.vadjustment.value; // stay at same content
        }
        was_upper = scrolled.vadjustment.upper;
        was_page_size = scrolled.vadjustment.page_size;
        lock(reloading_lock) {
            reloading = false;
        }
    }

    private void on_value_notify() {
        if (scrolled.vadjustment.value < 200) {
            load_earlier_messages();
        }
    }

    private void load_earlier_messages() {
        was_value = scrolled.vadjustment.value;
        lock(reloading_lock) {
            if(reloading) return;
            reloading = true;
        }
        Gee.List<Entities.Message>? messages = MessageManager.get_instance(stream_interactor).get_messages_before(conversation, earliest_message);
        if (messages != null && messages.size > 0) {
            earliest_message = messages[0];
            MergedMessageItem? current_item = null;
            int items_added = 0;
            for (int i = 0; i < messages.size; i++) {
                if (current_item == null || !current_item.merge(messages[i])) {
                    current_item = new MergedMessageItem(stream_interactor, conversation, messages[i]);
                    force_alloc_width(current_item, main.get_allocated_width());
                    main.add(current_item);
                    conversation_items[messages[i]] = current_item;
                    main.reorder_child(current_item, items_added);
                    items_added++;
                }
            }
            return;
        }
        reloading = false;
    }

    private void show_message(Entities.Message message, Conversation conversation, bool animate = false) {
        if (this.conversation != null && this.conversation.equals(conversation)) {
            if (last_conversation_item == null || !last_conversation_item.merge(message)) {
                ConversationItem conversation_item = ConversationItem.create_for_message(stream_interactor, conversation, message);
                if (animate) {
                    Revealer revealer = new Revealer() {transition_duration = 200, transition_type = RevealerTransitionType.SLIDE_UP, visible = true};
                    revealer.add(conversation_item);
                    force_alloc_width(revealer, main.get_allocated_width());
                    main.add(revealer);
                    revealer.set_reveal_child(true);
                } else {
                    force_alloc_width(conversation_item, main.get_allocated_width());
                    main.add(conversation_item);
                }
                last_conversation_item = conversation_item;
            }
            conversation_items[message] = last_conversation_item;
            update_chat_state();
        }
    }

    private void force_alloc_width(Widget widget, int width) {
        Allocation alloc = Allocation();
        widget.get_preferred_width(out alloc.width, null);
        widget.get_preferred_height(out alloc.height, null);
        alloc.width = width;
        widget.size_allocate(alloc);
    }

    private void clear() {
        main.@foreach((widget) => { main.remove(widget); });
    }
}

}