summaryrefslogtreecommitdiffstats
blob: c63d967f022a6184f96615e79edc098e2a339b4f (plain)
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
/*
 * Copyright (c) 2010 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.chat;

import com.isode.stroke.elements.Message;
import com.isode.stroke.elements.Presence;
import com.isode.stroke.elements.ChatState;
import com.isode.stroke.signals.Signal1;
import com.isode.stroke.base.NotNull;

public class ChatStateTracker {

	private ChatState.ChatStateType currentState_;
	public final Signal1<ChatState.ChatStateType> onChatStateChange = new Signal1<ChatState.ChatStateType>();

	/**
	* ChatStateTracker();
	*/
	public ChatStateTracker() {
		currentState_ = ChatState.ChatStateType.Gone;
	}

	/**
	* @param message, notnull
	*/
	public void handleMessageReceived(Message message) {
		NotNull.exceptIfNull(message, "message");
		if (message.getType() == Message.Type.Error) {
			return;
		}
		ChatState statePayload = message.getPayload(new ChatState());
		if (statePayload != null) {
			changeState(statePayload.getChatState());
		}
	}

	/**
	* @param newPresence, notnull
	*/
	public void handlePresenceChange(Presence newPresence) {
		NotNull.exceptIfNull(newPresence, "newPresence");
		if (newPresence.getType() == Presence.Type.Unavailable) {
			onChatStateChange.emit(ChatState.ChatStateType.Gone);
		}
	}

	private void changeState(ChatState.ChatStateType state) {
		if (state != currentState_) {
			currentState_ = state;
			onChatStateChange.emit(state);
		}
	}
}