@@ -80,10 +80,10 @@ bool BitchatManager::initialize(
8080 return false ;
8181 }
8282
83- // Generate or load key pair
84- if (!cryptoService-> generateOrLoadKeyPair ( " bitchat-pk.pem " ))
83+ // Initialize UI with message service
84+ if (!userInterface-> initialize (messageService ))
8585 {
86- spdlog::error (" Failed to generate or load key pair " );
86+ spdlog::error (" Failed to initialize UserInterface " );
8787 return false ;
8888 }
8989
@@ -93,16 +93,6 @@ bool BitchatManager::initialize(
9393 return false ;
9494 }
9595
96- // Initialize UI with message service
97- if (!userInterface->initialize (messageService))
98- {
99- spdlog::error (" Failed to initialize UserInterface" );
100- return false ;
101- }
102-
103- // Set up callbacks
104- setupCallbacks ();
105-
10696 spdlog::info (" BitchatManager initialized successfully" );
10797
10898 return true ;
@@ -140,18 +130,26 @@ void BitchatManager::stop()
140130
141131bool BitchatManager::sendMessage (const std::string &content)
142132{
133+ if (!messageService)
134+ {
135+ return false ;
136+ }
137+
143138 return messageService->sendMessage (content);
144139}
145140
146141bool BitchatManager::sendPrivateMessage (const std::string &content, const std::string &recipientNickname)
147142{
143+ if (!messageService)
144+ {
145+ return false ;
146+ }
147+
148148 return messageService->sendPrivateMessage (content, recipientNickname);
149149}
150150
151151void BitchatManager::joinChannel (const std::string &channel)
152152{
153- BitchatData::shared ()->setCurrentChannel (channel);
154-
155153 if (messageService)
156154 {
157155 messageService->joinChannel (channel);
@@ -160,8 +158,6 @@ void BitchatManager::joinChannel(const std::string &channel)
160158
161159void BitchatManager::leaveChannel ()
162160{
163- BitchatData::shared ()->setCurrentChannel (" " );
164-
165161 if (messageService)
166162 {
167163 messageService->leaveChannel ();
@@ -173,177 +169,6 @@ void BitchatManager::changeNickname(const std::string &nickname)
173169 BitchatData::shared ()->setNickname (nickname);
174170}
175171
176- void BitchatManager::setMessageCallback (MessageCallback callback)
177- {
178- messageCallback = callback;
179- }
180-
181- void BitchatManager::setPeerJoinedCallback (PeerJoinedCallback callback)
182- {
183- peerJoinedCallback = callback;
184- }
185-
186- void BitchatManager::setPeerLeftCallback (PeerLeftCallback callback)
187- {
188- peerLeftCallback = callback;
189- }
190-
191- void BitchatManager::setPeerConnectedCallback (PeerConnectedCallback callback)
192- {
193- peerConnectedCallback = callback;
194- }
195-
196- void BitchatManager::setPeerDisconnectedCallback (PeerDisconnectedCallback callback)
197- {
198- peerDisconnectedCallback = callback;
199- }
200-
201- void BitchatManager::setStatusCallback (StatusCallback callback)
202- {
203- statusCallback = callback;
204- }
205-
206- void BitchatManager::setChannelJoinedCallback (ChannelJoinedCallback callback)
207- {
208- channelJoinedCallback = callback;
209- }
210-
211- void BitchatManager::setChannelLeftCallback (ChannelLeftCallback callback)
212- {
213- channelLeftCallback = callback;
214- }
215-
216- void BitchatManager::setupCallbacks ()
217- {
218- // Set up network manager callbacks
219-
220- // clang-format off
221- networkService->setPeerConnectedCallback ([this ](const std::string &peripheralID) {
222- onPeerConnected (peripheralID);
223- });
224- // clang-format on
225-
226- // clang-format off
227- networkService->setPeerDisconnectedCallback ([this ](const std::string &peripheralID) {
228- onPeerDisconnected (peripheralID);
229- });
230- // clang-format on
231-
232- // Set up message manager callbacks
233-
234- // clang-format off
235- messageService->setMessageReceivedCallback ([this ](const BitchatMessage &message) {
236- onMessageReceived (message);
237- });
238- // clang-format on
239-
240- // clang-format off
241- messageService->setChannelJoinedCallback ([this ](const std::string &channel) {
242- onChannelJoined (channel);
243- });
244- // clang-format on
245-
246- // clang-format off
247- messageService->setChannelLeftCallback ([this ](const std::string &channel) {
248- onChannelLeft (channel);
249- });
250- // clang-format on
251-
252- // clang-format off
253- messageService->setPeerJoinedCallback ([this ](const std::string &peerID, const std::string &nickname) {
254- onPeerJoined (peerID, nickname);
255- });
256- // clang-format on
257-
258- // clang-format off
259- messageService->setPeerLeftCallback ([this ](const std::string &peerID, const std::string &nickname) {
260- onPeerLeft (peerID, nickname);
261- });
262- // clang-format on
263- }
264-
265- void BitchatManager::onMessageReceived (const BitchatMessage &message)
266- {
267- if (messageCallback)
268- {
269- messageCallback (message);
270- }
271- }
272-
273- void BitchatManager::onPeerJoined (const std::string &peerID, const std::string &nickname)
274- {
275- if (peerJoinedCallback)
276- {
277- peerJoinedCallback (peerID, nickname);
278- }
279- }
280-
281- void BitchatManager::onPeerLeft (const std::string &peerID, const std::string &nickname)
282- {
283- if (peerLeftCallback)
284- {
285- peerLeftCallback (peerID, nickname);
286- }
287- }
288-
289- void BitchatManager::onPeerConnected (const std::string &peripheralID)
290- {
291- if (peerConnectedCallback)
292- {
293- peerConnectedCallback (peripheralID);
294- }
295- }
296-
297- void BitchatManager::onPeerDisconnected (const std::string &peripheralID)
298- {
299- // Remove peer from data store
300- auto peers = BitchatData::shared ()->getPeers ();
301-
302- for (const auto &peer : peers)
303- {
304- if (peer.getPeripheralID () == peripheralID)
305- {
306- std::string peerID = peer.getPeerID ();
307- std::string nickname = peer.getNickname ();
308- BitchatData::shared ()->removePeer (peerID);
309-
310- onPeerLeft (peerID, nickname);
311-
312- break ;
313- }
314- }
315-
316- // Notify callback
317- if (peerDisconnectedCallback)
318- {
319- peerDisconnectedCallback (peripheralID);
320- }
321- }
322-
323- void BitchatManager::onStatusUpdate (const std::string &status)
324- {
325- if (statusCallback)
326- {
327- statusCallback (status);
328- }
329- }
330-
331- void BitchatManager::onChannelJoined (const std::string &channel)
332- {
333- if (channelJoinedCallback)
334- {
335- channelJoinedCallback (channel);
336- }
337- }
338-
339- void BitchatManager::onChannelLeft (const std::string &channel)
340- {
341- if (channelLeftCallback)
342- {
343- channelLeftCallback (channel);
344- }
345- }
346-
347172std::shared_ptr<IUserInterface> BitchatManager::getUserInterface () const
348173{
349174 return userInterface;
0 commit comments