Integrated WriteProxy into the sending process
This commit is contained in:
		| @@ -3,7 +3,7 @@ package envoy.client.data; | |||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| import java.io.Serializable; | import java.io.Serializable; | ||||||
|  |  | ||||||
| import envoy.client.net.Client; | import envoy.client.net.WriteProxy; | ||||||
| import envoy.client.ui.list.ComponentListModel; | import envoy.client.ui.list.ComponentListModel; | ||||||
| import envoy.data.Message; | import envoy.data.Message; | ||||||
| import envoy.data.Message.MessageStatus; | import envoy.data.Message.MessageStatus; | ||||||
| @@ -52,22 +52,20 @@ public class Chat implements Serializable { | |||||||
| 	 * {@code READ} starting from the bottom and stopping once a read message is | 	 * {@code READ} starting from the bottom and stopping once a read message is | ||||||
| 	 * found. | 	 * found. | ||||||
| 	 * | 	 * | ||||||
| 	 * @param client the client instance used to notify the server about the message | 	 * @param writeProxy the write proxy instance used to notify the server about | ||||||
| 	 *               status changes | 	 *                   the message status changes | ||||||
| 	 * @throws IOException if a {@link MessageStatusChangeEvent} could not be | 	 * @throws IOException if a {@link MessageStatusChangeEvent} could not be | ||||||
| 	 *                     delivered to the server | 	 *                     delivered to the server | ||||||
| 	 * @since Envoy v0.3-alpha | 	 * @since Envoy v0.3-alpha | ||||||
| 	 */ | 	 */ | ||||||
| 	public void read(Client client) throws IOException { | 	public void read(WriteProxy writeProxy) throws IOException { | ||||||
| 		for (int i = model.size() - 1; i >= 0; --i) { | 		for (int i = model.size() - 1; i >= 0; --i) { | ||||||
| 			final Message m = model.get(i); | 			final Message m = model.get(i); | ||||||
| 			if (m.getSenderId() == recipient.getId()) { | 			if (m.getSenderId() == recipient.getId()) { | ||||||
| 				if (m.getStatus() == MessageStatus.READ) break; | 				if (m.getStatus() == MessageStatus.READ) break; | ||||||
| 				else { | 				else { | ||||||
| 					m.setStatus(MessageStatus.READ); | 					m.setStatus(MessageStatus.READ); | ||||||
|  | 					writeProxy.writeMessageStatusChangeEvent(new MessageStatusChangeEvent(m)); | ||||||
| 					// TODO: Cache events in offline mode |  | ||||||
| 					client.sendEvent(new MessageStatusChangeEvent(m)); |  | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|   | |||||||
| @@ -16,6 +16,7 @@ import envoy.client.data.LocalDb; | |||||||
| import envoy.client.event.MessageCreationEvent; | import envoy.client.event.MessageCreationEvent; | ||||||
| import envoy.client.event.ThemeChangeEvent; | import envoy.client.event.ThemeChangeEvent; | ||||||
| import envoy.client.net.Client; | import envoy.client.net.Client; | ||||||
|  | import envoy.client.net.WriteProxy; | ||||||
| import envoy.client.ui.list.ComponentList; | import envoy.client.ui.list.ComponentList; | ||||||
| import envoy.client.ui.settings.SettingsScreen; | import envoy.client.ui.settings.SettingsScreen; | ||||||
| import envoy.client.util.EnvoyLog; | import envoy.client.util.EnvoyLog; | ||||||
| @@ -39,8 +40,9 @@ import envoy.event.MessageStatusChangeEvent; | |||||||
| public class ChatWindow extends JFrame { | public class ChatWindow extends JFrame { | ||||||
|  |  | ||||||
| 	// User specific objects | 	// User specific objects | ||||||
| 	private Client	client; | 	private Client		client; | ||||||
| 	private LocalDb	localDb; | 	private WriteProxy	writeProxy; | ||||||
|  | 	private LocalDb		localDb; | ||||||
|  |  | ||||||
| 	// GUI components | 	// GUI components | ||||||
| 	private JPanel					contentPane				= new JPanel(); | 	private JPanel					contentPane				= new JPanel(); | ||||||
| @@ -211,7 +213,7 @@ public class ChatWindow extends JFrame { | |||||||
|  |  | ||||||
| 		// Listen to received messages | 		// Listen to received messages | ||||||
| 		EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> { | 		EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> { | ||||||
| 			Message message = ((MessageCreationEvent) evt).get(); | 			Message	message	= ((MessageCreationEvent) evt).get(); | ||||||
| 			Chat	chat	= localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get(); | 			Chat	chat	= localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get(); | ||||||
| 			chat.appendMessage(message); | 			chat.appendMessage(message); | ||||||
|  |  | ||||||
| @@ -301,8 +303,7 @@ public class ChatWindow extends JFrame { | |||||||
| 				.build(); | 				.build(); | ||||||
|  |  | ||||||
| 			// Send message | 			// Send message | ||||||
| 			// TODO: Store offline messages | 			writeProxy.writeMessage(message); | ||||||
| 			client.sendMessage(message); |  | ||||||
|  |  | ||||||
| 			// Add message to PersistentLocalDb and update UI | 			// Add message to PersistentLocalDb and update UI | ||||||
| 			currentChat.appendMessage(message); | 			currentChat.appendMessage(message); | ||||||
| @@ -345,7 +346,7 @@ public class ChatWindow extends JFrame { | |||||||
|  |  | ||||||
| 	private void readCurrentChat() { | 	private void readCurrentChat() { | ||||||
| 		try { | 		try { | ||||||
| 			currentChat.read(client); | 			currentChat.read(writeProxy); | ||||||
| 			messageList.synchronizeModel(); | 			messageList.synchronizeModel(); | ||||||
| 		} catch (IOException e) { | 		} catch (IOException e) { | ||||||
| 			e.printStackTrace(); | 			e.printStackTrace(); | ||||||
| @@ -354,24 +355,23 @@ public class ChatWindow extends JFrame { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * Sets the {@link Client} used by this {@link ChatWindow}. | 	 * Initializes the components responsible server communication and | ||||||
|  | 	 * persistence.<br> | ||||||
|  | 	 * <br> | ||||||
|  | 	 * This will trigger the display of the contact list. | ||||||
| 	 * | 	 * | ||||||
| 	 * @param client the {@link Client} used to send and receive messages | 	 * @param client     the client used to send and receive messages | ||||||
| 	 * @since Envoy v0.2-alpha | 	 * @param localDb    the local database used to manage stored messages | ||||||
|  | 	 *                   and users | ||||||
|  | 	 * @param writeProxy the write proxy used to send messages and status change | ||||||
|  | 	 *                   events to the server or cache them inside the local | ||||||
|  | 	 *                   database | ||||||
|  | 	 * @since Envoy v0.3-alpha | ||||||
| 	 */ | 	 */ | ||||||
| 	public void setClient(Client client) { this.client = client; } | 	public void initContent(Client client, LocalDb localDb, WriteProxy writeProxy) { | ||||||
|  | 		this.client		= client; | ||||||
| 	/** | 		this.localDb	= localDb; | ||||||
| 	 * Sets the {@link LocalDb} used by this {@link ChatWindow}. After | 		this.writeProxy	= writeProxy; | ||||||
| 	 * invoking this |  | ||||||
| 	 * method, users and chats will be loaded from the database into the GUI. |  | ||||||
| 	 * |  | ||||||
| 	 * @param localDb the {@link LocalDb} used to manage stored messages |  | ||||||
| 	 *                and users |  | ||||||
| 	 * @since Envoy v0.2-alpha |  | ||||||
| 	 */ |  | ||||||
| 	public void setLocalDB(LocalDb localDb) { |  | ||||||
| 		this.localDb = localDb; |  | ||||||
| 		loadUsersAndChats(); | 		loadUsersAndChats(); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
| @@ -15,6 +15,7 @@ import envoy.client.Config; | |||||||
| import envoy.client.Settings; | import envoy.client.Settings; | ||||||
| import envoy.client.data.*; | import envoy.client.data.*; | ||||||
| import envoy.client.net.Client; | import envoy.client.net.Client; | ||||||
|  | import envoy.client.net.WriteProxy; | ||||||
| import envoy.client.util.EnvoyLog; | import envoy.client.util.EnvoyLog; | ||||||
| import envoy.data.LoginCredentials; | import envoy.data.LoginCredentials; | ||||||
| import envoy.data.Message; | import envoy.data.Message; | ||||||
| @@ -148,14 +149,19 @@ public class Startup { | |||||||
| 					JOptionPane.WARNING_MESSAGE); | 					JOptionPane.WARNING_MESSAGE); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		// Save all users to the local database | 		// Initialize write proxy | ||||||
| 		if (client.isOnline()) localDb.setUsers(client.getUsers()); | 		final WriteProxy writeProxy = client.createWriteProxy(localDb); | ||||||
|  |  | ||||||
|  | 		// Save all users to the local database and flush cache | ||||||
|  | 		if (client.isOnline()) { | ||||||
|  | 			localDb.setUsers(client.getUsers()); | ||||||
|  | 			writeProxy.flushCache(); | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		// Display ChatWindow and StatusTrayIcon | 		// Display ChatWindow and StatusTrayIcon | ||||||
| 		EventQueue.invokeLater(() -> { | 		EventQueue.invokeLater(() -> { | ||||||
| 			try { | 			try { | ||||||
| 				chatWindow.setClient(client); | 				chatWindow.initContent(client, localDb, writeProxy); | ||||||
| 				chatWindow.setLocalDB(localDb); |  | ||||||
|  |  | ||||||
| 				try { | 				try { | ||||||
| 					new StatusTrayIcon(chatWindow).show(); | 					new StatusTrayIcon(chatWindow).show(); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user